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/regmap.h>
36 #include <linux/clk.h>
37 #include <linux/err.h>
38 #include <linux/device.h>
40 #include <linux/of_irq.h>
41 #include <linux/of_platform.h>
42 #include <linux/irq.h>
43 #include <linux/irqdomain.h>
45 #include <linux/regulator/machine.h>
47 #include <linux/i2c.h>
48 #include <linux/i2c/twl.h>
53 * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
54 * Management and System Companion Device" chips originally designed for
55 * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
56 * often at around 3 Mbit/sec, including for interrupt handling.
58 * This driver core provides genirq support for the interrupts emitted,
59 * by the various modules, and exports register access primitives.
61 * FIXME this driver currently requires use of the first interrupt line
62 * (and associated registers).
65 #define DRIVER_NAME "twl"
67 /* Triton Core internal information (BEGIN) */
69 #define TWL_NUM_SLAVES 4
71 #define SUB_CHIP_ID0 0
72 #define SUB_CHIP_ID1 1
73 #define SUB_CHIP_ID2 2
74 #define SUB_CHIP_ID3 3
75 #define SUB_CHIP_ID_INVAL 0xff
77 #define TWL_MODULE_LAST TWL4030_MODULE_LAST
79 /* Base Address defns for twl4030_map[] */
81 /* subchip/slave 0 - USB ID */
82 #define TWL4030_BASEADD_USB 0x0000
84 /* subchip/slave 1 - AUD ID */
85 #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
86 #define TWL4030_BASEADD_GPIO 0x0098
87 #define TWL4030_BASEADD_INTBR 0x0085
88 #define TWL4030_BASEADD_PIH 0x0080
89 #define TWL4030_BASEADD_TEST 0x004C
91 /* subchip/slave 2 - AUX ID */
92 #define TWL4030_BASEADD_INTERRUPTS 0x00B9
93 #define TWL4030_BASEADD_LED 0x00EE
94 #define TWL4030_BASEADD_MADC 0x0000
95 #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
96 #define TWL4030_BASEADD_PRECHARGE 0x00AA
97 #define TWL4030_BASEADD_PWM0 0x00F8
98 #define TWL4030_BASEADD_PWM1 0x00FB
99 #define TWL4030_BASEADD_PWMA 0x00EF
100 #define TWL4030_BASEADD_PWMB 0x00F1
101 #define TWL4030_BASEADD_KEYPAD 0x00D2
103 #define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
104 #define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
107 /* subchip/slave 3 - POWER ID */
108 #define TWL4030_BASEADD_BACKUP 0x0014
109 #define TWL4030_BASEADD_INT 0x002E
110 #define TWL4030_BASEADD_PM_MASTER 0x0036
111 #define TWL4030_BASEADD_PM_RECEIVER 0x005B
112 #define TWL4030_BASEADD_RTC 0x001C
113 #define TWL4030_BASEADD_SECURED_REG 0x0000
115 /* Triton Core internal information (END) */
118 /* subchip/slave 0 0x48 - POWER */
119 #define TWL6030_BASEADD_RTC 0x0000
120 #define TWL6030_BASEADD_MEM 0x0017
121 #define TWL6030_BASEADD_PM_MASTER 0x001F
122 #define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
123 #define TWL6030_BASEADD_PM_MISC 0x00E2
124 #define TWL6030_BASEADD_PM_PUPD 0x00F0
126 /* subchip/slave 1 0x49 - FEATURE */
127 #define TWL6030_BASEADD_USB 0x0000
128 #define TWL6030_BASEADD_GPADC_CTRL 0x002E
129 #define TWL6030_BASEADD_AUX 0x0090
130 #define TWL6030_BASEADD_PWM 0x00BA
131 #define TWL6030_BASEADD_GASGAUGE 0x00C0
132 #define TWL6030_BASEADD_PIH 0x00D0
133 #define TWL6030_BASEADD_CHARGER 0x00E0
134 #define TWL6025_BASEADD_CHARGER 0x00DA
136 /* subchip/slave 2 0x4A - DFT */
137 #define TWL6030_BASEADD_DIEID 0x00C0
139 /* subchip/slave 3 0x4B - AUDIO */
140 #define TWL6030_BASEADD_AUDIO 0x0000
141 #define TWL6030_BASEADD_RSV 0x0000
142 #define TWL6030_BASEADD_ZERO 0x0000
144 /* Few power values */
145 #define R_CFG_BOOT 0x05
147 /* some fields in R_CFG_BOOT */
148 #define HFCLK_FREQ_19p2_MHZ (1 << 0)
149 #define HFCLK_FREQ_26_MHZ (2 << 0)
150 #define HFCLK_FREQ_38p4_MHZ (3 << 0)
151 #define HIGH_PERF_SQ (1 << 3)
152 #define CK32K_LOWPWR_EN (1 << 7)
154 /*----------------------------------------------------------------------*/
156 /* is driver active, bound to a chip? */
159 /* TWL IDCODE Register value */
160 static u32 twl_idcode
;
162 static unsigned int twl_id
;
163 unsigned int twl_rev(void)
167 EXPORT_SYMBOL(twl_rev
);
169 /* Structure for each TWL4030/TWL6030 Slave */
171 struct i2c_client
*client
;
172 struct regmap
*regmap
;
175 static struct twl_client twl_modules
[TWL_NUM_SLAVES
];
177 /* mapping the module id to slave id and base address */
179 unsigned char sid
; /* Slave ID */
180 unsigned char base
; /* base address */
182 static struct twl_mapping
*twl_map
;
184 static struct twl_mapping twl4030_map
[] = {
186 * NOTE: don't change this table without updating the
187 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
188 * so they continue to match the order in this table.
191 { 0, TWL4030_BASEADD_USB
},
192 { 1, TWL4030_BASEADD_AUDIO_VOICE
},
193 { 1, TWL4030_BASEADD_GPIO
},
194 { 1, TWL4030_BASEADD_INTBR
},
195 { 1, TWL4030_BASEADD_PIH
},
197 { 1, TWL4030_BASEADD_TEST
},
198 { 2, TWL4030_BASEADD_KEYPAD
},
199 { 2, TWL4030_BASEADD_MADC
},
200 { 2, TWL4030_BASEADD_INTERRUPTS
},
201 { 2, TWL4030_BASEADD_LED
},
203 { 2, TWL4030_BASEADD_MAIN_CHARGE
},
204 { 2, TWL4030_BASEADD_PRECHARGE
},
205 { 2, TWL4030_BASEADD_PWM0
},
206 { 2, TWL4030_BASEADD_PWM1
},
207 { 2, TWL4030_BASEADD_PWMA
},
209 { 2, TWL4030_BASEADD_PWMB
},
210 { 2, TWL5031_BASEADD_ACCESSORY
},
211 { 2, TWL5031_BASEADD_INTERRUPTS
},
212 { 3, TWL4030_BASEADD_BACKUP
},
213 { 3, TWL4030_BASEADD_INT
},
215 { 3, TWL4030_BASEADD_PM_MASTER
},
216 { 3, TWL4030_BASEADD_PM_RECEIVER
},
217 { 3, TWL4030_BASEADD_RTC
},
218 { 3, TWL4030_BASEADD_SECURED_REG
},
221 static struct regmap_config twl4030_regmap_config
[4] = {
226 .max_register
= 0xff,
232 .max_register
= 0xff,
238 .max_register
= 0xff,
244 .max_register
= 0xff,
248 static struct twl_mapping twl6030_map
[] = {
250 * NOTE: don't change this table without updating the
251 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
252 * so they continue to match the order in this table.
254 { SUB_CHIP_ID1
, TWL6030_BASEADD_USB
},
255 { SUB_CHIP_ID_INVAL
, TWL6030_BASEADD_AUDIO
},
256 { SUB_CHIP_ID2
, TWL6030_BASEADD_DIEID
},
257 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
258 { SUB_CHIP_ID1
, TWL6030_BASEADD_PIH
},
260 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
261 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
262 { SUB_CHIP_ID1
, TWL6030_BASEADD_GPADC_CTRL
},
263 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
264 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
266 { SUB_CHIP_ID1
, TWL6030_BASEADD_CHARGER
},
267 { SUB_CHIP_ID1
, TWL6030_BASEADD_GASGAUGE
},
268 { SUB_CHIP_ID1
, TWL6030_BASEADD_PWM
},
269 { SUB_CHIP_ID0
, TWL6030_BASEADD_ZERO
},
270 { SUB_CHIP_ID1
, TWL6030_BASEADD_ZERO
},
272 { SUB_CHIP_ID2
, TWL6030_BASEADD_ZERO
},
273 { SUB_CHIP_ID2
, TWL6030_BASEADD_ZERO
},
274 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
275 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
276 { SUB_CHIP_ID2
, TWL6030_BASEADD_RSV
},
278 { SUB_CHIP_ID0
, TWL6030_BASEADD_PM_MASTER
},
279 { SUB_CHIP_ID0
, TWL6030_BASEADD_PM_SLAVE_MISC
},
280 { SUB_CHIP_ID0
, TWL6030_BASEADD_RTC
},
281 { SUB_CHIP_ID0
, TWL6030_BASEADD_MEM
},
282 { SUB_CHIP_ID1
, TWL6025_BASEADD_CHARGER
},
285 static struct regmap_config twl6030_regmap_config
[3] = {
290 .max_register
= 0xff,
296 .max_register
= 0xff,
302 .max_register
= 0xff,
306 /*----------------------------------------------------------------------*/
308 /* Exported Functions */
311 * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
312 * @mod_no: module number
313 * @value: an array of num_bytes+1 containing data to write
314 * @reg: register address (just offset will do)
315 * @num_bytes: number of bytes to transfer
317 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
318 * valid data starts at Offset 1.
320 * Returns the result of operation - 0 is success
322 int twl_i2c_write(u8 mod_no
, u8
*value
, u8 reg
, unsigned num_bytes
)
326 struct twl_client
*twl
;
328 if (unlikely(mod_no
>= TWL_MODULE_LAST
)) {
329 pr_err("%s: invalid module number %d\n", DRIVER_NAME
, mod_no
);
332 if (unlikely(!inuse
)) {
333 pr_err("%s: not initialized\n", DRIVER_NAME
);
336 sid
= twl_map
[mod_no
].sid
;
337 if (unlikely(sid
== SUB_CHIP_ID_INVAL
)) {
338 pr_err("%s: module %d is not part of the pmic\n",
339 DRIVER_NAME
, mod_no
);
342 twl
= &twl_modules
[sid
];
344 ret
= regmap_bulk_write(twl
->regmap
, twl_map
[mod_no
].base
+ reg
,
348 pr_err("%s: Write failed (mod %d, reg 0x%02x count %d)\n",
349 DRIVER_NAME
, mod_no
, reg
, num_bytes
);
353 EXPORT_SYMBOL(twl_i2c_write
);
356 * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
357 * @mod_no: module number
358 * @value: an array of num_bytes containing data to be read
359 * @reg: register address (just offset will do)
360 * @num_bytes: number of bytes to transfer
362 * Returns result of operation - num_bytes is success else failure.
364 int twl_i2c_read(u8 mod_no
, u8
*value
, u8 reg
, unsigned num_bytes
)
368 struct twl_client
*twl
;
370 if (unlikely(mod_no
>= TWL_MODULE_LAST
)) {
371 pr_err("%s: invalid module number %d\n", DRIVER_NAME
, mod_no
);
374 if (unlikely(!inuse
)) {
375 pr_err("%s: not initialized\n", DRIVER_NAME
);
378 sid
= twl_map
[mod_no
].sid
;
379 if (unlikely(sid
== SUB_CHIP_ID_INVAL
)) {
380 pr_err("%s: module %d is not part of the pmic\n",
381 DRIVER_NAME
, mod_no
);
384 twl
= &twl_modules
[sid
];
386 ret
= regmap_bulk_read(twl
->regmap
, twl_map
[mod_no
].base
+ reg
,
390 pr_err("%s: Read failed (mod %d, reg 0x%02x count %d)\n",
391 DRIVER_NAME
, mod_no
, reg
, num_bytes
);
395 EXPORT_SYMBOL(twl_i2c_read
);
398 * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
399 * @mod_no: module number
400 * @value: the value to be written 8 bit
401 * @reg: register address (just offset will do)
403 * Returns result of operation - 0 is success
405 int twl_i2c_write_u8(u8 mod_no
, u8 value
, u8 reg
)
407 return twl_i2c_write(mod_no
, &value
, reg
, 1);
409 EXPORT_SYMBOL(twl_i2c_write_u8
);
412 * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
413 * @mod_no: module number
414 * @value: the value read 8 bit
415 * @reg: register address (just offset will do)
417 * Returns result of operation - 0 is success
419 int twl_i2c_read_u8(u8 mod_no
, u8
*value
, u8 reg
)
421 return twl_i2c_read(mod_no
, value
, reg
, 1);
423 EXPORT_SYMBOL(twl_i2c_read_u8
);
425 /*----------------------------------------------------------------------*/
428 * twl_read_idcode_register - API to read the IDCODE register.
430 * Unlocks the IDCODE register and read the 32 bit value.
432 static int twl_read_idcode_register(void)
436 err
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, TWL_EEPROM_R_UNLOCK
,
437 REG_UNLOCK_TEST_REG
);
439 pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err
);
443 err
= twl_i2c_read(TWL4030_MODULE_INTBR
, (u8
*)(&twl_idcode
),
446 pr_err("TWL4030: unable to read IDCODE -%d\n", err
);
450 err
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, 0x0, REG_UNLOCK_TEST_REG
);
452 pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err
);
458 * twl_get_type - API to get TWL Si type.
460 * Api to get the TWL Si type from IDCODE value.
462 int twl_get_type(void)
464 return TWL_SIL_TYPE(twl_idcode
);
466 EXPORT_SYMBOL_GPL(twl_get_type
);
469 * twl_get_version - API to get TWL Si version.
471 * Api to get the TWL Si version from IDCODE value.
473 int twl_get_version(void)
475 return TWL_SIL_REV(twl_idcode
);
477 EXPORT_SYMBOL_GPL(twl_get_version
);
480 * twl_get_hfclk_rate - API to get TWL external HFCLK clock rate.
482 * Api to get the TWL HFCLK rate based on BOOT_CFG register.
484 int twl_get_hfclk_rate(void)
489 twl_i2c_read_u8(TWL_MODULE_PM_MASTER
, &ctrl
, R_CFG_BOOT
);
491 switch (ctrl
& 0x3) {
492 case HFCLK_FREQ_19p2_MHZ
:
495 case HFCLK_FREQ_26_MHZ
:
498 case HFCLK_FREQ_38p4_MHZ
:
502 pr_err("TWL4030: HFCLK is not configured\n");
509 EXPORT_SYMBOL_GPL(twl_get_hfclk_rate
);
511 static struct device
*
512 add_numbered_child(unsigned chip
, const char *name
, int num
,
513 void *pdata
, unsigned pdata_len
,
514 bool can_wakeup
, int irq0
, int irq1
)
516 struct platform_device
*pdev
;
517 struct twl_client
*twl
= &twl_modules
[chip
];
520 pdev
= platform_device_alloc(name
, num
);
522 dev_dbg(&twl
->client
->dev
, "can't alloc dev\n");
527 pdev
->dev
.parent
= &twl
->client
->dev
;
530 status
= platform_device_add_data(pdev
, pdata
, pdata_len
);
532 dev_dbg(&pdev
->dev
, "can't add platform_data\n");
538 struct resource r
[2] = {
539 { .start
= irq0
, .flags
= IORESOURCE_IRQ
, },
540 { .start
= irq1
, .flags
= IORESOURCE_IRQ
, },
543 status
= platform_device_add_resources(pdev
, r
, irq1
? 2 : 1);
545 dev_dbg(&pdev
->dev
, "can't add irqs\n");
550 status
= platform_device_add(pdev
);
552 device_init_wakeup(&pdev
->dev
, can_wakeup
);
556 platform_device_put(pdev
);
557 dev_err(&twl
->client
->dev
, "can't add %s dev\n", name
);
558 return ERR_PTR(status
);
563 static inline struct device
*add_child(unsigned chip
, const char *name
,
564 void *pdata
, unsigned pdata_len
,
565 bool can_wakeup
, int irq0
, int irq1
)
567 return add_numbered_child(chip
, name
, -1, pdata
, pdata_len
,
568 can_wakeup
, irq0
, irq1
);
571 static struct device
*
572 add_regulator_linked(int num
, struct regulator_init_data
*pdata
,
573 struct regulator_consumer_supply
*consumers
,
574 unsigned num_consumers
, unsigned long features
)
576 unsigned sub_chip_id
;
577 struct twl_regulator_driver_data drv_data
;
579 /* regulator framework demands init_data ... */
584 pdata
->consumer_supplies
= consumers
;
585 pdata
->num_consumer_supplies
= num_consumers
;
588 if (pdata
->driver_data
) {
589 /* If we have existing drv_data, just add the flags */
590 struct twl_regulator_driver_data
*tmp
;
591 tmp
= pdata
->driver_data
;
592 tmp
->features
|= features
;
594 /* add new driver data struct, used only during init */
595 drv_data
.features
= features
;
596 drv_data
.set_voltage
= NULL
;
597 drv_data
.get_voltage
= NULL
;
598 drv_data
.data
= NULL
;
599 pdata
->driver_data
= &drv_data
;
602 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
603 sub_chip_id
= twl_map
[TWL_MODULE_PM_MASTER
].sid
;
604 return add_numbered_child(sub_chip_id
, "twl_reg", num
,
605 pdata
, sizeof(*pdata
), false, 0, 0);
608 static struct device
*
609 add_regulator(int num
, struct regulator_init_data
*pdata
,
610 unsigned long features
)
612 return add_regulator_linked(num
, pdata
, NULL
, 0, features
);
616 * NOTE: We know the first 8 IRQs after pdata->base_irq are
617 * for the PIH, and the next are for the PWR_INT SIH, since
618 * that's how twl_init_irq() sets things up.
622 add_children(struct twl4030_platform_data
*pdata
, unsigned irq_base
,
623 unsigned long features
)
625 struct device
*child
;
626 unsigned sub_chip_id
;
628 if (IS_ENABLED(CONFIG_GPIO_TWL4030
) && pdata
->gpio
) {
629 child
= add_child(SUB_CHIP_ID1
, "twl4030_gpio",
630 pdata
->gpio
, sizeof(*pdata
->gpio
),
631 false, irq_base
+ GPIO_INTR_OFFSET
, 0);
633 return PTR_ERR(child
);
636 if (IS_ENABLED(CONFIG_KEYBOARD_TWL4030
) && pdata
->keypad
) {
637 child
= add_child(SUB_CHIP_ID2
, "twl4030_keypad",
638 pdata
->keypad
, sizeof(*pdata
->keypad
),
639 true, irq_base
+ KEYPAD_INTR_OFFSET
, 0);
641 return PTR_ERR(child
);
644 if (IS_ENABLED(CONFIG_TWL4030_MADC
) && pdata
->madc
&&
645 twl_class_is_4030()) {
646 child
= add_child(SUB_CHIP_ID2
, "twl4030_madc",
647 pdata
->madc
, sizeof(*pdata
->madc
),
648 true, irq_base
+ MADC_INTR_OFFSET
, 0);
650 return PTR_ERR(child
);
653 if (IS_ENABLED(CONFIG_RTC_DRV_TWL4030
)) {
655 * REVISIT platform_data here currently might expose the
656 * "msecure" line ... but for now we just expect board
657 * setup to tell the chip "it's always ok to SET_TIME".
658 * Eventually, Linux might become more aware of such
659 * HW security concerns, and "least privilege".
661 sub_chip_id
= twl_map
[TWL_MODULE_RTC
].sid
;
662 child
= add_child(sub_chip_id
, "twl_rtc", NULL
, 0,
663 true, irq_base
+ RTC_INTR_OFFSET
, 0);
665 return PTR_ERR(child
);
668 if (IS_ENABLED(CONFIG_PWM_TWL
)) {
669 child
= add_child(SUB_CHIP_ID1
, "twl-pwm", NULL
, 0,
672 return PTR_ERR(child
);
675 if (IS_ENABLED(CONFIG_PWM_TWL_LED
)) {
676 child
= add_child(SUB_CHIP_ID1
, "twl-pwmled", NULL
, 0,
679 return PTR_ERR(child
);
682 if (IS_ENABLED(CONFIG_TWL4030_USB
) && pdata
->usb
&&
683 twl_class_is_4030()) {
685 static struct regulator_consumer_supply usb1v5
= {
688 static struct regulator_consumer_supply usb1v8
= {
691 static struct regulator_consumer_supply usb3v1
[] = {
692 { .supply
= "usb3v1" },
693 { .supply
= "bci3v1" },
696 /* First add the regulators so that they can be used by transceiver */
697 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
)) {
698 /* this is a template that gets copied */
699 struct regulator_init_data usb_fixed
= {
700 .constraints
.valid_modes_mask
=
701 REGULATOR_MODE_NORMAL
702 | REGULATOR_MODE_STANDBY
,
703 .constraints
.valid_ops_mask
=
704 REGULATOR_CHANGE_MODE
705 | REGULATOR_CHANGE_STATUS
,
708 child
= add_regulator_linked(TWL4030_REG_VUSB1V5
,
709 &usb_fixed
, &usb1v5
, 1,
712 return PTR_ERR(child
);
714 child
= add_regulator_linked(TWL4030_REG_VUSB1V8
,
715 &usb_fixed
, &usb1v8
, 1,
718 return PTR_ERR(child
);
720 child
= add_regulator_linked(TWL4030_REG_VUSB3V1
,
721 &usb_fixed
, usb3v1
, 2,
724 return PTR_ERR(child
);
728 child
= add_child(SUB_CHIP_ID0
, "twl4030_usb",
729 pdata
->usb
, sizeof(*pdata
->usb
), true,
730 /* irq0 = USB_PRES, irq1 = USB */
731 irq_base
+ USB_PRES_INTR_OFFSET
,
732 irq_base
+ USB_INTR_OFFSET
);
735 return PTR_ERR(child
);
737 /* we need to connect regulators to this transceiver */
738 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && child
) {
739 usb1v5
.dev_name
= dev_name(child
);
740 usb1v8
.dev_name
= dev_name(child
);
741 usb3v1
[0].dev_name
= dev_name(child
);
744 if (IS_ENABLED(CONFIG_TWL6030_USB
) && pdata
->usb
&&
745 twl_class_is_6030()) {
747 static struct regulator_consumer_supply usb3v3
;
750 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
)) {
751 /* this is a template that gets copied */
752 struct regulator_init_data usb_fixed
= {
753 .constraints
.valid_modes_mask
=
754 REGULATOR_MODE_NORMAL
755 | REGULATOR_MODE_STANDBY
,
756 .constraints
.valid_ops_mask
=
757 REGULATOR_CHANGE_MODE
758 | REGULATOR_CHANGE_STATUS
,
761 if (features
& TWL6025_SUBCLASS
) {
762 usb3v3
.supply
= "ldousb";
763 regulator
= TWL6025_REG_LDOUSB
;
765 usb3v3
.supply
= "vusb";
766 regulator
= TWL6030_REG_VUSB
;
768 child
= add_regulator_linked(regulator
, &usb_fixed
,
772 return PTR_ERR(child
);
775 pdata
->usb
->features
= features
;
777 child
= add_child(SUB_CHIP_ID0
, "twl6030_usb",
778 pdata
->usb
, sizeof(*pdata
->usb
), true,
779 /* irq1 = VBUS_PRES, irq0 = USB ID */
780 irq_base
+ USBOTG_INTR_OFFSET
,
781 irq_base
+ USB_PRES_INTR_OFFSET
);
784 return PTR_ERR(child
);
785 /* we need to connect regulators to this transceiver */
786 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && child
)
787 usb3v3
.dev_name
= dev_name(child
);
788 } else if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) &&
789 twl_class_is_6030()) {
790 if (features
& TWL6025_SUBCLASS
)
791 child
= add_regulator(TWL6025_REG_LDOUSB
,
792 pdata
->ldousb
, features
);
794 child
= add_regulator(TWL6030_REG_VUSB
,
795 pdata
->vusb
, features
);
798 return PTR_ERR(child
);
801 if (IS_ENABLED(CONFIG_TWL4030_WATCHDOG
) && twl_class_is_4030()) {
802 child
= add_child(SUB_CHIP_ID3
, "twl4030_wdt", NULL
, 0,
805 return PTR_ERR(child
);
808 if (IS_ENABLED(CONFIG_INPUT_TWL4030_PWRBUTTON
) && twl_class_is_4030()) {
809 child
= add_child(SUB_CHIP_ID3
, "twl4030_pwrbutton", NULL
, 0,
810 true, irq_base
+ 8 + 0, 0);
812 return PTR_ERR(child
);
815 if (IS_ENABLED(CONFIG_MFD_TWL4030_AUDIO
) && pdata
->audio
&&
816 twl_class_is_4030()) {
817 child
= add_child(SUB_CHIP_ID1
, "twl4030-audio",
818 pdata
->audio
, sizeof(*pdata
->audio
),
821 return PTR_ERR(child
);
824 /* twl4030 regulators */
825 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && twl_class_is_4030()) {
826 child
= add_regulator(TWL4030_REG_VPLL1
, pdata
->vpll1
,
829 return PTR_ERR(child
);
831 child
= add_regulator(TWL4030_REG_VIO
, pdata
->vio
,
834 return PTR_ERR(child
);
836 child
= add_regulator(TWL4030_REG_VDD1
, pdata
->vdd1
,
839 return PTR_ERR(child
);
841 child
= add_regulator(TWL4030_REG_VDD2
, pdata
->vdd2
,
844 return PTR_ERR(child
);
846 child
= add_regulator(TWL4030_REG_VMMC1
, pdata
->vmmc1
,
849 return PTR_ERR(child
);
851 child
= add_regulator(TWL4030_REG_VDAC
, pdata
->vdac
,
854 return PTR_ERR(child
);
856 child
= add_regulator((features
& TWL4030_VAUX2
)
857 ? TWL4030_REG_VAUX2_4030
859 pdata
->vaux2
, features
);
861 return PTR_ERR(child
);
863 child
= add_regulator(TWL4030_REG_VINTANA1
, pdata
->vintana1
,
866 return PTR_ERR(child
);
868 child
= add_regulator(TWL4030_REG_VINTANA2
, pdata
->vintana2
,
871 return PTR_ERR(child
);
873 child
= add_regulator(TWL4030_REG_VINTDIG
, pdata
->vintdig
,
876 return PTR_ERR(child
);
879 /* maybe add LDOs that are omitted on cost-reduced parts */
880 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && !(features
& TPS_SUBSET
)
881 && twl_class_is_4030()) {
882 child
= add_regulator(TWL4030_REG_VPLL2
, pdata
->vpll2
,
885 return PTR_ERR(child
);
887 child
= add_regulator(TWL4030_REG_VMMC2
, pdata
->vmmc2
,
890 return PTR_ERR(child
);
892 child
= add_regulator(TWL4030_REG_VSIM
, pdata
->vsim
,
895 return PTR_ERR(child
);
897 child
= add_regulator(TWL4030_REG_VAUX1
, pdata
->vaux1
,
900 return PTR_ERR(child
);
902 child
= add_regulator(TWL4030_REG_VAUX3
, pdata
->vaux3
,
905 return PTR_ERR(child
);
907 child
= add_regulator(TWL4030_REG_VAUX4
, pdata
->vaux4
,
910 return PTR_ERR(child
);
913 /* twl6030 regulators */
914 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && twl_class_is_6030() &&
915 !(features
& TWL6025_SUBCLASS
)) {
916 child
= add_regulator(TWL6030_REG_VDD1
, pdata
->vdd1
,
919 return PTR_ERR(child
);
921 child
= add_regulator(TWL6030_REG_VDD2
, pdata
->vdd2
,
924 return PTR_ERR(child
);
926 child
= add_regulator(TWL6030_REG_VDD3
, pdata
->vdd3
,
929 return PTR_ERR(child
);
931 child
= add_regulator(TWL6030_REG_V1V8
, pdata
->v1v8
,
934 return PTR_ERR(child
);
936 child
= add_regulator(TWL6030_REG_V2V1
, pdata
->v2v1
,
939 return PTR_ERR(child
);
941 child
= add_regulator(TWL6030_REG_VMMC
, pdata
->vmmc
,
944 return PTR_ERR(child
);
946 child
= add_regulator(TWL6030_REG_VPP
, pdata
->vpp
,
949 return PTR_ERR(child
);
951 child
= add_regulator(TWL6030_REG_VUSIM
, pdata
->vusim
,
954 return PTR_ERR(child
);
956 child
= add_regulator(TWL6030_REG_VCXIO
, pdata
->vcxio
,
959 return PTR_ERR(child
);
961 child
= add_regulator(TWL6030_REG_VDAC
, pdata
->vdac
,
964 return PTR_ERR(child
);
966 child
= add_regulator(TWL6030_REG_VAUX1_6030
, pdata
->vaux1
,
969 return PTR_ERR(child
);
971 child
= add_regulator(TWL6030_REG_VAUX2_6030
, pdata
->vaux2
,
974 return PTR_ERR(child
);
976 child
= add_regulator(TWL6030_REG_VAUX3_6030
, pdata
->vaux3
,
979 return PTR_ERR(child
);
981 child
= add_regulator(TWL6030_REG_CLK32KG
, pdata
->clk32kg
,
984 return PTR_ERR(child
);
987 /* 6030 and 6025 share this regulator */
988 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && twl_class_is_6030()) {
989 child
= add_regulator(TWL6030_REG_VANA
, pdata
->vana
,
992 return PTR_ERR(child
);
995 /* twl6025 regulators */
996 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && twl_class_is_6030() &&
997 (features
& TWL6025_SUBCLASS
)) {
998 child
= add_regulator(TWL6025_REG_LDO5
, pdata
->ldo5
,
1001 return PTR_ERR(child
);
1003 child
= add_regulator(TWL6025_REG_LDO1
, pdata
->ldo1
,
1006 return PTR_ERR(child
);
1008 child
= add_regulator(TWL6025_REG_LDO7
, pdata
->ldo7
,
1011 return PTR_ERR(child
);
1013 child
= add_regulator(TWL6025_REG_LDO6
, pdata
->ldo6
,
1016 return PTR_ERR(child
);
1018 child
= add_regulator(TWL6025_REG_LDOLN
, pdata
->ldoln
,
1021 return PTR_ERR(child
);
1023 child
= add_regulator(TWL6025_REG_LDO2
, pdata
->ldo2
,
1026 return PTR_ERR(child
);
1028 child
= add_regulator(TWL6025_REG_LDO4
, pdata
->ldo4
,
1031 return PTR_ERR(child
);
1033 child
= add_regulator(TWL6025_REG_LDO3
, pdata
->ldo3
,
1036 return PTR_ERR(child
);
1038 child
= add_regulator(TWL6025_REG_SMPS3
, pdata
->smps3
,
1041 return PTR_ERR(child
);
1043 child
= add_regulator(TWL6025_REG_SMPS4
, pdata
->smps4
,
1046 return PTR_ERR(child
);
1048 child
= add_regulator(TWL6025_REG_VIO
, pdata
->vio6025
,
1051 return PTR_ERR(child
);
1055 if (IS_ENABLED(CONFIG_CHARGER_TWL4030
) && pdata
->bci
&&
1056 !(features
& (TPS_SUBSET
| TWL5031
))) {
1057 child
= add_child(SUB_CHIP_ID3
, "twl4030_bci",
1058 pdata
->bci
, sizeof(*pdata
->bci
), false,
1059 /* irq0 = CHG_PRES, irq1 = BCI */
1060 irq_base
+ BCI_PRES_INTR_OFFSET
,
1061 irq_base
+ BCI_INTR_OFFSET
);
1063 return PTR_ERR(child
);
1069 /*----------------------------------------------------------------------*/
1072 * These three functions initialize the on-chip clock framework,
1073 * letting it generate the right frequencies for USB, MADC, and
1076 static inline int __init
protect_pm_master(void)
1080 e
= twl_i2c_write_u8(TWL_MODULE_PM_MASTER
, 0,
1081 TWL4030_PM_MASTER_PROTECT_KEY
);
1085 static inline int __init
unprotect_pm_master(void)
1089 e
|= twl_i2c_write_u8(TWL_MODULE_PM_MASTER
, TWL4030_PM_MASTER_KEY_CFG1
,
1090 TWL4030_PM_MASTER_PROTECT_KEY
);
1091 e
|= twl_i2c_write_u8(TWL_MODULE_PM_MASTER
, TWL4030_PM_MASTER_KEY_CFG2
,
1092 TWL4030_PM_MASTER_PROTECT_KEY
);
1097 static void clocks_init(struct device
*dev
,
1098 struct twl4030_clock_init_data
*clock
)
1103 u8 ctrl
= HFCLK_FREQ_26_MHZ
;
1105 osc
= clk_get(dev
, "fck");
1107 printk(KERN_WARNING
"Skipping twl internal clock init and "
1108 "using bootloader value (unknown osc rate)\n");
1112 rate
= clk_get_rate(osc
);
1117 ctrl
= HFCLK_FREQ_19p2_MHZ
;
1120 ctrl
= HFCLK_FREQ_26_MHZ
;
1123 ctrl
= HFCLK_FREQ_38p4_MHZ
;
1127 ctrl
|= HIGH_PERF_SQ
;
1128 if (clock
&& clock
->ck32k_lowpwr_enable
)
1129 ctrl
|= CK32K_LOWPWR_EN
;
1131 e
|= unprotect_pm_master();
1132 /* effect->MADC+USB ck en */
1133 e
|= twl_i2c_write_u8(TWL_MODULE_PM_MASTER
, ctrl
, R_CFG_BOOT
);
1134 e
|= protect_pm_master();
1137 pr_err("%s: clock init err [%d]\n", DRIVER_NAME
, e
);
1140 /*----------------------------------------------------------------------*/
1143 static int twl_remove(struct i2c_client
*client
)
1145 unsigned i
, num_slaves
;
1148 if (twl_class_is_4030()) {
1149 status
= twl4030_exit_irq();
1150 num_slaves
= TWL_NUM_SLAVES
;
1152 status
= twl6030_exit_irq();
1153 num_slaves
= TWL_NUM_SLAVES
- 1;
1159 for (i
= 0; i
< num_slaves
; i
++) {
1160 struct twl_client
*twl
= &twl_modules
[i
];
1162 if (twl
->client
&& twl
->client
!= client
)
1163 i2c_unregister_device(twl
->client
);
1164 twl_modules
[i
].client
= NULL
;
1170 /* NOTE: This driver only handles a single twl4030/tps659x0 chip */
1172 twl_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1174 struct twl4030_platform_data
*pdata
= client
->dev
.platform_data
;
1175 struct device_node
*node
= client
->dev
.of_node
;
1176 struct platform_device
*pdev
;
1177 struct regmap_config
*twl_regmap_config
;
1180 unsigned i
, num_slaves
;
1182 pdev
= platform_device_alloc(DRIVER_NAME
, -1);
1184 dev_err(&client
->dev
, "can't alloc pdev\n");
1188 status
= platform_device_add(pdev
);
1190 platform_device_put(pdev
);
1194 if (node
&& !pdata
) {
1196 * XXX: Temporary pdata until the information is correctly
1197 * retrieved by every TWL modules from DT.
1199 pdata
= devm_kzalloc(&client
->dev
,
1200 sizeof(struct twl4030_platform_data
),
1209 dev_dbg(&client
->dev
, "no platform data?\n");
1214 platform_set_drvdata(pdev
, pdata
);
1216 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
) == 0) {
1217 dev_dbg(&client
->dev
, "can't talk I2C?\n");
1223 dev_dbg(&client
->dev
, "driver is already in use\n");
1228 if ((id
->driver_data
) & TWL6030_CLASS
) {
1229 twl_id
= TWL6030_CLASS_ID
;
1230 twl_map
= &twl6030_map
[0];
1231 twl_regmap_config
= twl6030_regmap_config
;
1232 num_slaves
= TWL_NUM_SLAVES
- 1;
1234 twl_id
= TWL4030_CLASS_ID
;
1235 twl_map
= &twl4030_map
[0];
1236 twl_regmap_config
= twl4030_regmap_config
;
1237 num_slaves
= TWL_NUM_SLAVES
;
1240 for (i
= 0; i
< num_slaves
; i
++) {
1241 struct twl_client
*twl
= &twl_modules
[i
];
1244 twl
->client
= client
;
1246 twl
->client
= i2c_new_dummy(client
->adapter
,
1249 dev_err(&client
->dev
,
1250 "can't attach client %d\n", i
);
1256 twl
->regmap
= devm_regmap_init_i2c(twl
->client
,
1257 &twl_regmap_config
[i
]);
1258 if (IS_ERR(twl
->regmap
)) {
1259 status
= PTR_ERR(twl
->regmap
);
1260 dev_err(&client
->dev
,
1261 "Failed to allocate regmap %d, err: %d\n", i
,
1269 /* setup clock framework */
1270 clocks_init(&pdev
->dev
, pdata
->clock
);
1272 /* read TWL IDCODE Register */
1273 if (twl_id
== TWL4030_CLASS_ID
) {
1274 status
= twl_read_idcode_register();
1275 WARN(status
< 0, "Error: reading twl_idcode register value\n");
1278 /* load power event scripts */
1279 if (IS_ENABLED(CONFIG_TWL4030_POWER
) && pdata
->power
)
1280 twl4030_power_init(pdata
->power
);
1282 /* Maybe init the T2 Interrupt subsystem */
1284 if (twl_class_is_4030()) {
1285 twl4030_init_chip_irq(id
->name
);
1286 irq_base
= twl4030_init_irq(&client
->dev
, client
->irq
);
1288 irq_base
= twl6030_init_irq(&client
->dev
, client
->irq
);
1298 * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
1299 * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1300 * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1302 if (twl_class_is_4030()) {
1305 twl_i2c_read_u8(TWL4030_MODULE_INTBR
, &temp
, REG_GPPUPDCTR1
);
1306 temp
&= ~(SR_I2C_SDA_CTRL_PU
| SR_I2C_SCL_CTRL_PU
| \
1307 I2C_SDA_CTRL_PU
| I2C_SCL_CTRL_PU
);
1308 twl_i2c_write_u8(TWL4030_MODULE_INTBR
, temp
, REG_GPPUPDCTR1
);
1313 status
= of_platform_populate(node
, NULL
, NULL
, &client
->dev
);
1315 status
= add_children(pdata
, irq_base
, id
->driver_data
);
1322 platform_device_unregister(pdev
);
1327 static const struct i2c_device_id twl_ids
[] = {
1328 { "twl4030", TWL4030_VAUX2
}, /* "Triton 2" */
1329 { "twl5030", 0 }, /* T2 updated */
1330 { "twl5031", TWL5031
}, /* TWL5030 updated */
1331 { "tps65950", 0 }, /* catalog version of twl5030 */
1332 { "tps65930", TPS_SUBSET
}, /* fewer LDOs and DACs; no charger */
1333 { "tps65920", TPS_SUBSET
}, /* fewer LDOs; no codec or charger */
1334 { "tps65921", TPS_SUBSET
}, /* fewer LDOs; no codec, no LED
1335 and vibrator. Charger in USB module*/
1336 { "twl6030", TWL6030_CLASS
}, /* "Phoenix power chip" */
1337 { "twl6025", TWL6030_CLASS
| TWL6025_SUBCLASS
}, /* "Phoenix lite" */
1338 { /* end of list */ },
1340 MODULE_DEVICE_TABLE(i2c
, twl_ids
);
1342 /* One Client Driver , 4 Clients */
1343 static struct i2c_driver twl_driver
= {
1344 .driver
.name
= DRIVER_NAME
,
1345 .id_table
= twl_ids
,
1347 .remove
= twl_remove
,
1350 static int __init
twl_init(void)
1352 return i2c_add_driver(&twl_driver
);
1354 subsys_initcall(twl_init
);
1356 static void __exit
twl_exit(void)
1358 i2c_del_driver(&twl_driver
);
1360 module_exit(twl_exit
);
1362 MODULE_AUTHOR("Texas Instruments, Inc.");
1363 MODULE_DESCRIPTION("I2C Core interface for TWL");
1364 MODULE_LICENSE("GPL");