Add power-off support for the TWL4030 companion
[pandora-kernel.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_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
62 #define twl_has_keypad()        true
63 #else
64 #define twl_has_keypad()        false
65 #endif
66
67 #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
68 #define twl_has_gpio()  true
69 #else
70 #define twl_has_gpio()  false
71 #endif
72
73 #if defined(CONFIG_REGULATOR_TWL4030) \
74         || defined(CONFIG_REGULATOR_TWL4030_MODULE)
75 #define twl_has_regulator()     true
76 #else
77 #define twl_has_regulator()     false
78 #endif
79
80 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
81 #define twl_has_madc()  true
82 #else
83 #define twl_has_madc()  false
84 #endif
85
86 #if defined(CONFIG_SENSORS_TWL4030_MADC) ||\
87          defined(CONFIG_SENSORS_TWL4030_MADC_MODULE)
88 #define twl_has_madc_hwmon()  true
89 #else
90 #define twl_has_madc_hwmon()  false
91 #endif
92
93 #ifdef CONFIG_TWL4030_POWER
94 #define twl_has_power()        true
95 #else
96 #define twl_has_power()        false
97 #endif
98
99 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
100 #define twl_has_rtc()   true
101 #else
102 #define twl_has_rtc()   false
103 #endif
104
105 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE) ||\
106         defined(CONFIG_TWL6030_USB) || defined(CONFIG_TWL6030_USB_MODULE)
107 #define twl_has_usb()   true
108 #else
109 #define twl_has_usb()   false
110 #endif
111
112 #if defined(CONFIG_TWL4030_WATCHDOG) || \
113         defined(CONFIG_TWL4030_WATCHDOG_MODULE)
114 #define twl_has_watchdog()        true
115 #else
116 #define twl_has_watchdog()        false
117 #endif
118
119 #if defined(CONFIG_TWL4030_CODEC) || defined(CONFIG_TWL4030_CODEC_MODULE) ||\
120         defined(CONFIG_SND_SOC_TWL6040) || defined(CONFIG_SND_SOC_TWL6040_MODULE)
121 #define twl_has_codec() true
122 #else
123 #define twl_has_codec() false
124 #endif
125
126 #if defined(CONFIG_CHARGER_TWL4030) || defined(CONFIG_CHARGER_TWL4030_MODULE)
127 #define twl_has_bci()   true
128 #else
129 #define twl_has_bci()   false
130 #endif
131
132 #if defined (CONFIG_TWL4030_POWEROFF)
133 #define twl_has_poweroff()      true
134 #else
135 #define twl_has_poweroff()      false
136 #endif
137
138 /* Triton Core internal information (BEGIN) */
139
140 /* Last - for index max*/
141 #define TWL4030_MODULE_LAST             TWL4030_MODULE_SECURED_REG
142
143 #define TWL_NUM_SLAVES          4
144
145 #if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
146         || defined(CONFIG_INPUT_TWL4030_PWRBUTTON_MODULE)
147 #define twl_has_pwrbutton()     true
148 #else
149 #define twl_has_pwrbutton()     false
150 #endif
151
152 #define SUB_CHIP_ID0 0
153 #define SUB_CHIP_ID1 1
154 #define SUB_CHIP_ID2 2
155 #define SUB_CHIP_ID3 3
156
157 #define TWL_MODULE_LAST TWL4030_MODULE_LAST
158
159 /* Base Address defns for twl4030_map[] */
160
161 /* subchip/slave 0 - USB ID */
162 #define TWL4030_BASEADD_USB             0x0000
163
164 /* subchip/slave 1 - AUD ID */
165 #define TWL4030_BASEADD_AUDIO_VOICE     0x0000
166 #define TWL4030_BASEADD_GPIO            0x0098
167 #define TWL4030_BASEADD_INTBR           0x0085
168 #define TWL4030_BASEADD_PIH             0x0080
169 #define TWL4030_BASEADD_TEST            0x004C
170
171 /* subchip/slave 2 - AUX ID */
172 #define TWL4030_BASEADD_INTERRUPTS      0x00B9
173 #define TWL4030_BASEADD_LED             0x00EE
174 #define TWL4030_BASEADD_MADC            0x0000
175 #define TWL4030_BASEADD_MAIN_CHARGE     0x0074
176 #define TWL4030_BASEADD_PRECHARGE       0x00AA
177 #define TWL4030_BASEADD_PWM0            0x00F8
178 #define TWL4030_BASEADD_PWM1            0x00FB
179 #define TWL4030_BASEADD_PWMA            0x00EF
180 #define TWL4030_BASEADD_PWMB            0x00F1
181 #define TWL4030_BASEADD_KEYPAD          0x00D2
182
183 #define TWL5031_BASEADD_ACCESSORY       0x0074 /* Replaces Main Charge */
184 #define TWL5031_BASEADD_INTERRUPTS      0x00B9 /* Different than TWL4030's
185                                                   one */
186
187 /* subchip/slave 3 - POWER ID */
188 #define TWL4030_BASEADD_BACKUP          0x0014
189 #define TWL4030_BASEADD_INT             0x002E
190 #define TWL4030_BASEADD_PM_MASTER       0x0036
191 #define TWL4030_BASEADD_PM_RECEIVER     0x005B
192 #define TWL4030_BASEADD_RTC             0x001C
193 #define TWL4030_BASEADD_SECURED_REG     0x0000
194
195 /* Triton Core internal information (END) */
196
197
198 /* subchip/slave 0 0x48 - POWER */
199 #define TWL6030_BASEADD_RTC             0x0000
200 #define TWL6030_BASEADD_MEM             0x0017
201 #define TWL6030_BASEADD_PM_MASTER       0x001F
202 #define TWL6030_BASEADD_PM_SLAVE_MISC   0x0030 /* PM_RECEIVER */
203 #define TWL6030_BASEADD_PM_MISC         0x00E2
204 #define TWL6030_BASEADD_PM_PUPD         0x00F0
205
206 /* subchip/slave 1 0x49 - FEATURE */
207 #define TWL6030_BASEADD_USB             0x0000
208 #define TWL6030_BASEADD_GPADC_CTRL      0x002E
209 #define TWL6030_BASEADD_AUX             0x0090
210 #define TWL6030_BASEADD_PWM             0x00BA
211 #define TWL6030_BASEADD_GASGAUGE        0x00C0
212 #define TWL6030_BASEADD_PIH             0x00D0
213 #define TWL6030_BASEADD_CHARGER         0x00E0
214
215 /* subchip/slave 2 0x4A - DFT */
216 #define TWL6030_BASEADD_DIEID           0x00C0
217
218 /* subchip/slave 3 0x4B - AUDIO */
219 #define TWL6030_BASEADD_AUDIO           0x0000
220 #define TWL6030_BASEADD_RSV             0x0000
221 #define TWL6030_BASEADD_ZERO            0x0000
222
223 /* Few power values */
224 #define R_CFG_BOOT                      0x05
225
226 /* some fields in R_CFG_BOOT */
227 #define HFCLK_FREQ_19p2_MHZ             (1 << 0)
228 #define HFCLK_FREQ_26_MHZ               (2 << 0)
229 #define HFCLK_FREQ_38p4_MHZ             (3 << 0)
230 #define HIGH_PERF_SQ                    (1 << 3)
231 #define CK32K_LOWPWR_EN                 (1 << 7)
232
233
234 /* chip-specific feature flags, for i2c_device_id.driver_data */
235 #define TWL4030_VAUX2           BIT(0)  /* pre-5030 voltage ranges */
236 #define TPS_SUBSET              BIT(1)  /* tps659[23]0 have fewer LDOs */
237 #define TWL5031                 BIT(2)  /* twl5031 has different registers */
238 #define TWL6030_CLASS           BIT(3)  /* TWL6030 class */
239
240 /* for pm_power_off */
241 #define PWR_P1_SW_EVENTS        0x10
242 #define PWR_DEVOFF              (1 << 0)
243
244 /*----------------------------------------------------------------------*/
245
246 /* is driver active, bound to a chip? */
247 static bool inuse;
248
249 static unsigned int twl_id;
250 unsigned int twl_rev(void)
251 {
252         return twl_id;
253 }
254 EXPORT_SYMBOL(twl_rev);
255
256 /* Structure for each TWL4030/TWL6030 Slave */
257 struct twl_client {
258         struct i2c_client *client;
259         u8 address;
260
261         /* max numb of i2c_msg required is for read =2 */
262         struct i2c_msg xfer_msg[2];
263
264         /* To lock access to xfer_msg */
265         struct mutex xfer_lock;
266 };
267
268 static struct twl_client twl_modules[TWL_NUM_SLAVES];
269
270
271 /* mapping the module id to slave id and base address */
272 struct twl_mapping {
273         unsigned char sid;      /* Slave ID */
274         unsigned char base;     /* base address */
275 };
276 static struct twl_mapping *twl_map;
277
278 static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
279         /*
280          * NOTE:  don't change this table without updating the
281          * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
282          * so they continue to match the order in this table.
283          */
284
285         { 0, TWL4030_BASEADD_USB },
286
287         { 1, TWL4030_BASEADD_AUDIO_VOICE },
288         { 1, TWL4030_BASEADD_GPIO },
289         { 1, TWL4030_BASEADD_INTBR },
290         { 1, TWL4030_BASEADD_PIH },
291         { 1, TWL4030_BASEADD_TEST },
292
293         { 2, TWL4030_BASEADD_KEYPAD },
294         { 2, TWL4030_BASEADD_MADC },
295         { 2, TWL4030_BASEADD_INTERRUPTS },
296         { 2, TWL4030_BASEADD_LED },
297         { 2, TWL4030_BASEADD_MAIN_CHARGE },
298         { 2, TWL4030_BASEADD_PRECHARGE },
299         { 2, TWL4030_BASEADD_PWM0 },
300         { 2, TWL4030_BASEADD_PWM1 },
301         { 2, TWL4030_BASEADD_PWMA },
302         { 2, TWL4030_BASEADD_PWMB },
303         { 2, TWL5031_BASEADD_ACCESSORY },
304         { 2, TWL5031_BASEADD_INTERRUPTS },
305
306         { 3, TWL4030_BASEADD_BACKUP },
307         { 3, TWL4030_BASEADD_INT },
308         { 3, TWL4030_BASEADD_PM_MASTER },
309         { 3, TWL4030_BASEADD_PM_RECEIVER },
310         { 3, TWL4030_BASEADD_RTC },
311         { 3, TWL4030_BASEADD_SECURED_REG },
312 };
313
314 static struct twl_mapping twl6030_map[] = {
315         /*
316          * NOTE:  don't change this table without updating the
317          * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
318          * so they continue to match the order in this table.
319          */
320         { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
321         { SUB_CHIP_ID3, TWL6030_BASEADD_AUDIO },
322         { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
323         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
324         { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
325
326         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
327         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
328         { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
329         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
330         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
331
332         { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
333         { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
334         { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
335         { SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
336         { SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
337
338         { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
339         { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
340         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
341         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
342         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
343         { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
344         { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
345
346         { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
347         { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
348 };
349
350 /*----------------------------------------------------------------------*/
351
352 /* Exported Functions */
353
354 /**
355  * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
356  * @mod_no: module number
357  * @value: an array of num_bytes+1 containing data to write
358  * @reg: register address (just offset will do)
359  * @num_bytes: number of bytes to transfer
360  *
361  * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
362  * valid data starts at Offset 1.
363  *
364  * Returns the result of operation - 0 is success
365  */
366 int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
367 {
368         int ret;
369         int sid;
370         struct twl_client *twl;
371         struct i2c_msg *msg;
372
373         if (unlikely(mod_no > TWL_MODULE_LAST)) {
374                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
375                 return -EPERM;
376         }
377         sid = twl_map[mod_no].sid;
378         twl = &twl_modules[sid];
379
380         if (unlikely(!inuse)) {
381                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
382                 return -EPERM;
383         }
384         mutex_lock(&twl->xfer_lock);
385         /*
386          * [MSG1]: fill the register address data
387          * fill the data Tx buffer
388          */
389         msg = &twl->xfer_msg[0];
390         msg->addr = twl->address;
391         msg->len = num_bytes + 1;
392         msg->flags = 0;
393         msg->buf = value;
394         /* over write the first byte of buffer with the register address */
395         *value = twl_map[mod_no].base + reg;
396         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
397         mutex_unlock(&twl->xfer_lock);
398
399         /* i2c_transfer returns number of messages transferred */
400         if (ret != 1) {
401                 pr_err("%s: i2c_write failed to transfer all messages\n",
402                         DRIVER_NAME);
403                 if (ret < 0)
404                         return ret;
405                 else
406                         return -EIO;
407         } else {
408                 return 0;
409         }
410 }
411 EXPORT_SYMBOL(twl_i2c_write);
412
413 /**
414  * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
415  * @mod_no: module number
416  * @value: an array of num_bytes containing data to be read
417  * @reg: register address (just offset will do)
418  * @num_bytes: number of bytes to transfer
419  *
420  * Returns result of operation - num_bytes is success else failure.
421  */
422 int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
423 {
424         int ret;
425         u8 val;
426         int sid;
427         struct twl_client *twl;
428         struct i2c_msg *msg;
429
430         if (unlikely(mod_no > TWL_MODULE_LAST)) {
431                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
432                 return -EPERM;
433         }
434         sid = twl_map[mod_no].sid;
435         twl = &twl_modules[sid];
436
437         if (unlikely(!inuse)) {
438                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
439                 return -EPERM;
440         }
441         mutex_lock(&twl->xfer_lock);
442         /* [MSG1] fill the register address data */
443         msg = &twl->xfer_msg[0];
444         msg->addr = twl->address;
445         msg->len = 1;
446         msg->flags = 0; /* Read the register value */
447         val = twl_map[mod_no].base + reg;
448         msg->buf = &val;
449         /* [MSG2] fill the data rx buffer */
450         msg = &twl->xfer_msg[1];
451         msg->addr = twl->address;
452         msg->flags = I2C_M_RD;  /* Read the register value */
453         msg->len = num_bytes;   /* only n bytes */
454         msg->buf = value;
455         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
456         mutex_unlock(&twl->xfer_lock);
457
458         /* i2c_transfer returns number of messages transferred */
459         if (ret != 2) {
460                 pr_err("%s: i2c_read failed to transfer all messages\n",
461                         DRIVER_NAME);
462                 if (ret < 0)
463                         return ret;
464                 else
465                         return -EIO;
466         } else {
467                 return 0;
468         }
469 }
470 EXPORT_SYMBOL(twl_i2c_read);
471
472 /**
473  * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
474  * @mod_no: module number
475  * @value: the value to be written 8 bit
476  * @reg: register address (just offset will do)
477  *
478  * Returns result of operation - 0 is success
479  */
480 int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
481 {
482
483         /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
484         u8 temp_buffer[2] = { 0 };
485         /* offset 1 contains the data */
486         temp_buffer[1] = value;
487         return twl_i2c_write(mod_no, temp_buffer, reg, 1);
488 }
489 EXPORT_SYMBOL(twl_i2c_write_u8);
490
491 /**
492  * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
493  * @mod_no: module number
494  * @value: the value read 8 bit
495  * @reg: register address (just offset will do)
496  *
497  * Returns result of operation - 0 is success
498  */
499 int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
500 {
501         return twl_i2c_read(mod_no, value, reg, 1);
502 }
503 EXPORT_SYMBOL(twl_i2c_read_u8);
504
505 /*----------------------------------------------------------------------*/
506
507 static struct device *
508 add_numbered_child(unsigned chip, const char *name, int num,
509                 void *pdata, unsigned pdata_len,
510                 bool can_wakeup, int irq0, int irq1)
511 {
512         struct platform_device  *pdev;
513         struct twl_client       *twl = &twl_modules[chip];
514         int                     status;
515
516         pdev = platform_device_alloc(name, num);
517         if (!pdev) {
518                 dev_dbg(&twl->client->dev, "can't alloc dev\n");
519                 status = -ENOMEM;
520                 goto err;
521         }
522
523         device_init_wakeup(&pdev->dev, can_wakeup);
524         pdev->dev.parent = &twl->client->dev;
525
526         if (pdata) {
527                 status = platform_device_add_data(pdev, pdata, pdata_len);
528                 if (status < 0) {
529                         dev_dbg(&pdev->dev, "can't add platform_data\n");
530                         goto err;
531                 }
532         }
533
534         if (irq0) {
535                 struct resource r[2] = {
536                         { .start = irq0, .flags = IORESOURCE_IRQ, },
537                         { .start = irq1, .flags = IORESOURCE_IRQ, },
538                 };
539
540                 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
541                 if (status < 0) {
542                         dev_dbg(&pdev->dev, "can't add irqs\n");
543                         goto err;
544                 }
545         }
546
547         status = platform_device_add(pdev);
548
549 err:
550         if (status < 0) {
551                 platform_device_put(pdev);
552                 dev_err(&twl->client->dev, "can't add %s dev\n", name);
553                 return ERR_PTR(status);
554         }
555         return &pdev->dev;
556 }
557
558 static inline struct device *add_child(unsigned chip, const char *name,
559                 void *pdata, unsigned pdata_len,
560                 bool can_wakeup, int irq0, int irq1)
561 {
562         return add_numbered_child(chip, name, -1, pdata, pdata_len,
563                 can_wakeup, irq0, irq1);
564 }
565
566 static struct device *
567 add_regulator_linked(int num, struct regulator_init_data *pdata,
568                 struct regulator_consumer_supply *consumers,
569                 unsigned num_consumers)
570 {
571         unsigned sub_chip_id;
572         /* regulator framework demands init_data ... */
573         if (!pdata)
574                 return NULL;
575
576         if (consumers) {
577                 pdata->consumer_supplies = consumers;
578                 pdata->num_consumer_supplies = num_consumers;
579         }
580
581         /* NOTE:  we currently ignore regulator IRQs, e.g. for short circuits */
582         sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
583         return add_numbered_child(sub_chip_id, "twl_reg", num,
584                 pdata, sizeof(*pdata), false, 0, 0);
585 }
586
587 static struct device *
588 add_regulator(int num, struct regulator_init_data *pdata)
589 {
590         return add_regulator_linked(num, pdata, NULL, 0);
591 }
592
593 /*
594  * NOTE:  We know the first 8 IRQs after pdata->base_irq are
595  * for the PIH, and the next are for the PWR_INT SIH, since
596  * that's how twl_init_irq() sets things up.
597  */
598
599 static int
600 add_children(struct twl4030_platform_data *pdata, unsigned long features)
601 {
602         struct device   *child;
603         unsigned sub_chip_id;
604
605         if (twl_has_gpio() && pdata->gpio) {
606                 child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
607                                 pdata->gpio, sizeof(*pdata->gpio),
608                                 false, pdata->irq_base + GPIO_INTR_OFFSET, 0);
609                 if (IS_ERR(child))
610                         return PTR_ERR(child);
611         }
612
613         if (twl_has_keypad() && pdata->keypad) {
614                 child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
615                                 pdata->keypad, sizeof(*pdata->keypad),
616                                 true, pdata->irq_base + KEYPAD_INTR_OFFSET, 0);
617                 if (IS_ERR(child))
618                         return PTR_ERR(child);
619         }
620
621         if (twl_has_madc() && pdata->madc) {
622                 child = add_child(2, "twl4030_madc",
623                                 pdata->madc, sizeof(*pdata->madc),
624                                 true, pdata->irq_base + MADC_INTR_OFFSET, 0);
625                 if (IS_ERR(child))
626                         return PTR_ERR(child);
627         }
628
629 if (twl_has_madc_hwmon()) {
630                 child = add_child(2, "twl4030_madc_hwmon",
631                                 NULL, 0,
632                                 true, pdata->irq_base + MADC_INTR_OFFSET, 0);
633                 if (IS_ERR(child))
634                         return PTR_ERR(child);
635         }
636
637         if (twl_has_rtc()) {
638                 /*
639                  * REVISIT platform_data here currently might expose the
640                  * "msecure" line ... but for now we just expect board
641                  * setup to tell the chip "it's always ok to SET_TIME".
642                  * Eventually, Linux might become more aware of such
643                  * HW security concerns, and "least privilege".
644                  */
645                 sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
646                 child = add_child(sub_chip_id, "twl_rtc",
647                                 NULL, 0,
648                                 true, pdata->irq_base + RTC_INTR_OFFSET, 0);
649                 if (IS_ERR(child))
650                         return PTR_ERR(child);
651         }
652
653         if (twl_has_usb() && pdata->usb && twl_class_is_4030()) {
654
655                 static struct regulator_consumer_supply usb1v5 = {
656                         .supply =       "usb1v5",
657                 };
658                 static struct regulator_consumer_supply usb1v8 = {
659                         .supply =       "usb1v8",
660                 };
661                 static struct regulator_consumer_supply usb3v1 = {
662                         .supply =       "usb3v1",
663                 };
664
665         /* First add the regulators so that they can be used by transceiver */
666                 if (twl_has_regulator()) {
667                         /* this is a template that gets copied */
668                         struct regulator_init_data usb_fixed = {
669                                 .constraints.valid_modes_mask =
670                                         REGULATOR_MODE_NORMAL
671                                         | REGULATOR_MODE_STANDBY,
672                                 .constraints.valid_ops_mask =
673                                         REGULATOR_CHANGE_MODE
674                                         | REGULATOR_CHANGE_STATUS,
675                         };
676
677                         child = add_regulator_linked(TWL4030_REG_VUSB1V5,
678                                                       &usb_fixed, &usb1v5, 1);
679                         if (IS_ERR(child))
680                                 return PTR_ERR(child);
681
682                         child = add_regulator_linked(TWL4030_REG_VUSB1V8,
683                                                       &usb_fixed, &usb1v8, 1);
684                         if (IS_ERR(child))
685                                 return PTR_ERR(child);
686
687                         child = add_regulator_linked(TWL4030_REG_VUSB3V1,
688                                                       &usb_fixed, &usb3v1, 1);
689                         if (IS_ERR(child))
690                                 return PTR_ERR(child);
691
692                 }
693
694                 child = add_child(0, "twl4030_usb",
695                                 pdata->usb, sizeof(*pdata->usb),
696                                 true,
697                                 /* irq0 = USB_PRES, irq1 = USB */
698                                 pdata->irq_base + USB_PRES_INTR_OFFSET,
699                                 pdata->irq_base + USB_INTR_OFFSET);
700
701                 if (IS_ERR(child))
702                         return PTR_ERR(child);
703
704                 /* we need to connect regulators to this transceiver */
705                 if (twl_has_regulator() && child) {
706                         usb1v5.dev = child;
707                         usb1v8.dev = child;
708                         usb3v1.dev = child;
709                 }
710         }
711         if (twl_has_usb() && pdata->usb && twl_class_is_6030()) {
712
713                 static struct regulator_consumer_supply usb3v3 = {
714                         .supply =       "vusb",
715                 };
716
717                 if (twl_has_regulator()) {
718                         /* this is a template that gets copied */
719                         struct regulator_init_data usb_fixed = {
720                                 .constraints.valid_modes_mask =
721                                         REGULATOR_MODE_NORMAL
722                                         | REGULATOR_MODE_STANDBY,
723                                 .constraints.valid_ops_mask =
724                                         REGULATOR_CHANGE_MODE
725                                         | REGULATOR_CHANGE_STATUS,
726                         };
727
728                         child = add_regulator_linked(TWL6030_REG_VUSB,
729                                                       &usb_fixed, &usb3v3, 1);
730                         if (IS_ERR(child))
731                                 return PTR_ERR(child);
732                 }
733
734                 child = add_child(0, "twl6030_usb",
735                         pdata->usb, sizeof(*pdata->usb),
736                         true,
737                         /* irq1 = VBUS_PRES, irq0 = USB ID */
738                         pdata->irq_base + USBOTG_INTR_OFFSET,
739                         pdata->irq_base + USB_PRES_INTR_OFFSET);
740
741                 if (IS_ERR(child))
742                         return PTR_ERR(child);
743                 /* we need to connect regulators to this transceiver */
744                 if (twl_has_regulator() && child)
745                         usb3v3.dev = child;
746
747         }
748
749         if (twl_has_watchdog() && twl_class_is_4030()) {
750                 child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
751                 if (IS_ERR(child))
752                         return PTR_ERR(child);
753         }
754
755         if (twl_has_pwrbutton() && twl_class_is_4030()) {
756                 child = add_child(1, "twl4030_pwrbutton",
757                                 NULL, 0, true, pdata->irq_base + 8 + 0, 0);
758                 if (IS_ERR(child))
759                         return PTR_ERR(child);
760         }
761
762         if (twl_has_codec() && pdata->codec && twl_class_is_4030()) {
763                 sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
764                 child = add_child(sub_chip_id, "twl4030-audio",
765                                 pdata->codec, sizeof(*pdata->codec),
766                                 false, 0, 0);
767                 if (IS_ERR(child))
768                         return PTR_ERR(child);
769         }
770
771         /* Phoenix codec driver is probed directly atm */
772         if (twl_has_codec() && pdata->codec && twl_class_is_6030()) {
773                 sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
774                 child = add_child(sub_chip_id, "twl6040-codec",
775                                 pdata->codec, sizeof(*pdata->codec),
776                                 false, 0, 0);
777                 if (IS_ERR(child))
778                         return PTR_ERR(child);
779         }
780
781         /* twl4030 regulators */
782         if (twl_has_regulator() && twl_class_is_4030()) {
783                 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
784                 if (IS_ERR(child))
785                         return PTR_ERR(child);
786
787                 child = add_regulator(TWL4030_REG_VIO, pdata->vio);
788                 if (IS_ERR(child))
789                         return PTR_ERR(child);
790
791                 child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1);
792                 if (IS_ERR(child))
793                         return PTR_ERR(child);
794
795                 child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2);
796                 if (IS_ERR(child))
797                         return PTR_ERR(child);
798
799                 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
800                 if (IS_ERR(child))
801                         return PTR_ERR(child);
802
803                 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
804                 if (IS_ERR(child))
805                         return PTR_ERR(child);
806
807                 child = add_regulator((features & TWL4030_VAUX2)
808                                         ? TWL4030_REG_VAUX2_4030
809                                         : TWL4030_REG_VAUX2,
810                                 pdata->vaux2);
811                 if (IS_ERR(child))
812                         return PTR_ERR(child);
813
814                 child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1);
815                 if (IS_ERR(child))
816                         return PTR_ERR(child);
817
818                 child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2);
819                 if (IS_ERR(child))
820                         return PTR_ERR(child);
821
822                 child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig);
823                 if (IS_ERR(child))
824                         return PTR_ERR(child);
825         }
826
827         /* maybe add LDOs that are omitted on cost-reduced parts */
828         if (twl_has_regulator() && !(features & TPS_SUBSET)
829           && twl_class_is_4030()) {
830                 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
831                 if (IS_ERR(child))
832                         return PTR_ERR(child);
833
834                 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
835                 if (IS_ERR(child))
836                         return PTR_ERR(child);
837
838                 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
839                 if (IS_ERR(child))
840                         return PTR_ERR(child);
841
842                 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
843                 if (IS_ERR(child))
844                         return PTR_ERR(child);
845
846                 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
847                 if (IS_ERR(child))
848                         return PTR_ERR(child);
849
850                 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
851                 if (IS_ERR(child))
852                         return PTR_ERR(child);
853         }
854
855         /* twl6030 regulators */
856         if (twl_has_regulator() && twl_class_is_6030()) {
857                 child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc);
858                 if (IS_ERR(child))
859                         return PTR_ERR(child);
860
861                 child = add_regulator(TWL6030_REG_VPP, pdata->vpp);
862                 if (IS_ERR(child))
863                         return PTR_ERR(child);
864
865                 child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim);
866                 if (IS_ERR(child))
867                         return PTR_ERR(child);
868
869                 child = add_regulator(TWL6030_REG_VANA, pdata->vana);
870                 if (IS_ERR(child))
871                         return PTR_ERR(child);
872
873                 child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio);
874                 if (IS_ERR(child))
875                         return PTR_ERR(child);
876
877                 child = add_regulator(TWL6030_REG_VDAC, pdata->vdac);
878                 if (IS_ERR(child))
879                         return PTR_ERR(child);
880
881                 child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1);
882                 if (IS_ERR(child))
883                         return PTR_ERR(child);
884
885                 child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2);
886                 if (IS_ERR(child))
887                         return PTR_ERR(child);
888
889                 child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3);
890                 if (IS_ERR(child))
891                         return PTR_ERR(child);
892
893                 child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg);
894                 if (IS_ERR(child))
895                         return PTR_ERR(child);
896         }
897
898         if (twl_has_bci() && pdata->bci &&
899                         !(features & (TPS_SUBSET | TWL5031))) {
900                 child = add_child(3, "twl4030_bci",
901                                 pdata->bci, sizeof(*pdata->bci), false,
902                                 /* irq0 = CHG_PRES, irq1 = BCI */
903                                 pdata->irq_base + BCI_PRES_INTR_OFFSET,
904                                 pdata->irq_base + BCI_INTR_OFFSET);
905                 if (IS_ERR(child))
906                         return PTR_ERR(child);
907         }
908
909         return 0;
910 }
911
912 /*----------------------------------------------------------------------*/
913
914 /*
915  * These three functions initialize the on-chip clock framework,
916  * letting it generate the right frequencies for USB, MADC, and
917  * other purposes.
918  */
919 static inline int __init protect_pm_master(void)
920 {
921         int e = 0;
922
923         e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
924                         TWL4030_PM_MASTER_PROTECT_KEY);
925         return e;
926 }
927
928 static inline int __init unprotect_pm_master(void)
929 {
930         int e = 0;
931
932         e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
933                         TWL4030_PM_MASTER_KEY_CFG1,
934                         TWL4030_PM_MASTER_PROTECT_KEY);
935         e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
936                         TWL4030_PM_MASTER_KEY_CFG2,
937                         TWL4030_PM_MASTER_PROTECT_KEY);
938
939         return e;
940 }
941
942 static void clocks_init(struct device *dev,
943                         struct twl4030_clock_init_data *clock)
944 {
945         int e = 0;
946         struct clk *osc;
947         u32 rate;
948         u8 ctrl = HFCLK_FREQ_26_MHZ;
949
950 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
951         if (cpu_is_omap2430())
952                 osc = clk_get(dev, "osc_ck");
953         else
954                 osc = clk_get(dev, "osc_sys_ck");
955
956         if (IS_ERR(osc)) {
957                 printk(KERN_WARNING "Skipping twl internal clock init and "
958                                 "using bootloader value (unknown osc rate)\n");
959                 return;
960         }
961
962         rate = clk_get_rate(osc);
963         clk_put(osc);
964
965 #else
966         /* REVISIT for non-OMAP systems, pass the clock rate from
967          * board init code, using platform_data.
968          */
969         osc = ERR_PTR(-EIO);
970
971         printk(KERN_WARNING "Skipping twl internal clock init and "
972                "using bootloader value (unknown osc rate)\n");
973
974         return;
975 #endif
976
977         switch (rate) {
978         case 19200000:
979                 ctrl = HFCLK_FREQ_19p2_MHZ;
980                 break;
981         case 26000000:
982                 ctrl = HFCLK_FREQ_26_MHZ;
983                 break;
984         case 38400000:
985                 ctrl = HFCLK_FREQ_38p4_MHZ;
986                 break;
987         }
988
989         ctrl |= HIGH_PERF_SQ;
990         if (clock && clock->ck32k_lowpwr_enable)
991                 ctrl |= CK32K_LOWPWR_EN;
992
993         e |= unprotect_pm_master();
994         /* effect->MADC+USB ck en */
995         e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
996         e |= protect_pm_master();
997
998         if (e < 0)
999                 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
1000 }
1001
1002 /*----------------------------------------------------------------------*/
1003
1004 int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
1005 int twl4030_exit_irq(void);
1006 int twl4030_init_chip_irq(const char *chip);
1007 int twl6030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
1008 int twl6030_exit_irq(void);
1009
1010 static int twl_remove(struct i2c_client *client)
1011 {
1012         unsigned i;
1013         int status;
1014
1015         if (twl_class_is_4030())
1016                 status = twl4030_exit_irq();
1017         else
1018                 status = twl6030_exit_irq();
1019
1020         if (status < 0)
1021                 return status;
1022
1023         for (i = 0; i < TWL_NUM_SLAVES; i++) {
1024                 struct twl_client       *twl = &twl_modules[i];
1025
1026                 if (twl->client && twl->client != client)
1027                         i2c_unregister_device(twl->client);
1028                 twl_modules[i].client = NULL;
1029         }
1030         inuse = false;
1031         return 0;
1032 }
1033
1034 static void twl_poweroff(void)
1035 {
1036         int err;
1037         u8 val;
1038
1039         err = twl_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &val,
1040                                   PWR_P1_SW_EVENTS);
1041         if (err) {
1042                 pr_err("%s: i2c error %d while reading TWL4030"
1043                         "PM_MASTER P1_SW_EVENTS\n",
1044                         DRIVER_NAME, err);
1045                 return;
1046         }
1047
1048         val |= PWR_DEVOFF;
1049
1050         err = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, val,
1051                                    PWR_P1_SW_EVENTS);
1052         if (err)
1053                 pr_err("%s: i2c error %d while writing TWL4030"
1054                         "PM_MASTER P1_SW_EVENTS\n",
1055                         DRIVER_NAME, err);
1056 }
1057
1058 /* NOTE:  this driver only handles a single twl4030/tps659x0 chip */
1059 static int __devinit
1060 twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
1061 {
1062         int                             status;
1063         unsigned                        i;
1064         struct twl4030_platform_data    *pdata = client->dev.platform_data;
1065         u8 temp;
1066
1067         if (!pdata) {
1068                 dev_dbg(&client->dev, "no platform data?\n");
1069                 return -EINVAL;
1070         }
1071
1072         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1073                 dev_dbg(&client->dev, "can't talk I2C?\n");
1074                 return -EIO;
1075         }
1076
1077         if (inuse) {
1078                 dev_dbg(&client->dev, "driver is already in use\n");
1079                 return -EBUSY;
1080         }
1081
1082         for (i = 0; i < TWL_NUM_SLAVES; i++) {
1083                 struct twl_client       *twl = &twl_modules[i];
1084
1085                 twl->address = client->addr + i;
1086                 if (i == 0)
1087                         twl->client = client;
1088                 else {
1089                         twl->client = i2c_new_dummy(client->adapter,
1090                                         twl->address);
1091                         if (!twl->client) {
1092                                 dev_err(&client->dev,
1093                                         "can't attach client %d\n", i);
1094                                 status = -ENOMEM;
1095                                 goto fail;
1096                         }
1097                 }
1098                 mutex_init(&twl->xfer_lock);
1099         }
1100         inuse = true;
1101         if ((id->driver_data) & TWL6030_CLASS) {
1102                 twl_id = TWL6030_CLASS_ID;
1103                 twl_map = &twl6030_map[0];
1104         } else {
1105                 twl_id = TWL4030_CLASS_ID;
1106                 twl_map = &twl4030_map[0];
1107         }
1108
1109         /* setup clock framework */
1110         clocks_init(&client->dev, pdata->clock);
1111
1112         /* load power event scripts */
1113         if (twl_has_power() && pdata->power)
1114                 twl4030_power_init(pdata->power);
1115
1116         /* Maybe init the T2 Interrupt subsystem */
1117         if (client->irq
1118                         && pdata->irq_base
1119                         && pdata->irq_end > pdata->irq_base) {
1120                 if (twl_class_is_4030()) {
1121                         twl4030_init_chip_irq(id->name);
1122                         status = twl4030_init_irq(client->irq, pdata->irq_base,
1123                         pdata->irq_end);
1124                 } else {
1125                         status = twl6030_init_irq(client->irq, pdata->irq_base,
1126                         pdata->irq_end);
1127                 }
1128
1129                 if (status < 0)
1130                         goto fail;
1131         }
1132
1133         /* Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
1134          * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1135          * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1136          */
1137
1138         if (twl_class_is_4030()) {
1139                 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
1140                 temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
1141                 I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
1142                 twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
1143         }
1144
1145         if(twl_has_poweroff())
1146         {
1147                 /* initialize pm_power_off routine */
1148                 pm_power_off = twl_poweroff;
1149         }
1150
1151         status = add_children(pdata, id->driver_data);
1152 fail:
1153         if (status < 0)
1154                 twl_remove(client);
1155         return status;
1156 }
1157
1158 static const struct i2c_device_id twl_ids[] = {
1159         { "twl4030", TWL4030_VAUX2 },   /* "Triton 2" */
1160         { "twl5030", 0 },               /* T2 updated */
1161         { "twl5031", TWL5031 },         /* TWL5030 updated */
1162         { "tps65950", 0 },              /* catalog version of twl5030 */
1163         { "tps65930", TPS_SUBSET },     /* fewer LDOs and DACs; no charger */
1164         { "tps65920", TPS_SUBSET },     /* fewer LDOs; no codec or charger */
1165         { "twl6030", TWL6030_CLASS },   /* "Phoenix power chip" */
1166         { /* end of list */ },
1167 };
1168 MODULE_DEVICE_TABLE(i2c, twl_ids);
1169
1170 /* One Client Driver , 4 Clients */
1171 static struct i2c_driver twl_driver = {
1172         .driver.name    = DRIVER_NAME,
1173         .id_table       = twl_ids,
1174         .probe          = twl_probe,
1175         .remove         = twl_remove,
1176 };
1177
1178 static int __init twl_init(void)
1179 {
1180         return i2c_add_driver(&twl_driver);
1181 }
1182 subsys_initcall(twl_init);
1183
1184 static void __exit twl_exit(void)
1185 {
1186         i2c_del_driver(&twl_driver);
1187 }
1188 module_exit(twl_exit);
1189
1190 MODULE_AUTHOR("Texas Instruments, Inc.");
1191 MODULE_DESCRIPTION("I2C Core interface for TWL");
1192 MODULE_LICENSE("GPL");