config: tegra3: enable /dev mount with ACL
[linux-2.6.git] / drivers / mfd / twl-core.c
1 /*
2  * twl_core.c - driver for TWL4030/TWL5030/TWL60X0/TPS659x0 PM
3  * and audio CODEC devices
4  *
5  * Copyright (C) 2005-2006 Texas Instruments, Inc.
6  *
7  * Modifications to defer interrupt handling to a kernel thread:
8  * Copyright (C) 2006 MontaVista Software, Inc.
9  *
10  * Based on tlv320aic23.c:
11  * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
12  *
13  * Code cleanup and modifications to IRQ handler.
14  * by syed khasim <x0khasim@ti.com>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
29  */
30
31 #include <linux/init.h>
32 #include <linux/mutex.h>
33 #include <linux/platform_device.h>
34 #include <linux/clk.h>
35 #include <linux/err.h>
36
37 #include <linux/regulator/machine.h>
38
39 #include <linux/i2c.h>
40 #include <linux/i2c/twl.h>
41
42 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
43 #include <plat/cpu.h>
44 #endif
45
46 /*
47  * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
48  * Management and System Companion Device" chips originally designed for
49  * use in OMAP2 and OMAP 3 based systems.  Its control interfaces use I2C,
50  * often at around 3 Mbit/sec, including for interrupt handling.
51  *
52  * This driver core provides genirq support for the interrupts emitted,
53  * by the various modules, and exports register access primitives.
54  *
55  * FIXME this driver currently requires use of the first interrupt line
56  * (and associated registers).
57  */
58
59 #define DRIVER_NAME                     "twl"
60
61 #if defined(CONFIG_TWL4030_BCI_BATTERY) || \
62         defined(CONFIG_TWL4030_BCI_BATTERY_MODULE) || \
63         defined(CONFIG_TWL6030_BCI_BATTERY) || \
64         defined(CONFIG_TWL6030_BCI_BATTERY_MODULE)
65 #define twl_has_bci()           true
66 #else
67 #define twl_has_bci()           false
68 #endif
69 #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
70 #define twl_has_keypad()        true
71 #else
72 #define twl_has_keypad()        false
73 #endif
74
75 #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
76 #define twl_has_gpio()  true
77 #else
78 #define twl_has_gpio()  false
79 #endif
80
81 #if defined(CONFIG_REGULATOR_TWL4030) \
82         || defined(CONFIG_REGULATOR_TWL4030_MODULE)
83 #define twl_has_regulator()     true
84 #else
85 #define twl_has_regulator()     false
86 #endif
87
88 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE) ||\
89     defined(CONFIG_TWL6030_GPADC) || defined(CONFIG_TWL6030_GPADC_MODULE)
90 #define twl_has_madc()  true
91 #else
92 #define twl_has_madc()  false
93 #endif
94
95 #ifdef CONFIG_TWL4030_POWER
96 #define twl_has_power()        true
97 #else
98 #define twl_has_power()        false
99 #endif
100
101 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
102 #define twl_has_rtc()   true
103 #else
104 #define twl_has_rtc()   false
105 #endif
106
107 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE) ||\
108         defined(CONFIG_TWL6030_USB) || defined(CONFIG_TWL6030_USB_MODULE)
109 #define twl_has_usb()   true
110 #else
111 #define twl_has_usb()   false
112 #endif
113
114 #if defined(CONFIG_TWL4030_WATCHDOG) || \
115         defined(CONFIG_TWL4030_WATCHDOG_MODULE)
116 #define twl_has_watchdog()        true
117 #else
118 #define twl_has_watchdog()        false
119 #endif
120
121 #if defined(CONFIG_MFD_TWL4030_AUDIO) || defined(CONFIG_MFD_TWL4030_AUDIO_MODULE) ||\
122         defined(CONFIG_TWL6040_CORE) || defined(CONFIG_TWL6040_CORE_MODULE)
123 #define twl_has_codec() true
124 #else
125 #define twl_has_codec() false
126 #endif
127
128 #if defined(CONFIG_CHARGER_TWL4030) || defined(CONFIG_CHARGER_TWL4030_MODULE)
129 #define twl_has_bci()   true
130 #else
131 #define twl_has_bci()   false
132 #endif
133
134 /* Triton Core internal information (BEGIN) */
135
136 /* Last - for index max*/
137 #define TWL4030_MODULE_LAST             TWL6025_MODULE_CHARGER
138
139 #define TWL_NUM_SLAVES          4
140
141 #if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
142         || defined(CONFIG_INPUT_TWL4030_PWRBUTTON_MODULE)
143 #define twl_has_pwrbutton()     true
144 #else
145 #define twl_has_pwrbutton()     false
146 #endif
147
148 #define SUB_CHIP_ID0 0
149 #define SUB_CHIP_ID1 1
150 #define SUB_CHIP_ID2 2
151 #define SUB_CHIP_ID3 3
152
153 #define TWL_MODULE_LAST TWL4030_MODULE_LAST
154
155 /* Base Address defns for twl4030_map[] */
156
157 /* subchip/slave 0 - USB ID */
158 #define TWL4030_BASEADD_USB             0x0000
159
160 /* subchip/slave 1 - AUD ID */
161 #define TWL4030_BASEADD_AUDIO_VOICE     0x0000
162 #define TWL4030_BASEADD_GPIO            0x0098
163 #define TWL4030_BASEADD_INTBR           0x0085
164 #define TWL4030_BASEADD_PIH             0x0080
165 #define TWL4030_BASEADD_TEST            0x004C
166
167 /* subchip/slave 2 - AUX ID */
168 #define TWL4030_BASEADD_INTERRUPTS      0x00B9
169 #define TWL4030_BASEADD_LED             0x00EE
170 #define TWL4030_BASEADD_MADC            0x0000
171 #define TWL4030_BASEADD_MAIN_CHARGE     0x0074
172 #define TWL4030_BASEADD_PRECHARGE       0x00AA
173 #define TWL4030_BASEADD_PWM0            0x00F8
174 #define TWL4030_BASEADD_PWM1            0x00FB
175 #define TWL4030_BASEADD_PWMA            0x00EF
176 #define TWL4030_BASEADD_PWMB            0x00F1
177 #define TWL4030_BASEADD_KEYPAD          0x00D2
178
179 #define TWL5031_BASEADD_ACCESSORY       0x0074 /* Replaces Main Charge */
180 #define TWL5031_BASEADD_INTERRUPTS      0x00B9 /* Different than TWL4030's
181                                                   one */
182
183 /* subchip/slave 3 - POWER ID */
184 #define TWL4030_BASEADD_BACKUP          0x0014
185 #define TWL4030_BASEADD_INT             0x002E
186 #define TWL4030_BASEADD_PM_MASTER       0x0036
187 #define TWL4030_BASEADD_PM_RECEIVER     0x005B
188 #define TWL4030_BASEADD_RTC             0x001C
189 #define TWL4030_BASEADD_SECURED_REG     0x0000
190
191 /* Triton Core internal information (END) */
192
193
194 /* subchip/slave 0 0x48 - POWER */
195 #define TWL6030_BASEADD_RTC             0x0000
196 #define TWL6030_BASEADD_MEM             0x0017
197 #define TWL6030_BASEADD_PM_MASTER       0x001F
198 #define TWL6030_BASEADD_PM_SLAVE_MISC   0x0030 /* PM_RECEIVER */
199 #define TWL6030_BASEADD_PM_MISC         0x00E2
200 #define TWL6030_BASEADD_PM_PUPD         0x00F0
201
202 /* subchip/slave 1 0x49 - FEATURE */
203 #define TWL6030_BASEADD_USB             0x0000
204 #define TWL6030_BASEADD_GPADC_CTRL      0x002E
205 #define TWL6030_BASEADD_AUX             0x0090
206 #define TWL6030_BASEADD_PWM             0x00BA
207 #define TWL6030_BASEADD_GASGAUGE        0x00C0
208 #define TWL6030_BASEADD_PIH             0x00D0
209 #define TWL6030_BASEADD_CHARGER         0x00E0
210 #define TWL6025_BASEADD_CHARGER         0x00DA
211
212 /* subchip/slave 2 0x4A - DFT */
213 #define TWL6030_BASEADD_DIEID           0x00C0
214
215 /* subchip/slave 3 0x4B - AUDIO */
216 #define TWL6030_BASEADD_AUDIO           0x0000
217 #define TWL6030_BASEADD_RSV             0x0000
218 #define TWL6030_BASEADD_ZERO            0x0000
219
220 /* twl6030 SMPS EPROM values */
221 #define TWL6030_SMPS_OFFSET             0xB0
222 #define TWL6030_SMPS_MULT               0xB3
223
224
225 /* Few power values */
226 #define R_CFG_BOOT                      0x05
227
228 /* some fields in R_CFG_BOOT */
229 #define HFCLK_FREQ_19p2_MHZ             (1 << 0)
230 #define HFCLK_FREQ_26_MHZ               (2 << 0)
231 #define HFCLK_FREQ_38p4_MHZ             (3 << 0)
232 #define HIGH_PERF_SQ                    (1 << 3)
233 #define CK32K_LOWPWR_EN                 (1 << 7)
234
235 /* MPU80031 specific clock32 generation register */
236 #define REG_CLK32KG_CFG_TRANS 0x8D
237 #define REG_CLK32KG_CFG_STATE 0x8E
238
239 /* chip-specific feature flags, for i2c_device_id.driver_data */
240 #define TWL4030_VAUX2           BIT(0)  /* pre-5030 voltage ranges */
241 #define TPS_SUBSET              BIT(1)  /* tps659[23]0 have fewer LDOs */
242 #define TWL5031                 BIT(2)  /* twl5031 has different registers */
243 #define TWL6030_CLASS           BIT(3)  /* TWL6030 class */
244
245 /*----------------------------------------------------------------------*/
246
247 /* is driver active, bound to a chip? */
248 static bool inuse;
249
250 /* TWL IDCODE Register value */
251 static u32 twl_idcode;
252
253 static unsigned int twl_id;
254 unsigned int twl_rev(void)
255 {
256         return twl_id;
257 }
258 EXPORT_SYMBOL(twl_rev);
259
260 static unsigned int twl_feat;
261 unsigned int twl_features(void)
262 {
263         return twl_feat;
264 }
265 EXPORT_SYMBOL(twl_features);
266
267 u8 twl_get_smps_offset(void)
268 {
269         u8 value;
270
271         twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
272                         TWL6030_SMPS_OFFSET);
273         return value;
274 }
275 EXPORT_SYMBOL(twl_get_smps_offset);
276
277 u8 twl_get_smps_mult(void)
278 {
279         u8 value;
280
281         twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
282                         TWL6030_SMPS_MULT);
283         return value;
284 }
285 EXPORT_SYMBOL(twl_get_smps_mult);
286
287 /* Structure for each TWL4030/TWL6030 Slave */
288 struct twl_client {
289         struct i2c_client *client;
290         u8 address;
291
292         /* max numb of i2c_msg required is for read =2 */
293         struct i2c_msg xfer_msg[2];
294
295         /* To lock access to xfer_msg */
296         struct mutex xfer_lock;
297 };
298
299 static struct twl_client twl_modules[TWL_NUM_SLAVES];
300
301
302 /* mapping the module id to slave id and base address */
303 struct twl_mapping {
304         unsigned char sid;      /* Slave ID */
305         unsigned char base;     /* base address */
306 };
307 static struct twl_mapping *twl_map;
308
309 static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
310         /*
311          * NOTE:  don't change this table without updating the
312          * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
313          * so they continue to match the order in this table.
314          */
315
316         { 0, TWL4030_BASEADD_USB },
317
318         { 1, TWL4030_BASEADD_AUDIO_VOICE },
319         { 1, TWL4030_BASEADD_GPIO },
320         { 1, TWL4030_BASEADD_INTBR },
321         { 1, TWL4030_BASEADD_PIH },
322         { 1, TWL4030_BASEADD_TEST },
323
324         { 2, TWL4030_BASEADD_KEYPAD },
325         { 2, TWL4030_BASEADD_MADC },
326         { 2, TWL4030_BASEADD_INTERRUPTS },
327         { 2, TWL4030_BASEADD_LED },
328         { 2, TWL4030_BASEADD_MAIN_CHARGE },
329         { 2, TWL4030_BASEADD_PRECHARGE },
330         { 2, TWL4030_BASEADD_PWM0 },
331         { 2, TWL4030_BASEADD_PWM1 },
332         { 2, TWL4030_BASEADD_PWMA },
333         { 2, TWL4030_BASEADD_PWMB },
334         { 2, TWL5031_BASEADD_ACCESSORY },
335         { 2, TWL5031_BASEADD_INTERRUPTS },
336
337         { 3, TWL4030_BASEADD_BACKUP },
338         { 3, TWL4030_BASEADD_INT },
339         { 3, TWL4030_BASEADD_PM_MASTER },
340         { 3, TWL4030_BASEADD_PM_RECEIVER },
341         { 3, TWL4030_BASEADD_RTC },
342         { 3, TWL4030_BASEADD_SECURED_REG },
343 };
344
345 static struct twl_mapping twl6030_map[] = {
346         /*
347          * NOTE:  don't change this table without updating the
348          * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
349          * so they continue to match the order in this table.
350          */
351         { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
352         { SUB_CHIP_ID3, TWL6030_BASEADD_AUDIO },
353         { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
354         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
355         { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
356
357         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
358         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
359         { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
360         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
361         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
362
363         { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
364         { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
365         { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
366         { SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
367         { SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
368
369         { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
370         { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
371         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
372         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
373         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
374         { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
375         { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
376
377         { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
378         { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
379         { SUB_CHIP_ID1, TWL6025_BASEADD_CHARGER },
380 };
381
382 /*----------------------------------------------------------------------*/
383
384 /* Exported Functions */
385
386 /**
387  * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
388  * @mod_no: module number
389  * @value: an array of num_bytes+1 containing data to write
390  * @reg: register address (just offset will do)
391  * @num_bytes: number of bytes to transfer
392  *
393  * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
394  * valid data starts at Offset 1.
395  *
396  * Returns the result of operation - 0 is success
397  */
398 int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
399 {
400         int ret;
401         int sid;
402         struct twl_client *twl;
403         struct i2c_msg *msg;
404
405         if (unlikely(mod_no > TWL_MODULE_LAST)) {
406                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
407                 return -EPERM;
408         }
409         if (unlikely(!inuse)) {
410                 pr_err("%s: not initialized\n", DRIVER_NAME);
411                 return -EPERM;
412         }
413         sid = twl_map[mod_no].sid;
414         twl = &twl_modules[sid];
415
416         mutex_lock(&twl->xfer_lock);
417         /*
418          * [MSG1]: fill the register address data
419          * fill the data Tx buffer
420          */
421         msg = &twl->xfer_msg[0];
422         msg->addr = twl->address;
423         msg->len = num_bytes + 1;
424         msg->flags = 0;
425         msg->buf = value;
426         /* over write the first byte of buffer with the register address */
427         *value = twl_map[mod_no].base + reg;
428         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
429         mutex_unlock(&twl->xfer_lock);
430
431         /* i2c_transfer returns number of messages transferred */
432         if (ret != 1) {
433                 pr_err("%s: i2c_write failed to transfer all messages\n",
434                         DRIVER_NAME);
435                 if (ret < 0)
436                         return ret;
437                 else
438                         return -EIO;
439         } else {
440                 return 0;
441         }
442 }
443 EXPORT_SYMBOL(twl_i2c_write);
444
445 /**
446  * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
447  * @mod_no: module number
448  * @value: an array of num_bytes containing data to be read
449  * @reg: register address (just offset will do)
450  * @num_bytes: number of bytes to transfer
451  *
452  * Returns result of operation - num_bytes is success else failure.
453  */
454 int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
455 {
456         int ret;
457         u8 val;
458         int sid;
459         struct twl_client *twl;
460         struct i2c_msg *msg;
461
462         if (unlikely(mod_no > TWL_MODULE_LAST)) {
463                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
464                 return -EPERM;
465         }
466         if (unlikely(!inuse)) {
467                 pr_err("%s: not initialized\n", DRIVER_NAME);
468                 return -EPERM;
469         }
470         sid = twl_map[mod_no].sid;
471         twl = &twl_modules[sid];
472
473         mutex_lock(&twl->xfer_lock);
474         /* [MSG1] fill the register address data */
475         msg = &twl->xfer_msg[0];
476         msg->addr = twl->address;
477         msg->len = 1;
478         msg->flags = 0; /* Read the register value */
479         val = twl_map[mod_no].base + reg;
480         msg->buf = &val;
481         /* [MSG2] fill the data rx buffer */
482         msg = &twl->xfer_msg[1];
483         msg->addr = twl->address;
484         msg->flags = I2C_M_RD;  /* Read the register value */
485         msg->len = num_bytes;   /* only n bytes */
486         msg->buf = value;
487         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
488         mutex_unlock(&twl->xfer_lock);
489
490         /* i2c_transfer returns number of messages transferred */
491         if (ret != 2) {
492                 pr_err("%s: i2c_read failed to transfer all messages\n",
493                         DRIVER_NAME);
494                 if (ret < 0)
495                         return ret;
496                 else
497                         return -EIO;
498         } else {
499                 return 0;
500         }
501 }
502 EXPORT_SYMBOL(twl_i2c_read);
503
504 /**
505  * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
506  * @mod_no: module number
507  * @value: the value to be written 8 bit
508  * @reg: register address (just offset will do)
509  *
510  * Returns result of operation - 0 is success
511  */
512 int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
513 {
514
515         /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
516         u8 temp_buffer[2] = { 0 };
517         /* offset 1 contains the data */
518         temp_buffer[1] = value;
519         return twl_i2c_write(mod_no, temp_buffer, reg, 1);
520 }
521 EXPORT_SYMBOL(twl_i2c_write_u8);
522
523 /**
524  * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
525  * @mod_no: module number
526  * @value: the value read 8 bit
527  * @reg: register address (just offset will do)
528  *
529  * Returns result of operation - 0 is success
530  */
531 int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
532 {
533         return twl_i2c_read(mod_no, value, reg, 1);
534 }
535 EXPORT_SYMBOL(twl_i2c_read_u8);
536
537
538 void twl_reg_dump(int module, int start, int end)
539 {
540         int i;
541         u8 val;
542
543         for (i = start; i < end; i++) {
544                 twl_i2c_read_u8(module, &val, i);
545                 printk(KERN_ERR "reg 0x%2x val 0x%2x\n", i, val);
546         }
547 }
548 EXPORT_SYMBOL(twl_reg_dump);
549
550 /*----------------------------------------------------------------------*/
551
552 /**
553  * twl_read_idcode_register - API to read the IDCODE register.
554  *
555  * Unlocks the IDCODE register and read the 32 bit value.
556  */
557 static int twl_read_idcode_register(void)
558 {
559         int err;
560
561         err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK,
562                                                 REG_UNLOCK_TEST_REG);
563         if (err) {
564                 pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
565                 goto fail;
566         }
567
568         err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode),
569                                                 REG_IDCODE_7_0, 4);
570         if (err) {
571                 pr_err("TWL4030: unable to read IDCODE -%d\n", err);
572                 goto fail;
573         }
574
575         err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG);
576         if (err)
577                 pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
578 fail:
579         return err;
580 }
581
582 /**
583  * twl_get_type - API to get TWL Si type.
584  *
585  * Api to get the TWL Si type from IDCODE value.
586  */
587 int twl_get_type(void)
588 {
589         return TWL_SIL_TYPE(twl_idcode);
590 }
591 EXPORT_SYMBOL_GPL(twl_get_type);
592
593 /**
594  * twl_get_version - API to get TWL Si version.
595  *
596  * Api to get the TWL Si version from IDCODE value.
597  */
598 int twl_get_version(void)
599 {
600         return TWL_SIL_REV(twl_idcode);
601 }
602 EXPORT_SYMBOL_GPL(twl_get_version);
603
604 static struct device *
605 add_numbered_child(unsigned chip, const char *name, int num,
606                 void *pdata, unsigned pdata_len,
607                 bool can_wakeup, int irq0, int irq1)
608 {
609         struct platform_device  *pdev;
610         struct twl_client       *twl = &twl_modules[chip];
611         int                     status;
612
613         pdev = platform_device_alloc(name, num);
614         if (!pdev) {
615                 dev_dbg(&twl->client->dev, "can't alloc dev\n");
616                 status = -ENOMEM;
617                 goto err;
618         }
619
620         device_init_wakeup(&pdev->dev, can_wakeup);
621         pdev->dev.parent = &twl->client->dev;
622
623         if (pdata) {
624                 status = platform_device_add_data(pdev, pdata, pdata_len);
625                 if (status < 0) {
626                         dev_dbg(&pdev->dev, "can't add platform_data\n");
627                         goto err;
628                 }
629         }
630
631         if (irq0) {
632                 struct resource r[2] = {
633                         { .start = irq0, .flags = IORESOURCE_IRQ, },
634                         { .start = irq1, .flags = IORESOURCE_IRQ, },
635                 };
636
637                 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
638                 if (status < 0) {
639                         dev_dbg(&pdev->dev, "can't add irqs\n");
640                         goto err;
641                 }
642         }
643
644         status = platform_device_add(pdev);
645
646 err:
647         if (status < 0) {
648                 platform_device_put(pdev);
649                 dev_err(&twl->client->dev, "can't add %s dev\n", name);
650                 return ERR_PTR(status);
651         }
652         return &pdev->dev;
653 }
654
655 static inline struct device *add_child(unsigned chip, const char *name,
656                 void *pdata, unsigned pdata_len,
657                 bool can_wakeup, int irq0, int irq1)
658 {
659         return add_numbered_child(chip, name, -1, pdata, pdata_len,
660                 can_wakeup, irq0, irq1);
661 }
662
663 static struct device *
664 add_regulator_linked(int num, struct regulator_init_data *pdata,
665                 struct regulator_consumer_supply *consumers,
666                 unsigned num_consumers, unsigned long features)
667 {
668         unsigned sub_chip_id;
669         /* regulator framework demands init_data ... */
670         if (!pdata)
671                 return NULL;
672
673         if (consumers) {
674                 pdata->consumer_supplies = consumers;
675                 pdata->num_consumer_supplies = num_consumers;
676         }
677
678         pdata->driver_data = (void *)features;
679
680         /* NOTE:  we currently ignore regulator IRQs, e.g. for short circuits */
681         sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
682         return add_numbered_child(sub_chip_id, "twl_reg", num,
683                 pdata, sizeof(*pdata), false, 0, 0);
684 }
685
686 static struct device *
687 add_regulator(int num, struct regulator_init_data *pdata,
688                 unsigned long features)
689 {
690         return add_regulator_linked(num, pdata, NULL, 0, features);
691 }
692
693 /*
694  * NOTE:  We know the first 8 IRQs after pdata->base_irq are
695  * for the PIH, and the next are for the PWR_INT SIH, since
696  * that's how twl_init_irq() sets things up.
697  */
698
699 static int
700 add_children(struct twl4030_platform_data *pdata, unsigned long features)
701 {
702         struct device   *child;
703         unsigned sub_chip_id;
704
705         if (twl_has_gpio() && pdata->gpio) {
706                 child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
707                                 pdata->gpio, sizeof(*pdata->gpio),
708                                 false, pdata->irq_base + GPIO_INTR_OFFSET, 0);
709                 if (IS_ERR(child))
710                         return PTR_ERR(child);
711         }
712
713         if (twl_has_keypad() && pdata->keypad) {
714                 child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
715                                 pdata->keypad, sizeof(*pdata->keypad),
716                                 true, pdata->irq_base + KEYPAD_INTR_OFFSET, 0);
717                 if (IS_ERR(child))
718                         return PTR_ERR(child);
719         }
720         if (twl_has_bci() && pdata->bci &&
721             (features & TWL6030_CLASS)) {
722                 child = add_child(1, "twl6030_bci",
723                                 pdata->bci, sizeof(*pdata->bci),
724                                 false,
725                                 pdata->irq_base + CHARGER_INTR_OFFSET,
726                                 pdata->irq_base + CHARGERFAULT_INTR_OFFSET);
727         }
728
729         if (twl_has_madc() && pdata->madc && twl_class_is_4030()) {
730                 child = add_child(2, "twl4030_madc",
731                                 pdata->madc, sizeof(*pdata->madc),
732                                 true, pdata->irq_base + MADC_INTR_OFFSET, 0);
733                 if (IS_ERR(child))
734                         return PTR_ERR(child);
735         }
736
737         if (twl_has_madc() && pdata->madc && twl_class_is_6030()) {
738                 child = add_child(1, "twl6030_gpadc",
739                                 pdata->madc, sizeof(*pdata->madc),
740                                 true, pdata->irq_base + MADC_INTR_OFFSET,
741                                 pdata->irq_base + GPADCSW_INTR_OFFSET);
742                 if (IS_ERR(child))
743                         return PTR_ERR(child);
744         }
745
746         if (twl_has_rtc()) {
747                 /*
748                  * REVISIT platform_data here currently might expose the
749                  * "msecure" line ... but for now we just expect board
750                  * setup to tell the chip "it's always ok to SET_TIME".
751                  * Eventually, Linux might become more aware of such
752                  * HW security concerns, and "least privilege".
753                  */
754                 sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
755                 child = add_child(sub_chip_id, "twl_rtc",
756                                 NULL, 0,
757                                 true, pdata->irq_base + RTC_INTR_OFFSET, 0);
758                 if (IS_ERR(child))
759                         return PTR_ERR(child);
760         }
761
762         if (twl_has_usb() && pdata->usb && twl_class_is_4030()) {
763
764                 static struct regulator_consumer_supply usb1v5 = {
765                         .supply =       "usb1v5",
766                 };
767                 static struct regulator_consumer_supply usb1v8 = {
768                         .supply =       "usb1v8",
769                 };
770                 static struct regulator_consumer_supply usb3v1 = {
771                         .supply =       "usb3v1",
772                 };
773
774         /* First add the regulators so that they can be used by transceiver */
775                 if (twl_has_regulator()) {
776                         /* this is a template that gets copied */
777                         struct regulator_init_data usb_fixed = {
778                                 .constraints.valid_modes_mask =
779                                         REGULATOR_MODE_NORMAL
780                                         | REGULATOR_MODE_STANDBY,
781                                 .constraints.valid_ops_mask =
782                                         REGULATOR_CHANGE_MODE
783                                         | REGULATOR_CHANGE_STATUS,
784                         };
785
786                         child = add_regulator_linked(TWL4030_REG_VUSB1V5,
787                                                       &usb_fixed, &usb1v5, 1,
788                                                       features);
789                         if (IS_ERR(child))
790                                 return PTR_ERR(child);
791
792                         child = add_regulator_linked(TWL4030_REG_VUSB1V8,
793                                                       &usb_fixed, &usb1v8, 1,
794                                                       features);
795                         if (IS_ERR(child))
796                                 return PTR_ERR(child);
797
798                         child = add_regulator_linked(TWL4030_REG_VUSB3V1,
799                                                       &usb_fixed, &usb3v1, 1,
800                                                       features);
801                         if (IS_ERR(child))
802                                 return PTR_ERR(child);
803
804                 }
805
806                 child = add_child(0, "twl4030_usb",
807                                 pdata->usb, sizeof(*pdata->usb),
808                                 true,
809                                 /* irq0 = USB_PRES, irq1 = USB */
810                                 pdata->irq_base + USB_PRES_INTR_OFFSET,
811                                 pdata->irq_base + USB_INTR_OFFSET);
812
813                 if (IS_ERR(child))
814                         return PTR_ERR(child);
815
816                 /* we need to connect regulators to this transceiver */
817                 if (twl_has_regulator() && child) {
818                         usb1v5.dev = child;
819                         usb1v8.dev = child;
820                         usb3v1.dev = child;
821                 }
822         }
823         if (twl_has_usb() && pdata->usb && twl_class_is_6030()) {
824
825                 static struct regulator_consumer_supply usb3v3;
826                 int regulator;
827
828                 if (twl_has_regulator()) {
829                         /* this is a template that gets copied */
830                         struct regulator_init_data usb_fixed = {
831                                 .constraints.valid_modes_mask =
832                                         REGULATOR_MODE_NORMAL
833                                         | REGULATOR_MODE_STANDBY,
834                                 .constraints.valid_ops_mask =
835                                         REGULATOR_CHANGE_MODE
836                                         | REGULATOR_CHANGE_STATUS,
837                         };
838
839                         if (features & TWL6025_SUBCLASS) {
840                                 usb3v3.supply = "ldousb";
841                                 regulator = TWL6025_REG_LDOUSB;
842                         } else {
843                                 usb3v3.supply = "vusb";
844                                 regulator = TWL6030_REG_VUSB;
845                         }
846                         child = add_regulator_linked(regulator, &usb_fixed,
847                                                         &usb3v3, 1,
848                                                         features);
849                         if (IS_ERR(child))
850                                 return PTR_ERR(child);
851                 }
852
853                 pdata->usb->features = features;
854
855                 child = add_child(0, "twl6030_usb",
856                         pdata->usb, sizeof(*pdata->usb),
857                         true,
858                         /* irq1 = VBUS_PRES, irq0 = USB ID */
859                         pdata->irq_base + USBOTG_INTR_OFFSET,
860                         pdata->irq_base + USB_PRES_INTR_OFFSET);
861
862                 if (IS_ERR(child))
863                         return PTR_ERR(child);
864                 /* we need to connect regulators to this transceiver */
865                 if (twl_has_regulator() && child)
866                         usb3v3.dev = child;
867         } else if (twl_has_regulator() && twl_class_is_6030()) {
868                 if (features & TWL6025_SUBCLASS)
869                         child = add_regulator(TWL6025_REG_LDOUSB,
870                                                 pdata->ldousb, features);
871                 else
872                         child = add_regulator(TWL6030_REG_VUSB,
873                                                 pdata->vusb, features);
874
875                         if (IS_ERR(child))
876                                         return PTR_ERR(child);
877         }
878
879         if (twl_has_watchdog() && twl_class_is_4030()) {
880                 child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
881                 if (IS_ERR(child))
882                         return PTR_ERR(child);
883         }
884
885         if (twl_has_pwrbutton() && twl_class_is_4030()) {
886                 child = add_child(1, "twl4030_pwrbutton",
887                                 NULL, 0, true, pdata->irq_base + 8 + 0, 0);
888                 if (IS_ERR(child))
889                         return PTR_ERR(child);
890         }
891
892         if (twl_has_codec() && pdata->audio && twl_class_is_4030()) {
893                 sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
894                 child = add_child(sub_chip_id, "twl4030-audio",
895                                 pdata->audio, sizeof(*pdata->audio),
896                                 false, 0, 0);
897                 if (IS_ERR(child))
898                         return PTR_ERR(child);
899         }
900
901         if (twl_has_codec() && pdata->audio && twl_class_is_6030()) {
902                 sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
903                 child = add_child(sub_chip_id, "twl6040",
904                                 pdata->audio, sizeof(*pdata->audio),
905                                 false, 0, 0);
906                 if (IS_ERR(child))
907                         return PTR_ERR(child);
908         }
909
910         /* twl4030 regulators */
911         if (twl_has_regulator() && twl_class_is_4030()) {
912                 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
913                                         features);
914                 if (IS_ERR(child))
915                         return PTR_ERR(child);
916
917                 child = add_regulator(TWL4030_REG_VIO, pdata->vio,
918                                         features);
919                 if (IS_ERR(child))
920                         return PTR_ERR(child);
921
922                 child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
923                                         features);
924                 if (IS_ERR(child))
925                         return PTR_ERR(child);
926
927                 child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
928                                         features);
929                 if (IS_ERR(child))
930                         return PTR_ERR(child);
931
932                 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
933                                         features);
934                 if (IS_ERR(child))
935                         return PTR_ERR(child);
936
937                 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
938                                         features);
939                 if (IS_ERR(child))
940                         return PTR_ERR(child);
941
942                 child = add_regulator((features & TWL4030_VAUX2)
943                                         ? TWL4030_REG_VAUX2_4030
944                                         : TWL4030_REG_VAUX2,
945                                 pdata->vaux2, features);
946                 if (IS_ERR(child))
947                         return PTR_ERR(child);
948
949                 child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
950                                         features);
951                 if (IS_ERR(child))
952                         return PTR_ERR(child);
953
954                 child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
955                                         features);
956                 if (IS_ERR(child))
957                         return PTR_ERR(child);
958
959                 child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
960                                         features);
961                 if (IS_ERR(child))
962                         return PTR_ERR(child);
963         }
964
965         /* maybe add LDOs that are omitted on cost-reduced parts */
966         if (twl_has_regulator() && !(features & TPS_SUBSET)
967           && twl_class_is_4030()) {
968                 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
969                                         features);
970                 if (IS_ERR(child))
971                         return PTR_ERR(child);
972
973                 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
974                                         features);
975                 if (IS_ERR(child))
976                         return PTR_ERR(child);
977
978                 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
979                                         features);
980                 if (IS_ERR(child))
981                         return PTR_ERR(child);
982
983                 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
984                                         features);
985                 if (IS_ERR(child))
986                         return PTR_ERR(child);
987
988                 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
989                                         features);
990                 if (IS_ERR(child))
991                         return PTR_ERR(child);
992
993                 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
994                                         features);
995                 if (IS_ERR(child))
996                         return PTR_ERR(child);
997         }
998
999         /* twl6030 regulators */
1000         if (twl_has_regulator() && twl_class_is_6030() &&
1001                         !(features & TWL6025_SUBCLASS)) {
1002                 child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc,
1003                                         features);
1004                 if (IS_ERR(child))
1005                         return PTR_ERR(child);
1006
1007                 child = add_regulator(TWL6030_REG_VPP, pdata->vpp,
1008                                         features);
1009                 if (IS_ERR(child))
1010                         return PTR_ERR(child);
1011
1012                 child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim,
1013                                         features);
1014                 if (IS_ERR(child))
1015                         return PTR_ERR(child);
1016
1017                 child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio,
1018                                         features);
1019                 if (IS_ERR(child))
1020                         return PTR_ERR(child);
1021
1022                 child = add_regulator(TWL6030_REG_VDAC, pdata->vdac,
1023                                         features);
1024                 if (IS_ERR(child))
1025                         return PTR_ERR(child);
1026
1027                 child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1,
1028                                         features);
1029                 if (IS_ERR(child))
1030                         return PTR_ERR(child);
1031
1032                 child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2,
1033                                         features);
1034                 if (IS_ERR(child))
1035                         return PTR_ERR(child);
1036
1037                 child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3,
1038                                         features);
1039                 if (IS_ERR(child))
1040                         return PTR_ERR(child);
1041
1042                 child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg,
1043                                         features);
1044                 if (IS_ERR(child))
1045                         return PTR_ERR(child);
1046         }
1047
1048         /* 6030 and 6025 share this regulator */
1049         if (twl_has_regulator() && twl_class_is_6030()) {
1050                 child = add_regulator(TWL6030_REG_VANA, pdata->vana,
1051                                         features);
1052                 if (IS_ERR(child))
1053                         return PTR_ERR(child);
1054         }
1055
1056         /* twl6025 regulators */
1057         if (twl_has_regulator() && twl_class_is_6030() &&
1058                         (features & TWL6025_SUBCLASS)) {
1059                 child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5,
1060                                         features);
1061                 if (IS_ERR(child))
1062                         return PTR_ERR(child);
1063
1064                 child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1,
1065                                         features);
1066                 if (IS_ERR(child))
1067                         return PTR_ERR(child);
1068
1069                 child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7,
1070                                         features);
1071                 if (IS_ERR(child))
1072                         return PTR_ERR(child);
1073
1074                 child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6,
1075                                         features);
1076                 if (IS_ERR(child))
1077                         return PTR_ERR(child);
1078
1079                 child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln,
1080                                         features);
1081                 if (IS_ERR(child))
1082                         return PTR_ERR(child);
1083
1084                 child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2,
1085                                         features);
1086                 if (IS_ERR(child))
1087                         return PTR_ERR(child);
1088
1089                 child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4,
1090                                         features);
1091                 if (IS_ERR(child))
1092                         return PTR_ERR(child);
1093
1094                 child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3,
1095                                         features);
1096                 if (IS_ERR(child))
1097                         return PTR_ERR(child);
1098
1099                 child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3,
1100                                         features);
1101                 if (IS_ERR(child))
1102                         return PTR_ERR(child);
1103
1104                 child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4,
1105                                         features);
1106                 if (IS_ERR(child))
1107                         return PTR_ERR(child);
1108
1109                 child = add_regulator(TWL6025_REG_VIO, pdata->vio6025,
1110                                         features);
1111                 if (IS_ERR(child))
1112                         return PTR_ERR(child);
1113
1114         }
1115
1116         if (twl_has_bci() && pdata->bci &&
1117                         !(features & (TPS_SUBSET | TWL5031))) {
1118                 child = add_child(3, "twl4030_bci",
1119                                 pdata->bci, sizeof(*pdata->bci), false,
1120                                 /* irq0 = CHG_PRES, irq1 = BCI */
1121                                 pdata->irq_base + BCI_PRES_INTR_OFFSET,
1122                                 pdata->irq_base + BCI_INTR_OFFSET);
1123                 if (IS_ERR(child))
1124                         return PTR_ERR(child);
1125         }
1126
1127         return 0;
1128 }
1129
1130 /*----------------------------------------------------------------------*/
1131
1132 /*
1133  * These three functions initialize the on-chip clock framework,
1134  * letting it generate the right frequencies for USB, MADC, and
1135  * other purposes.
1136  */
1137 static inline int __init protect_pm_master(void)
1138 {
1139         int e = 0;
1140
1141         e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
1142                         TWL4030_PM_MASTER_PROTECT_KEY);
1143         return e;
1144 }
1145
1146 static inline int __init unprotect_pm_master(void)
1147 {
1148         int e = 0;
1149
1150         e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
1151                         TWL4030_PM_MASTER_KEY_CFG1,
1152                         TWL4030_PM_MASTER_PROTECT_KEY);
1153         e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
1154                         TWL4030_PM_MASTER_KEY_CFG2,
1155                         TWL4030_PM_MASTER_PROTECT_KEY);
1156
1157         return e;
1158 }
1159
1160 static void clocks_init(struct device *dev,
1161                         struct twl4030_clock_init_data *clock,
1162                         unsigned long features)
1163 {
1164         int e = 0;
1165         struct clk *osc;
1166         u32 rate;
1167         u8 ctrl = HFCLK_FREQ_26_MHZ;
1168
1169         if (features & MPU80031_SUBCLASS) {
1170                 if (clock && clock->clk32_active_state_on) {
1171                         e = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER, 0x1,
1172                                         REG_CLK32KG_CFG_TRANS);
1173                         if (!e)
1174                                 e = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER, 0x1,
1175                                         REG_CLK32KG_CFG_STATE);
1176                         if (e) {
1177                                 dev_err(dev, "Error in initialization"
1178                                                 " of 32K output\n");
1179                                 return;
1180                         }
1181                 }
1182         }
1183
1184 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
1185         if (cpu_is_omap2430())
1186                 osc = clk_get(dev, "osc_ck");
1187         else
1188                 osc = clk_get(dev, "osc_sys_ck");
1189
1190         if (IS_ERR(osc)) {
1191                 printk(KERN_WARNING "Skipping twl internal clock init and "
1192                                 "using bootloader value (unknown osc rate)\n");
1193                 return;
1194         }
1195
1196         rate = clk_get_rate(osc);
1197         clk_put(osc);
1198
1199 #else
1200         /* REVISIT for non-OMAP systems, pass the clock rate from
1201          * board init code, using platform_data.
1202          */
1203         osc = ERR_PTR(-EIO);
1204
1205         printk(KERN_WARNING "Skipping twl internal clock init and "
1206                "using bootloader value (unknown osc rate)\n");
1207
1208         return;
1209 #endif
1210
1211         switch (rate) {
1212         case 19200000:
1213                 ctrl = HFCLK_FREQ_19p2_MHZ;
1214                 break;
1215         case 26000000:
1216                 ctrl = HFCLK_FREQ_26_MHZ;
1217                 break;
1218         case 38400000:
1219                 ctrl = HFCLK_FREQ_38p4_MHZ;
1220                 break;
1221         }
1222
1223         ctrl |= HIGH_PERF_SQ;
1224         if (clock && clock->ck32k_lowpwr_enable)
1225                 ctrl |= CK32K_LOWPWR_EN;
1226
1227         e |= unprotect_pm_master();
1228         /* effect->MADC+USB ck en */
1229         e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
1230         e |= protect_pm_master();
1231
1232         if (e < 0)
1233                 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
1234 }
1235
1236 /*----------------------------------------------------------------------*/
1237
1238 int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
1239 int twl4030_exit_irq(void);
1240 int twl4030_init_chip_irq(const char *chip);
1241 int twl6030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
1242 int twl6030_exit_irq(void);
1243
1244 static int twl_remove(struct i2c_client *client)
1245 {
1246         unsigned i;
1247         int status;
1248
1249         if (twl_class_is_4030())
1250                 status = twl4030_exit_irq();
1251         else
1252                 status = twl6030_exit_irq();
1253
1254         if (status < 0)
1255                 return status;
1256
1257         for (i = 0; i < TWL_NUM_SLAVES; i++) {
1258                 struct twl_client       *twl = &twl_modules[i];
1259
1260                 if (twl->client && twl->client != client)
1261                         i2c_unregister_device(twl->client);
1262                 twl_modules[i].client = NULL;
1263         }
1264         inuse = false;
1265         return 0;
1266 }
1267
1268 /* NOTE:  this driver only handles a single twl4030/tps659x0 chip */
1269 static int __devinit
1270 twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
1271 {
1272         int                             status;
1273         unsigned                        i;
1274         struct twl4030_platform_data    *pdata = client->dev.platform_data;
1275         u8 temp;
1276         int ret = 0;
1277
1278         if (!pdata) {
1279                 dev_dbg(&client->dev, "no platform data?\n");
1280                 return -EINVAL;
1281         }
1282
1283         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1284                 dev_dbg(&client->dev, "can't talk I2C?\n");
1285                 return -EIO;
1286         }
1287
1288         if (inuse) {
1289                 dev_dbg(&client->dev, "driver is already in use\n");
1290                 return -EBUSY;
1291         }
1292
1293         for (i = 0; i < TWL_NUM_SLAVES; i++) {
1294                 struct twl_client       *twl = &twl_modules[i];
1295
1296                 twl->address = client->addr + i;
1297                 if (i == 0)
1298                         twl->client = client;
1299                 else {
1300                         twl->client = i2c_new_dummy(client->adapter,
1301                                         twl->address);
1302                         if (!twl->client) {
1303                                 dev_err(&client->dev,
1304                                         "can't attach client %d\n", i);
1305                                 status = -ENOMEM;
1306                                 goto fail;
1307                         }
1308                 }
1309                 mutex_init(&twl->xfer_lock);
1310         }
1311         inuse = true;
1312         if ((id->driver_data) & TWL6030_CLASS) {
1313                 twl_id = TWL6030_CLASS_ID;
1314                 twl_map = &twl6030_map[0];
1315         } else {
1316                 twl_id = TWL4030_CLASS_ID;
1317                 twl_map = &twl4030_map[0];
1318         }
1319
1320         /* setup clock framework */
1321         clocks_init(&client->dev, pdata->clock, id->driver_data);
1322
1323         /* read TWL IDCODE Register */
1324         if (twl_id == TWL4030_CLASS_ID) {
1325                 ret = twl_read_idcode_register();
1326                 WARN(ret < 0, "Error: reading twl_idcode register value\n");
1327         }
1328
1329         /* load power event scripts */
1330         if (twl_has_power() && pdata->power)
1331                 twl4030_power_init(pdata->power);
1332
1333         /* Maybe init the T2 Interrupt subsystem */
1334         if (client->irq
1335                         && pdata->irq_base
1336                         && pdata->irq_end > pdata->irq_base) {
1337                 if (twl_class_is_4030()) {
1338                         twl4030_init_chip_irq(id->name);
1339                         status = twl4030_init_irq(client->irq, pdata->irq_base,
1340                         pdata->irq_end);
1341                 } else {
1342                         status = twl6030_init_irq(client->irq, pdata->irq_base,
1343                         pdata->irq_end);
1344                 }
1345
1346                 if (status < 0)
1347                         goto fail;
1348         }
1349
1350         /* Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
1351          * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1352          * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1353          */
1354
1355         if (twl_class_is_4030()) {
1356                 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
1357                 temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
1358                 I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
1359                 twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
1360         }
1361
1362         twl_feat = id->driver_data;
1363
1364         status = add_children(pdata, id->driver_data);
1365         if (status < 0 )
1366                 goto fail;
1367
1368         /* Board Specific Init Callback */
1369         if(pdata->init)
1370                 status = pdata->init();
1371
1372 fail:
1373         if (status < 0)
1374                 twl_remove(client);
1375         return status;
1376 }
1377
1378 static const struct i2c_device_id twl_ids[] = {
1379         { "twl4030", TWL4030_VAUX2 },   /* "Triton 2" */
1380         { "twl5030", 0 },               /* T2 updated */
1381         { "twl5031", TWL5031 },         /* TWL5030 updated */
1382         { "tps65950", 0 },              /* catalog version of twl5030 */
1383         { "tps65930", TPS_SUBSET },     /* fewer LDOs and DACs; no charger */
1384         { "tps65920", TPS_SUBSET },     /* fewer LDOs; no codec or charger */
1385         { "tps65921", TPS_SUBSET },     /* fewer LDOs; no codec, no LED
1386                                            and vibrator. Charger in USB module*/
1387         { "twl6030", TWL6030_CLASS },   /* "Phoenix power chip" */
1388         { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */
1389         { "mpu80031", TWL6030_CLASS | TWL6025_SUBCLASS | MPU80031_SUBCLASS},
1390         { /* end of list */ },
1391 };
1392 MODULE_DEVICE_TABLE(i2c, twl_ids);
1393
1394 /* One Client Driver , 4 Clients */
1395 static struct i2c_driver twl_driver = {
1396         .driver.name    = DRIVER_NAME,
1397         .id_table       = twl_ids,
1398         .probe          = twl_probe,
1399         .remove         = twl_remove,
1400 };
1401
1402 static int __init twl_init(void)
1403 {
1404         return i2c_add_driver(&twl_driver);
1405 }
1406 subsys_initcall(twl_init);
1407
1408 static void __exit twl_exit(void)
1409 {
1410         i2c_del_driver(&twl_driver);
1411 }
1412 module_exit(twl_exit);
1413
1414 MODULE_AUTHOR("Texas Instruments, Inc.");
1415 MODULE_DESCRIPTION("I2C Core interface for TWL");
1416 MODULE_LICENSE("GPL");