]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mfd/twl4030-core.c
Linux 2.6.29-rc1
[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
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
662         if (IS_ERR(osc)) {
663                 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
664                                 "using bootloader value (unknown osc rate)\n");
665                 return;
666         }
667
668         rate = clk_get_rate(osc);
669         clk_put(osc);
670
671 #else
672         /* REVISIT for non-OMAP systems, pass the clock rate from
673          * board init code, using platform_data.
674          */
675         osc = ERR_PTR(-EIO);
676
677         printk(KERN_WARNING "Skipping twl4030 internal clock init and "
678                "using bootloader value (unknown osc rate)\n");
679
680         return;
681 #endif
682
683         switch (rate) {
684         case 19200000:
685                 ctrl = HFCLK_FREQ_19p2_MHZ;
686                 break;
687         case 26000000:
688                 ctrl = HFCLK_FREQ_26_MHZ;
689                 break;
690         case 38400000:
691                 ctrl = HFCLK_FREQ_38p4_MHZ;
692                 break;
693         }
694
695         ctrl |= HIGH_PERF_SQ;
696         e |= unprotect_pm_master();
697         /* effect->MADC+USB ck en */
698         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
699         e |= protect_pm_master();
700
701         if (e < 0)
702                 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
703 }
704
705 /*----------------------------------------------------------------------*/
706
707 int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
708 int twl_exit_irq(void);
709
710 static int twl4030_remove(struct i2c_client *client)
711 {
712         unsigned i;
713         int status;
714
715         status = twl_exit_irq();
716         if (status < 0)
717                 return status;
718
719         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
720                 struct twl4030_client   *twl = &twl4030_modules[i];
721
722                 if (twl->client && twl->client != client)
723                         i2c_unregister_device(twl->client);
724                 twl4030_modules[i].client = NULL;
725         }
726         inuse = false;
727         return 0;
728 }
729
730 /* NOTE:  this driver only handles a single twl4030/tps659x0 chip */
731 static int
732 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
733 {
734         int                             status;
735         unsigned                        i;
736         struct twl4030_platform_data    *pdata = client->dev.platform_data;
737
738         if (!pdata) {
739                 dev_dbg(&client->dev, "no platform data?\n");
740                 return -EINVAL;
741         }
742
743         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
744                 dev_dbg(&client->dev, "can't talk I2C?\n");
745                 return -EIO;
746         }
747
748         if (inuse) {
749                 dev_dbg(&client->dev, "driver is already in use\n");
750                 return -EBUSY;
751         }
752
753         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
754                 struct twl4030_client   *twl = &twl4030_modules[i];
755
756                 twl->address = client->addr + i;
757                 if (i == 0)
758                         twl->client = client;
759                 else {
760                         twl->client = i2c_new_dummy(client->adapter,
761                                         twl->address);
762                         if (!twl->client) {
763                                 dev_err(&twl->client->dev,
764                                         "can't attach client %d\n", i);
765                                 status = -ENOMEM;
766                                 goto fail;
767                         }
768                         strlcpy(twl->client->name, id->name,
769                                         sizeof(twl->client->name));
770                 }
771                 mutex_init(&twl->xfer_lock);
772         }
773         inuse = true;
774
775         /* setup clock framework */
776         clocks_init();
777
778         /* Maybe init the T2 Interrupt subsystem */
779         if (client->irq
780                         && pdata->irq_base
781                         && pdata->irq_end > pdata->irq_base) {
782                 status = twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
783                 if (status < 0)
784                         goto fail;
785         }
786
787         status = add_children(pdata, id->driver_data);
788 fail:
789         if (status < 0)
790                 twl4030_remove(client);
791         return status;
792 }
793
794 static const struct i2c_device_id twl4030_ids[] = {
795         { "twl4030", TWL4030_VAUX2 },   /* "Triton 2" */
796         { "twl5030", 0 },               /* T2 updated */
797         { "tps65950", 0 },              /* catalog version of twl5030 */
798         { "tps65930", TPS_SUBSET },     /* fewer LDOs and DACs; no charger */
799         { "tps65920", TPS_SUBSET },     /* fewer LDOs; no codec or charger */
800         { /* end of list */ },
801 };
802 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
803
804 /* One Client Driver , 4 Clients */
805 static struct i2c_driver twl4030_driver = {
806         .driver.name    = DRIVER_NAME,
807         .id_table       = twl4030_ids,
808         .probe          = twl4030_probe,
809         .remove         = twl4030_remove,
810 };
811
812 static int __init twl4030_init(void)
813 {
814         return i2c_add_driver(&twl4030_driver);
815 }
816 subsys_initcall(twl4030_init);
817
818 static void __exit twl4030_exit(void)
819 {
820         i2c_del_driver(&twl4030_driver);
821 }
822 module_exit(twl4030_exit);
823
824 MODULE_AUTHOR("Texas Instruments, Inc.");
825 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
826 MODULE_LICENSE("GPL");