]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/mfd/twl-core.c
mfd: Clarify twl4030 return value for read and write
[net-next-2.6.git] / drivers / mfd / twl-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/init.h>
31 #include <linux/mutex.h>
32 #include <linux/platform_device.h>
33 #include <linux/clk.h>
34 #include <linux/err.h>
35
36 #include <linux/regulator/machine.h>
37
38 #include <linux/i2c.h>
39 #include <linux/i2c/twl.h>
40
41 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
42 #include <plat/cpu.h>
43 #endif
44
45 /*
46  * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
47  * Management and System Companion Device" chips originally designed for
48  * use in OMAP2 and OMAP 3 based systems.  Its control interfaces use I2C,
49  * often at around 3 Mbit/sec, including for interrupt handling.
50  *
51  * This driver core provides genirq support for the interrupts emitted,
52  * by the various modules, and exports register access primitives.
53  *
54  * FIXME this driver currently requires use of the first interrupt line
55  * (and associated registers).
56  */
57
58 #define DRIVER_NAME                     "twl4030"
59
60 #if defined(CONFIG_TWL4030_BCI_BATTERY) || \
61         defined(CONFIG_TWL4030_BCI_BATTERY_MODULE)
62 #define twl_has_bci()           true
63 #else
64 #define twl_has_bci()           false
65 #endif
66
67 #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
68 #define twl_has_keypad()        true
69 #else
70 #define twl_has_keypad()        false
71 #endif
72
73 #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
74 #define twl_has_gpio()  true
75 #else
76 #define twl_has_gpio()  false
77 #endif
78
79 #if defined(CONFIG_REGULATOR_TWL4030) \
80         || defined(CONFIG_REGULATOR_TWL4030_MODULE)
81 #define twl_has_regulator()     true
82 #else
83 #define twl_has_regulator()     false
84 #endif
85
86 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
87 #define twl_has_madc()  true
88 #else
89 #define twl_has_madc()  false
90 #endif
91
92 #ifdef CONFIG_TWL4030_POWER
93 #define twl_has_power()        true
94 #else
95 #define twl_has_power()        false
96 #endif
97
98 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
99 #define twl_has_rtc()   true
100 #else
101 #define twl_has_rtc()   false
102 #endif
103
104 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
105 #define twl_has_usb()   true
106 #else
107 #define twl_has_usb()   false
108 #endif
109
110 #if defined(CONFIG_TWL4030_WATCHDOG) || \
111         defined(CONFIG_TWL4030_WATCHDOG_MODULE)
112 #define twl_has_watchdog()        true
113 #else
114 #define twl_has_watchdog()        false
115 #endif
116
117 #if defined(CONFIG_TWL4030_CODEC) || defined(CONFIG_TWL4030_CODEC_MODULE)
118 #define twl_has_codec() true
119 #else
120 #define twl_has_codec() false
121 #endif
122
123 /* Triton Core internal information (BEGIN) */
124
125 /* Last - for index max*/
126 #define TWL4030_MODULE_LAST             TWL4030_MODULE_SECURED_REG
127
128 #define TWL4030_NUM_SLAVES              4
129
130 #if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
131         || defined(CONFIG_INPUT_TWL4030_PWBUTTON_MODULE)
132 #define twl_has_pwrbutton()     true
133 #else
134 #define twl_has_pwrbutton()     false
135 #endif
136
137 /* Base Address defns for twl4030_map[] */
138
139 /* subchip/slave 0 - USB ID */
140 #define TWL4030_BASEADD_USB             0x0000
141
142 /* subchip/slave 1 - AUD ID */
143 #define TWL4030_BASEADD_AUDIO_VOICE     0x0000
144 #define TWL4030_BASEADD_GPIO            0x0098
145 #define TWL4030_BASEADD_INTBR           0x0085
146 #define TWL4030_BASEADD_PIH             0x0080
147 #define TWL4030_BASEADD_TEST            0x004C
148
149 /* subchip/slave 2 - AUX ID */
150 #define TWL4030_BASEADD_INTERRUPTS      0x00B9
151 #define TWL4030_BASEADD_LED             0x00EE
152 #define TWL4030_BASEADD_MADC            0x0000
153 #define TWL4030_BASEADD_MAIN_CHARGE     0x0074
154 #define TWL4030_BASEADD_PRECHARGE       0x00AA
155 #define TWL4030_BASEADD_PWM0            0x00F8
156 #define TWL4030_BASEADD_PWM1            0x00FB
157 #define TWL4030_BASEADD_PWMA            0x00EF
158 #define TWL4030_BASEADD_PWMB            0x00F1
159 #define TWL4030_BASEADD_KEYPAD          0x00D2
160
161 #define TWL5031_BASEADD_ACCESSORY       0x0074 /* Replaces Main Charge */
162 #define TWL5031_BASEADD_INTERRUPTS      0x00B9 /* Different than TWL4030's
163                                                   one */
164
165 /* subchip/slave 3 - POWER ID */
166 #define TWL4030_BASEADD_BACKUP          0x0014
167 #define TWL4030_BASEADD_INT             0x002E
168 #define TWL4030_BASEADD_PM_MASTER       0x0036
169 #define TWL4030_BASEADD_PM_RECEIVER     0x005B
170 #define TWL4030_BASEADD_RTC             0x001C
171 #define TWL4030_BASEADD_SECURED_REG     0x0000
172
173 /* Triton Core internal information (END) */
174
175
176 /* Few power values */
177 #define R_CFG_BOOT                      0x05
178 #define R_PROTECT_KEY                   0x0E
179
180 /* access control values for R_PROTECT_KEY */
181 #define KEY_UNLOCK1                     0xce
182 #define KEY_UNLOCK2                     0xec
183 #define KEY_LOCK                        0x00
184
185 /* some fields in R_CFG_BOOT */
186 #define HFCLK_FREQ_19p2_MHZ             (1 << 0)
187 #define HFCLK_FREQ_26_MHZ               (2 << 0)
188 #define HFCLK_FREQ_38p4_MHZ             (3 << 0)
189 #define HIGH_PERF_SQ                    (1 << 3)
190 #define CK32K_LOWPWR_EN                 (1 << 7)
191
192
193 /* chip-specific feature flags, for i2c_device_id.driver_data */
194 #define TWL4030_VAUX2           BIT(0)  /* pre-5030 voltage ranges */
195 #define TPS_SUBSET              BIT(1)  /* tps659[23]0 have fewer LDOs */
196 #define TWL5031                 BIT(2)  /* twl5031 has different registers */
197
198 /*----------------------------------------------------------------------*/
199
200 /* is driver active, bound to a chip? */
201 static bool inuse;
202
203 /* Structure for each TWL4030 Slave */
204 struct twl4030_client {
205         struct i2c_client *client;
206         u8 address;
207
208         /* max numb of i2c_msg required is for read =2 */
209         struct i2c_msg xfer_msg[2];
210
211         /* To lock access to xfer_msg */
212         struct mutex xfer_lock;
213 };
214
215 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
216
217
218 /* mapping the module id to slave id and base address */
219 struct twl4030mapping {
220         unsigned char sid;      /* Slave ID */
221         unsigned char base;     /* base address */
222 };
223
224 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
225         /*
226          * NOTE:  don't change this table without updating the
227          * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
228          * so they continue to match the order in this table.
229          */
230
231         { 0, TWL4030_BASEADD_USB },
232
233         { 1, TWL4030_BASEADD_AUDIO_VOICE },
234         { 1, TWL4030_BASEADD_GPIO },
235         { 1, TWL4030_BASEADD_INTBR },
236         { 1, TWL4030_BASEADD_PIH },
237         { 1, TWL4030_BASEADD_TEST },
238
239         { 2, TWL4030_BASEADD_KEYPAD },
240         { 2, TWL4030_BASEADD_MADC },
241         { 2, TWL4030_BASEADD_INTERRUPTS },
242         { 2, TWL4030_BASEADD_LED },
243         { 2, TWL4030_BASEADD_MAIN_CHARGE },
244         { 2, TWL4030_BASEADD_PRECHARGE },
245         { 2, TWL4030_BASEADD_PWM0 },
246         { 2, TWL4030_BASEADD_PWM1 },
247         { 2, TWL4030_BASEADD_PWMA },
248         { 2, TWL4030_BASEADD_PWMB },
249         { 2, TWL5031_BASEADD_ACCESSORY },
250         { 2, TWL5031_BASEADD_INTERRUPTS },
251
252         { 3, TWL4030_BASEADD_BACKUP },
253         { 3, TWL4030_BASEADD_INT },
254         { 3, TWL4030_BASEADD_PM_MASTER },
255         { 3, TWL4030_BASEADD_PM_RECEIVER },
256         { 3, TWL4030_BASEADD_RTC },
257         { 3, TWL4030_BASEADD_SECURED_REG },
258 };
259
260 /*----------------------------------------------------------------------*/
261
262 /* Exported Functions */
263
264 /**
265  * twl4030_i2c_write - Writes a n bit register in TWL4030
266  * @mod_no: module number
267  * @value: an array of num_bytes+1 containing data to write
268  * @reg: register address (just offset will do)
269  * @num_bytes: number of bytes to transfer
270  *
271  * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
272  * valid data starts at Offset 1.
273  *
274  * Returns the result of operation - 0 is success
275  */
276 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
277 {
278         int ret;
279         int sid;
280         struct twl4030_client *twl;
281         struct i2c_msg *msg;
282
283         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
284                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
285                 return -EPERM;
286         }
287         sid = twl4030_map[mod_no].sid;
288         twl = &twl4030_modules[sid];
289
290         if (unlikely(!inuse)) {
291                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
292                 return -EPERM;
293         }
294         mutex_lock(&twl->xfer_lock);
295         /*
296          * [MSG1]: fill the register address data
297          * fill the data Tx buffer
298          */
299         msg = &twl->xfer_msg[0];
300         msg->addr = twl->address;
301         msg->len = num_bytes + 1;
302         msg->flags = 0;
303         msg->buf = value;
304         /* over write the first byte of buffer with the register address */
305         *value = twl4030_map[mod_no].base + reg;
306         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
307         mutex_unlock(&twl->xfer_lock);
308
309         /* i2c_transfer returns number of messages transferred */
310         if (ret != 1) {
311                 pr_err("%s: i2c_write failed to transfer all messages\n",
312                         DRIVER_NAME);
313                 if (ret < 0)
314                         return ret;
315                 else
316                         return -EIO;
317         } else {
318                 return 0;
319         }
320 }
321 EXPORT_SYMBOL(twl4030_i2c_write);
322
323 /**
324  * twl4030_i2c_read - Reads a n bit register in TWL4030
325  * @mod_no: module number
326  * @value: an array of num_bytes containing data to be read
327  * @reg: register address (just offset will do)
328  * @num_bytes: number of bytes to transfer
329  *
330  * Returns result of operation - num_bytes is success else failure.
331  */
332 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
333 {
334         int ret;
335         u8 val;
336         int sid;
337         struct twl4030_client *twl;
338         struct i2c_msg *msg;
339
340         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
341                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
342                 return -EPERM;
343         }
344         sid = twl4030_map[mod_no].sid;
345         twl = &twl4030_modules[sid];
346
347         if (unlikely(!inuse)) {
348                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
349                 return -EPERM;
350         }
351         mutex_lock(&twl->xfer_lock);
352         /* [MSG1] fill the register address data */
353         msg = &twl->xfer_msg[0];
354         msg->addr = twl->address;
355         msg->len = 1;
356         msg->flags = 0; /* Read the register value */
357         val = twl4030_map[mod_no].base + reg;
358         msg->buf = &val;
359         /* [MSG2] fill the data rx buffer */
360         msg = &twl->xfer_msg[1];
361         msg->addr = twl->address;
362         msg->flags = I2C_M_RD;  /* Read the register value */
363         msg->len = num_bytes;   /* only n bytes */
364         msg->buf = value;
365         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
366         mutex_unlock(&twl->xfer_lock);
367
368         /* i2c_transfer returns number of messages transferred */
369         if (ret != 2) {
370                 pr_err("%s: i2c_read failed to transfer all messages\n",
371                         DRIVER_NAME);
372                 if (ret < 0)
373                         return ret;
374                 else
375                         return -EIO;
376         } else {
377                 return 0;
378         }
379 }
380 EXPORT_SYMBOL(twl4030_i2c_read);
381
382 /**
383  * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
384  * @mod_no: module number
385  * @value: the value to be written 8 bit
386  * @reg: register address (just offset will do)
387  *
388  * Returns result of operation - 0 is success
389  */
390 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
391 {
392
393         /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
394         u8 temp_buffer[2] = { 0 };
395         /* offset 1 contains the data */
396         temp_buffer[1] = value;
397         return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
398 }
399 EXPORT_SYMBOL(twl4030_i2c_write_u8);
400
401 /**
402  * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
403  * @mod_no: module number
404  * @value: the value read 8 bit
405  * @reg: register address (just offset will do)
406  *
407  * Returns result of operation - 0 is success
408  */
409 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
410 {
411         return twl4030_i2c_read(mod_no, value, reg, 1);
412 }
413 EXPORT_SYMBOL(twl4030_i2c_read_u8);
414
415 /*----------------------------------------------------------------------*/
416
417 static struct device *
418 add_numbered_child(unsigned chip, const char *name, int num,
419                 void *pdata, unsigned pdata_len,
420                 bool can_wakeup, int irq0, int irq1)
421 {
422         struct platform_device  *pdev;
423         struct twl4030_client   *twl = &twl4030_modules[chip];
424         int                     status;
425
426         pdev = platform_device_alloc(name, num);
427         if (!pdev) {
428                 dev_dbg(&twl->client->dev, "can't alloc dev\n");
429                 status = -ENOMEM;
430                 goto err;
431         }
432
433         device_init_wakeup(&pdev->dev, can_wakeup);
434         pdev->dev.parent = &twl->client->dev;
435
436         if (pdata) {
437                 status = platform_device_add_data(pdev, pdata, pdata_len);
438                 if (status < 0) {
439                         dev_dbg(&pdev->dev, "can't add platform_data\n");
440                         goto err;
441                 }
442         }
443
444         if (irq0) {
445                 struct resource r[2] = {
446                         { .start = irq0, .flags = IORESOURCE_IRQ, },
447                         { .start = irq1, .flags = IORESOURCE_IRQ, },
448                 };
449
450                 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
451                 if (status < 0) {
452                         dev_dbg(&pdev->dev, "can't add irqs\n");
453                         goto err;
454                 }
455         }
456
457         status = platform_device_add(pdev);
458
459 err:
460         if (status < 0) {
461                 platform_device_put(pdev);
462                 dev_err(&twl->client->dev, "can't add %s dev\n", name);
463                 return ERR_PTR(status);
464         }
465         return &pdev->dev;
466 }
467
468 static inline struct device *add_child(unsigned chip, const char *name,
469                 void *pdata, unsigned pdata_len,
470                 bool can_wakeup, int irq0, int irq1)
471 {
472         return add_numbered_child(chip, name, -1, pdata, pdata_len,
473                 can_wakeup, irq0, irq1);
474 }
475
476 static struct device *
477 add_regulator_linked(int num, struct regulator_init_data *pdata,
478                 struct regulator_consumer_supply *consumers,
479                 unsigned num_consumers)
480 {
481         /* regulator framework demands init_data ... */
482         if (!pdata)
483                 return NULL;
484
485         if (consumers) {
486                 pdata->consumer_supplies = consumers;
487                 pdata->num_consumer_supplies = num_consumers;
488         }
489
490         /* NOTE:  we currently ignore regulator IRQs, e.g. for short circuits */
491         return add_numbered_child(3, "twl4030_reg", num,
492                 pdata, sizeof(*pdata), false, 0, 0);
493 }
494
495 static struct device *
496 add_regulator(int num, struct regulator_init_data *pdata)
497 {
498         return add_regulator_linked(num, pdata, NULL, 0);
499 }
500
501 /*
502  * NOTE:  We know the first 8 IRQs after pdata->base_irq are
503  * for the PIH, and the next are for the PWR_INT SIH, since
504  * that's how twl_init_irq() sets things up.
505  */
506
507 static int
508 add_children(struct twl4030_platform_data *pdata, unsigned long features)
509 {
510         struct device   *child;
511
512         if (twl_has_bci() && pdata->bci &&
513             !(features & (TPS_SUBSET | TWL5031))) {
514                 child = add_child(3, "twl4030_bci",
515                                 pdata->bci, sizeof(*pdata->bci),
516                                 false,
517                                 /* irq0 = CHG_PRES, irq1 = BCI */
518                                 pdata->irq_base + 8 + 1, pdata->irq_base + 2);
519                 if (IS_ERR(child))
520                         return PTR_ERR(child);
521         }
522
523         if (twl_has_gpio() && pdata->gpio) {
524                 child = add_child(1, "twl4030_gpio",
525                                 pdata->gpio, sizeof(*pdata->gpio),
526                                 false, pdata->irq_base + 0, 0);
527                 if (IS_ERR(child))
528                         return PTR_ERR(child);
529         }
530
531         if (twl_has_keypad() && pdata->keypad) {
532                 child = add_child(2, "twl4030_keypad",
533                                 pdata->keypad, sizeof(*pdata->keypad),
534                                 true, pdata->irq_base + 1, 0);
535                 if (IS_ERR(child))
536                         return PTR_ERR(child);
537         }
538
539         if (twl_has_madc() && pdata->madc) {
540                 child = add_child(2, "twl4030_madc",
541                                 pdata->madc, sizeof(*pdata->madc),
542                                 true, pdata->irq_base + 3, 0);
543                 if (IS_ERR(child))
544                         return PTR_ERR(child);
545         }
546
547         if (twl_has_rtc()) {
548                 /*
549                  * REVISIT platform_data here currently might expose the
550                  * "msecure" line ... but for now we just expect board
551                  * setup to tell the chip "it's always ok to SET_TIME".
552                  * Eventually, Linux might become more aware of such
553                  * HW security concerns, and "least privilege".
554                  */
555                 child = add_child(3, "twl4030_rtc",
556                                 NULL, 0,
557                                 true, pdata->irq_base + 8 + 3, 0);
558                 if (IS_ERR(child))
559                         return PTR_ERR(child);
560         }
561
562         if (twl_has_usb() && pdata->usb) {
563
564                 static struct regulator_consumer_supply usb1v5 = {
565                         .supply =       "usb1v5",
566                 };
567                 static struct regulator_consumer_supply usb1v8 = {
568                         .supply =       "usb1v8",
569                 };
570                 static struct regulator_consumer_supply usb3v1 = {
571                         .supply =       "usb3v1",
572                 };
573
574         /* First add the regulators so that they can be used by transceiver */
575                 if (twl_has_regulator()) {
576                         /* this is a template that gets copied */
577                         struct regulator_init_data usb_fixed = {
578                                 .constraints.valid_modes_mask =
579                                         REGULATOR_MODE_NORMAL
580                                         | REGULATOR_MODE_STANDBY,
581                                 .constraints.valid_ops_mask =
582                                         REGULATOR_CHANGE_MODE
583                                         | REGULATOR_CHANGE_STATUS,
584                         };
585
586                         child = add_regulator_linked(TWL4030_REG_VUSB1V5,
587                                                       &usb_fixed, &usb1v5, 1);
588                         if (IS_ERR(child))
589                                 return PTR_ERR(child);
590
591                         child = add_regulator_linked(TWL4030_REG_VUSB1V8,
592                                                       &usb_fixed, &usb1v8, 1);
593                         if (IS_ERR(child))
594                                 return PTR_ERR(child);
595
596                         child = add_regulator_linked(TWL4030_REG_VUSB3V1,
597                                                       &usb_fixed, &usb3v1, 1);
598                         if (IS_ERR(child))
599                                 return PTR_ERR(child);
600
601                 }
602
603                 child = add_child(0, "twl4030_usb",
604                                 pdata->usb, sizeof(*pdata->usb),
605                                 true,
606                                 /* irq0 = USB_PRES, irq1 = USB */
607                                 pdata->irq_base + 8 + 2, pdata->irq_base + 4);
608
609                 if (IS_ERR(child))
610                         return PTR_ERR(child);
611
612                 /* we need to connect regulators to this transceiver */
613                 if (twl_has_regulator() && child) {
614                         usb1v5.dev = child;
615                         usb1v8.dev = child;
616                         usb3v1.dev = child;
617                 }
618         }
619
620         if (twl_has_watchdog()) {
621                 child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
622                 if (IS_ERR(child))
623                         return PTR_ERR(child);
624         }
625
626         if (twl_has_pwrbutton()) {
627                 child = add_child(1, "twl4030_pwrbutton",
628                                 NULL, 0, true, pdata->irq_base + 8 + 0, 0);
629                 if (IS_ERR(child))
630                         return PTR_ERR(child);
631         }
632
633         if (twl_has_codec() && pdata->codec) {
634                 child = add_child(1, "twl4030_codec",
635                                 pdata->codec, sizeof(*pdata->codec),
636                                 false, 0, 0);
637                 if (IS_ERR(child))
638                         return PTR_ERR(child);
639         }
640
641         if (twl_has_regulator()) {
642                 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
643                 if (IS_ERR(child))
644                         return PTR_ERR(child);
645
646                 child = add_regulator(TWL4030_REG_VIO, pdata->vio);
647                 if (IS_ERR(child))
648                         return PTR_ERR(child);
649
650                 child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1);
651                 if (IS_ERR(child))
652                         return PTR_ERR(child);
653
654                 child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2);
655                 if (IS_ERR(child))
656                         return PTR_ERR(child);
657
658                 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
659                 if (IS_ERR(child))
660                         return PTR_ERR(child);
661
662                 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
663                 if (IS_ERR(child))
664                         return PTR_ERR(child);
665
666                 child = add_regulator((features & TWL4030_VAUX2)
667                                         ? TWL4030_REG_VAUX2_4030
668                                         : TWL4030_REG_VAUX2,
669                                 pdata->vaux2);
670                 if (IS_ERR(child))
671                         return PTR_ERR(child);
672
673                 child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1);
674                 if (IS_ERR(child))
675                         return PTR_ERR(child);
676
677                 child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2);
678                 if (IS_ERR(child))
679                         return PTR_ERR(child);
680
681                 child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig);
682                 if (IS_ERR(child))
683                         return PTR_ERR(child);
684         }
685
686         /* maybe add LDOs that are omitted on cost-reduced parts */
687         if (twl_has_regulator() && !(features & TPS_SUBSET)) {
688                 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
689                 if (IS_ERR(child))
690                         return PTR_ERR(child);
691
692                 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
693                 if (IS_ERR(child))
694                         return PTR_ERR(child);
695
696                 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
697                 if (IS_ERR(child))
698                         return PTR_ERR(child);
699
700                 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
701                 if (IS_ERR(child))
702                         return PTR_ERR(child);
703
704                 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
705                 if (IS_ERR(child))
706                         return PTR_ERR(child);
707
708                 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
709                 if (IS_ERR(child))
710                         return PTR_ERR(child);
711         }
712
713         return 0;
714 }
715
716 /*----------------------------------------------------------------------*/
717
718 /*
719  * These three functions initialize the on-chip clock framework,
720  * letting it generate the right frequencies for USB, MADC, and
721  * other purposes.
722  */
723 static inline int __init protect_pm_master(void)
724 {
725         int e = 0;
726
727         e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
728                         R_PROTECT_KEY);
729         return e;
730 }
731
732 static inline int __init unprotect_pm_master(void)
733 {
734         int e = 0;
735
736         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
737                         R_PROTECT_KEY);
738         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
739                         R_PROTECT_KEY);
740         return e;
741 }
742
743 static void clocks_init(struct device *dev,
744                         struct twl4030_clock_init_data *clock)
745 {
746         int e = 0;
747         struct clk *osc;
748         u32 rate;
749         u8 ctrl = HFCLK_FREQ_26_MHZ;
750
751 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
752         if (cpu_is_omap2430())
753                 osc = clk_get(dev, "osc_ck");
754         else
755                 osc = clk_get(dev, "osc_sys_ck");
756
757         if (IS_ERR(osc)) {
758                 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
759                                 "using bootloader value (unknown osc rate)\n");
760                 return;
761         }
762
763         rate = clk_get_rate(osc);
764         clk_put(osc);
765
766 #else
767         /* REVISIT for non-OMAP systems, pass the clock rate from
768          * board init code, using platform_data.
769          */
770         osc = ERR_PTR(-EIO);
771
772         printk(KERN_WARNING "Skipping twl4030 internal clock init and "
773                "using bootloader value (unknown osc rate)\n");
774
775         return;
776 #endif
777
778         switch (rate) {
779         case 19200000:
780                 ctrl = HFCLK_FREQ_19p2_MHZ;
781                 break;
782         case 26000000:
783                 ctrl = HFCLK_FREQ_26_MHZ;
784                 break;
785         case 38400000:
786                 ctrl = HFCLK_FREQ_38p4_MHZ;
787                 break;
788         }
789
790         ctrl |= HIGH_PERF_SQ;
791         if (clock && clock->ck32k_lowpwr_enable)
792                 ctrl |= CK32K_LOWPWR_EN;
793
794         e |= unprotect_pm_master();
795         /* effect->MADC+USB ck en */
796         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
797         e |= protect_pm_master();
798
799         if (e < 0)
800                 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
801 }
802
803 /*----------------------------------------------------------------------*/
804
805 int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
806 int twl_exit_irq(void);
807 int twl_init_chip_irq(const char *chip);
808
809 static int twl4030_remove(struct i2c_client *client)
810 {
811         unsigned i;
812         int status;
813
814         status = twl_exit_irq();
815         if (status < 0)
816                 return status;
817
818         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
819                 struct twl4030_client   *twl = &twl4030_modules[i];
820
821                 if (twl->client && twl->client != client)
822                         i2c_unregister_device(twl->client);
823                 twl4030_modules[i].client = NULL;
824         }
825         inuse = false;
826         return 0;
827 }
828
829 /* NOTE:  this driver only handles a single twl4030/tps659x0 chip */
830 static int __init
831 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
832 {
833         int                             status;
834         unsigned                        i;
835         struct twl4030_platform_data    *pdata = client->dev.platform_data;
836
837         if (!pdata) {
838                 dev_dbg(&client->dev, "no platform data?\n");
839                 return -EINVAL;
840         }
841
842         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
843                 dev_dbg(&client->dev, "can't talk I2C?\n");
844                 return -EIO;
845         }
846
847         if (inuse) {
848                 dev_dbg(&client->dev, "driver is already in use\n");
849                 return -EBUSY;
850         }
851
852         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
853                 struct twl4030_client   *twl = &twl4030_modules[i];
854
855                 twl->address = client->addr + i;
856                 if (i == 0)
857                         twl->client = client;
858                 else {
859                         twl->client = i2c_new_dummy(client->adapter,
860                                         twl->address);
861                         if (!twl->client) {
862                                 dev_err(&client->dev,
863                                         "can't attach client %d\n", i);
864                                 status = -ENOMEM;
865                                 goto fail;
866                         }
867                 }
868                 mutex_init(&twl->xfer_lock);
869         }
870         inuse = true;
871
872         /* setup clock framework */
873         clocks_init(&client->dev, pdata->clock);
874
875         /* load power event scripts */
876         if (twl_has_power() && pdata->power)
877                 twl4030_power_init(pdata->power);
878
879         /* Maybe init the T2 Interrupt subsystem */
880         if (client->irq
881                         && pdata->irq_base
882                         && pdata->irq_end > pdata->irq_base) {
883                 twl_init_chip_irq(id->name);
884                 status = twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
885                 if (status < 0)
886                         goto fail;
887         }
888
889         status = add_children(pdata, id->driver_data);
890 fail:
891         if (status < 0)
892                 twl4030_remove(client);
893         return status;
894 }
895
896 static const struct i2c_device_id twl4030_ids[] = {
897         { "twl4030", TWL4030_VAUX2 },   /* "Triton 2" */
898         { "twl5030", 0 },               /* T2 updated */
899         { "twl5031", TWL5031 },         /* TWL5030 updated */
900         { "tps65950", 0 },              /* catalog version of twl5030 */
901         { "tps65930", TPS_SUBSET },     /* fewer LDOs and DACs; no charger */
902         { "tps65920", TPS_SUBSET },     /* fewer LDOs; no codec or charger */
903         { /* end of list */ },
904 };
905 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
906
907 /* One Client Driver , 4 Clients */
908 static struct i2c_driver twl4030_driver = {
909         .driver.name    = DRIVER_NAME,
910         .id_table       = twl4030_ids,
911         .probe          = twl4030_probe,
912         .remove         = twl4030_remove,
913 };
914
915 static int __init twl4030_init(void)
916 {
917         return i2c_add_driver(&twl4030_driver);
918 }
919 subsys_initcall(twl4030_init);
920
921 static void __exit twl4030_exit(void)
922 {
923         i2c_del_driver(&twl4030_driver);
924 }
925 module_exit(twl4030_exit);
926
927 MODULE_AUTHOR("Texas Instruments, Inc.");
928 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
929 MODULE_LICENSE("GPL");