Full support for Ginger Console
[linux-ginger.git] / drivers / mfd / twl4030-core.c
blobfcc5dbc8a1b985abb46fd65cedfba3e41c6542fd
1 /*
2 * twl4030_core.c - driver for TWL4030/TPS659x0 PM and audio CODEC devices
4 * Copyright (C) 2005-2006 Texas Instruments, Inc.
6 * Modifications to defer interrupt handling to a kernel thread:
7 * Copyright (C) 2006 MontaVista Software, Inc.
9 * Based on tlv320aic23.c:
10 * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
12 * Code cleanup and modifications to IRQ handler.
13 * by syed khasim <x0khasim@ti.com>
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.
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.
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
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>
36 #include <linux/regulator/machine.h>
38 #include <linux/i2c.h>
39 #include <linux/i2c/twl4030.h>
40 #include <plat/board.h>
42 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
43 #include <plat/cpu.h>
44 #endif
47 * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
48 * Management and System Companion Device" chips originally designed for
49 * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
50 * often at around 3 Mbit/sec, including for interrupt handling.
52 * This driver core provides genirq support for the interrupts emitted,
53 * by the various modules, and exports register access primitives.
55 * FIXME this driver currently requires use of the first interrupt line
56 * (and associated registers).
59 #define DRIVER_NAME "twl4030"
61 #if defined(CONFIG_TWL4030_BCI_BATTERY) || \
62 defined(CONFIG_TWL4030_BCI_BATTERY_MODULE)
63 #define twl_has_bci() true
64 #else
65 #define twl_has_bci() false
66 #endif
68 #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
69 #define twl_has_keypad() true
70 #else
71 #define twl_has_keypad() false
72 #endif
74 #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
75 #define twl_has_gpio() true
76 #else
77 #define twl_has_gpio() false
78 #endif
80 #if defined(CONFIG_REGULATOR_TWL4030) \
81 || defined(CONFIG_REGULATOR_TWL4030_MODULE)
82 #define twl_has_regulator() true
83 #else
84 #define twl_has_regulator() false
85 #endif
87 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
88 #define twl_has_madc() true
89 #else
90 #define twl_has_madc() false
91 #endif
93 #ifdef CONFIG_TWL4030_POWER
94 #define twl_has_power() true
95 #else
96 #define twl_has_power() false
97 #endif
99 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
100 #define twl_has_rtc() true
101 #else
102 #define twl_has_rtc() false
103 #endif
105 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
106 #define twl_has_usb() true
107 #else
108 #define twl_has_usb() false
109 #endif
111 #if defined(CONFIG_TWL4030_WATCHDOG) || \
112 defined(CONFIG_TWL4030_WATCHDOG_MODULE)
113 #define twl_has_watchdog() true
114 #else
115 #define twl_has_watchdog() false
116 #endif
118 /* Triton Core internal information (BEGIN) */
120 /* Last - for index max*/
121 #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
123 #define TWL4030_NUM_SLAVES 4
125 #if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
126 || defined(CONFIG_INPUT_TWL4030_PWBUTTON_MODULE)
127 #define twl_has_pwrbutton() true
128 #else
129 #define twl_has_pwrbutton() false
130 #endif
132 /* Base Address defns for twl4030_map[] */
134 /* subchip/slave 0 - USB ID */
135 #define TWL4030_BASEADD_USB 0x0000
137 /* subchip/slave 1 - AUD ID */
138 #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
139 #define TWL4030_BASEADD_GPIO 0x0098
140 #define TWL4030_BASEADD_INTBR 0x0085
141 #define TWL4030_BASEADD_PIH 0x0080
142 #define TWL4030_BASEADD_TEST 0x004C
144 /* subchip/slave 2 - AUX ID */
145 #define TWL4030_BASEADD_INTERRUPTS 0x00B9
146 #define TWL4030_BASEADD_LED 0x00EE
147 #define TWL4030_BASEADD_MADC 0x0000
148 #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
149 #define TWL4030_BASEADD_PRECHARGE 0x00AA
150 #define TWL4030_BASEADD_PWM0 0x00F8
151 #define TWL4030_BASEADD_PWM1 0x00FB
152 #define TWL4030_BASEADD_PWMA 0x00EF
153 #define TWL4030_BASEADD_PWMB 0x00F1
154 #define TWL4030_BASEADD_KEYPAD 0x00D2
156 /* subchip/slave 3 - POWER ID */
157 #define TWL4030_BASEADD_BACKUP 0x0014
158 #define TWL4030_BASEADD_INT 0x002E
159 #define TWL4030_BASEADD_PM_MASTER 0x0036
160 #define TWL4030_BASEADD_PM_RECEIVER 0x005B
161 #define TWL4030_BASEADD_RTC 0x001C
162 #define TWL4030_BASEADD_SECURED_REG 0x0000
164 /* Triton Core internal information (END) */
167 /* Few power values */
168 #define R_CFG_BOOT 0x05
169 #define R_PROTECT_KEY 0x0E
171 /* access control values for R_PROTECT_KEY */
172 #define KEY_UNLOCK1 0xce
173 #define KEY_UNLOCK2 0xec
174 #define KEY_LOCK 0x00
176 /* some fields in R_CFG_BOOT */
177 #define HFCLK_FREQ_19p2_MHZ (1 << 0)
178 #define HFCLK_FREQ_26_MHZ (2 << 0)
179 #define HFCLK_FREQ_38p4_MHZ (3 << 0)
180 #define HIGH_PERF_SQ (1 << 3)
183 /* chip-specific feature flags, for i2c_device_id.driver_data */
184 #define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
185 #define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
187 /*----------------------------------------------------------------------*/
189 /* is driver active, bound to a chip? */
190 static bool inuse;
192 /* Structure for each TWL4030 Slave */
193 struct twl4030_client {
194 struct i2c_client *client;
195 u8 address;
197 /* max numb of i2c_msg required is for read =2 */
198 struct i2c_msg xfer_msg[2];
200 /* To lock access to xfer_msg */
201 struct mutex xfer_lock;
204 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
207 /* mapping the module id to slave id and base address */
208 struct twl4030mapping {
209 unsigned char sid; /* Slave ID */
210 unsigned char base; /* base address */
213 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
215 * NOTE: don't change this table without updating the
216 * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
217 * so they continue to match the order in this table.
220 { 0, TWL4030_BASEADD_USB },
222 { 1, TWL4030_BASEADD_AUDIO_VOICE },
223 { 1, TWL4030_BASEADD_GPIO },
224 { 1, TWL4030_BASEADD_INTBR },
225 { 1, TWL4030_BASEADD_PIH },
226 { 1, TWL4030_BASEADD_TEST },
228 { 2, TWL4030_BASEADD_KEYPAD },
229 { 2, TWL4030_BASEADD_MADC },
230 { 2, TWL4030_BASEADD_INTERRUPTS },
231 { 2, TWL4030_BASEADD_LED },
232 { 2, TWL4030_BASEADD_MAIN_CHARGE },
233 { 2, TWL4030_BASEADD_PRECHARGE },
234 { 2, TWL4030_BASEADD_PWM0 },
235 { 2, TWL4030_BASEADD_PWM1 },
236 { 2, TWL4030_BASEADD_PWMA },
237 { 2, TWL4030_BASEADD_PWMB },
239 { 3, TWL4030_BASEADD_BACKUP },
240 { 3, TWL4030_BASEADD_INT },
241 { 3, TWL4030_BASEADD_PM_MASTER },
242 { 3, TWL4030_BASEADD_PM_RECEIVER },
243 { 3, TWL4030_BASEADD_RTC },
244 { 3, TWL4030_BASEADD_SECURED_REG },
247 /*----------------------------------------------------------------------*/
249 /* Exported Functions */
252 * twl4030_i2c_write - Writes a n bit register in TWL4030
253 * @mod_no: module number
254 * @value: an array of num_bytes+1 containing data to write
255 * @reg: register address (just offset will do)
256 * @num_bytes: number of bytes to transfer
258 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
259 * valid data starts at Offset 1.
261 * Returns the result of operation - 0 is success
263 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
265 int ret;
266 int sid;
267 struct twl4030_client *twl;
268 struct i2c_msg *msg;
270 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
271 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
272 return -EPERM;
274 sid = twl4030_map[mod_no].sid;
275 twl = &twl4030_modules[sid];
277 if (unlikely(!inuse)) {
278 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
279 return -EPERM;
281 mutex_lock(&twl->xfer_lock);
283 * [MSG1]: fill the register address data
284 * fill the data Tx buffer
286 msg = &twl->xfer_msg[0];
287 msg->addr = twl->address;
288 msg->len = num_bytes + 1;
289 msg->flags = 0;
290 msg->buf = value;
291 /* over write the first byte of buffer with the register address */
292 *value = twl4030_map[mod_no].base + reg;
293 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
294 mutex_unlock(&twl->xfer_lock);
296 /* i2cTransfer returns num messages.translate it pls.. */
297 if (ret >= 0)
298 ret = 0;
299 return ret;
301 EXPORT_SYMBOL(twl4030_i2c_write);
304 * twl4030_i2c_read - Reads a n bit register in TWL4030
305 * @mod_no: module number
306 * @value: an array of num_bytes containing data to be read
307 * @reg: register address (just offset will do)
308 * @num_bytes: number of bytes to transfer
310 * Returns result of operation - num_bytes is success else failure.
312 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
314 int ret;
315 u8 val;
316 int sid;
317 struct twl4030_client *twl;
318 struct i2c_msg *msg;
320 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
321 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
322 return -EPERM;
324 sid = twl4030_map[mod_no].sid;
325 twl = &twl4030_modules[sid];
327 if (unlikely(!inuse)) {
328 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
329 return -EPERM;
331 mutex_lock(&twl->xfer_lock);
332 /* [MSG1] fill the register address data */
333 msg = &twl->xfer_msg[0];
334 msg->addr = twl->address;
335 msg->len = 1;
336 msg->flags = 0; /* Read the register value */
337 val = twl4030_map[mod_no].base + reg;
338 msg->buf = &val;
339 /* [MSG2] fill the data rx buffer */
340 msg = &twl->xfer_msg[1];
341 msg->addr = twl->address;
342 msg->flags = I2C_M_RD; /* Read the register value */
343 msg->len = num_bytes; /* only n bytes */
344 msg->buf = value;
345 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
346 mutex_unlock(&twl->xfer_lock);
348 /* i2cTransfer returns num messages.translate it pls.. */
349 if (ret >= 0)
350 ret = 0;
351 return ret;
353 EXPORT_SYMBOL(twl4030_i2c_read);
356 * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
357 * @mod_no: module number
358 * @value: the value to be written 8 bit
359 * @reg: register address (just offset will do)
361 * Returns result of operation - 0 is success
363 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
366 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
367 u8 temp_buffer[2] = { 0 };
368 /* offset 1 contains the data */
369 temp_buffer[1] = value;
370 return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
372 EXPORT_SYMBOL(twl4030_i2c_write_u8);
375 * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
376 * @mod_no: module number
377 * @value: the value read 8 bit
378 * @reg: register address (just offset will do)
380 * Returns result of operation - 0 is success
382 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
384 return twl4030_i2c_read(mod_no, value, reg, 1);
386 EXPORT_SYMBOL(twl4030_i2c_read_u8);
388 /*----------------------------------------------------------------------*/
390 static struct device *
391 add_numbered_child(unsigned chip, const char *name, int num,
392 void *pdata, unsigned pdata_len,
393 bool can_wakeup, int irq0, int irq1)
395 struct platform_device *pdev;
396 struct twl4030_client *twl = &twl4030_modules[chip];
397 int status;
399 pdev = platform_device_alloc(name, num);
400 if (!pdev) {
401 dev_dbg(&twl->client->dev, "can't alloc dev\n");
402 status = -ENOMEM;
403 goto err;
406 device_init_wakeup(&pdev->dev, can_wakeup);
407 pdev->dev.parent = &twl->client->dev;
409 if (pdata) {
410 status = platform_device_add_data(pdev, pdata, pdata_len);
411 if (status < 0) {
412 dev_dbg(&pdev->dev, "can't add platform_data\n");
413 goto err;
417 if (irq0) {
418 struct resource r[2] = {
419 { .start = irq0, .flags = IORESOURCE_IRQ, },
420 { .start = irq1, .flags = IORESOURCE_IRQ, },
423 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
424 if (status < 0) {
425 dev_dbg(&pdev->dev, "can't add irqs\n");
426 goto err;
430 status = platform_device_add(pdev);
432 err:
433 if (status < 0) {
434 platform_device_put(pdev);
435 dev_err(&twl->client->dev, "can't add %s dev\n", name);
436 return ERR_PTR(status);
438 return &pdev->dev;
441 static inline struct device *add_child(unsigned chip, const char *name,
442 void *pdata, unsigned pdata_len,
443 bool can_wakeup, int irq0, int irq1)
445 return add_numbered_child(chip, name, -1, pdata, pdata_len,
446 can_wakeup, irq0, irq1);
449 static struct device *
450 add_regulator_linked(int num, struct regulator_init_data *pdata,
451 struct regulator_consumer_supply *consumers,
452 unsigned num_consumers)
454 /* regulator framework demands init_data ... */
455 if (!pdata)
456 return NULL;
458 if (consumers) {
459 pdata->consumer_supplies = consumers;
460 pdata->num_consumer_supplies = num_consumers;
463 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
464 return add_numbered_child(3, "twl4030_reg", num,
465 pdata, sizeof(*pdata), false, 0, 0);
468 static struct device *
469 add_regulator(int num, struct regulator_init_data *pdata)
471 return add_regulator_linked(num, pdata, NULL, 0);
475 * NOTE: We know the first 8 IRQs after pdata->base_irq are
476 * for the PIH, and the next are for the PWR_INT SIH, since
477 * that's how twl_init_irq() sets things up.
480 static int
481 add_children(struct twl4030_platform_data *pdata, unsigned long features)
483 struct device *child;
485 if (twl_has_bci() && pdata->bci && !(features & TPS_SUBSET)) {
486 child = add_child(3, "twl4030_bci",
487 pdata->bci, sizeof(*pdata->bci),
488 false,
489 /* irq0 = CHG_PRES, irq1 = BCI */
490 pdata->irq_base + 8 + 1, pdata->irq_base + 2);
491 if (IS_ERR(child))
492 return PTR_ERR(child);
495 if (twl_has_gpio() && pdata->gpio) {
496 child = add_child(1, "twl4030_gpio",
497 pdata->gpio, sizeof(*pdata->gpio),
498 false, pdata->irq_base + 0, 0);
499 if (IS_ERR(child))
500 return PTR_ERR(child);
503 if (twl_has_keypad() && pdata->keypad) {
504 child = add_child(2, "twl4030_keypad",
505 pdata->keypad, sizeof(*pdata->keypad),
506 true, pdata->irq_base + 1, 0);
507 if (IS_ERR(child))
508 return PTR_ERR(child);
511 if (twl_has_madc() && pdata->madc) {
512 child = add_child(2, "twl4030_madc",
513 pdata->madc, sizeof(*pdata->madc),
514 true, pdata->irq_base + 3, 0);
515 if (IS_ERR(child))
516 return PTR_ERR(child);
519 if (twl_has_rtc()) {
521 * REVISIT platform_data here currently might expose the
522 * "msecure" line ... but for now we just expect board
523 * setup to tell the chip "it's always ok to SET_TIME".
524 * Eventually, Linux might become more aware of such
525 * HW security concerns, and "least privilege".
527 child = add_child(3, "twl4030_rtc",
528 NULL, 0,
529 true, pdata->irq_base + 8 + 3, 0);
530 if (IS_ERR(child))
531 return PTR_ERR(child);
534 if (twl_has_usb() && pdata->usb) {
536 static struct regulator_consumer_supply usb1v5 = {
537 .supply = "usb1v5",
539 static struct regulator_consumer_supply usb1v8 = {
540 .supply = "usb1v8",
542 static struct regulator_consumer_supply usb3v1 = {
543 .supply = "usb3v1",
546 /* First add the regulators so that they can be used by transceiver */
547 if (twl_has_regulator()) {
548 /* this is a template that gets copied */
549 struct regulator_init_data usb_fixed = {
550 .constraints.valid_modes_mask =
551 REGULATOR_MODE_NORMAL
552 | REGULATOR_MODE_STANDBY,
553 .constraints.valid_ops_mask =
554 REGULATOR_CHANGE_MODE
555 | REGULATOR_CHANGE_STATUS,
558 child = add_regulator_linked(TWL4030_REG_VUSB1V5,
559 &usb_fixed, &usb1v5, 1);
560 if (IS_ERR(child))
561 return PTR_ERR(child);
563 child = add_regulator_linked(TWL4030_REG_VUSB1V8,
564 &usb_fixed, &usb1v8, 1);
565 if (IS_ERR(child))
566 return PTR_ERR(child);
568 child = add_regulator_linked(TWL4030_REG_VUSB3V1,
569 &usb_fixed, &usb3v1, 1);
570 if (IS_ERR(child))
571 return PTR_ERR(child);
575 child = add_child(0, "twl4030_usb",
576 pdata->usb, sizeof(*pdata->usb),
577 true,
578 /* irq0 = USB_PRES, irq1 = USB */
579 pdata->irq_base + 8 + 2, pdata->irq_base + 4);
581 if (IS_ERR(child))
582 return PTR_ERR(child);
584 /* we need to connect regulators to this transceiver */
585 if (twl_has_regulator() && child) {
586 usb1v5.dev = child;
587 usb1v8.dev = child;
588 usb3v1.dev = child;
592 if (twl_has_watchdog()) {
593 child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
594 if (IS_ERR(child))
595 return PTR_ERR(child);
598 if (twl_has_pwrbutton()) {
599 child = add_child(1, "twl4030_pwrbutton",
600 NULL, 0, true, pdata->irq_base + 8 + 0, 0);
601 if (IS_ERR(child))
602 return PTR_ERR(child);
605 if (twl_has_regulator()) {
607 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
608 if (IS_ERR(child))
609 return PTR_ERR(child);
612 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
613 if (IS_ERR(child))
614 return PTR_ERR(child);
616 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
617 if (IS_ERR(child))
618 return PTR_ERR(child);
620 child = add_regulator((features & TWL4030_VAUX2)
621 ? TWL4030_REG_VAUX2_4030
622 : TWL4030_REG_VAUX2,
623 pdata->vaux2);
624 if (IS_ERR(child))
625 return PTR_ERR(child);
628 /* maybe add LDOs that are omitted on cost-reduced parts */
629 if (twl_has_regulator() && !(features & TPS_SUBSET)) {
630 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
631 if (IS_ERR(child))
632 return PTR_ERR(child);
634 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
635 if (IS_ERR(child))
636 return PTR_ERR(child);
638 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
639 if (IS_ERR(child))
640 return PTR_ERR(child);
642 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
643 if (IS_ERR(child))
644 return PTR_ERR(child);
646 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
647 if (IS_ERR(child))
648 return PTR_ERR(child);
650 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
651 if (IS_ERR(child))
652 return PTR_ERR(child);
654 child = add_regulator(TWL4030_REG_VUSB1V5, pdata->vusb1v5);
655 if (IS_ERR(child))
656 return PTR_ERR(child);
658 child = add_regulator(TWL4030_REG_VUSB1V8, pdata->vusb1v8);
659 if (IS_ERR(child))
660 return PTR_ERR(child);
662 child = add_regulator(TWL4030_REG_VUSB3V1, pdata->vusb3v1);
663 if (IS_ERR(child))
664 return PTR_ERR(child);
667 return 0;
670 /*----------------------------------------------------------------------*/
673 * These three functions initialize the on-chip clock framework,
674 * letting it generate the right frequencies for USB, MADC, and
675 * other purposes.
677 static inline int __init protect_pm_master(void)
679 int e = 0;
681 e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
682 R_PROTECT_KEY);
683 return e;
686 static inline int __init unprotect_pm_master(void)
688 int e = 0;
690 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
691 R_PROTECT_KEY);
692 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
693 R_PROTECT_KEY);
694 return e;
697 static void clocks_init(struct device *dev)
699 int e = 0;
700 struct clk *osc;
701 u32 rate;
702 u8 ctrl = HFCLK_FREQ_26_MHZ;
704 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
705 if (cpu_is_omap2430())
706 osc = clk_get(dev, "osc_ck");
707 else
708 osc = clk_get(dev, "osc_sys_ck");
710 if (IS_ERR(osc)) {
711 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
712 "using bootloader value (unknown osc rate)\n");
713 return;
716 rate = clk_get_rate(osc);
717 clk_put(osc);
719 #else
720 /* REVISIT for non-OMAP systems, pass the clock rate from
721 * board init code, using platform_data.
723 osc = ERR_PTR(-EIO);
725 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
726 "using bootloader value (unknown osc rate)\n");
728 return;
729 #endif
731 switch (rate) {
732 case 19200000:
733 ctrl = HFCLK_FREQ_19p2_MHZ;
734 break;
735 case 26000000:
736 ctrl = HFCLK_FREQ_26_MHZ;
737 break;
738 case 38400000:
739 ctrl = HFCLK_FREQ_38p4_MHZ;
740 break;
743 ctrl |= HIGH_PERF_SQ;
744 e |= unprotect_pm_master();
745 /* effect->MADC+USB ck en */
746 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
747 e |= protect_pm_master();
749 if (e < 0)
750 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
753 /*----------------------------------------------------------------------*/
755 int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
756 int twl_exit_irq(void);
758 static int twl4030_remove(struct i2c_client *client)
760 unsigned i;
761 int status;
763 status = twl_exit_irq();
764 if (status < 0)
765 return status;
767 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
768 struct twl4030_client *twl = &twl4030_modules[i];
770 if (twl->client && twl->client != client)
771 i2c_unregister_device(twl->client);
772 twl4030_modules[i].client = NULL;
774 inuse = false;
775 return 0;
778 #define TWL4030_VAUX2_1P8V 0x5
779 #define ENABLE_VAUX2_DEV_GRP 0x20
781 /* Enable USB GPIOs on new OMAP3EVM */
782 static void usb_gpio_settings(void)
784 unsigned char val;
786 if (get_omap3_evm_rev() < OMAP3EVM_BOARD_GEN_2)
787 return;
789 /* Enable TWL GPIO Module */
790 twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0x04, REG_GPIO_CTRL);
792 /* Configure GPIO-6 as output */
793 twl4030_i2c_read_u8(TWL4030_MODULE_GPIO, &val, REG_GPIODATADIR1);
794 val |= 0x4;
795 twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, val, REG_GPIODATADIR1);
797 /* Set GPIO6 = 1 */
798 twl4030_i2c_read_u8(TWL4030_MODULE_GPIO, &val, REG_GPIODATAOUT1);
799 val |= 0x40;
800 twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, val, REG_GPIODATAOUT1);
803 /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
804 static int
805 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
807 int status;
808 unsigned i;
809 struct twl4030_platform_data *pdata = client->dev.platform_data;
811 if (!pdata) {
812 dev_dbg(&client->dev, "no platform data?\n");
813 return -EINVAL;
816 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
817 dev_dbg(&client->dev, "can't talk I2C?\n");
818 return -EIO;
821 if (inuse) {
822 dev_dbg(&client->dev, "driver is already in use\n");
823 return -EBUSY;
826 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
827 struct twl4030_client *twl = &twl4030_modules[i];
829 twl->address = client->addr + i;
830 if (i == 0)
831 twl->client = client;
832 else {
833 twl->client = i2c_new_dummy(client->adapter,
834 twl->address);
835 if (!twl->client) {
836 dev_err(&twl->client->dev,
837 "can't attach client %d\n", i);
838 status = -ENOMEM;
839 goto fail;
841 strlcpy(twl->client->name, id->name,
842 sizeof(twl->client->name));
844 mutex_init(&twl->xfer_lock);
846 inuse = true;
848 /* setup clock framework */
849 clocks_init(&client->dev);
851 /* load power event scripts */
852 if (twl_has_power() && pdata->power)
853 twl4030_power_init(pdata->power);
855 /* Maybe init the T2 Interrupt subsystem */
856 if (client->irq
857 && pdata->irq_base
858 && pdata->irq_end > pdata->irq_base) {
859 status = twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
860 if (status < 0)
861 goto fail;
864 status = add_children(pdata, id->driver_data);
866 usb_gpio_settings();
867 fail:
868 if (status < 0)
869 twl4030_remove(client);
870 return status;
873 static const struct i2c_device_id twl4030_ids[] = {
874 { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
875 { "twl5030", 0 }, /* T2 updated */
876 { "tps65950", 0 }, /* catalog version of twl5030 */
877 { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
878 { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
879 { /* end of list */ },
881 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
883 /* One Client Driver , 4 Clients */
884 static struct i2c_driver twl4030_driver = {
885 .driver.name = DRIVER_NAME,
886 .id_table = twl4030_ids,
887 .probe = twl4030_probe,
888 .remove = twl4030_remove,
891 static int __init twl4030_init(void)
893 return i2c_add_driver(&twl4030_driver);
895 subsys_initcall(twl4030_init);
897 static void __exit twl4030_exit(void)
899 i2c_del_driver(&twl4030_driver);
901 module_exit(twl4030_exit);
903 MODULE_AUTHOR("Texas Instruments, Inc.");
904 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
905 MODULE_LICENSE("GPL");