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 /* Base Address defns for twl4030_map[] */
71 /* subchip/slave 0 - USB ID */
72 #define TWL4030_BASEADD_USB 0x0000
74 /* subchip/slave 1 - AUD ID */
75 #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
76 #define TWL4030_BASEADD_GPIO 0x0098
77 #define TWL4030_BASEADD_INTBR 0x0085
78 #define TWL4030_BASEADD_PIH 0x0080
79 #define TWL4030_BASEADD_TEST 0x004C
81 /* subchip/slave 2 - AUX ID */
82 #define TWL4030_BASEADD_INTERRUPTS 0x00B9
83 #define TWL4030_BASEADD_LED 0x00EE
84 #define TWL4030_BASEADD_MADC 0x0000
85 #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
86 #define TWL4030_BASEADD_PRECHARGE 0x00AA
87 #define TWL4030_BASEADD_PWM 0x00F8
88 #define TWL4030_BASEADD_KEYPAD 0x00D2
90 #define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
91 #define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
94 /* subchip/slave 3 - POWER ID */
95 #define TWL4030_BASEADD_BACKUP 0x0014
96 #define TWL4030_BASEADD_INT 0x002E
97 #define TWL4030_BASEADD_PM_MASTER 0x0036
98 #define TWL4030_BASEADD_PM_RECEIVER 0x005B
99 #define TWL4030_BASEADD_RTC 0x001C
100 #define TWL4030_BASEADD_SECURED_REG 0x0000
102 /* Triton Core internal information (END) */
105 /* subchip/slave 0 0x48 - POWER */
106 #define TWL6030_BASEADD_RTC 0x0000
107 #define TWL6030_BASEADD_SECURED_REG 0x0017
108 #define TWL6030_BASEADD_PM_MASTER 0x001F
109 #define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
110 #define TWL6030_BASEADD_PM_MISC 0x00E2
111 #define TWL6030_BASEADD_PM_PUPD 0x00F0
113 /* subchip/slave 1 0x49 - FEATURE */
114 #define TWL6030_BASEADD_USB 0x0000
115 #define TWL6030_BASEADD_GPADC_CTRL 0x002E
116 #define TWL6030_BASEADD_AUX 0x0090
117 #define TWL6030_BASEADD_PWM 0x00BA
118 #define TWL6030_BASEADD_GASGAUGE 0x00C0
119 #define TWL6030_BASEADD_PIH 0x00D0
120 #define TWL6030_BASEADD_CHARGER 0x00E0
121 #define TWL6025_BASEADD_CHARGER 0x00DA
122 #define TWL6030_BASEADD_LED 0x00F4
124 /* subchip/slave 2 0x4A - DFT */
125 #define TWL6030_BASEADD_DIEID 0x00C0
127 /* subchip/slave 3 0x4B - AUDIO */
128 #define TWL6030_BASEADD_AUDIO 0x0000
129 #define TWL6030_BASEADD_RSV 0x0000
130 #define TWL6030_BASEADD_ZERO 0x0000
132 /* Few power values */
133 #define R_CFG_BOOT 0x05
135 /* some fields in R_CFG_BOOT */
136 #define HFCLK_FREQ_19p2_MHZ (1 << 0)
137 #define HFCLK_FREQ_26_MHZ (2 << 0)
138 #define HFCLK_FREQ_38p4_MHZ (3 << 0)
139 #define HIGH_PERF_SQ (1 << 3)
140 #define CK32K_LOWPWR_EN (1 << 7)
142 /*----------------------------------------------------------------------*/
144 /* is driver active, bound to a chip? */
147 /* TWL IDCODE Register value */
148 static u32 twl_idcode
;
150 static unsigned int twl_id
;
151 unsigned int twl_rev(void)
155 EXPORT_SYMBOL(twl_rev
);
157 /* Structure for each TWL4030/TWL6030 Slave */
159 struct i2c_client
*client
;
160 struct regmap
*regmap
;
163 static struct twl_client
*twl_modules
;
165 /* mapping the module id to slave id and base address */
167 unsigned char sid
; /* Slave ID */
168 unsigned char base
; /* base address */
170 static struct twl_mapping
*twl_map
;
172 static struct twl_mapping twl4030_map
[] = {
174 * NOTE: don't change this table without updating the
175 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
176 * so they continue to match the order in this table.
180 { 0, TWL4030_BASEADD_USB
},
181 { 1, TWL4030_BASEADD_PIH
},
182 { 2, TWL4030_BASEADD_MAIN_CHARGE
},
183 { 3, TWL4030_BASEADD_PM_MASTER
},
184 { 3, TWL4030_BASEADD_PM_RECEIVER
},
186 { 3, TWL4030_BASEADD_RTC
},
187 { 2, TWL4030_BASEADD_PWM
},
188 { 2, TWL4030_BASEADD_LED
},
189 { 3, TWL4030_BASEADD_SECURED_REG
},
191 /* TWL4030 specific IPs */
192 { 1, TWL4030_BASEADD_AUDIO_VOICE
},
193 { 1, TWL4030_BASEADD_GPIO
},
194 { 1, TWL4030_BASEADD_INTBR
},
195 { 1, TWL4030_BASEADD_TEST
},
196 { 2, TWL4030_BASEADD_KEYPAD
},
198 { 2, TWL4030_BASEADD_MADC
},
199 { 2, TWL4030_BASEADD_INTERRUPTS
},
200 { 2, TWL4030_BASEADD_PRECHARGE
},
201 { 3, TWL4030_BASEADD_BACKUP
},
202 { 3, TWL4030_BASEADD_INT
},
204 { 2, TWL5031_BASEADD_ACCESSORY
},
205 { 2, TWL5031_BASEADD_INTERRUPTS
},
208 static struct regmap_config twl4030_regmap_config
[4] = {
213 .max_register
= 0xff,
219 .max_register
= 0xff,
225 .max_register
= 0xff,
231 .max_register
= 0xff,
235 static struct twl_mapping twl6030_map
[] = {
237 * NOTE: don't change this table without updating the
238 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
239 * so they continue to match the order in this table.
243 { 1, TWL6030_BASEADD_USB
},
244 { 1, TWL6030_BASEADD_PIH
},
245 { 1, TWL6030_BASEADD_CHARGER
},
246 { 0, TWL6030_BASEADD_PM_MASTER
},
247 { 0, TWL6030_BASEADD_PM_SLAVE_MISC
},
249 { 0, TWL6030_BASEADD_RTC
},
250 { 1, TWL6030_BASEADD_PWM
},
251 { 1, TWL6030_BASEADD_LED
},
252 { 0, TWL6030_BASEADD_SECURED_REG
},
254 /* TWL6030 specific IPs */
255 { 0, TWL6030_BASEADD_ZERO
},
256 { 1, TWL6030_BASEADD_ZERO
},
257 { 2, TWL6030_BASEADD_ZERO
},
258 { 1, TWL6030_BASEADD_GPADC_CTRL
},
259 { 1, TWL6030_BASEADD_GASGAUGE
},
262 static struct regmap_config twl6030_regmap_config
[3] = {
267 .max_register
= 0xff,
273 .max_register
= 0xff,
279 .max_register
= 0xff,
283 /*----------------------------------------------------------------------*/
285 static inline int twl_get_num_slaves(void)
287 if (twl_class_is_4030())
288 return 4; /* TWL4030 class have four slave address */
290 return 3; /* TWL6030 class have three slave address */
293 static inline int twl_get_last_module(void)
295 if (twl_class_is_4030())
296 return TWL4030_MODULE_LAST
;
298 return TWL6030_MODULE_LAST
;
301 /* Exported Functions */
304 * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
305 * @mod_no: module number
306 * @value: an array of num_bytes+1 containing data to write
307 * @reg: register address (just offset will do)
308 * @num_bytes: number of bytes to transfer
310 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
311 * valid data starts at Offset 1.
313 * Returns the result of operation - 0 is success
315 int twl_i2c_write(u8 mod_no
, u8
*value
, u8 reg
, unsigned num_bytes
)
319 struct twl_client
*twl
;
321 if (unlikely(mod_no
>= twl_get_last_module())) {
322 pr_err("%s: invalid module number %d\n", DRIVER_NAME
, mod_no
);
325 if (unlikely(!inuse
)) {
326 pr_err("%s: not initialized\n", DRIVER_NAME
);
330 sid
= twl_map
[mod_no
].sid
;
331 twl
= &twl_modules
[sid
];
333 ret
= regmap_bulk_write(twl
->regmap
, twl_map
[mod_no
].base
+ reg
,
337 pr_err("%s: Write failed (mod %d, reg 0x%02x count %d)\n",
338 DRIVER_NAME
, mod_no
, reg
, num_bytes
);
342 EXPORT_SYMBOL(twl_i2c_write
);
345 * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
346 * @mod_no: module number
347 * @value: an array of num_bytes containing data to be read
348 * @reg: register address (just offset will do)
349 * @num_bytes: number of bytes to transfer
351 * Returns result of operation - num_bytes is success else failure.
353 int twl_i2c_read(u8 mod_no
, u8
*value
, u8 reg
, unsigned num_bytes
)
357 struct twl_client
*twl
;
359 if (unlikely(mod_no
>= twl_get_last_module())) {
360 pr_err("%s: invalid module number %d\n", DRIVER_NAME
, mod_no
);
363 if (unlikely(!inuse
)) {
364 pr_err("%s: not initialized\n", DRIVER_NAME
);
368 sid
= twl_map
[mod_no
].sid
;
369 twl
= &twl_modules
[sid
];
371 ret
= regmap_bulk_read(twl
->regmap
, twl_map
[mod_no
].base
+ reg
,
375 pr_err("%s: Read failed (mod %d, reg 0x%02x count %d)\n",
376 DRIVER_NAME
, mod_no
, reg
, num_bytes
);
380 EXPORT_SYMBOL(twl_i2c_read
);
383 * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
384 * @mod_no: module number
385 * @value: the value to be written 8 bit
386 * @reg: register address (just offset will do)
388 * Returns result of operation - 0 is success
390 int twl_i2c_write_u8(u8 mod_no
, u8 value
, u8 reg
)
392 return twl_i2c_write(mod_no
, &value
, reg
, 1);
394 EXPORT_SYMBOL(twl_i2c_write_u8
);
397 * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
398 * @mod_no: module number
399 * @value: the value read 8 bit
400 * @reg: register address (just offset will do)
402 * Returns result of operation - 0 is success
404 int twl_i2c_read_u8(u8 mod_no
, u8
*value
, u8 reg
)
406 return twl_i2c_read(mod_no
, value
, reg
, 1);
408 EXPORT_SYMBOL(twl_i2c_read_u8
);
410 /*----------------------------------------------------------------------*/
413 * twl_read_idcode_register - API to read the IDCODE register.
415 * Unlocks the IDCODE register and read the 32 bit value.
417 static int twl_read_idcode_register(void)
421 err
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, TWL_EEPROM_R_UNLOCK
,
422 REG_UNLOCK_TEST_REG
);
424 pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err
);
428 err
= twl_i2c_read(TWL4030_MODULE_INTBR
, (u8
*)(&twl_idcode
),
431 pr_err("TWL4030: unable to read IDCODE -%d\n", err
);
435 err
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, 0x0, REG_UNLOCK_TEST_REG
);
437 pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err
);
443 * twl_get_type - API to get TWL Si type.
445 * Api to get the TWL Si type from IDCODE value.
447 int twl_get_type(void)
449 return TWL_SIL_TYPE(twl_idcode
);
451 EXPORT_SYMBOL_GPL(twl_get_type
);
454 * twl_get_version - API to get TWL Si version.
456 * Api to get the TWL Si version from IDCODE value.
458 int twl_get_version(void)
460 return TWL_SIL_REV(twl_idcode
);
462 EXPORT_SYMBOL_GPL(twl_get_version
);
465 * twl_get_hfclk_rate - API to get TWL external HFCLK clock rate.
467 * Api to get the TWL HFCLK rate based on BOOT_CFG register.
469 int twl_get_hfclk_rate(void)
474 twl_i2c_read_u8(TWL_MODULE_PM_MASTER
, &ctrl
, R_CFG_BOOT
);
476 switch (ctrl
& 0x3) {
477 case HFCLK_FREQ_19p2_MHZ
:
480 case HFCLK_FREQ_26_MHZ
:
483 case HFCLK_FREQ_38p4_MHZ
:
487 pr_err("TWL4030: HFCLK is not configured\n");
494 EXPORT_SYMBOL_GPL(twl_get_hfclk_rate
);
496 static struct device
*
497 add_numbered_child(unsigned mod_no
, const char *name
, int num
,
498 void *pdata
, unsigned pdata_len
,
499 bool can_wakeup
, int irq0
, int irq1
)
501 struct platform_device
*pdev
;
502 struct twl_client
*twl
;
505 if (unlikely(mod_no
>= twl_get_last_module())) {
506 pr_err("%s: invalid module number %d\n", DRIVER_NAME
, mod_no
);
507 return ERR_PTR(-EPERM
);
509 sid
= twl_map
[mod_no
].sid
;
510 twl
= &twl_modules
[sid
];
512 pdev
= platform_device_alloc(name
, num
);
514 dev_dbg(&twl
->client
->dev
, "can't alloc dev\n");
519 pdev
->dev
.parent
= &twl
->client
->dev
;
522 status
= platform_device_add_data(pdev
, pdata
, pdata_len
);
524 dev_dbg(&pdev
->dev
, "can't add platform_data\n");
530 struct resource r
[2] = {
531 { .start
= irq0
, .flags
= IORESOURCE_IRQ
, },
532 { .start
= irq1
, .flags
= IORESOURCE_IRQ
, },
535 status
= platform_device_add_resources(pdev
, r
, irq1
? 2 : 1);
537 dev_dbg(&pdev
->dev
, "can't add irqs\n");
542 status
= platform_device_add(pdev
);
544 device_init_wakeup(&pdev
->dev
, can_wakeup
);
548 platform_device_put(pdev
);
549 dev_err(&twl
->client
->dev
, "can't add %s dev\n", name
);
550 return ERR_PTR(status
);
555 static inline struct device
*add_child(unsigned mod_no
, const char *name
,
556 void *pdata
, unsigned pdata_len
,
557 bool can_wakeup
, int irq0
, int irq1
)
559 return add_numbered_child(mod_no
, name
, -1, pdata
, pdata_len
,
560 can_wakeup
, irq0
, irq1
);
563 static struct device
*
564 add_regulator_linked(int num
, struct regulator_init_data
*pdata
,
565 struct regulator_consumer_supply
*consumers
,
566 unsigned num_consumers
, unsigned long features
)
568 struct twl_regulator_driver_data drv_data
;
570 /* regulator framework demands init_data ... */
575 pdata
->consumer_supplies
= consumers
;
576 pdata
->num_consumer_supplies
= num_consumers
;
579 if (pdata
->driver_data
) {
580 /* If we have existing drv_data, just add the flags */
581 struct twl_regulator_driver_data
*tmp
;
582 tmp
= pdata
->driver_data
;
583 tmp
->features
|= features
;
585 /* add new driver data struct, used only during init */
586 drv_data
.features
= features
;
587 drv_data
.set_voltage
= NULL
;
588 drv_data
.get_voltage
= NULL
;
589 drv_data
.data
= NULL
;
590 pdata
->driver_data
= &drv_data
;
593 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
594 return add_numbered_child(TWL_MODULE_PM_MASTER
, "twl_reg", num
,
595 pdata
, sizeof(*pdata
), false, 0, 0);
598 static struct device
*
599 add_regulator(int num
, struct regulator_init_data
*pdata
,
600 unsigned long features
)
602 return add_regulator_linked(num
, pdata
, NULL
, 0, features
);
606 * NOTE: We know the first 8 IRQs after pdata->base_irq are
607 * for the PIH, and the next are for the PWR_INT SIH, since
608 * that's how twl_init_irq() sets things up.
612 add_children(struct twl4030_platform_data
*pdata
, unsigned irq_base
,
613 unsigned long features
)
615 struct device
*child
;
617 if (IS_ENABLED(CONFIG_GPIO_TWL4030
) && pdata
->gpio
) {
618 child
= add_child(TWL4030_MODULE_GPIO
, "twl4030_gpio",
619 pdata
->gpio
, sizeof(*pdata
->gpio
),
620 false, irq_base
+ GPIO_INTR_OFFSET
, 0);
622 return PTR_ERR(child
);
625 if (IS_ENABLED(CONFIG_KEYBOARD_TWL4030
) && pdata
->keypad
) {
626 child
= add_child(TWL4030_MODULE_KEYPAD
, "twl4030_keypad",
627 pdata
->keypad
, sizeof(*pdata
->keypad
),
628 true, irq_base
+ KEYPAD_INTR_OFFSET
, 0);
630 return PTR_ERR(child
);
633 if (IS_ENABLED(CONFIG_TWL4030_MADC
) && pdata
->madc
&&
634 twl_class_is_4030()) {
635 child
= add_child(TWL4030_MODULE_MADC
, "twl4030_madc",
636 pdata
->madc
, sizeof(*pdata
->madc
),
637 true, irq_base
+ MADC_INTR_OFFSET
, 0);
639 return PTR_ERR(child
);
642 if (IS_ENABLED(CONFIG_RTC_DRV_TWL4030
)) {
644 * REVISIT platform_data here currently might expose the
645 * "msecure" line ... but for now we just expect board
646 * setup to tell the chip "it's always ok to SET_TIME".
647 * Eventually, Linux might become more aware of such
648 * HW security concerns, and "least privilege".
650 child
= add_child(TWL_MODULE_RTC
, "twl_rtc", NULL
, 0,
651 true, irq_base
+ RTC_INTR_OFFSET
, 0);
653 return PTR_ERR(child
);
656 if (IS_ENABLED(CONFIG_PWM_TWL
)) {
657 child
= add_child(TWL_MODULE_PWM
, "twl-pwm", NULL
, 0,
660 return PTR_ERR(child
);
663 if (IS_ENABLED(CONFIG_PWM_TWL_LED
)) {
664 child
= add_child(TWL_MODULE_LED
, "twl-pwmled", NULL
, 0,
667 return PTR_ERR(child
);
670 if (IS_ENABLED(CONFIG_TWL4030_USB
) && pdata
->usb
&&
671 twl_class_is_4030()) {
673 static struct regulator_consumer_supply usb1v5
= {
676 static struct regulator_consumer_supply usb1v8
= {
679 static struct regulator_consumer_supply usb3v1
[] = {
680 { .supply
= "usb3v1" },
681 { .supply
= "bci3v1" },
684 /* First add the regulators so that they can be used by transceiver */
685 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
)) {
686 /* this is a template that gets copied */
687 struct regulator_init_data usb_fixed
= {
688 .constraints
.valid_modes_mask
=
689 REGULATOR_MODE_NORMAL
690 | REGULATOR_MODE_STANDBY
,
691 .constraints
.valid_ops_mask
=
692 REGULATOR_CHANGE_MODE
693 | REGULATOR_CHANGE_STATUS
,
696 child
= add_regulator_linked(TWL4030_REG_VUSB1V5
,
697 &usb_fixed
, &usb1v5
, 1,
700 return PTR_ERR(child
);
702 child
= add_regulator_linked(TWL4030_REG_VUSB1V8
,
703 &usb_fixed
, &usb1v8
, 1,
706 return PTR_ERR(child
);
708 child
= add_regulator_linked(TWL4030_REG_VUSB3V1
,
709 &usb_fixed
, usb3v1
, 2,
712 return PTR_ERR(child
);
716 child
= add_child(TWL_MODULE_USB
, "twl4030_usb",
717 pdata
->usb
, sizeof(*pdata
->usb
), true,
718 /* irq0 = USB_PRES, irq1 = USB */
719 irq_base
+ USB_PRES_INTR_OFFSET
,
720 irq_base
+ USB_INTR_OFFSET
);
723 return PTR_ERR(child
);
725 /* we need to connect regulators to this transceiver */
726 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && child
) {
727 usb1v5
.dev_name
= dev_name(child
);
728 usb1v8
.dev_name
= dev_name(child
);
729 usb3v1
[0].dev_name
= dev_name(child
);
732 if (IS_ENABLED(CONFIG_TWL6030_USB
) && pdata
->usb
&&
733 twl_class_is_6030()) {
735 static struct regulator_consumer_supply usb3v3
;
738 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
)) {
739 /* this is a template that gets copied */
740 struct regulator_init_data usb_fixed
= {
741 .constraints
.valid_modes_mask
=
742 REGULATOR_MODE_NORMAL
743 | REGULATOR_MODE_STANDBY
,
744 .constraints
.valid_ops_mask
=
745 REGULATOR_CHANGE_MODE
746 | REGULATOR_CHANGE_STATUS
,
749 if (features
& TWL6025_SUBCLASS
) {
750 usb3v3
.supply
= "ldousb";
751 regulator
= TWL6025_REG_LDOUSB
;
753 usb3v3
.supply
= "vusb";
754 regulator
= TWL6030_REG_VUSB
;
756 child
= add_regulator_linked(regulator
, &usb_fixed
,
760 return PTR_ERR(child
);
763 pdata
->usb
->features
= features
;
765 child
= add_child(TWL_MODULE_USB
, "twl6030_usb",
766 pdata
->usb
, sizeof(*pdata
->usb
), true,
767 /* irq1 = VBUS_PRES, irq0 = USB ID */
768 irq_base
+ USBOTG_INTR_OFFSET
,
769 irq_base
+ USB_PRES_INTR_OFFSET
);
772 return PTR_ERR(child
);
773 /* we need to connect regulators to this transceiver */
774 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && child
)
775 usb3v3
.dev_name
= dev_name(child
);
776 } else if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) &&
777 twl_class_is_6030()) {
778 if (features
& TWL6025_SUBCLASS
)
779 child
= add_regulator(TWL6025_REG_LDOUSB
,
780 pdata
->ldousb
, features
);
782 child
= add_regulator(TWL6030_REG_VUSB
,
783 pdata
->vusb
, features
);
786 return PTR_ERR(child
);
789 if (IS_ENABLED(CONFIG_TWL4030_WATCHDOG
) && twl_class_is_4030()) {
790 child
= add_child(TWL_MODULE_PM_RECEIVER
, "twl4030_wdt", NULL
,
793 return PTR_ERR(child
);
796 if (IS_ENABLED(CONFIG_INPUT_TWL4030_PWRBUTTON
) && twl_class_is_4030()) {
797 child
= add_child(TWL_MODULE_PM_MASTER
, "twl4030_pwrbutton",
798 NULL
, 0, true, irq_base
+ 8 + 0, 0);
800 return PTR_ERR(child
);
803 if (IS_ENABLED(CONFIG_MFD_TWL4030_AUDIO
) && pdata
->audio
&&
804 twl_class_is_4030()) {
805 child
= add_child(TWL4030_MODULE_AUDIO_VOICE
, "twl4030-audio",
806 pdata
->audio
, sizeof(*pdata
->audio
),
809 return PTR_ERR(child
);
812 /* twl4030 regulators */
813 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && twl_class_is_4030()) {
814 child
= add_regulator(TWL4030_REG_VPLL1
, pdata
->vpll1
,
817 return PTR_ERR(child
);
819 child
= add_regulator(TWL4030_REG_VIO
, pdata
->vio
,
822 return PTR_ERR(child
);
824 child
= add_regulator(TWL4030_REG_VDD1
, pdata
->vdd1
,
827 return PTR_ERR(child
);
829 child
= add_regulator(TWL4030_REG_VDD2
, pdata
->vdd2
,
832 return PTR_ERR(child
);
834 child
= add_regulator(TWL4030_REG_VMMC1
, pdata
->vmmc1
,
837 return PTR_ERR(child
);
839 child
= add_regulator(TWL4030_REG_VDAC
, pdata
->vdac
,
842 return PTR_ERR(child
);
844 child
= add_regulator((features
& TWL4030_VAUX2
)
845 ? TWL4030_REG_VAUX2_4030
847 pdata
->vaux2
, features
);
849 return PTR_ERR(child
);
851 child
= add_regulator(TWL4030_REG_VINTANA1
, pdata
->vintana1
,
854 return PTR_ERR(child
);
856 child
= add_regulator(TWL4030_REG_VINTANA2
, pdata
->vintana2
,
859 return PTR_ERR(child
);
861 child
= add_regulator(TWL4030_REG_VINTDIG
, pdata
->vintdig
,
864 return PTR_ERR(child
);
867 /* maybe add LDOs that are omitted on cost-reduced parts */
868 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && !(features
& TPS_SUBSET
)
869 && twl_class_is_4030()) {
870 child
= add_regulator(TWL4030_REG_VPLL2
, pdata
->vpll2
,
873 return PTR_ERR(child
);
875 child
= add_regulator(TWL4030_REG_VMMC2
, pdata
->vmmc2
,
878 return PTR_ERR(child
);
880 child
= add_regulator(TWL4030_REG_VSIM
, pdata
->vsim
,
883 return PTR_ERR(child
);
885 child
= add_regulator(TWL4030_REG_VAUX1
, pdata
->vaux1
,
888 return PTR_ERR(child
);
890 child
= add_regulator(TWL4030_REG_VAUX3
, pdata
->vaux3
,
893 return PTR_ERR(child
);
895 child
= add_regulator(TWL4030_REG_VAUX4
, pdata
->vaux4
,
898 return PTR_ERR(child
);
901 /* twl6030 regulators */
902 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && twl_class_is_6030() &&
903 !(features
& TWL6025_SUBCLASS
)) {
904 child
= add_regulator(TWL6030_REG_VDD1
, pdata
->vdd1
,
907 return PTR_ERR(child
);
909 child
= add_regulator(TWL6030_REG_VDD2
, pdata
->vdd2
,
912 return PTR_ERR(child
);
914 child
= add_regulator(TWL6030_REG_VDD3
, pdata
->vdd3
,
917 return PTR_ERR(child
);
919 child
= add_regulator(TWL6030_REG_V1V8
, pdata
->v1v8
,
922 return PTR_ERR(child
);
924 child
= add_regulator(TWL6030_REG_V2V1
, pdata
->v2v1
,
927 return PTR_ERR(child
);
929 child
= add_regulator(TWL6030_REG_VMMC
, pdata
->vmmc
,
932 return PTR_ERR(child
);
934 child
= add_regulator(TWL6030_REG_VPP
, pdata
->vpp
,
937 return PTR_ERR(child
);
939 child
= add_regulator(TWL6030_REG_VUSIM
, pdata
->vusim
,
942 return PTR_ERR(child
);
944 child
= add_regulator(TWL6030_REG_VCXIO
, pdata
->vcxio
,
947 return PTR_ERR(child
);
949 child
= add_regulator(TWL6030_REG_VDAC
, pdata
->vdac
,
952 return PTR_ERR(child
);
954 child
= add_regulator(TWL6030_REG_VAUX1_6030
, pdata
->vaux1
,
957 return PTR_ERR(child
);
959 child
= add_regulator(TWL6030_REG_VAUX2_6030
, pdata
->vaux2
,
962 return PTR_ERR(child
);
964 child
= add_regulator(TWL6030_REG_VAUX3_6030
, pdata
->vaux3
,
967 return PTR_ERR(child
);
969 child
= add_regulator(TWL6030_REG_CLK32KG
, pdata
->clk32kg
,
972 return PTR_ERR(child
);
975 /* 6030 and 6025 share this regulator */
976 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && twl_class_is_6030()) {
977 child
= add_regulator(TWL6030_REG_VANA
, pdata
->vana
,
980 return PTR_ERR(child
);
983 /* twl6025 regulators */
984 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030
) && twl_class_is_6030() &&
985 (features
& TWL6025_SUBCLASS
)) {
986 child
= add_regulator(TWL6025_REG_LDO5
, pdata
->ldo5
,
989 return PTR_ERR(child
);
991 child
= add_regulator(TWL6025_REG_LDO1
, pdata
->ldo1
,
994 return PTR_ERR(child
);
996 child
= add_regulator(TWL6025_REG_LDO7
, pdata
->ldo7
,
999 return PTR_ERR(child
);
1001 child
= add_regulator(TWL6025_REG_LDO6
, pdata
->ldo6
,
1004 return PTR_ERR(child
);
1006 child
= add_regulator(TWL6025_REG_LDOLN
, pdata
->ldoln
,
1009 return PTR_ERR(child
);
1011 child
= add_regulator(TWL6025_REG_LDO2
, pdata
->ldo2
,
1014 return PTR_ERR(child
);
1016 child
= add_regulator(TWL6025_REG_LDO4
, pdata
->ldo4
,
1019 return PTR_ERR(child
);
1021 child
= add_regulator(TWL6025_REG_LDO3
, pdata
->ldo3
,
1024 return PTR_ERR(child
);
1026 child
= add_regulator(TWL6025_REG_SMPS3
, pdata
->smps3
,
1029 return PTR_ERR(child
);
1031 child
= add_regulator(TWL6025_REG_SMPS4
, pdata
->smps4
,
1034 return PTR_ERR(child
);
1036 child
= add_regulator(TWL6025_REG_VIO
, pdata
->vio6025
,
1039 return PTR_ERR(child
);
1043 if (IS_ENABLED(CONFIG_CHARGER_TWL4030
) && pdata
->bci
&&
1044 !(features
& (TPS_SUBSET
| TWL5031
))) {
1045 child
= add_child(TWL_MODULE_MAIN_CHARGE
, "twl4030_bci",
1046 pdata
->bci
, sizeof(*pdata
->bci
), false,
1047 /* irq0 = CHG_PRES, irq1 = BCI */
1048 irq_base
+ BCI_PRES_INTR_OFFSET
,
1049 irq_base
+ BCI_INTR_OFFSET
);
1051 return PTR_ERR(child
);
1057 /*----------------------------------------------------------------------*/
1060 * These three functions initialize the on-chip clock framework,
1061 * letting it generate the right frequencies for USB, MADC, and
1064 static inline int __init
protect_pm_master(void)
1068 e
= twl_i2c_write_u8(TWL_MODULE_PM_MASTER
, 0,
1069 TWL4030_PM_MASTER_PROTECT_KEY
);
1073 static inline int __init
unprotect_pm_master(void)
1077 e
|= twl_i2c_write_u8(TWL_MODULE_PM_MASTER
, TWL4030_PM_MASTER_KEY_CFG1
,
1078 TWL4030_PM_MASTER_PROTECT_KEY
);
1079 e
|= twl_i2c_write_u8(TWL_MODULE_PM_MASTER
, TWL4030_PM_MASTER_KEY_CFG2
,
1080 TWL4030_PM_MASTER_PROTECT_KEY
);
1085 static void clocks_init(struct device
*dev
,
1086 struct twl4030_clock_init_data
*clock
)
1091 u8 ctrl
= HFCLK_FREQ_26_MHZ
;
1093 osc
= clk_get(dev
, "fck");
1095 printk(KERN_WARNING
"Skipping twl internal clock init and "
1096 "using bootloader value (unknown osc rate)\n");
1100 rate
= clk_get_rate(osc
);
1105 ctrl
= HFCLK_FREQ_19p2_MHZ
;
1108 ctrl
= HFCLK_FREQ_26_MHZ
;
1111 ctrl
= HFCLK_FREQ_38p4_MHZ
;
1115 ctrl
|= HIGH_PERF_SQ
;
1116 if (clock
&& clock
->ck32k_lowpwr_enable
)
1117 ctrl
|= CK32K_LOWPWR_EN
;
1119 e
|= unprotect_pm_master();
1120 /* effect->MADC+USB ck en */
1121 e
|= twl_i2c_write_u8(TWL_MODULE_PM_MASTER
, ctrl
, R_CFG_BOOT
);
1122 e
|= protect_pm_master();
1125 pr_err("%s: clock init err [%d]\n", DRIVER_NAME
, e
);
1128 /*----------------------------------------------------------------------*/
1131 static int twl_remove(struct i2c_client
*client
)
1133 unsigned i
, num_slaves
;
1136 if (twl_class_is_4030())
1137 status
= twl4030_exit_irq();
1139 status
= twl6030_exit_irq();
1144 num_slaves
= twl_get_num_slaves();
1145 for (i
= 0; i
< num_slaves
; i
++) {
1146 struct twl_client
*twl
= &twl_modules
[i
];
1148 if (twl
->client
&& twl
->client
!= client
)
1149 i2c_unregister_device(twl
->client
);
1150 twl_modules
[i
].client
= NULL
;
1156 /* NOTE: This driver only handles a single twl4030/tps659x0 chip */
1158 twl_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1160 struct twl4030_platform_data
*pdata
= client
->dev
.platform_data
;
1161 struct device_node
*node
= client
->dev
.of_node
;
1162 struct platform_device
*pdev
;
1163 struct regmap_config
*twl_regmap_config
;
1166 unsigned i
, num_slaves
;
1168 if (!node
&& !pdata
) {
1169 dev_err(&client
->dev
, "no platform data\n");
1174 dev_dbg(&client
->dev
, "only one instance of %s allowed\n",
1179 pdev
= platform_device_alloc(DRIVER_NAME
, -1);
1181 dev_err(&client
->dev
, "can't alloc pdev\n");
1185 status
= platform_device_add(pdev
);
1187 platform_device_put(pdev
);
1191 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
) == 0) {
1192 dev_dbg(&client
->dev
, "can't talk I2C?\n");
1197 if ((id
->driver_data
) & TWL6030_CLASS
) {
1198 twl_id
= TWL6030_CLASS_ID
;
1199 twl_map
= &twl6030_map
[0];
1200 /* The charger base address is different in twl6025 */
1201 if ((id
->driver_data
) & TWL6025_SUBCLASS
)
1202 twl_map
[TWL_MODULE_MAIN_CHARGE
].base
=
1203 TWL6025_BASEADD_CHARGER
;
1204 twl_regmap_config
= twl6030_regmap_config
;
1206 twl_id
= TWL4030_CLASS_ID
;
1207 twl_map
= &twl4030_map
[0];
1208 twl_regmap_config
= twl4030_regmap_config
;
1211 num_slaves
= twl_get_num_slaves();
1212 twl_modules
= devm_kzalloc(&client
->dev
,
1213 sizeof(struct twl_client
) * num_slaves
,
1220 for (i
= 0; i
< num_slaves
; i
++) {
1221 struct twl_client
*twl
= &twl_modules
[i
];
1224 twl
->client
= client
;
1226 twl
->client
= i2c_new_dummy(client
->adapter
,
1229 dev_err(&client
->dev
,
1230 "can't attach client %d\n", i
);
1236 twl
->regmap
= devm_regmap_init_i2c(twl
->client
,
1237 &twl_regmap_config
[i
]);
1238 if (IS_ERR(twl
->regmap
)) {
1239 status
= PTR_ERR(twl
->regmap
);
1240 dev_err(&client
->dev
,
1241 "Failed to allocate regmap %d, err: %d\n", i
,
1249 /* setup clock framework */
1250 clocks_init(&pdev
->dev
, pdata
? pdata
->clock
: NULL
);
1252 /* read TWL IDCODE Register */
1253 if (twl_id
== TWL4030_CLASS_ID
) {
1254 status
= twl_read_idcode_register();
1255 WARN(status
< 0, "Error: reading twl_idcode register value\n");
1258 /* load power event scripts */
1259 if (IS_ENABLED(CONFIG_TWL4030_POWER
) && pdata
&& pdata
->power
)
1260 twl4030_power_init(pdata
->power
);
1262 /* Maybe init the T2 Interrupt subsystem */
1264 if (twl_class_is_4030()) {
1265 twl4030_init_chip_irq(id
->name
);
1266 irq_base
= twl4030_init_irq(&client
->dev
, client
->irq
);
1268 irq_base
= twl6030_init_irq(&client
->dev
, client
->irq
);
1278 * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
1279 * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1280 * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1282 if (twl_class_is_4030()) {
1285 twl_i2c_read_u8(TWL4030_MODULE_INTBR
, &temp
, REG_GPPUPDCTR1
);
1286 temp
&= ~(SR_I2C_SDA_CTRL_PU
| SR_I2C_SCL_CTRL_PU
| \
1287 I2C_SDA_CTRL_PU
| I2C_SCL_CTRL_PU
);
1288 twl_i2c_write_u8(TWL4030_MODULE_INTBR
, temp
, REG_GPPUPDCTR1
);
1292 status
= of_platform_populate(node
, NULL
, NULL
, &client
->dev
);
1294 status
= add_children(pdata
, irq_base
, id
->driver_data
);
1301 platform_device_unregister(pdev
);
1306 static const struct i2c_device_id twl_ids
[] = {
1307 { "twl4030", TWL4030_VAUX2
}, /* "Triton 2" */
1308 { "twl5030", 0 }, /* T2 updated */
1309 { "twl5031", TWL5031
}, /* TWL5030 updated */
1310 { "tps65950", 0 }, /* catalog version of twl5030 */
1311 { "tps65930", TPS_SUBSET
}, /* fewer LDOs and DACs; no charger */
1312 { "tps65920", TPS_SUBSET
}, /* fewer LDOs; no codec or charger */
1313 { "tps65921", TPS_SUBSET
}, /* fewer LDOs; no codec, no LED
1314 and vibrator. Charger in USB module*/
1315 { "twl6030", TWL6030_CLASS
}, /* "Phoenix power chip" */
1316 { "twl6025", TWL6030_CLASS
| TWL6025_SUBCLASS
}, /* "Phoenix lite" */
1317 { /* end of list */ },
1319 MODULE_DEVICE_TABLE(i2c
, twl_ids
);
1321 /* One Client Driver , 4 Clients */
1322 static struct i2c_driver twl_driver
= {
1323 .driver
.name
= DRIVER_NAME
,
1324 .id_table
= twl_ids
,
1326 .remove
= twl_remove
,
1329 static int __init
twl_init(void)
1331 return i2c_add_driver(&twl_driver
);
1333 subsys_initcall(twl_init
);
1335 static void __exit
twl_exit(void)
1337 i2c_del_driver(&twl_driver
);
1339 module_exit(twl_exit
);
1341 MODULE_AUTHOR("Texas Instruments, Inc.");
1342 MODULE_DESCRIPTION("I2C Core interface for TWL");
1343 MODULE_LICENSE("GPL");