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