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