Merge branch 'dmaengine' into async-tx-next
[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 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
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 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
93 #define twl_has_rtc()   true
94 #else
95 #define twl_has_rtc()   false
96 #endif
97
98 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
99 #define twl_has_usb()   true
100 #else
101 #define twl_has_usb()   false
102 #endif
103
104 #if defined(CONFIG_TWL4030_WATCHDOG) || \
105         defined(CONFIG_TWL4030_WATCHDOG_MODULE)
106 #define twl_has_watchdog()        true
107 #else
108 #define twl_has_watchdog()        false
109 #endif
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 /*----------------------------------------------------------------------*/
235
236 /* Exported Functions */
237
238 /**
239  * twl4030_i2c_write - Writes a n bit register in TWL4030
240  * @mod_no: module number
241  * @value: an array of num_bytes+1 containing data to write
242  * @reg: register address (just offset will do)
243  * @num_bytes: number of bytes to transfer
244  *
245  * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
246  * valid data starts at Offset 1.
247  *
248  * Returns the result of operation - 0 is success
249  */
250 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
251 {
252         int ret;
253         int sid;
254         struct twl4030_client *twl;
255         struct i2c_msg *msg;
256
257         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
258                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
259                 return -EPERM;
260         }
261         sid = twl4030_map[mod_no].sid;
262         twl = &twl4030_modules[sid];
263
264         if (unlikely(!inuse)) {
265                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
266                 return -EPERM;
267         }
268         mutex_lock(&twl->xfer_lock);
269         /*
270          * [MSG1]: fill the register address data
271          * fill the data Tx buffer
272          */
273         msg = &twl->xfer_msg[0];
274         msg->addr = twl->address;
275         msg->len = num_bytes + 1;
276         msg->flags = 0;
277         msg->buf = value;
278         /* over write the first byte of buffer with the register address */
279         *value = twl4030_map[mod_no].base + reg;
280         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
281         mutex_unlock(&twl->xfer_lock);
282
283         /* i2cTransfer returns num messages.translate it pls.. */
284         if (ret >= 0)
285                 ret = 0;
286         return ret;
287 }
288 EXPORT_SYMBOL(twl4030_i2c_write);
289
290 /**
291  * twl4030_i2c_read - Reads a n bit register in TWL4030
292  * @mod_no: module number
293  * @value: an array of num_bytes containing data to be read
294  * @reg: register address (just offset will do)
295  * @num_bytes: number of bytes to transfer
296  *
297  * Returns result of operation - num_bytes is success else failure.
298  */
299 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
300 {
301         int ret;
302         u8 val;
303         int sid;
304         struct twl4030_client *twl;
305         struct i2c_msg *msg;
306
307         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
308                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
309                 return -EPERM;
310         }
311         sid = twl4030_map[mod_no].sid;
312         twl = &twl4030_modules[sid];
313
314         if (unlikely(!inuse)) {
315                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
316                 return -EPERM;
317         }
318         mutex_lock(&twl->xfer_lock);
319         /* [MSG1] fill the register address data */
320         msg = &twl->xfer_msg[0];
321         msg->addr = twl->address;
322         msg->len = 1;
323         msg->flags = 0; /* Read the register value */
324         val = twl4030_map[mod_no].base + reg;
325         msg->buf = &val;
326         /* [MSG2] fill the data rx buffer */
327         msg = &twl->xfer_msg[1];
328         msg->addr = twl->address;
329         msg->flags = I2C_M_RD;  /* Read the register value */
330         msg->len = num_bytes;   /* only n bytes */
331         msg->buf = value;
332         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
333         mutex_unlock(&twl->xfer_lock);
334
335         /* i2cTransfer returns num messages.translate it pls.. */
336         if (ret >= 0)
337                 ret = 0;
338         return ret;
339 }
340 EXPORT_SYMBOL(twl4030_i2c_read);
341
342 /**
343  * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
344  * @mod_no: module number
345  * @value: the value to be written 8 bit
346  * @reg: register address (just offset will do)
347  *
348  * Returns result of operation - 0 is success
349  */
350 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
351 {
352
353         /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
354         u8 temp_buffer[2] = { 0 };
355         /* offset 1 contains the data */
356         temp_buffer[1] = value;
357         return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
358 }
359 EXPORT_SYMBOL(twl4030_i2c_write_u8);
360
361 /**
362  * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
363  * @mod_no: module number
364  * @value: the value read 8 bit
365  * @reg: register address (just offset will do)
366  *
367  * Returns result of operation - 0 is success
368  */
369 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
370 {
371         return twl4030_i2c_read(mod_no, value, reg, 1);
372 }
373 EXPORT_SYMBOL(twl4030_i2c_read_u8);
374
375 /*----------------------------------------------------------------------*/
376
377 static struct device *
378 add_numbered_child(unsigned chip, const char *name, int num,
379                 void *pdata, unsigned pdata_len,
380                 bool can_wakeup, int irq0, int irq1)
381 {
382         struct platform_device  *pdev;
383         struct twl4030_client   *twl = &twl4030_modules[chip];
384         int                     status;
385
386         pdev = platform_device_alloc(name, num);
387         if (!pdev) {
388                 dev_dbg(&twl->client->dev, "can't alloc dev\n");
389                 status = -ENOMEM;
390                 goto err;
391         }
392
393         device_init_wakeup(&pdev->dev, can_wakeup);
394         pdev->dev.parent = &twl->client->dev;
395
396         if (pdata) {
397                 status = platform_device_add_data(pdev, pdata, pdata_len);
398                 if (status < 0) {
399                         dev_dbg(&pdev->dev, "can't add platform_data\n");
400                         goto err;
401                 }
402         }
403
404         if (irq0) {
405                 struct resource r[2] = {
406                         { .start = irq0, .flags = IORESOURCE_IRQ, },
407                         { .start = irq1, .flags = IORESOURCE_IRQ, },
408                 };
409
410                 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
411                 if (status < 0) {
412                         dev_dbg(&pdev->dev, "can't add irqs\n");
413                         goto err;
414                 }
415         }
416
417         status = platform_device_add(pdev);
418
419 err:
420         if (status < 0) {
421                 platform_device_put(pdev);
422                 dev_err(&twl->client->dev, "can't add %s dev\n", name);
423                 return ERR_PTR(status);
424         }
425         return &pdev->dev;
426 }
427
428 static inline struct device *add_child(unsigned chip, const char *name,
429                 void *pdata, unsigned pdata_len,
430                 bool can_wakeup, int irq0, int irq1)
431 {
432         return add_numbered_child(chip, name, -1, pdata, pdata_len,
433                 can_wakeup, irq0, irq1);
434 }
435
436 static struct device *
437 add_regulator_linked(int num, struct regulator_init_data *pdata,
438                 struct regulator_consumer_supply *consumers,
439                 unsigned num_consumers)
440 {
441         /* regulator framework demands init_data ... */
442         if (!pdata)
443                 return NULL;
444
445         if (consumers) {
446                 pdata->consumer_supplies = consumers;
447                 pdata->num_consumer_supplies = num_consumers;
448         }
449
450         /* NOTE:  we currently ignore regulator IRQs, e.g. for short circuits */
451         return add_numbered_child(3, "twl4030_reg", num,
452                 pdata, sizeof(*pdata), false, 0, 0);
453 }
454
455 static struct device *
456 add_regulator(int num, struct regulator_init_data *pdata)
457 {
458         return add_regulator_linked(num, pdata, NULL, 0);
459 }
460
461 /*
462  * NOTE:  We know the first 8 IRQs after pdata->base_irq are
463  * for the PIH, and the next are for the PWR_INT SIH, since
464  * that's how twl_init_irq() sets things up.
465  */
466
467 static int
468 add_children(struct twl4030_platform_data *pdata, unsigned long features)
469 {
470         struct device   *child;
471         struct device   *usb_transceiver = NULL;
472
473         if (twl_has_bci() && pdata->bci && !(features & TPS_SUBSET)) {
474                 child = add_child(3, "twl4030_bci",
475                                 pdata->bci, sizeof(*pdata->bci),
476                                 false,
477                                 /* irq0 = CHG_PRES, irq1 = BCI */
478                                 pdata->irq_base + 8 + 1, pdata->irq_base + 2);
479                 if (IS_ERR(child))
480                         return PTR_ERR(child);
481         }
482
483         if (twl_has_gpio() && pdata->gpio) {
484                 child = add_child(1, "twl4030_gpio",
485                                 pdata->gpio, sizeof(*pdata->gpio),
486                                 false, pdata->irq_base + 0, 0);
487                 if (IS_ERR(child))
488                         return PTR_ERR(child);
489         }
490
491         if (twl_has_keypad() && pdata->keypad) {
492                 child = add_child(2, "twl4030_keypad",
493                                 pdata->keypad, sizeof(*pdata->keypad),
494                                 true, pdata->irq_base + 1, 0);
495                 if (IS_ERR(child))
496                         return PTR_ERR(child);
497         }
498
499         if (twl_has_madc() && pdata->madc) {
500                 child = add_child(2, "twl4030_madc",
501                                 pdata->madc, sizeof(*pdata->madc),
502                                 true, pdata->irq_base + 3, 0);
503                 if (IS_ERR(child))
504                         return PTR_ERR(child);
505         }
506
507         if (twl_has_rtc()) {
508                 /*
509                  * REVISIT platform_data here currently might expose the
510                  * "msecure" line ... but for now we just expect board
511                  * setup to tell the chip "it's always ok to SET_TIME".
512                  * Eventually, Linux might become more aware of such
513                  * HW security concerns, and "least privilege".
514                  */
515                 child = add_child(3, "twl4030_rtc",
516                                 NULL, 0,
517                                 true, pdata->irq_base + 8 + 3, 0);
518                 if (IS_ERR(child))
519                         return PTR_ERR(child);
520         }
521
522         if (twl_has_usb() && pdata->usb) {
523                 child = add_child(0, "twl4030_usb",
524                                 pdata->usb, sizeof(*pdata->usb),
525                                 true,
526                                 /* irq0 = USB_PRES, irq1 = USB */
527                                 pdata->irq_base + 8 + 2, pdata->irq_base + 4);
528                 if (IS_ERR(child))
529                         return PTR_ERR(child);
530
531                 /* we need to connect regulators to this transceiver */
532                 usb_transceiver = child;
533         }
534
535         if (twl_has_watchdog()) {
536                 child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
537                 if (IS_ERR(child))
538                         return PTR_ERR(child);
539         }
540
541         if (twl_has_regulator()) {
542                 /*
543                 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
544                 if (IS_ERR(child))
545                         return PTR_ERR(child);
546                 */
547
548                 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
549                 if (IS_ERR(child))
550                         return PTR_ERR(child);
551
552                 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
553                 if (IS_ERR(child))
554                         return PTR_ERR(child);
555
556                 child = add_regulator((features & TWL4030_VAUX2)
557                                         ? TWL4030_REG_VAUX2_4030
558                                         : TWL4030_REG_VAUX2,
559                                 pdata->vaux2);
560                 if (IS_ERR(child))
561                         return PTR_ERR(child);
562         }
563
564         if (twl_has_regulator() && usb_transceiver) {
565                 static struct regulator_consumer_supply usb1v5 = {
566                         .supply =       "usb1v5",
567                 };
568                 static struct regulator_consumer_supply usb1v8 = {
569                         .supply =       "usb1v8",
570                 };
571                 static struct regulator_consumer_supply usb3v1 = {
572                         .supply =       "usb3v1",
573                 };
574
575                 /* this is a template that gets copied */
576                 struct regulator_init_data usb_fixed = {
577                         .constraints.valid_modes_mask =
578                                   REGULATOR_MODE_NORMAL
579                                 | REGULATOR_MODE_STANDBY,
580                         .constraints.valid_ops_mask =
581                                   REGULATOR_CHANGE_MODE
582                                 | REGULATOR_CHANGE_STATUS,
583                 };
584
585                 usb1v5.dev = usb_transceiver;
586                 usb1v8.dev = usb_transceiver;
587                 usb3v1.dev = usb_transceiver;
588
589                 child = add_regulator_linked(TWL4030_REG_VUSB1V5, &usb_fixed,
590                                 &usb1v5, 1);
591                 if (IS_ERR(child))
592                         return PTR_ERR(child);
593
594                 child = add_regulator_linked(TWL4030_REG_VUSB1V8, &usb_fixed,
595                                 &usb1v8, 1);
596                 if (IS_ERR(child))
597                         return PTR_ERR(child);
598
599                 child = add_regulator_linked(TWL4030_REG_VUSB3V1, &usb_fixed,
600                                 &usb3v1, 1);
601                 if (IS_ERR(child))
602                         return PTR_ERR(child);
603         }
604
605         /* maybe add LDOs that are omitted on cost-reduced parts */
606         if (twl_has_regulator() && !(features & TPS_SUBSET)) {
607                 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
608                 if (IS_ERR(child))
609                         return PTR_ERR(child);
610
611                 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
612                 if (IS_ERR(child))
613                         return PTR_ERR(child);
614
615                 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
616                 if (IS_ERR(child))
617                         return PTR_ERR(child);
618
619                 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
620                 if (IS_ERR(child))
621                         return PTR_ERR(child);
622
623                 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
624                 if (IS_ERR(child))
625                         return PTR_ERR(child);
626
627                 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
628                 if (IS_ERR(child))
629                         return PTR_ERR(child);
630         }
631
632         return 0;
633 }
634
635 /*----------------------------------------------------------------------*/
636
637 /*
638  * These three functions initialize the on-chip clock framework,
639  * letting it generate the right frequencies for USB, MADC, and
640  * other purposes.
641  */
642 static inline int __init protect_pm_master(void)
643 {
644         int e = 0;
645
646         e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
647                         R_PROTECT_KEY);
648         return e;
649 }
650
651 static inline int __init unprotect_pm_master(void)
652 {
653         int e = 0;
654
655         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
656                         R_PROTECT_KEY);
657         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
658                         R_PROTECT_KEY);
659         return e;
660 }
661
662 static void clocks_init(struct device *dev)
663 {
664         int e = 0;
665         struct clk *osc;
666         u32 rate;
667         u8 ctrl = HFCLK_FREQ_26_MHZ;
668
669 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
670         if (cpu_is_omap2430())
671                 osc = clk_get(dev, "osc_ck");
672         else
673                 osc = clk_get(dev, "osc_sys_ck");
674
675         if (IS_ERR(osc)) {
676                 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
677                                 "using bootloader value (unknown osc rate)\n");
678                 return;
679         }
680
681         rate = clk_get_rate(osc);
682         clk_put(osc);
683
684 #else
685         /* REVISIT for non-OMAP systems, pass the clock rate from
686          * board init code, using platform_data.
687          */
688         osc = ERR_PTR(-EIO);
689
690         printk(KERN_WARNING "Skipping twl4030 internal clock init and "
691                "using bootloader value (unknown osc rate)\n");
692
693         return;
694 #endif
695
696         switch (rate) {
697         case 19200000:
698                 ctrl = HFCLK_FREQ_19p2_MHZ;
699                 break;
700         case 26000000:
701                 ctrl = HFCLK_FREQ_26_MHZ;
702                 break;
703         case 38400000:
704                 ctrl = HFCLK_FREQ_38p4_MHZ;
705                 break;
706         }
707
708         ctrl |= HIGH_PERF_SQ;
709         e |= unprotect_pm_master();
710         /* effect->MADC+USB ck en */
711         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
712         e |= protect_pm_master();
713
714         if (e < 0)
715                 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
716 }
717
718 /*----------------------------------------------------------------------*/
719
720 int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
721 int twl_exit_irq(void);
722
723 static int twl4030_remove(struct i2c_client *client)
724 {
725         unsigned i;
726         int status;
727
728         status = twl_exit_irq();
729         if (status < 0)
730                 return status;
731
732         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
733                 struct twl4030_client   *twl = &twl4030_modules[i];
734
735                 if (twl->client && twl->client != client)
736                         i2c_unregister_device(twl->client);
737                 twl4030_modules[i].client = NULL;
738         }
739         inuse = false;
740         return 0;
741 }
742
743 /* NOTE:  this driver only handles a single twl4030/tps659x0 chip */
744 static int
745 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
746 {
747         int                             status;
748         unsigned                        i;
749         struct twl4030_platform_data    *pdata = client->dev.platform_data;
750
751         if (!pdata) {
752                 dev_dbg(&client->dev, "no platform data?\n");
753                 return -EINVAL;
754         }
755
756         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
757                 dev_dbg(&client->dev, "can't talk I2C?\n");
758                 return -EIO;
759         }
760
761         if (inuse) {
762                 dev_dbg(&client->dev, "driver is already in use\n");
763                 return -EBUSY;
764         }
765
766         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
767                 struct twl4030_client   *twl = &twl4030_modules[i];
768
769                 twl->address = client->addr + i;
770                 if (i == 0)
771                         twl->client = client;
772                 else {
773                         twl->client = i2c_new_dummy(client->adapter,
774                                         twl->address);
775                         if (!twl->client) {
776                                 dev_err(&twl->client->dev,
777                                         "can't attach client %d\n", i);
778                                 status = -ENOMEM;
779                                 goto fail;
780                         }
781                         strlcpy(twl->client->name, id->name,
782                                         sizeof(twl->client->name));
783                 }
784                 mutex_init(&twl->xfer_lock);
785         }
786         inuse = true;
787
788         /* setup clock framework */
789         clocks_init(&client->dev);
790
791         /* Maybe init the T2 Interrupt subsystem */
792         if (client->irq
793                         && pdata->irq_base
794                         && pdata->irq_end > pdata->irq_base) {
795                 status = twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
796                 if (status < 0)
797                         goto fail;
798         }
799
800         status = add_children(pdata, id->driver_data);
801 fail:
802         if (status < 0)
803                 twl4030_remove(client);
804         return status;
805 }
806
807 static const struct i2c_device_id twl4030_ids[] = {
808         { "twl4030", TWL4030_VAUX2 },   /* "Triton 2" */
809         { "twl5030", 0 },               /* T2 updated */
810         { "tps65950", 0 },              /* catalog version of twl5030 */
811         { "tps65930", TPS_SUBSET },     /* fewer LDOs and DACs; no charger */
812         { "tps65920", TPS_SUBSET },     /* fewer LDOs; no codec or charger */
813         { /* end of list */ },
814 };
815 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
816
817 /* One Client Driver , 4 Clients */
818 static struct i2c_driver twl4030_driver = {
819         .driver.name    = DRIVER_NAME,
820         .id_table       = twl4030_ids,
821         .probe          = twl4030_probe,
822         .remove         = twl4030_remove,
823 };
824
825 static int __init twl4030_init(void)
826 {
827         return i2c_add_driver(&twl4030_driver);
828 }
829 subsys_initcall(twl4030_init);
830
831 static void __exit twl4030_exit(void)
832 {
833         i2c_del_driver(&twl4030_driver);
834 }
835 module_exit(twl4030_exit);
836
837 MODULE_AUTHOR("Texas Instruments, Inc.");
838 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
839 MODULE_LICENSE("GPL");