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/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/clk.h>
36 #include <linux/err.h>
37 #include <linux/device.h>
39 #include <linux/of_irq.h>
40 #include <linux/of_platform.h>
41 #include <linux/irq.h>
42 #include <linux/irqdomain.h>
44 #include <linux/regulator/machine.h>
46 #include <linux/i2c.h>
47 #include <linux/i2c/twl.h>
54 * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
55 * Management and System Companion Device" chips originally designed for
56 * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
57 * often at around 3 Mbit/sec, including for interrupt handling.
59 * This driver core provides genirq support for the interrupts emitted,
60 * by the various modules, and exports register access primitives.
62 * FIXME this driver currently requires use of the first interrupt line
63 * (and associated registers).
66 #define DRIVER_NAME "twl"
68 #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
69 #define twl_has_keypad() true
71 #define twl_has_keypad() false
74 #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
75 #define twl_has_gpio() true
77 #define twl_has_gpio() false
80 #if defined(CONFIG_REGULATOR_TWL4030) \
81 || defined(CONFIG_REGULATOR_TWL4030_MODULE)
82 #define twl_has_regulator() true
84 #define twl_has_regulator() false
87 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
88 #define twl_has_madc() true
90 #define twl_has_madc() 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_MFD_TWL4030_AUDIO) || \
120 defined(CONFIG_MFD_TWL4030_AUDIO_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 /* Triton Core internal information (BEGIN) */
134 /* Last - for index max*/
135 #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
137 #define TWL_NUM_SLAVES 4
139 #if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
140 || defined(CONFIG_INPUT_TWL4030_PWRBUTTON_MODULE)
141 #define twl_has_pwrbutton() true
143 #define twl_has_pwrbutton() false
146 #define SUB_CHIP_ID0 0
147 #define SUB_CHIP_ID1 1
148 #define SUB_CHIP_ID2 2
149 #define SUB_CHIP_ID3 3
150 #define SUB_CHIP_ID_INVAL 0xff
152 #define TWL_MODULE_LAST TWL4030_MODULE_LAST
154 /* Base Address defns for twl4030_map[] */
156 /* subchip/slave 0 - USB ID */
157 #define TWL4030_BASEADD_USB 0x0000
159 /* subchip/slave 1 - AUD ID */
160 #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
161 #define TWL4030_BASEADD_GPIO 0x0098
162 #define TWL4030_BASEADD_INTBR 0x0085
163 #define TWL4030_BASEADD_PIH 0x0080
164 #define TWL4030_BASEADD_TEST 0x004C
166 /* subchip/slave 2 - AUX ID */
167 #define TWL4030_BASEADD_INTERRUPTS 0x00B9
168 #define TWL4030_BASEADD_LED 0x00EE
169 #define TWL4030_BASEADD_MADC 0x0000
170 #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
171 #define TWL4030_BASEADD_PRECHARGE 0x00AA
172 #define TWL4030_BASEADD_PWM0 0x00F8
173 #define TWL4030_BASEADD_PWM1 0x00FB
174 #define TWL4030_BASEADD_PWMA 0x00EF
175 #define TWL4030_BASEADD_PWMB 0x00F1
176 #define TWL4030_BASEADD_KEYPAD 0x00D2
178 #define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
179 #define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
182 /* subchip/slave 3 - POWER ID */
183 #define TWL4030_BASEADD_BACKUP 0x0014
184 #define TWL4030_BASEADD_INT 0x002E
185 #define TWL4030_BASEADD_PM_MASTER 0x0036
186 #define TWL4030_BASEADD_PM_RECEIVER 0x005B
187 #define TWL4030_BASEADD_RTC 0x001C
188 #define TWL4030_BASEADD_SECURED_REG 0x0000
190 /* Triton Core internal information (END) */
193 /* subchip/slave 0 0x48 - POWER */
194 #define TWL6030_BASEADD_RTC 0x0000
195 #define TWL6030_BASEADD_MEM 0x0017
196 #define TWL6030_BASEADD_PM_MASTER 0x001F
197 #define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
198 #define TWL6030_BASEADD_PM_MISC 0x00E2
199 #define TWL6030_BASEADD_PM_PUPD 0x00F0
201 /* subchip/slave 1 0x49 - FEATURE */
202 #define TWL6030_BASEADD_USB 0x0000
203 #define TWL6030_BASEADD_GPADC_CTRL 0x002E
204 #define TWL6030_BASEADD_AUX 0x0090
205 #define TWL6030_BASEADD_PWM 0x00BA
206 #define TWL6030_BASEADD_GASGAUGE 0x00C0
207 #define TWL6030_BASEADD_PIH 0x00D0
208 #define TWL6030_BASEADD_CHARGER 0x00E0
209 #define TWL6025_BASEADD_CHARGER 0x00DA
211 /* subchip/slave 2 0x4A - DFT */
212 #define TWL6030_BASEADD_DIEID 0x00C0
214 /* subchip/slave 3 0x4B - AUDIO */
215 #define TWL6030_BASEADD_AUDIO 0x0000
216 #define TWL6030_BASEADD_RSV 0x0000
217 #define TWL6030_BASEADD_ZERO 0x0000
219 /* Few power values */
220 #define R_CFG_BOOT 0x05
222 /* some fields in R_CFG_BOOT */
223 #define HFCLK_FREQ_19p2_MHZ (1 << 0)
224 #define HFCLK_FREQ_26_MHZ (2 << 0)
225 #define HFCLK_FREQ_38p4_MHZ (3 << 0)
226 #define HIGH_PERF_SQ (1 << 3)
227 #define CK32K_LOWPWR_EN (1 << 7)
229 /*----------------------------------------------------------------------*/
231 /* is driver active, bound to a chip? */
234 /* TWL IDCODE Register value */
235 static u32 twl_idcode
;
237 static unsigned int twl_id
;
238 unsigned int twl_rev(void)
242 EXPORT_SYMBOL(twl_rev
);
244 /* Structure for each TWL4030/TWL6030 Slave */
246 struct i2c_client
*client
;
249 /* max numb of i2c_msg required is for read =2 */
250 struct i2c_msg xfer_msg
[2];
252 /* To lock access to xfer_msg */
253 struct mutex xfer_lock
;
256 static struct twl_client twl_modules
[TWL_NUM_SLAVES
];
258 /* mapping the module id to slave id and base address */
260 unsigned char sid
; /* Slave ID */
261 unsigned char base
; /* base address */
263 static struct twl_mapping
*twl_map
;
265 static struct twl_mapping twl4030_map
[TWL4030_MODULE_LAST
+ 1] = {
267 * NOTE: don't change this table without updating the
268 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
269 * so they continue to match the order in this table.
272 { 0, TWL4030_BASEADD_USB
},
274 { 1, TWL4030_BASEADD_AUDIO_VOICE
},
275 { 1, TWL4030_BASEADD_GPIO
},
276 { 1, TWL4030_BASEADD_INTBR
},
277 { 1, TWL4030_BASEADD_PIH
},
278 { 1, TWL4030_BASEADD_TEST
},
280 { 2, TWL4030_BASEADD_KEYPAD
},
281 { 2, TWL4030_BASEADD_MADC
},
282 { 2, TWL4030_BASEADD_INTERRUPTS
},
283 { 2, TWL4030_BASEADD_LED
},
284 { 2, TWL4030_BASEADD_MAIN_CHARGE
},
285 { 2, TWL4030_BASEADD_PRECHARGE
},
286 { 2, TWL4030_BASEADD_PWM0
},
287 { 2, TWL4030_BASEADD_PWM1
},
288 { 2, TWL4030_BASEADD_PWMA
},
289 { 2, TWL4030_BASEADD_PWMB
},
290 { 2, TWL5031_BASEADD_ACCESSORY
},
291 { 2, TWL5031_BASEADD_INTERRUPTS
},
293 { 3, TWL4030_BASEADD_BACKUP
},
294 { 3, TWL4030_BASEADD_INT
},
295 { 3, TWL4030_BASEADD_PM_MASTER
},
296 { 3, TWL4030_BASEADD_PM_RECEIVER
},
297 { 3, TWL4030_BASEADD_RTC
},
298 { 3, TWL4030_BASEADD_SECURED_REG
},
301 static struct twl_mapping twl6030_map
[] = {
303 * NOTE: don't change this table without updating the
304 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
305 * so they continue to match the order in this table.
307 { SUB_CHIP_ID1
, TWL6030_BASEADD_USB
},
308 { SUB_CHIP_ID_INVAL
, TWL6030_BASEADD_AUDIO
},
309 { SUB_CHIP_ID2
, TWL6030_BASEADD_DIEID
},
310 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
311 { SUB_CHIP_ID1
, TWL6030_BASEADD_PIH
},
313 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
314 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
315 { SUB_CHIP_ID1
, TWL6030_BASEADD_GPADC_CTRL
},
316 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
317 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
319 { SUB_CHIP_ID1
, TWL6030_BASEADD_CHARGER
},
320 { SUB_CHIP_ID1
, TWL6030_BASEADD_GASGAUGE
},
321 { SUB_CHIP_ID1
, TWL6030_BASEADD_PWM
},
322 { SUB_CHIP_ID0
, TWL6030_BASEADD_ZERO
},
323 { SUB_CHIP_ID1
, TWL6030_BASEADD_ZERO
},
325 { SUB_CHIP_ID2
, TWL6030_BASEADD_ZERO
},
326 { SUB_CHIP_ID2
, TWL6030_BASEADD_ZERO
},
327 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
328 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
329 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
330 { SUB_CHIP_ID0
, TWL6030_BASEADD_PM_MASTER
},
331 { SUB_CHIP_ID0
, TWL6030_BASEADD_PM_SLAVE_MISC
},
333 { SUB_CHIP_ID0
, TWL6030_BASEADD_RTC
},
334 { SUB_CHIP_ID0
, TWL6030_BASEADD_MEM
},
335 { SUB_CHIP_ID1
, TWL6025_BASEADD_CHARGER
},
338 /*----------------------------------------------------------------------*/
340 /* Exported Functions */
343 * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
344 * @mod_no: module number
345 * @value: an array of num_bytes+1 containing data to write
346 * @reg: register address (just offset will do)
347 * @num_bytes: number of bytes to transfer
349 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
350 * valid data starts at Offset 1.
352 * Returns the result of operation - 0 is success
354 int twl_i2c_write(u8 mod_no
, u8
*value
, u8 reg
, unsigned num_bytes
)
358 struct twl_client
*twl
;
361 if (unlikely(mod_no
> TWL_MODULE_LAST
)) {
362 pr_err("%s: invalid module number %d\n", DRIVER_NAME
, mod_no
);
365 if (unlikely(!inuse
)) {
366 pr_err("%s: not initialized\n", DRIVER_NAME
);
369 sid
= twl_map
[mod_no
].sid
;
370 if (unlikely(sid
== SUB_CHIP_ID_INVAL
)) {
371 pr_err("%s: module %d is not part of the pmic\n",
372 DRIVER_NAME
, mod_no
);
375 twl
= &twl_modules
[sid
];
377 mutex_lock(&twl
->xfer_lock
);
379 * [MSG1]: fill the register address data
380 * fill the data Tx buffer
382 msg
= &twl
->xfer_msg
[0];
383 msg
->addr
= twl
->address
;
384 msg
->len
= num_bytes
+ 1;
387 /* over write the first byte of buffer with the register address */
388 *value
= twl_map
[mod_no
].base
+ reg
;
389 ret
= i2c_transfer(twl
->client
->adapter
, twl
->xfer_msg
, 1);
390 mutex_unlock(&twl
->xfer_lock
);
392 /* i2c_transfer returns number of messages transferred */
394 pr_err("%s: i2c_write failed to transfer all messages\n",
404 EXPORT_SYMBOL(twl_i2c_write
);
407 * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
408 * @mod_no: module number
409 * @value: an array of num_bytes containing data to be read
410 * @reg: register address (just offset will do)
411 * @num_bytes: number of bytes to transfer
413 * Returns result of operation - num_bytes is success else failure.
415 int twl_i2c_read(u8 mod_no
, u8
*value
, u8 reg
, unsigned num_bytes
)
420 struct twl_client
*twl
;
423 if (unlikely(mod_no
> TWL_MODULE_LAST
)) {
424 pr_err("%s: invalid module number %d\n", DRIVER_NAME
, mod_no
);
427 if (unlikely(!inuse
)) {
428 pr_err("%s: not initialized\n", DRIVER_NAME
);
431 sid
= twl_map
[mod_no
].sid
;
432 if (unlikely(sid
== SUB_CHIP_ID_INVAL
)) {
433 pr_err("%s: module %d is not part of the pmic\n",
434 DRIVER_NAME
, mod_no
);
437 twl
= &twl_modules
[sid
];
439 mutex_lock(&twl
->xfer_lock
);
440 /* [MSG1] fill the register address data */
441 msg
= &twl
->xfer_msg
[0];
442 msg
->addr
= twl
->address
;
444 msg
->flags
= 0; /* Read the register value */
445 val
= twl_map
[mod_no
].base
+ reg
;
447 /* [MSG2] fill the data rx buffer */
448 msg
= &twl
->xfer_msg
[1];
449 msg
->addr
= twl
->address
;
450 msg
->flags
= I2C_M_RD
; /* Read the register value */
451 msg
->len
= num_bytes
; /* only n bytes */
453 ret
= i2c_transfer(twl
->client
->adapter
, twl
->xfer_msg
, 2);
454 mutex_unlock(&twl
->xfer_lock
);
456 /* i2c_transfer returns number of messages transferred */
458 pr_err("%s: i2c_read failed to transfer all messages\n",
468 EXPORT_SYMBOL(twl_i2c_read
);
471 * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
472 * @mod_no: module number
473 * @value: the value to be written 8 bit
474 * @reg: register address (just offset will do)
476 * Returns result of operation - 0 is success
478 int twl_i2c_write_u8(u8 mod_no
, u8 value
, u8 reg
)
481 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
482 u8 temp_buffer
[2] = { 0 };
483 /* offset 1 contains the data */
484 temp_buffer
[1] = value
;
485 return twl_i2c_write(mod_no
, temp_buffer
, reg
, 1);
487 EXPORT_SYMBOL(twl_i2c_write_u8
);
490 * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
491 * @mod_no: module number
492 * @value: the value read 8 bit
493 * @reg: register address (just offset will do)
495 * Returns result of operation - 0 is success
497 int twl_i2c_read_u8(u8 mod_no
, u8
*value
, u8 reg
)
499 return twl_i2c_read(mod_no
, value
, reg
, 1);
501 EXPORT_SYMBOL(twl_i2c_read_u8
);
503 /*----------------------------------------------------------------------*/
506 * twl_read_idcode_register - API to read the IDCODE register.
508 * Unlocks the IDCODE register and read the 32 bit value.
510 static int twl_read_idcode_register(void)
514 err
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, TWL_EEPROM_R_UNLOCK
,
515 REG_UNLOCK_TEST_REG
);
517 pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err
);
521 err
= twl_i2c_read(TWL4030_MODULE_INTBR
, (u8
*)(&twl_idcode
),
524 pr_err("TWL4030: unable to read IDCODE -%d\n", err
);
528 err
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, 0x0, REG_UNLOCK_TEST_REG
);
530 pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err
);
536 * twl_get_type - API to get TWL Si type.
538 * Api to get the TWL Si type from IDCODE value.
540 int twl_get_type(void)
542 return TWL_SIL_TYPE(twl_idcode
);
544 EXPORT_SYMBOL_GPL(twl_get_type
);
547 * twl_get_version - API to get TWL Si version.
549 * Api to get the TWL Si version from IDCODE value.
551 int twl_get_version(void)
553 return TWL_SIL_REV(twl_idcode
);
555 EXPORT_SYMBOL_GPL(twl_get_version
);
557 static struct device
*
558 add_numbered_child(unsigned chip
, const char *name
, int num
,
559 void *pdata
, unsigned pdata_len
,
560 bool can_wakeup
, int irq0
, int irq1
)
562 struct platform_device
*pdev
;
563 struct twl_client
*twl
= &twl_modules
[chip
];
566 pdev
= platform_device_alloc(name
, num
);
568 dev_dbg(&twl
->client
->dev
, "can't alloc dev\n");
573 pdev
->dev
.parent
= &twl
->client
->dev
;
576 status
= platform_device_add_data(pdev
, pdata
, pdata_len
);
578 dev_dbg(&pdev
->dev
, "can't add platform_data\n");
584 struct resource r
[2] = {
585 { .start
= irq0
, .flags
= IORESOURCE_IRQ
, },
586 { .start
= irq1
, .flags
= IORESOURCE_IRQ
, },
589 status
= platform_device_add_resources(pdev
, r
, irq1
? 2 : 1);
591 dev_dbg(&pdev
->dev
, "can't add irqs\n");
596 status
= platform_device_add(pdev
);
598 device_init_wakeup(&pdev
->dev
, can_wakeup
);
602 platform_device_put(pdev
);
603 dev_err(&twl
->client
->dev
, "can't add %s dev\n", name
);
604 return ERR_PTR(status
);
609 static inline struct device
*add_child(unsigned chip
, const char *name
,
610 void *pdata
, unsigned pdata_len
,
611 bool can_wakeup
, int irq0
, int irq1
)
613 return add_numbered_child(chip
, name
, -1, pdata
, pdata_len
,
614 can_wakeup
, irq0
, irq1
);
617 static struct device
*
618 add_regulator_linked(int num
, struct regulator_init_data
*pdata
,
619 struct regulator_consumer_supply
*consumers
,
620 unsigned num_consumers
, unsigned long features
)
622 unsigned sub_chip_id
;
623 struct twl_regulator_driver_data drv_data
;
625 /* regulator framework demands init_data ... */
630 pdata
->consumer_supplies
= consumers
;
631 pdata
->num_consumer_supplies
= num_consumers
;
634 if (pdata
->driver_data
) {
635 /* If we have existing drv_data, just add the flags */
636 struct twl_regulator_driver_data
*tmp
;
637 tmp
= pdata
->driver_data
;
638 tmp
->features
|= features
;
640 /* add new driver data struct, used only during init */
641 drv_data
.features
= features
;
642 drv_data
.set_voltage
= NULL
;
643 drv_data
.get_voltage
= NULL
;
644 drv_data
.data
= NULL
;
645 pdata
->driver_data
= &drv_data
;
648 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
649 sub_chip_id
= twl_map
[TWL_MODULE_PM_MASTER
].sid
;
650 return add_numbered_child(sub_chip_id
, "twl_reg", num
,
651 pdata
, sizeof(*pdata
), false, 0, 0);
654 static struct device
*
655 add_regulator(int num
, struct regulator_init_data
*pdata
,
656 unsigned long features
)
658 return add_regulator_linked(num
, pdata
, NULL
, 0, features
);
662 * NOTE: We know the first 8 IRQs after pdata->base_irq are
663 * for the PIH, and the next are for the PWR_INT SIH, since
664 * that's how twl_init_irq() sets things up.
668 add_children(struct twl4030_platform_data
*pdata
, unsigned irq_base
,
669 unsigned long features
)
671 struct device
*child
;
672 unsigned sub_chip_id
;
674 if (twl_has_gpio() && pdata
->gpio
) {
675 child
= add_child(SUB_CHIP_ID1
, "twl4030_gpio",
676 pdata
->gpio
, sizeof(*pdata
->gpio
),
677 false, irq_base
+ GPIO_INTR_OFFSET
, 0);
679 return PTR_ERR(child
);
682 if (twl_has_keypad() && pdata
->keypad
) {
683 child
= add_child(SUB_CHIP_ID2
, "twl4030_keypad",
684 pdata
->keypad
, sizeof(*pdata
->keypad
),
685 true, irq_base
+ KEYPAD_INTR_OFFSET
, 0);
687 return PTR_ERR(child
);
690 if (twl_has_madc() && pdata
->madc
) {
691 child
= add_child(2, "twl4030_madc",
692 pdata
->madc
, sizeof(*pdata
->madc
),
693 true, irq_base
+ MADC_INTR_OFFSET
, 0);
695 return PTR_ERR(child
);
700 * REVISIT platform_data here currently might expose the
701 * "msecure" line ... but for now we just expect board
702 * setup to tell the chip "it's always ok to SET_TIME".
703 * Eventually, Linux might become more aware of such
704 * HW security concerns, and "least privilege".
706 sub_chip_id
= twl_map
[TWL_MODULE_RTC
].sid
;
707 child
= add_child(sub_chip_id
, "twl_rtc",
709 true, irq_base
+ RTC_INTR_OFFSET
, 0);
711 return PTR_ERR(child
);
714 if (twl_has_usb() && pdata
->usb
&& twl_class_is_4030()) {
716 static struct regulator_consumer_supply usb1v5
= {
719 static struct regulator_consumer_supply usb1v8
= {
722 static struct regulator_consumer_supply usb3v1
[] = {
723 { .supply
= "usb3v1" },
724 { .supply
= "bci3v1" },
727 /* First add the regulators so that they can be used by transceiver */
728 if (twl_has_regulator()) {
729 /* this is a template that gets copied */
730 struct regulator_init_data usb_fixed
= {
731 .constraints
.valid_modes_mask
=
732 REGULATOR_MODE_NORMAL
733 | REGULATOR_MODE_STANDBY
,
734 .constraints
.valid_ops_mask
=
735 REGULATOR_CHANGE_MODE
736 | REGULATOR_CHANGE_STATUS
,
739 child
= add_regulator_linked(TWL4030_REG_VUSB1V5
,
740 &usb_fixed
, &usb1v5
, 1,
743 return PTR_ERR(child
);
745 child
= add_regulator_linked(TWL4030_REG_VUSB1V8
,
746 &usb_fixed
, &usb1v8
, 1,
749 return PTR_ERR(child
);
751 child
= add_regulator_linked(TWL4030_REG_VUSB3V1
,
752 &usb_fixed
, usb3v1
, 2,
755 return PTR_ERR(child
);
759 child
= add_child(0, "twl4030_usb",
760 pdata
->usb
, sizeof(*pdata
->usb
),
762 /* irq0 = USB_PRES, irq1 = USB */
763 irq_base
+ USB_PRES_INTR_OFFSET
,
764 irq_base
+ USB_INTR_OFFSET
);
767 return PTR_ERR(child
);
769 /* we need to connect regulators to this transceiver */
770 if (twl_has_regulator() && child
) {
771 usb1v5
.dev_name
= dev_name(child
);
772 usb1v8
.dev_name
= dev_name(child
);
773 usb3v1
[0].dev_name
= dev_name(child
);
776 if (twl_has_usb() && pdata
->usb
&& twl_class_is_6030()) {
778 static struct regulator_consumer_supply usb3v3
;
781 if (twl_has_regulator()) {
782 /* this is a template that gets copied */
783 struct regulator_init_data usb_fixed
= {
784 .constraints
.valid_modes_mask
=
785 REGULATOR_MODE_NORMAL
786 | REGULATOR_MODE_STANDBY
,
787 .constraints
.valid_ops_mask
=
788 REGULATOR_CHANGE_MODE
789 | REGULATOR_CHANGE_STATUS
,
792 if (features
& TWL6025_SUBCLASS
) {
793 usb3v3
.supply
= "ldousb";
794 regulator
= TWL6025_REG_LDOUSB
;
796 usb3v3
.supply
= "vusb";
797 regulator
= TWL6030_REG_VUSB
;
799 child
= add_regulator_linked(regulator
, &usb_fixed
,
803 return PTR_ERR(child
);
806 pdata
->usb
->features
= features
;
808 child
= add_child(0, "twl6030_usb",
809 pdata
->usb
, sizeof(*pdata
->usb
),
811 /* irq1 = VBUS_PRES, irq0 = USB ID */
812 irq_base
+ USBOTG_INTR_OFFSET
,
813 irq_base
+ USB_PRES_INTR_OFFSET
);
816 return PTR_ERR(child
);
817 /* we need to connect regulators to this transceiver */
818 if (twl_has_regulator() && child
)
819 usb3v3
.dev_name
= dev_name(child
);
820 } else if (twl_has_regulator() && twl_class_is_6030()) {
821 if (features
& TWL6025_SUBCLASS
)
822 child
= add_regulator(TWL6025_REG_LDOUSB
,
823 pdata
->ldousb
, features
);
825 child
= add_regulator(TWL6030_REG_VUSB
,
826 pdata
->vusb
, features
);
829 return PTR_ERR(child
);
832 if (twl_has_watchdog() && twl_class_is_4030()) {
833 child
= add_child(0, "twl4030_wdt", NULL
, 0, false, 0, 0);
835 return PTR_ERR(child
);
838 if (twl_has_pwrbutton() && twl_class_is_4030()) {
839 child
= add_child(1, "twl4030_pwrbutton",
840 NULL
, 0, true, irq_base
+ 8 + 0, 0);
842 return PTR_ERR(child
);
845 if (twl_has_codec() && pdata
->audio
&& twl_class_is_4030()) {
846 sub_chip_id
= twl_map
[TWL_MODULE_AUDIO_VOICE
].sid
;
847 child
= add_child(sub_chip_id
, "twl4030-audio",
848 pdata
->audio
, sizeof(*pdata
->audio
),
851 return PTR_ERR(child
);
854 /* twl4030 regulators */
855 if (twl_has_regulator() && twl_class_is_4030()) {
856 child
= add_regulator(TWL4030_REG_VPLL1
, pdata
->vpll1
,
859 return PTR_ERR(child
);
861 child
= add_regulator(TWL4030_REG_VIO
, pdata
->vio
,
864 return PTR_ERR(child
);
866 child
= add_regulator(TWL4030_REG_VDD1
, pdata
->vdd1
,
869 return PTR_ERR(child
);
871 child
= add_regulator(TWL4030_REG_VDD2
, pdata
->vdd2
,
874 return PTR_ERR(child
);
876 child
= add_regulator(TWL4030_REG_VMMC1
, pdata
->vmmc1
,
879 return PTR_ERR(child
);
881 child
= add_regulator(TWL4030_REG_VDAC
, pdata
->vdac
,
884 return PTR_ERR(child
);
886 child
= add_regulator((features
& TWL4030_VAUX2
)
887 ? TWL4030_REG_VAUX2_4030
889 pdata
->vaux2
, features
);
891 return PTR_ERR(child
);
893 child
= add_regulator(TWL4030_REG_VINTANA1
, pdata
->vintana1
,
896 return PTR_ERR(child
);
898 child
= add_regulator(TWL4030_REG_VINTANA2
, pdata
->vintana2
,
901 return PTR_ERR(child
);
903 child
= add_regulator(TWL4030_REG_VINTDIG
, pdata
->vintdig
,
906 return PTR_ERR(child
);
909 /* maybe add LDOs that are omitted on cost-reduced parts */
910 if (twl_has_regulator() && !(features
& TPS_SUBSET
)
911 && twl_class_is_4030()) {
912 child
= add_regulator(TWL4030_REG_VPLL2
, pdata
->vpll2
,
915 return PTR_ERR(child
);
917 child
= add_regulator(TWL4030_REG_VMMC2
, pdata
->vmmc2
,
920 return PTR_ERR(child
);
922 child
= add_regulator(TWL4030_REG_VSIM
, pdata
->vsim
,
925 return PTR_ERR(child
);
927 child
= add_regulator(TWL4030_REG_VAUX1
, pdata
->vaux1
,
930 return PTR_ERR(child
);
932 child
= add_regulator(TWL4030_REG_VAUX3
, pdata
->vaux3
,
935 return PTR_ERR(child
);
937 child
= add_regulator(TWL4030_REG_VAUX4
, pdata
->vaux4
,
940 return PTR_ERR(child
);
943 /* twl6030 regulators */
944 if (twl_has_regulator() && twl_class_is_6030() &&
945 !(features
& TWL6025_SUBCLASS
)) {
946 child
= add_regulator(TWL6030_REG_VDD1
, pdata
->vdd1
,
949 return PTR_ERR(child
);
951 child
= add_regulator(TWL6030_REG_VDD2
, pdata
->vdd2
,
954 return PTR_ERR(child
);
956 child
= add_regulator(TWL6030_REG_VDD3
, pdata
->vdd3
,
959 return PTR_ERR(child
);
961 child
= add_regulator(TWL6030_REG_V1V8
, pdata
->v1v8
,
964 return PTR_ERR(child
);
966 child
= add_regulator(TWL6030_REG_V2V1
, pdata
->v2v1
,
969 return PTR_ERR(child
);
971 child
= add_regulator(TWL6030_REG_VMMC
, pdata
->vmmc
,
974 return PTR_ERR(child
);
976 child
= add_regulator(TWL6030_REG_VPP
, pdata
->vpp
,
979 return PTR_ERR(child
);
981 child
= add_regulator(TWL6030_REG_VUSIM
, pdata
->vusim
,
984 return PTR_ERR(child
);
986 child
= add_regulator(TWL6030_REG_VCXIO
, pdata
->vcxio
,
989 return PTR_ERR(child
);
991 child
= add_regulator(TWL6030_REG_VDAC
, pdata
->vdac
,
994 return PTR_ERR(child
);
996 child
= add_regulator(TWL6030_REG_VAUX1_6030
, pdata
->vaux1
,
999 return PTR_ERR(child
);
1001 child
= add_regulator(TWL6030_REG_VAUX2_6030
, pdata
->vaux2
,
1004 return PTR_ERR(child
);
1006 child
= add_regulator(TWL6030_REG_VAUX3_6030
, pdata
->vaux3
,
1009 return PTR_ERR(child
);
1011 child
= add_regulator(TWL6030_REG_CLK32KG
, pdata
->clk32kg
,
1014 return PTR_ERR(child
);
1017 /* 6030 and 6025 share this regulator */
1018 if (twl_has_regulator() && twl_class_is_6030()) {
1019 child
= add_regulator(TWL6030_REG_VANA
, pdata
->vana
,
1022 return PTR_ERR(child
);
1025 /* twl6025 regulators */
1026 if (twl_has_regulator() && twl_class_is_6030() &&
1027 (features
& TWL6025_SUBCLASS
)) {
1028 child
= add_regulator(TWL6025_REG_LDO5
, pdata
->ldo5
,
1031 return PTR_ERR(child
);
1033 child
= add_regulator(TWL6025_REG_LDO1
, pdata
->ldo1
,
1036 return PTR_ERR(child
);
1038 child
= add_regulator(TWL6025_REG_LDO7
, pdata
->ldo7
,
1041 return PTR_ERR(child
);
1043 child
= add_regulator(TWL6025_REG_LDO6
, pdata
->ldo6
,
1046 return PTR_ERR(child
);
1048 child
= add_regulator(TWL6025_REG_LDOLN
, pdata
->ldoln
,
1051 return PTR_ERR(child
);
1053 child
= add_regulator(TWL6025_REG_LDO2
, pdata
->ldo2
,
1056 return PTR_ERR(child
);
1058 child
= add_regulator(TWL6025_REG_LDO4
, pdata
->ldo4
,
1061 return PTR_ERR(child
);
1063 child
= add_regulator(TWL6025_REG_LDO3
, pdata
->ldo3
,
1066 return PTR_ERR(child
);
1068 child
= add_regulator(TWL6025_REG_SMPS3
, pdata
->smps3
,
1071 return PTR_ERR(child
);
1073 child
= add_regulator(TWL6025_REG_SMPS4
, pdata
->smps4
,
1076 return PTR_ERR(child
);
1078 child
= add_regulator(TWL6025_REG_VIO
, pdata
->vio6025
,
1081 return PTR_ERR(child
);
1085 if (twl_has_bci() && pdata
->bci
&&
1086 !(features
& (TPS_SUBSET
| TWL5031
))) {
1087 child
= add_child(3, "twl4030_bci",
1088 pdata
->bci
, sizeof(*pdata
->bci
), false,
1089 /* irq0 = CHG_PRES, irq1 = BCI */
1090 irq_base
+ BCI_PRES_INTR_OFFSET
,
1091 irq_base
+ BCI_INTR_OFFSET
);
1093 return PTR_ERR(child
);
1099 /*----------------------------------------------------------------------*/
1102 * These three functions initialize the on-chip clock framework,
1103 * letting it generate the right frequencies for USB, MADC, and
1106 static inline int __init
protect_pm_master(void)
1110 e
= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, 0,
1111 TWL4030_PM_MASTER_PROTECT_KEY
);
1115 static inline int __init
unprotect_pm_master(void)
1119 e
|= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER
,
1120 TWL4030_PM_MASTER_KEY_CFG1
,
1121 TWL4030_PM_MASTER_PROTECT_KEY
);
1122 e
|= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER
,
1123 TWL4030_PM_MASTER_KEY_CFG2
,
1124 TWL4030_PM_MASTER_PROTECT_KEY
);
1129 static void clocks_init(struct device
*dev
,
1130 struct twl4030_clock_init_data
*clock
)
1135 u8 ctrl
= HFCLK_FREQ_26_MHZ
;
1137 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
1138 if (cpu_is_omap2430())
1139 osc
= clk_get(dev
, "osc_ck");
1141 osc
= clk_get(dev
, "osc_sys_ck");
1144 printk(KERN_WARNING
"Skipping twl internal clock init and "
1145 "using bootloader value (unknown osc rate)\n");
1149 rate
= clk_get_rate(osc
);
1153 /* REVISIT for non-OMAP systems, pass the clock rate from
1154 * board init code, using platform_data.
1156 osc
= ERR_PTR(-EIO
);
1158 printk(KERN_WARNING
"Skipping twl internal clock init and "
1159 "using bootloader value (unknown osc rate)\n");
1166 ctrl
= HFCLK_FREQ_19p2_MHZ
;
1169 ctrl
= HFCLK_FREQ_26_MHZ
;
1172 ctrl
= HFCLK_FREQ_38p4_MHZ
;
1176 ctrl
|= HIGH_PERF_SQ
;
1177 if (clock
&& clock
->ck32k_lowpwr_enable
)
1178 ctrl
|= CK32K_LOWPWR_EN
;
1180 e
|= unprotect_pm_master();
1181 /* effect->MADC+USB ck en */
1182 e
|= twl_i2c_write_u8(TWL_MODULE_PM_MASTER
, ctrl
, R_CFG_BOOT
);
1183 e
|= protect_pm_master();
1186 pr_err("%s: clock init err [%d]\n", DRIVER_NAME
, e
);
1189 /*----------------------------------------------------------------------*/
1192 static int twl_remove(struct i2c_client
*client
)
1194 unsigned i
, num_slaves
;
1197 if (twl_class_is_4030()) {
1198 status
= twl4030_exit_irq();
1199 num_slaves
= TWL_NUM_SLAVES
;
1201 status
= twl6030_exit_irq();
1202 num_slaves
= TWL_NUM_SLAVES
- 1;
1208 for (i
= 0; i
< num_slaves
; i
++) {
1209 struct twl_client
*twl
= &twl_modules
[i
];
1211 if (twl
->client
&& twl
->client
!= client
)
1212 i2c_unregister_device(twl
->client
);
1213 twl_modules
[i
].client
= NULL
;
1219 /* NOTE: This driver only handles a single twl4030/tps659x0 chip */
1220 static int __devinit
1221 twl_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1223 struct twl4030_platform_data
*pdata
= client
->dev
.platform_data
;
1224 struct device_node
*node
= client
->dev
.of_node
;
1227 unsigned i
, num_slaves
;
1229 if (node
&& !pdata
) {
1231 * XXX: Temporary pdata until the information is correctly
1232 * retrieved by every TWL modules from DT.
1234 pdata
= devm_kzalloc(&client
->dev
,
1235 sizeof(struct twl4030_platform_data
),
1242 dev_dbg(&client
->dev
, "no platform data?\n");
1246 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
) == 0) {
1247 dev_dbg(&client
->dev
, "can't talk I2C?\n");
1252 dev_dbg(&client
->dev
, "driver is already in use\n");
1256 if ((id
->driver_data
) & TWL6030_CLASS
) {
1257 twl_id
= TWL6030_CLASS_ID
;
1258 twl_map
= &twl6030_map
[0];
1259 num_slaves
= TWL_NUM_SLAVES
- 1;
1261 twl_id
= TWL4030_CLASS_ID
;
1262 twl_map
= &twl4030_map
[0];
1263 num_slaves
= TWL_NUM_SLAVES
;
1266 for (i
= 0; i
< num_slaves
; i
++) {
1267 struct twl_client
*twl
= &twl_modules
[i
];
1269 twl
->address
= client
->addr
+ i
;
1271 twl
->client
= client
;
1273 twl
->client
= i2c_new_dummy(client
->adapter
,
1276 dev_err(&client
->dev
,
1277 "can't attach client %d\n", i
);
1282 mutex_init(&twl
->xfer_lock
);
1287 /* setup clock framework */
1288 clocks_init(&client
->dev
, pdata
->clock
);
1290 /* read TWL IDCODE Register */
1291 if (twl_id
== TWL4030_CLASS_ID
) {
1292 status
= twl_read_idcode_register();
1293 WARN(status
< 0, "Error: reading twl_idcode register value\n");
1296 /* load power event scripts */
1297 if (twl_has_power() && pdata
->power
)
1298 twl4030_power_init(pdata
->power
);
1300 /* Maybe init the T2 Interrupt subsystem */
1302 if (twl_class_is_4030()) {
1303 twl4030_init_chip_irq(id
->name
);
1304 irq_base
= twl4030_init_irq(&client
->dev
, client
->irq
);
1306 irq_base
= twl6030_init_irq(&client
->dev
, client
->irq
);
1316 * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
1317 * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1318 * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1320 if (twl_class_is_4030()) {
1323 twl_i2c_read_u8(TWL4030_MODULE_INTBR
, &temp
, REG_GPPUPDCTR1
);
1324 temp
&= ~(SR_I2C_SDA_CTRL_PU
| SR_I2C_SCL_CTRL_PU
| \
1325 I2C_SDA_CTRL_PU
| I2C_SCL_CTRL_PU
);
1326 twl_i2c_write_u8(TWL4030_MODULE_INTBR
, temp
, REG_GPPUPDCTR1
);
1331 status
= of_platform_populate(node
, NULL
, NULL
, &client
->dev
);
1333 status
= add_children(pdata
, irq_base
, id
->driver_data
);
1342 static const struct i2c_device_id twl_ids
[] = {
1343 { "twl4030", TWL4030_VAUX2
}, /* "Triton 2" */
1344 { "twl5030", 0 }, /* T2 updated */
1345 { "twl5031", TWL5031
}, /* TWL5030 updated */
1346 { "tps65950", 0 }, /* catalog version of twl5030 */
1347 { "tps65930", TPS_SUBSET
}, /* fewer LDOs and DACs; no charger */
1348 { "tps65920", TPS_SUBSET
}, /* fewer LDOs; no codec or charger */
1349 { "tps65921", TPS_SUBSET
}, /* fewer LDOs; no codec, no LED
1350 and vibrator. Charger in USB module*/
1351 { "twl6030", TWL6030_CLASS
}, /* "Phoenix power chip" */
1352 { "twl6025", TWL6030_CLASS
| TWL6025_SUBCLASS
}, /* "Phoenix lite" */
1353 { /* end of list */ },
1355 MODULE_DEVICE_TABLE(i2c
, twl_ids
);
1357 /* One Client Driver , 4 Clients */
1358 static struct i2c_driver twl_driver
= {
1359 .driver
.name
= DRIVER_NAME
,
1360 .id_table
= twl_ids
,
1362 .remove
= twl_remove
,
1365 static int __init
twl_init(void)
1367 return i2c_add_driver(&twl_driver
);
1369 subsys_initcall(twl_init
);
1371 static void __exit
twl_exit(void)
1373 i2c_del_driver(&twl_driver
);
1375 module_exit(twl_exit
);
1377 MODULE_AUTHOR("Texas Instruments, Inc.");
1378 MODULE_DESCRIPTION("I2C Core interface for TWL");
1379 MODULE_LICENSE("GPL");