]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mfd/twl4030-core.c
Revert "twl4030-core simplification"
[linux-2.6-omap-h63xx.git] / drivers / mfd / twl4030-core.c
1 /*
2  * twl4030_core.c - driver for TWL4030/TPS659x0 PM and audio CODEC devices
3  *
4  * Copyright (C) 2005-2006 Texas Instruments, Inc.
5  *
6  * Modifications to defer interrupt handling to a kernel thread:
7  * Copyright (C) 2006 MontaVista Software, Inc.
8  *
9  * Based on tlv320aic23.c:
10  * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
11  *
12  * Code cleanup and modifications to IRQ handler.
13  * by syed khasim <x0khasim@ti.com>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28  */
29
30 #include <linux/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/i2c.h>
37 #include <linux/i2c/twl4030.h>
38
39
40 /*
41  * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
42  * Management and System Companion Device" chips originally designed for
43  * use in OMAP2 and OMAP 3 based systems.  Its control interfaces use I2C,
44  * often at around 3 Mbit/sec, including for interrupt handling.
45  *
46  * This driver core provides genirq support for the interrupts emitted,
47  * by the various modules, and exports register access primitives.
48  *
49  * FIXME this driver currently requires use of the first interrupt line
50  * (and associated registers).
51  */
52
53 #define DRIVER_NAME                     "twl4030"
54
55 #if defined(CONFIG_TWL4030_BCI_BATTERY) || \
56         defined(CONFIG_TWL4030_BCI_BATTERY_MODULE)
57 #define twl_has_bci()           true
58 #else
59 #define twl_has_bci()           false
60 #endif
61
62 #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
63 #define twl_has_keypad()        true
64 #else
65 #define twl_has_keypad()        false
66 #endif
67
68 #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
69 #define twl_has_gpio()  true
70 #else
71 #define twl_has_gpio()  false
72 #endif
73
74 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
75 #define twl_has_madc()  true
76 #else
77 #define twl_has_madc()  false
78 #endif
79
80 #ifdef CONFIG_TWL4030_POWER
81 #define twl_has_power()        true
82 #else
83 #define twl_has_power()        false
84 #endif
85
86 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
87 #define twl_has_rtc()   true
88 #else
89 #define twl_has_rtc()   false
90 #endif
91
92 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
93 #define twl_has_usb()   true
94 #else
95 #define twl_has_usb()   false
96 #endif
97
98
99 /* Triton Core internal information (BEGIN) */
100
101 /* Last - for index max*/
102 #define TWL4030_MODULE_LAST             TWL4030_MODULE_SECURED_REG
103
104 #define TWL4030_NUM_SLAVES              4
105
106
107 /* Base Address defns for twl4030_map[] */
108
109 /* subchip/slave 0 - USB ID */
110 #define TWL4030_BASEADD_USB             0x0000
111
112 /* subchip/slave 1 - AUD ID */
113 #define TWL4030_BASEADD_AUDIO_VOICE     0x0000
114 #define TWL4030_BASEADD_GPIO            0x0098
115 #define TWL4030_BASEADD_INTBR           0x0085
116 #define TWL4030_BASEADD_PIH             0x0080
117 #define TWL4030_BASEADD_TEST            0x004C
118
119 /* subchip/slave 2 - AUX ID */
120 #define TWL4030_BASEADD_INTERRUPTS      0x00B9
121 #define TWL4030_BASEADD_LED             0x00EE
122 #define TWL4030_BASEADD_MADC            0x0000
123 #define TWL4030_BASEADD_MAIN_CHARGE     0x0074
124 #define TWL4030_BASEADD_PRECHARGE       0x00AA
125 #define TWL4030_BASEADD_PWM0            0x00F8
126 #define TWL4030_BASEADD_PWM1            0x00FB
127 #define TWL4030_BASEADD_PWMA            0x00EF
128 #define TWL4030_BASEADD_PWMB            0x00F1
129 #define TWL4030_BASEADD_KEYPAD          0x00D2
130
131 /* subchip/slave 3 - POWER ID */
132 #define TWL4030_BASEADD_BACKUP          0x0014
133 #define TWL4030_BASEADD_INT             0x002E
134 #define TWL4030_BASEADD_PM_MASTER       0x0036
135 #define TWL4030_BASEADD_PM_RECEIVER     0x005B
136 #define TWL4030_BASEADD_RTC             0x001C
137 #define TWL4030_BASEADD_SECURED_REG     0x0000
138
139 /* Triton Core internal information (END) */
140
141
142 /* Few power values */
143 #define R_CFG_BOOT                      0x05
144 #define R_PROTECT_KEY                   0x0E
145
146 /* access control values for R_PROTECT_KEY */
147 #define KEY_UNLOCK1                     0xce
148 #define KEY_UNLOCK2                     0xec
149 #define KEY_LOCK                        0x00
150
151 /* some fields in R_CFG_BOOT */
152 #define HFCLK_FREQ_19p2_MHZ             (1 << 0)
153 #define HFCLK_FREQ_26_MHZ               (2 << 0)
154 #define HFCLK_FREQ_38p4_MHZ             (3 << 0)
155 #define HIGH_PERF_SQ                    (1 << 3)
156
157
158 /*----------------------------------------------------------------------*/
159
160 /* is driver active, bound to a chip? */
161 static bool inuse;
162
163 /* Structure for each TWL4030 Slave */
164 struct twl4030_client {
165         struct i2c_client *client;
166         u8 address;
167
168         /* max numb of i2c_msg required is for read =2 */
169         struct i2c_msg xfer_msg[2];
170
171         /* To lock access to xfer_msg */
172         struct mutex xfer_lock;
173 };
174
175 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
176
177
178 /* mapping the module id to slave id and base address */
179 struct twl4030mapping {
180         unsigned char sid;      /* Slave ID */
181         unsigned char base;     /* base address */
182 };
183
184 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
185         /*
186          * NOTE:  don't change this table without updating the
187          * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
188          * so they continue to match the order in this table.
189          */
190
191         { 0, TWL4030_BASEADD_USB },
192
193         { 1, TWL4030_BASEADD_AUDIO_VOICE },
194         { 1, TWL4030_BASEADD_GPIO },
195         { 1, TWL4030_BASEADD_INTBR },
196         { 1, TWL4030_BASEADD_PIH },
197         { 1, TWL4030_BASEADD_TEST },
198
199         { 2, TWL4030_BASEADD_KEYPAD },
200         { 2, TWL4030_BASEADD_MADC },
201         { 2, TWL4030_BASEADD_INTERRUPTS },
202         { 2, TWL4030_BASEADD_LED },
203         { 2, TWL4030_BASEADD_MAIN_CHARGE },
204         { 2, TWL4030_BASEADD_PRECHARGE },
205         { 2, TWL4030_BASEADD_PWM0 },
206         { 2, TWL4030_BASEADD_PWM1 },
207         { 2, TWL4030_BASEADD_PWMA },
208         { 2, TWL4030_BASEADD_PWMB },
209
210         { 3, TWL4030_BASEADD_BACKUP },
211         { 3, TWL4030_BASEADD_INT },
212         { 3, TWL4030_BASEADD_PM_MASTER },
213         { 3, TWL4030_BASEADD_PM_RECEIVER },
214         { 3, TWL4030_BASEADD_RTC },
215         { 3, TWL4030_BASEADD_SECURED_REG },
216 };
217
218 extern void twl4030_power_init(struct twl4030_power_data *triton2_scripts);
219
220 /*----------------------------------------------------------------------*/
221
222 /* Exported Functions */
223
224 /**
225  * twl4030_i2c_write - Writes a n bit register in TWL4030
226  * @mod_no: module number
227  * @value: an array of num_bytes+1 containing data to write
228  * @reg: register address (just offset will do)
229  * @num_bytes: number of bytes to transfer
230  *
231  * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
232  * valid data starts at Offset 1.
233  *
234  * Returns the result of operation - 0 is success
235  */
236 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
237 {
238         int ret;
239         int sid;
240         struct twl4030_client *twl;
241         struct i2c_msg *msg;
242
243         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
244                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
245                 return -EPERM;
246         }
247         sid = twl4030_map[mod_no].sid;
248         twl = &twl4030_modules[sid];
249
250         if (unlikely(!inuse)) {
251                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
252                 return -EPERM;
253         }
254         mutex_lock(&twl->xfer_lock);
255         /*
256          * [MSG1]: fill the register address data
257          * fill the data Tx buffer
258          */
259         msg = &twl->xfer_msg[0];
260         msg->addr = twl->address;
261         msg->len = num_bytes + 1;
262         msg->flags = 0;
263         msg->buf = value;
264         /* over write the first byte of buffer with the register address */
265         *value = twl4030_map[mod_no].base + reg;
266         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
267         mutex_unlock(&twl->xfer_lock);
268
269         /* i2cTransfer returns num messages.translate it pls.. */
270         if (ret >= 0)
271                 ret = 0;
272         return ret;
273 }
274 EXPORT_SYMBOL(twl4030_i2c_write);
275
276 /**
277  * twl4030_i2c_read - Reads a n bit register in TWL4030
278  * @mod_no: module number
279  * @value: an array of num_bytes containing data to be read
280  * @reg: register address (just offset will do)
281  * @num_bytes: number of bytes to transfer
282  *
283  * Returns result of operation - num_bytes is success else failure.
284  */
285 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
286 {
287         int ret;
288         u8 val;
289         int sid;
290         struct twl4030_client *twl;
291         struct i2c_msg *msg;
292
293         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
294                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
295                 return -EPERM;
296         }
297         sid = twl4030_map[mod_no].sid;
298         twl = &twl4030_modules[sid];
299
300         if (unlikely(!inuse)) {
301                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
302                 return -EPERM;
303         }
304         mutex_lock(&twl->xfer_lock);
305         /* [MSG1] fill the register address data */
306         msg = &twl->xfer_msg[0];
307         msg->addr = twl->address;
308         msg->len = 1;
309         msg->flags = 0; /* Read the register value */
310         val = twl4030_map[mod_no].base + reg;
311         msg->buf = &val;
312         /* [MSG2] fill the data rx buffer */
313         msg = &twl->xfer_msg[1];
314         msg->addr = twl->address;
315         msg->flags = I2C_M_RD;  /* Read the register value */
316         msg->len = num_bytes;   /* only n bytes */
317         msg->buf = value;
318         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
319         mutex_unlock(&twl->xfer_lock);
320
321         /* i2cTransfer returns num messages.translate it pls.. */
322         if (ret >= 0)
323                 ret = 0;
324         return ret;
325 }
326 EXPORT_SYMBOL(twl4030_i2c_read);
327
328 /**
329  * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
330  * @mod_no: module number
331  * @value: the value to be written 8 bit
332  * @reg: register address (just offset will do)
333  *
334  * Returns result of operation - 0 is success
335  */
336 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
337 {
338
339         /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
340         u8 temp_buffer[2] = { 0 };
341         /* offset 1 contains the data */
342         temp_buffer[1] = value;
343         return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
344 }
345 EXPORT_SYMBOL(twl4030_i2c_write_u8);
346
347 /**
348  * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
349  * @mod_no: module number
350  * @value: the value read 8 bit
351  * @reg: register address (just offset will do)
352  *
353  * Returns result of operation - 0 is success
354  */
355 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
356 {
357         return twl4030_i2c_read(mod_no, value, reg, 1);
358 }
359 EXPORT_SYMBOL(twl4030_i2c_read_u8);
360
361 /*----------------------------------------------------------------------*/
362
363 /*
364  * NOTE:  We know the first 8 IRQs after pdata->base_irq are
365  * for the PIH, and the next are for the PWR_INT SIH, since
366  * that's how twl_init_irq() sets things up.
367  */
368
369 static int add_children(struct twl4030_platform_data *pdata)
370 {
371         struct platform_device  *pdev = NULL;
372         struct twl4030_client   *twl = NULL;
373         int                     status = 0;
374
375         if (twl_has_bci() && pdata->bci) {
376                 twl = &twl4030_modules[3];
377
378                 pdev = platform_device_alloc("twl4030_bci", -1);
379                 if (!pdev) {
380                         pr_debug("%s: can't alloc bci dev\n", DRIVER_NAME);
381                         status = -ENOMEM;
382                         goto err;
383                 }
384
385                 if (status == 0) {
386                         pdev->dev.parent = &twl->client->dev;
387                         status = platform_device_add_data(pdev, pdata->bci,
388                                         sizeof(*pdata->bci));
389                         if (status < 0) {
390                                 dev_dbg(&twl->client->dev,
391                                         "can't add bci data, %d\n",
392                                         status);
393                                 goto err;
394                         }
395                 }
396
397                 if (status == 0) {
398                         struct resource r = {
399                                 .start = pdata->irq_base + 8 + 1,
400                                 .flags = IORESOURCE_IRQ,
401                         };
402
403                         status = platform_device_add_resources(pdev, &r, 1);
404                 }
405
406                 if (status == 0)
407                         status = platform_device_add(pdev);
408
409                 if (status < 0) {
410                         platform_device_put(pdev);
411                         dev_dbg(&twl->client->dev,
412                                         "can't create bci dev, %d\n",
413                                         status);
414                         goto err;
415                 }
416         }
417
418         if (twl_has_gpio() && pdata->gpio) {
419                 twl = &twl4030_modules[1];
420
421                 pdev = platform_device_alloc("twl4030_gpio", -1);
422                 if (!pdev) {
423                         pr_debug("%s: can't alloc gpio dev\n", DRIVER_NAME);
424                         status = -ENOMEM;
425                         goto err;
426                 }
427
428                 /* more driver model init */
429                 if (status == 0) {
430                         pdev->dev.parent = &twl->client->dev;
431                         /* device_init_wakeup(&pdev->dev, 1); */
432
433                         status = platform_device_add_data(pdev, pdata->gpio,
434                                         sizeof(*pdata->gpio));
435                         if (status < 0) {
436                                 dev_dbg(&twl->client->dev,
437                                         "can't add gpio data, %d\n",
438                                         status);
439                                 goto err;
440                         }
441                 }
442
443                 /* GPIO module IRQ */
444                 if (status == 0) {
445                         struct resource r = {
446                                 .start = pdata->irq_base + 0,
447                                 .flags = IORESOURCE_IRQ,
448                         };
449
450                         status = platform_device_add_resources(pdev, &r, 1);
451                 }
452
453                 if (status == 0)
454                         status = platform_device_add(pdev);
455
456                 if (status < 0) {
457                         platform_device_put(pdev);
458                         dev_dbg(&twl->client->dev,
459                                         "can't create gpio dev, %d\n",
460                                         status);
461                         goto err;
462                 }
463         }
464
465         if (twl_has_keypad() && pdata->keypad) {
466                 pdev = platform_device_alloc("twl4030_keypad", -1);
467                 if (pdev) {
468                         twl = &twl4030_modules[2];
469                         pdev->dev.parent = &twl->client->dev;
470                         device_init_wakeup(&pdev->dev, 1);
471                         status = platform_device_add_data(pdev, pdata->keypad,
472                                         sizeof(*pdata->keypad));
473                         if (status < 0) {
474                                 dev_dbg(&twl->client->dev,
475                                         "can't add keypad data, %d\n",
476                                         status);
477                                 platform_device_put(pdev);
478                                 goto err;
479                         }
480                         status = platform_device_add(pdev);
481                         if (status < 0) {
482                                 platform_device_put(pdev);
483                                 dev_dbg(&twl->client->dev,
484                                                 "can't create keypad dev, %d\n",
485                                                 status);
486                                 goto err;
487                         }
488                 } else {
489                         pr_debug("%s: can't alloc keypad dev\n", DRIVER_NAME);
490                         status = -ENOMEM;
491                         goto err;
492                 }
493         }
494
495         if (twl_has_madc() && pdata->madc) {
496                 pdev = platform_device_alloc("twl4030_madc", -1);
497                 if (pdev) {
498                         twl = &twl4030_modules[2];
499                         pdev->dev.parent = &twl->client->dev;
500                         device_init_wakeup(&pdev->dev, 1);
501                         status = platform_device_add_data(pdev, pdata->madc,
502                                         sizeof(*pdata->madc));
503                         if (status < 0) {
504                                 platform_device_put(pdev);
505                                 dev_dbg(&twl->client->dev,
506                                         "can't add madc data, %d\n",
507                                         status);
508                                 goto err;
509                         }
510                         status = platform_device_add(pdev);
511                         if (status < 0) {
512                                 platform_device_put(pdev);
513                                 dev_dbg(&twl->client->dev,
514                                                 "can't create madc dev, %d\n",
515                                                 status);
516                                 goto err;
517                         }
518                 } else {
519                         pr_debug("%s: can't alloc madc dev\n", DRIVER_NAME);
520                         status = -ENOMEM;
521                         goto err;
522                 }
523         }
524
525         if (twl_has_power() && pdata->power)
526                 twl4030_power_init(pdata->power);
527
528         if (twl_has_rtc()) {
529                 twl = &twl4030_modules[3];
530
531                 pdev = platform_device_alloc("twl4030_rtc", -1);
532                 if (!pdev) {
533                         pr_debug("%s: can't alloc rtc dev\n", DRIVER_NAME);
534                         status = -ENOMEM;
535                 } else {
536                         pdev->dev.parent = &twl->client->dev;
537                         device_init_wakeup(&pdev->dev, 1);
538                 }
539
540                 /*
541                  * REVISIT platform_data here currently might use of
542                  * "msecure" line ... but for now we just expect board
543                  * setup to tell the chip "we are secure" at all times.
544                  * Eventually, Linux might become more aware of such
545                  * HW security concerns, and "least privilege".
546                  */
547
548                 /* RTC module IRQ */
549                 if (status == 0) {
550                         struct resource r = {
551                                 .start = pdata->irq_base + 8 + 3,
552                                 .flags = IORESOURCE_IRQ,
553                         };
554
555                         status = platform_device_add_resources(pdev, &r, 1);
556                 }
557
558                 if (status == 0)
559                         status = platform_device_add(pdev);
560
561                 if (status < 0) {
562                         platform_device_put(pdev);
563                         dev_dbg(&twl->client->dev,
564                                         "can't create rtc dev, %d\n",
565                                         status);
566                         goto err;
567                 }
568         }
569
570         if (twl_has_usb() && pdata->usb) {
571                 twl = &twl4030_modules[0];
572
573                 pdev = platform_device_alloc("twl4030_usb", -1);
574                 if (!pdev) {
575                         pr_debug("%s: can't alloc usb dev\n", DRIVER_NAME);
576                         status = -ENOMEM;
577                         goto err;
578                 }
579
580                 if (status == 0) {
581                         pdev->dev.parent = &twl->client->dev;
582                         device_init_wakeup(&pdev->dev, 1);
583                         status = platform_device_add_data(pdev, pdata->usb,
584                                         sizeof(*pdata->usb));
585                         if (status < 0) {
586                                 platform_device_put(pdev);
587                                 dev_dbg(&twl->client->dev,
588                                         "can't add usb data, %d\n",
589                                         status);
590                                 goto err;
591                         }
592                 }
593
594                 if (status == 0) {
595                         struct resource r = {
596                                 .start = pdata->irq_base + 8 + 2,
597                                 .flags = IORESOURCE_IRQ,
598                         };
599
600                         status = platform_device_add_resources(pdev, &r, 1);
601                 }
602
603                 if (status == 0)
604                         status = platform_device_add(pdev);
605
606                 if (status < 0) {
607                         platform_device_put(pdev);
608                         dev_dbg(&twl->client->dev,
609                                         "can't create usb dev, %d\n",
610                                         status);
611                 }
612         }
613
614 err:
615         if (status)
616                 pr_err("failed to add twl4030's children (status %d)\n", status);
617         return status;
618 }
619
620 /*----------------------------------------------------------------------*/
621
622 /*
623  * These three functions initialize the on-chip clock framework,
624  * letting it generate the right frequencies for USB, MADC, and
625  * other purposes.
626  */
627 static inline int __init protect_pm_master(void)
628 {
629         int e = 0;
630
631         e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
632                         R_PROTECT_KEY);
633         return e;
634 }
635
636 static inline int __init unprotect_pm_master(void)
637 {
638         int e = 0;
639
640         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
641                         R_PROTECT_KEY);
642         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
643                         R_PROTECT_KEY);
644         return e;
645 }
646
647 static void __init clocks_init(void)
648 {
649         int e = 0;
650         struct clk *osc;
651         u32 rate;
652         u8 ctrl = HFCLK_FREQ_26_MHZ;
653
654 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
655         if (cpu_is_omap2430())
656                 osc = clk_get(NULL, "osc_ck");
657         else
658                 osc = clk_get(NULL, "osc_sys_ck");
659 #else
660         /* REVISIT for non-OMAP systems, pass the clock rate from
661          * board init code, using platform_data.
662          */
663         osc = ERR_PTR(-EIO);
664 #endif
665         if (IS_ERR(osc)) {
666                 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
667                                 "using bootloader value (unknown osc rate)\n");
668                 return;
669         }
670
671         rate = clk_get_rate(osc);
672         clk_put(osc);
673
674         switch (rate) {
675         case 19200000:
676                 ctrl = HFCLK_FREQ_19p2_MHZ;
677                 break;
678         case 26000000:
679                 ctrl = HFCLK_FREQ_26_MHZ;
680                 break;
681         case 38400000:
682                 ctrl = HFCLK_FREQ_38p4_MHZ;
683                 break;
684         }
685
686         ctrl |= HIGH_PERF_SQ;
687         e |= unprotect_pm_master();
688         /* effect->MADC+USB ck en */
689         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
690         e |= protect_pm_master();
691
692         if (e < 0)
693                 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
694 }
695
696 /*----------------------------------------------------------------------*/
697
698 int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
699 int twl_exit_irq(void);
700
701 static int twl4030_remove(struct i2c_client *client)
702 {
703         unsigned i;
704         int status;
705
706         status = twl_exit_irq();
707         if (status < 0)
708                 return status;
709
710         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
711                 struct twl4030_client   *twl = &twl4030_modules[i];
712
713                 if (twl->client && twl->client != client)
714                         i2c_unregister_device(twl->client);
715                 twl4030_modules[i].client = NULL;
716         }
717         inuse = false;
718         return 0;
719 }
720
721 /* NOTE:  this driver only handles a single twl4030/tps659x0 chip */
722 static int
723 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
724 {
725         int                             status;
726         unsigned                        i;
727         struct twl4030_platform_data    *pdata = client->dev.platform_data;
728
729         if (!pdata) {
730                 dev_dbg(&client->dev, "no platform data?\n");
731                 return -EINVAL;
732         }
733
734         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
735                 dev_dbg(&client->dev, "can't talk I2C?\n");
736                 return -EIO;
737         }
738
739         if (inuse) {
740                 dev_dbg(&client->dev, "driver is already in use\n");
741                 return -EBUSY;
742         }
743
744         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
745                 struct twl4030_client   *twl = &twl4030_modules[i];
746
747                 twl->address = client->addr + i;
748                 if (i == 0)
749                         twl->client = client;
750                 else {
751                         twl->client = i2c_new_dummy(client->adapter,
752                                         twl->address);
753                         if (!twl->client) {
754                                 dev_err(&twl->client->dev,
755                                         "can't attach client %d\n", i);
756                                 status = -ENOMEM;
757                                 goto fail;
758                         }
759                         strlcpy(twl->client->name, id->name,
760                                         sizeof(twl->client->name));
761                 }
762                 mutex_init(&twl->xfer_lock);
763         }
764         inuse = true;
765
766         /* setup clock framework */
767         clocks_init();
768
769         /* Maybe init the T2 Interrupt subsystem */
770         if (client->irq
771                         && pdata->irq_base
772                         && pdata->irq_end > pdata->irq_base) {
773                 status = twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
774                 if (status < 0)
775                         goto fail;
776         }
777
778         status = add_children(pdata);
779 fail:
780         if (status < 0)
781                 twl4030_remove(client);
782         return status;
783 }
784
785 static const struct i2c_device_id twl4030_ids[] = {
786         { "twl4030", 0 },       /* "Triton 2" */
787         { "tps65950", 0 },      /* catalog version of twl4030 */
788         { "tps65930", 0 },      /* fewer LDOs and DACs; no charger */
789         { "tps65920", 0 },      /* fewer LDOs; no codec or charger */
790         { "twl5030", 0 },       /* T2 updated */
791         { /* end of list */ },
792 };
793 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
794
795 /* One Client Driver , 4 Clients */
796 static struct i2c_driver twl4030_driver = {
797         .driver.name    = DRIVER_NAME,
798         .id_table       = twl4030_ids,
799         .probe          = twl4030_probe,
800         .remove         = twl4030_remove,
801 };
802
803 static int __init twl4030_init(void)
804 {
805         return i2c_add_driver(&twl4030_driver);
806 }
807 subsys_initcall(twl4030_init);
808
809 static void __exit twl4030_exit(void)
810 {
811         i2c_del_driver(&twl4030_driver);
812 }
813 module_exit(twl4030_exit);
814
815 MODULE_AUTHOR("Texas Instruments, Inc.");
816 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
817 MODULE_LICENSE("GPL");