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