2 * twl_core.c - driver for TWL4030/TWL5030/TWL60X0/TPS659x0 PM
3 * and audio CODEC devices
5 * Copyright (C) 2005-2006 Texas Instruments, Inc.
7 * Modifications to defer interrupt handling to a kernel thread:
8 * Copyright (C) 2006 MontaVista Software, Inc.
10 * Based on tlv320aic23.c:
11 * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
13 * Code cleanup and modifications to IRQ handler.
14 * by syed khasim <x0khasim@ti.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <linux/init.h>
32 #include <linux/mutex.h>
33 #include <linux/platform_device.h>
34 #include <linux/clk.h>
35 #include <linux/err.h>
37 #include <linux/regulator/machine.h>
39 #include <linux/i2c.h>
40 #include <linux/i2c/twl.h>
42 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
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 "twl"
61 #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
62 #define twl_has_keypad() true
64 #define twl_has_keypad() false
67 #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
68 #define twl_has_gpio() true
70 #define twl_has_gpio() false
73 #if defined(CONFIG_REGULATOR_TWL4030) \
74 || defined(CONFIG_REGULATOR_TWL4030_MODULE)
75 #define twl_has_regulator() true
77 #define twl_has_regulator() false
80 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
81 #define twl_has_madc() true
83 #define twl_has_madc() false
86 #if defined(CONFIG_SENSORS_TWL4030_MADC) ||\
87 defined(CONFIG_SENSORS_TWL4030_MADC_MODULE)
88 #define twl_has_madc_hwmon() true
90 #define twl_has_madc_hwmon() false
93 #ifdef CONFIG_TWL4030_POWER
94 #define twl_has_power() true
96 #define twl_has_power() false
99 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
100 #define twl_has_rtc() true
102 #define twl_has_rtc() false
105 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE) ||\
106 defined(CONFIG_TWL6030_USB) || defined(CONFIG_TWL6030_USB_MODULE)
107 #define twl_has_usb() true
109 #define twl_has_usb() false
112 #if defined(CONFIG_TWL4030_WATCHDOG) || \
113 defined(CONFIG_TWL4030_WATCHDOG_MODULE)
114 #define twl_has_watchdog() true
116 #define twl_has_watchdog() false
119 #if defined(CONFIG_TWL4030_CODEC) || defined(CONFIG_TWL4030_CODEC_MODULE) ||\
120 defined(CONFIG_SND_SOC_TWL6040) || defined(CONFIG_SND_SOC_TWL6040_MODULE)
121 #define twl_has_codec() true
123 #define twl_has_codec() false
126 #if defined(CONFIG_CHARGER_TWL4030) || defined(CONFIG_CHARGER_TWL4030_MODULE)
127 #define twl_has_bci() true
129 #define twl_has_bci() false
132 #if defined (CONFIG_TWL4030_POWEROFF)
133 #define twl_has_poweroff() true
135 #define twl_has_poweroff() false
138 /* Triton Core internal information (BEGIN) */
140 /* Last - for index max*/
141 #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
143 #define TWL_NUM_SLAVES 4
145 #if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
146 || defined(CONFIG_INPUT_TWL4030_PWRBUTTON_MODULE)
147 #define twl_has_pwrbutton() true
149 #define twl_has_pwrbutton() false
152 #define SUB_CHIP_ID0 0
153 #define SUB_CHIP_ID1 1
154 #define SUB_CHIP_ID2 2
155 #define SUB_CHIP_ID3 3
157 #define TWL_MODULE_LAST TWL4030_MODULE_LAST
159 /* Base Address defns for twl4030_map[] */
161 /* subchip/slave 0 - USB ID */
162 #define TWL4030_BASEADD_USB 0x0000
164 /* subchip/slave 1 - AUD ID */
165 #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
166 #define TWL4030_BASEADD_GPIO 0x0098
167 #define TWL4030_BASEADD_INTBR 0x0085
168 #define TWL4030_BASEADD_PIH 0x0080
169 #define TWL4030_BASEADD_TEST 0x004C
171 /* subchip/slave 2 - AUX ID */
172 #define TWL4030_BASEADD_INTERRUPTS 0x00B9
173 #define TWL4030_BASEADD_LED 0x00EE
174 #define TWL4030_BASEADD_MADC 0x0000
175 #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
176 #define TWL4030_BASEADD_PRECHARGE 0x00AA
177 #define TWL4030_BASEADD_PWM0 0x00F8
178 #define TWL4030_BASEADD_PWM1 0x00FB
179 #define TWL4030_BASEADD_PWMA 0x00EF
180 #define TWL4030_BASEADD_PWMB 0x00F1
181 #define TWL4030_BASEADD_KEYPAD 0x00D2
183 #define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
184 #define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
187 /* subchip/slave 3 - POWER ID */
188 #define TWL4030_BASEADD_BACKUP 0x0014
189 #define TWL4030_BASEADD_INT 0x002E
190 #define TWL4030_BASEADD_PM_MASTER 0x0036
191 #define TWL4030_BASEADD_PM_RECEIVER 0x005B
192 #define TWL4030_BASEADD_RTC 0x001C
193 #define TWL4030_BASEADD_SECURED_REG 0x0000
195 /* Triton Core internal information (END) */
198 /* subchip/slave 0 0x48 - POWER */
199 #define TWL6030_BASEADD_RTC 0x0000
200 #define TWL6030_BASEADD_MEM 0x0017
201 #define TWL6030_BASEADD_PM_MASTER 0x001F
202 #define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
203 #define TWL6030_BASEADD_PM_MISC 0x00E2
204 #define TWL6030_BASEADD_PM_PUPD 0x00F0
206 /* subchip/slave 1 0x49 - FEATURE */
207 #define TWL6030_BASEADD_USB 0x0000
208 #define TWL6030_BASEADD_GPADC_CTRL 0x002E
209 #define TWL6030_BASEADD_AUX 0x0090
210 #define TWL6030_BASEADD_PWM 0x00BA
211 #define TWL6030_BASEADD_GASGAUGE 0x00C0
212 #define TWL6030_BASEADD_PIH 0x00D0
213 #define TWL6030_BASEADD_CHARGER 0x00E0
215 /* subchip/slave 2 0x4A - DFT */
216 #define TWL6030_BASEADD_DIEID 0x00C0
218 /* subchip/slave 3 0x4B - AUDIO */
219 #define TWL6030_BASEADD_AUDIO 0x0000
220 #define TWL6030_BASEADD_RSV 0x0000
221 #define TWL6030_BASEADD_ZERO 0x0000
223 /* Few power values */
224 #define R_CFG_BOOT 0x05
226 /* some fields in R_CFG_BOOT */
227 #define HFCLK_FREQ_19p2_MHZ (1 << 0)
228 #define HFCLK_FREQ_26_MHZ (2 << 0)
229 #define HFCLK_FREQ_38p4_MHZ (3 << 0)
230 #define HIGH_PERF_SQ (1 << 3)
231 #define CK32K_LOWPWR_EN (1 << 7)
234 /* chip-specific feature flags, for i2c_device_id.driver_data */
235 #define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
236 #define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
237 #define TWL5031 BIT(2) /* twl5031 has different registers */
238 #define TWL6030_CLASS BIT(3) /* TWL6030 class */
240 /* for pm_power_off */
241 #define PWR_P1_SW_EVENTS 0x10
242 #define PWR_DEVOFF (1 << 0)
244 /*----------------------------------------------------------------------*/
246 /* is driver active, bound to a chip? */
249 static unsigned int twl_id;
250 unsigned int twl_rev(void)
254 EXPORT_SYMBOL(twl_rev);
256 /* Structure for each TWL4030/TWL6030 Slave */
258 struct i2c_client *client;
261 /* max numb of i2c_msg required is for read =2 */
262 struct i2c_msg xfer_msg[2];
264 /* To lock access to xfer_msg */
265 struct mutex xfer_lock;
268 static struct twl_client twl_modules[TWL_NUM_SLAVES];
271 /* mapping the module id to slave id and base address */
273 unsigned char sid; /* Slave ID */
274 unsigned char base; /* base address */
276 static struct twl_mapping *twl_map;
278 static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
280 * NOTE: don't change this table without updating the
281 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
282 * so they continue to match the order in this table.
285 { 0, TWL4030_BASEADD_USB },
287 { 1, TWL4030_BASEADD_AUDIO_VOICE },
288 { 1, TWL4030_BASEADD_GPIO },
289 { 1, TWL4030_BASEADD_INTBR },
290 { 1, TWL4030_BASEADD_PIH },
291 { 1, TWL4030_BASEADD_TEST },
293 { 2, TWL4030_BASEADD_KEYPAD },
294 { 2, TWL4030_BASEADD_MADC },
295 { 2, TWL4030_BASEADD_INTERRUPTS },
296 { 2, TWL4030_BASEADD_LED },
297 { 2, TWL4030_BASEADD_MAIN_CHARGE },
298 { 2, TWL4030_BASEADD_PRECHARGE },
299 { 2, TWL4030_BASEADD_PWM0 },
300 { 2, TWL4030_BASEADD_PWM1 },
301 { 2, TWL4030_BASEADD_PWMA },
302 { 2, TWL4030_BASEADD_PWMB },
303 { 2, TWL5031_BASEADD_ACCESSORY },
304 { 2, TWL5031_BASEADD_INTERRUPTS },
306 { 3, TWL4030_BASEADD_BACKUP },
307 { 3, TWL4030_BASEADD_INT },
308 { 3, TWL4030_BASEADD_PM_MASTER },
309 { 3, TWL4030_BASEADD_PM_RECEIVER },
310 { 3, TWL4030_BASEADD_RTC },
311 { 3, TWL4030_BASEADD_SECURED_REG },
314 static struct twl_mapping twl6030_map[] = {
316 * NOTE: don't change this table without updating the
317 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
318 * so they continue to match the order in this table.
320 { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
321 { SUB_CHIP_ID3, TWL6030_BASEADD_AUDIO },
322 { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
323 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
324 { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
326 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
327 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
328 { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
329 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
330 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
332 { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
333 { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
334 { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
335 { SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
336 { SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
338 { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
339 { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
340 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
341 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
342 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
343 { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
344 { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
346 { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
347 { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
350 /*----------------------------------------------------------------------*/
352 /* Exported Functions */
355 * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
356 * @mod_no: module number
357 * @value: an array of num_bytes+1 containing data to write
358 * @reg: register address (just offset will do)
359 * @num_bytes: number of bytes to transfer
361 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
362 * valid data starts at Offset 1.
364 * Returns the result of operation - 0 is success
366 int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
370 struct twl_client *twl;
373 if (unlikely(mod_no > TWL_MODULE_LAST)) {
374 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
377 sid = twl_map[mod_no].sid;
378 twl = &twl_modules[sid];
380 if (unlikely(!inuse)) {
381 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
384 mutex_lock(&twl->xfer_lock);
386 * [MSG1]: fill the register address data
387 * fill the data Tx buffer
389 msg = &twl->xfer_msg[0];
390 msg->addr = twl->address;
391 msg->len = num_bytes + 1;
394 /* over write the first byte of buffer with the register address */
395 *value = twl_map[mod_no].base + reg;
396 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
397 mutex_unlock(&twl->xfer_lock);
399 /* i2c_transfer returns number of messages transferred */
401 pr_err("%s: i2c_write failed to transfer all messages\n",
411 EXPORT_SYMBOL(twl_i2c_write);
414 * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
415 * @mod_no: module number
416 * @value: an array of num_bytes containing data to be read
417 * @reg: register address (just offset will do)
418 * @num_bytes: number of bytes to transfer
420 * Returns result of operation - num_bytes is success else failure.
422 int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
427 struct twl_client *twl;
430 if (unlikely(mod_no > TWL_MODULE_LAST)) {
431 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
434 sid = twl_map[mod_no].sid;
435 twl = &twl_modules[sid];
437 if (unlikely(!inuse)) {
438 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
441 mutex_lock(&twl->xfer_lock);
442 /* [MSG1] fill the register address data */
443 msg = &twl->xfer_msg[0];
444 msg->addr = twl->address;
446 msg->flags = 0; /* Read the register value */
447 val = twl_map[mod_no].base + reg;
449 /* [MSG2] fill the data rx buffer */
450 msg = &twl->xfer_msg[1];
451 msg->addr = twl->address;
452 msg->flags = I2C_M_RD; /* Read the register value */
453 msg->len = num_bytes; /* only n bytes */
455 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
456 mutex_unlock(&twl->xfer_lock);
458 /* i2c_transfer returns number of messages transferred */
460 pr_err("%s: i2c_read failed to transfer all messages\n",
470 EXPORT_SYMBOL(twl_i2c_read);
473 * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
474 * @mod_no: module number
475 * @value: the value to be written 8 bit
476 * @reg: register address (just offset will do)
478 * Returns result of operation - 0 is success
480 int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
483 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
484 u8 temp_buffer[2] = { 0 };
485 /* offset 1 contains the data */
486 temp_buffer[1] = value;
487 return twl_i2c_write(mod_no, temp_buffer, reg, 1);
489 EXPORT_SYMBOL(twl_i2c_write_u8);
492 * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
493 * @mod_no: module number
494 * @value: the value read 8 bit
495 * @reg: register address (just offset will do)
497 * Returns result of operation - 0 is success
499 int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
501 return twl_i2c_read(mod_no, value, reg, 1);
503 EXPORT_SYMBOL(twl_i2c_read_u8);
505 /*----------------------------------------------------------------------*/
507 static struct device *
508 add_numbered_child(unsigned chip, const char *name, int num,
509 void *pdata, unsigned pdata_len,
510 bool can_wakeup, int irq0, int irq1)
512 struct platform_device *pdev;
513 struct twl_client *twl = &twl_modules[chip];
516 pdev = platform_device_alloc(name, num);
518 dev_dbg(&twl->client->dev, "can't alloc dev\n");
523 device_init_wakeup(&pdev->dev, can_wakeup);
524 pdev->dev.parent = &twl->client->dev;
527 status = platform_device_add_data(pdev, pdata, pdata_len);
529 dev_dbg(&pdev->dev, "can't add platform_data\n");
535 struct resource r[2] = {
536 { .start = irq0, .flags = IORESOURCE_IRQ, },
537 { .start = irq1, .flags = IORESOURCE_IRQ, },
540 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
542 dev_dbg(&pdev->dev, "can't add irqs\n");
547 status = platform_device_add(pdev);
551 platform_device_put(pdev);
552 dev_err(&twl->client->dev, "can't add %s dev\n", name);
553 return ERR_PTR(status);
558 static inline struct device *add_child(unsigned chip, const char *name,
559 void *pdata, unsigned pdata_len,
560 bool can_wakeup, int irq0, int irq1)
562 return add_numbered_child(chip, name, -1, pdata, pdata_len,
563 can_wakeup, irq0, irq1);
566 static struct device *
567 add_regulator_linked(int num, struct regulator_init_data *pdata,
568 struct regulator_consumer_supply *consumers,
569 unsigned num_consumers)
571 unsigned sub_chip_id;
572 /* regulator framework demands init_data ... */
577 pdata->consumer_supplies = consumers;
578 pdata->num_consumer_supplies = num_consumers;
581 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
582 sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
583 return add_numbered_child(sub_chip_id, "twl_reg", num,
584 pdata, sizeof(*pdata), false, 0, 0);
587 static struct device *
588 add_regulator(int num, struct regulator_init_data *pdata)
590 return add_regulator_linked(num, pdata, NULL, 0);
594 * NOTE: We know the first 8 IRQs after pdata->base_irq are
595 * for the PIH, and the next are for the PWR_INT SIH, since
596 * that's how twl_init_irq() sets things up.
600 add_children(struct twl4030_platform_data *pdata, unsigned long features)
602 struct device *child;
603 unsigned sub_chip_id;
605 if (twl_has_gpio() && pdata->gpio) {
606 child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
607 pdata->gpio, sizeof(*pdata->gpio),
608 false, pdata->irq_base + GPIO_INTR_OFFSET, 0);
610 return PTR_ERR(child);
613 if (twl_has_keypad() && pdata->keypad) {
614 child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
615 pdata->keypad, sizeof(*pdata->keypad),
616 true, pdata->irq_base + KEYPAD_INTR_OFFSET, 0);
618 return PTR_ERR(child);
621 if (twl_has_madc() && pdata->madc) {
622 child = add_child(2, "twl4030_madc",
623 pdata->madc, sizeof(*pdata->madc),
624 true, pdata->irq_base + MADC_INTR_OFFSET, 0);
626 return PTR_ERR(child);
629 if (twl_has_madc_hwmon()) {
630 child = add_child(2, "twl4030_madc_hwmon",
632 true, pdata->irq_base + MADC_INTR_OFFSET, 0);
634 return PTR_ERR(child);
639 * REVISIT platform_data here currently might expose the
640 * "msecure" line ... but for now we just expect board
641 * setup to tell the chip "it's always ok to SET_TIME".
642 * Eventually, Linux might become more aware of such
643 * HW security concerns, and "least privilege".
645 sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
646 child = add_child(sub_chip_id, "twl_rtc",
648 true, pdata->irq_base + RTC_INTR_OFFSET, 0);
650 return PTR_ERR(child);
653 if (twl_has_usb() && pdata->usb && twl_class_is_4030()) {
655 static struct regulator_consumer_supply usb1v5 = {
658 static struct regulator_consumer_supply usb1v8 = {
661 static struct regulator_consumer_supply usb3v1 = {
665 /* First add the regulators so that they can be used by transceiver */
666 if (twl_has_regulator()) {
667 /* this is a template that gets copied */
668 struct regulator_init_data usb_fixed = {
669 .constraints.valid_modes_mask =
670 REGULATOR_MODE_NORMAL
671 | REGULATOR_MODE_STANDBY,
672 .constraints.valid_ops_mask =
673 REGULATOR_CHANGE_MODE
674 | REGULATOR_CHANGE_STATUS,
677 child = add_regulator_linked(TWL4030_REG_VUSB1V5,
678 &usb_fixed, &usb1v5, 1);
680 return PTR_ERR(child);
682 child = add_regulator_linked(TWL4030_REG_VUSB1V8,
683 &usb_fixed, &usb1v8, 1);
685 return PTR_ERR(child);
687 child = add_regulator_linked(TWL4030_REG_VUSB3V1,
688 &usb_fixed, &usb3v1, 1);
690 return PTR_ERR(child);
694 child = add_child(0, "twl4030_usb",
695 pdata->usb, sizeof(*pdata->usb),
697 /* irq0 = USB_PRES, irq1 = USB */
698 pdata->irq_base + USB_PRES_INTR_OFFSET,
699 pdata->irq_base + USB_INTR_OFFSET);
702 return PTR_ERR(child);
704 /* we need to connect regulators to this transceiver */
705 if (twl_has_regulator() && child) {
711 if (twl_has_usb() && pdata->usb && twl_class_is_6030()) {
713 static struct regulator_consumer_supply usb3v3 = {
717 if (twl_has_regulator()) {
718 /* this is a template that gets copied */
719 struct regulator_init_data usb_fixed = {
720 .constraints.valid_modes_mask =
721 REGULATOR_MODE_NORMAL
722 | REGULATOR_MODE_STANDBY,
723 .constraints.valid_ops_mask =
724 REGULATOR_CHANGE_MODE
725 | REGULATOR_CHANGE_STATUS,
728 child = add_regulator_linked(TWL6030_REG_VUSB,
729 &usb_fixed, &usb3v3, 1);
731 return PTR_ERR(child);
734 child = add_child(0, "twl6030_usb",
735 pdata->usb, sizeof(*pdata->usb),
737 /* irq1 = VBUS_PRES, irq0 = USB ID */
738 pdata->irq_base + USBOTG_INTR_OFFSET,
739 pdata->irq_base + USB_PRES_INTR_OFFSET);
742 return PTR_ERR(child);
743 /* we need to connect regulators to this transceiver */
744 if (twl_has_regulator() && child)
749 if (twl_has_watchdog() && twl_class_is_4030()) {
750 child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
752 return PTR_ERR(child);
755 if (twl_has_pwrbutton() && twl_class_is_4030()) {
756 child = add_child(1, "twl4030_pwrbutton",
757 NULL, 0, true, pdata->irq_base + 8 + 0, 0);
759 return PTR_ERR(child);
762 if (twl_has_codec() && pdata->codec && twl_class_is_4030()) {
763 sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
764 child = add_child(sub_chip_id, "twl4030-audio",
765 pdata->codec, sizeof(*pdata->codec),
768 return PTR_ERR(child);
771 /* Phoenix codec driver is probed directly atm */
772 if (twl_has_codec() && pdata->codec && twl_class_is_6030()) {
773 sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
774 child = add_child(sub_chip_id, "twl6040-codec",
775 pdata->codec, sizeof(*pdata->codec),
778 return PTR_ERR(child);
781 /* twl4030 regulators */
782 if (twl_has_regulator() && twl_class_is_4030()) {
783 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
785 return PTR_ERR(child);
787 child = add_regulator(TWL4030_REG_VIO, pdata->vio);
789 return PTR_ERR(child);
791 child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1);
793 return PTR_ERR(child);
795 child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2);
797 return PTR_ERR(child);
799 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
801 return PTR_ERR(child);
803 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
805 return PTR_ERR(child);
807 child = add_regulator((features & TWL4030_VAUX2)
808 ? TWL4030_REG_VAUX2_4030
812 return PTR_ERR(child);
814 child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1);
816 return PTR_ERR(child);
818 child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2);
820 return PTR_ERR(child);
822 child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig);
824 return PTR_ERR(child);
827 /* maybe add LDOs that are omitted on cost-reduced parts */
828 if (twl_has_regulator() && !(features & TPS_SUBSET)
829 && twl_class_is_4030()) {
830 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
832 return PTR_ERR(child);
834 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
836 return PTR_ERR(child);
838 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
840 return PTR_ERR(child);
842 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
844 return PTR_ERR(child);
846 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
848 return PTR_ERR(child);
850 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
852 return PTR_ERR(child);
855 /* twl6030 regulators */
856 if (twl_has_regulator() && twl_class_is_6030()) {
857 child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc);
859 return PTR_ERR(child);
861 child = add_regulator(TWL6030_REG_VPP, pdata->vpp);
863 return PTR_ERR(child);
865 child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim);
867 return PTR_ERR(child);
869 child = add_regulator(TWL6030_REG_VANA, pdata->vana);
871 return PTR_ERR(child);
873 child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio);
875 return PTR_ERR(child);
877 child = add_regulator(TWL6030_REG_VDAC, pdata->vdac);
879 return PTR_ERR(child);
881 child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1);
883 return PTR_ERR(child);
885 child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2);
887 return PTR_ERR(child);
889 child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3);
891 return PTR_ERR(child);
893 child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg);
895 return PTR_ERR(child);
898 if (twl_has_bci() && pdata->bci &&
899 !(features & (TPS_SUBSET | TWL5031))) {
900 child = add_child(3, "twl4030_bci",
901 pdata->bci, sizeof(*pdata->bci), false,
902 /* irq0 = CHG_PRES, irq1 = BCI */
903 pdata->irq_base + BCI_PRES_INTR_OFFSET,
904 pdata->irq_base + BCI_INTR_OFFSET);
906 return PTR_ERR(child);
912 /*----------------------------------------------------------------------*/
915 * These three functions initialize the on-chip clock framework,
916 * letting it generate the right frequencies for USB, MADC, and
919 static inline int __init protect_pm_master(void)
923 e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
924 TWL4030_PM_MASTER_PROTECT_KEY);
928 static inline int __init unprotect_pm_master(void)
932 e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
933 TWL4030_PM_MASTER_KEY_CFG1,
934 TWL4030_PM_MASTER_PROTECT_KEY);
935 e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
936 TWL4030_PM_MASTER_KEY_CFG2,
937 TWL4030_PM_MASTER_PROTECT_KEY);
942 static void clocks_init(struct device *dev,
943 struct twl4030_clock_init_data *clock)
948 u8 ctrl = HFCLK_FREQ_26_MHZ;
950 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
951 if (cpu_is_omap2430())
952 osc = clk_get(dev, "osc_ck");
954 osc = clk_get(dev, "osc_sys_ck");
957 printk(KERN_WARNING "Skipping twl internal clock init and "
958 "using bootloader value (unknown osc rate)\n");
962 rate = clk_get_rate(osc);
966 /* REVISIT for non-OMAP systems, pass the clock rate from
967 * board init code, using platform_data.
971 printk(KERN_WARNING "Skipping twl internal clock init and "
972 "using bootloader value (unknown osc rate)\n");
979 ctrl = HFCLK_FREQ_19p2_MHZ;
982 ctrl = HFCLK_FREQ_26_MHZ;
985 ctrl = HFCLK_FREQ_38p4_MHZ;
989 ctrl |= HIGH_PERF_SQ;
990 if (clock && clock->ck32k_lowpwr_enable)
991 ctrl |= CK32K_LOWPWR_EN;
993 e |= unprotect_pm_master();
994 /* effect->MADC+USB ck en */
995 e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
996 e |= protect_pm_master();
999 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
1002 /*----------------------------------------------------------------------*/
1004 int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
1005 int twl4030_exit_irq(void);
1006 int twl4030_init_chip_irq(const char *chip);
1007 int twl6030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
1008 int twl6030_exit_irq(void);
1010 static int twl_remove(struct i2c_client *client)
1015 if (twl_class_is_4030())
1016 status = twl4030_exit_irq();
1018 status = twl6030_exit_irq();
1023 for (i = 0; i < TWL_NUM_SLAVES; i++) {
1024 struct twl_client *twl = &twl_modules[i];
1026 if (twl->client && twl->client != client)
1027 i2c_unregister_device(twl->client);
1028 twl_modules[i].client = NULL;
1034 static void twl_poweroff(void)
1039 err = twl_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &val,
1042 pr_err("%s: i2c error %d while reading TWL4030"
1043 "PM_MASTER P1_SW_EVENTS\n",
1050 err = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, val,
1053 pr_err("%s: i2c error %d while writing TWL4030"
1054 "PM_MASTER P1_SW_EVENTS\n",
1058 /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
1059 static int __devinit
1060 twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
1064 struct twl4030_platform_data *pdata = client->dev.platform_data;
1068 dev_dbg(&client->dev, "no platform data?\n");
1072 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1073 dev_dbg(&client->dev, "can't talk I2C?\n");
1078 dev_dbg(&client->dev, "driver is already in use\n");
1082 for (i = 0; i < TWL_NUM_SLAVES; i++) {
1083 struct twl_client *twl = &twl_modules[i];
1085 twl->address = client->addr + i;
1087 twl->client = client;
1089 twl->client = i2c_new_dummy(client->adapter,
1092 dev_err(&client->dev,
1093 "can't attach client %d\n", i);
1098 mutex_init(&twl->xfer_lock);
1101 if ((id->driver_data) & TWL6030_CLASS) {
1102 twl_id = TWL6030_CLASS_ID;
1103 twl_map = &twl6030_map[0];
1105 twl_id = TWL4030_CLASS_ID;
1106 twl_map = &twl4030_map[0];
1109 /* setup clock framework */
1110 clocks_init(&client->dev, pdata->clock);
1112 /* load power event scripts */
1113 if (twl_has_power() && pdata->power)
1114 twl4030_power_init(pdata->power);
1116 /* Maybe init the T2 Interrupt subsystem */
1119 && pdata->irq_end > pdata->irq_base) {
1120 if (twl_class_is_4030()) {
1121 twl4030_init_chip_irq(id->name);
1122 status = twl4030_init_irq(client->irq, pdata->irq_base,
1125 status = twl6030_init_irq(client->irq, pdata->irq_base,
1133 /* Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
1134 * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1135 * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1138 if (twl_class_is_4030()) {
1139 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
1140 temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
1141 I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
1142 twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
1145 if(twl_has_poweroff())
1147 /* initialize pm_power_off routine */
1148 pm_power_off = twl_poweroff;
1151 status = add_children(pdata, id->driver_data);
1158 static const struct i2c_device_id twl_ids[] = {
1159 { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
1160 { "twl5030", 0 }, /* T2 updated */
1161 { "twl5031", TWL5031 }, /* TWL5030 updated */
1162 { "tps65950", 0 }, /* catalog version of twl5030 */
1163 { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
1164 { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
1165 { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
1166 { /* end of list */ },
1168 MODULE_DEVICE_TABLE(i2c, twl_ids);
1170 /* One Client Driver , 4 Clients */
1171 static struct i2c_driver twl_driver = {
1172 .driver.name = DRIVER_NAME,
1173 .id_table = twl_ids,
1175 .remove = twl_remove,
1178 static int __init twl_init(void)
1180 return i2c_add_driver(&twl_driver);
1182 subsys_initcall(twl_init);
1184 static void __exit twl_exit(void)
1186 i2c_del_driver(&twl_driver);
1188 module_exit(twl_exit);
1190 MODULE_AUTHOR("Texas Instruments, Inc.");
1191 MODULE_DESCRIPTION("I2C Core interface for TWL");
1192 MODULE_LICENSE("GPL");