2 * linux/drivers/i2c/chips/twl4030-power.c
4 * Handle TWL4030 Power initialization
6 * Copyright (C) 2008 Nokia Corporation
7 * Copyright (C) 2006 Texas Instruments, Inc
9 * Written by Kalle Jokiniemi
10 * Peter De Schrijver <peter.de-schrijver@nokia.com>
11 * Several fixes by Amit Kucheria <amit.kucheria@verdurent.com>
13 * This file is subject to the terms and conditions of the GNU General
14 * Public License. See the file "COPYING" in the main directory of this
15 * archive for more details.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/module.h>
29 #include <linux/i2c/twl4030.h>
30 #include <linux/platform_device.h>
32 #include <asm/mach-types.h>
34 static u8 twl4030_start_script_address
= 0x2b;
36 #define PWR_P1_SW_EVENTS 0x10
37 #define PWR_DEVOFF (1<<0)
39 #define PHY_TO_OFF_PM_MASTER(p) (p - 0x36)
40 #define PHY_TO_OFF_PM_RECEIVER(p) (p - 0x5b)
42 /* resource - hfclk */
43 #define R_HFCLKOUT_DEV_GRP PHY_TO_OFF_PM_RECEIVER(0xe6)
46 #define R_P1_SW_EVENTS PHY_TO_OFF_PM_MASTER(0x46)
47 #define R_P2_SW_EVENTS PHY_TO_OFF_PM_MASTER(0x47)
48 #define R_P3_SW_EVENTS PHY_TO_OFF_PM_MASTER(0x48)
49 #define R_CFG_P1_TRANSITION PHY_TO_OFF_PM_MASTER(0x36)
50 #define R_CFG_P2_TRANSITION PHY_TO_OFF_PM_MASTER(0x37)
51 #define R_CFG_P3_TRANSITION PHY_TO_OFF_PM_MASTER(0x38)
53 #define LVL_WAKEUP 0x08
55 #define ENABLE_WARMRESET (1<<4)
57 #define END_OF_SCRIPT 0x3f
59 #define R_SEQ_ADD_A2S PHY_TO_OFF_PM_MASTER(0x55)
60 #define R_SEQ_ADD_S2A12 PHY_TO_OFF_PM_MASTER(0x56)
61 #define R_SEQ_ADD_S2A3 PHY_TO_OFF_PM_MASTER(0x57)
62 #define R_SEQ_ADD_WARM PHY_TO_OFF_PM_MASTER(0x58)
63 #define R_MEMORY_ADDRESS PHY_TO_OFF_PM_MASTER(0x59)
64 #define R_MEMORY_DATA PHY_TO_OFF_PM_MASTER(0x5a)
66 #define R_PROTECT_KEY 0x0E
70 /* resource configuration registers */
72 #define DEVGROUP_OFFSET 0
76 #define DEVGROUP_SHIFT 5
77 #define DEVGROUP_MASK (7 << DEVGROUP_SHIFT)
79 #define TYPE_MASK (7 << TYPE_SHIFT)
81 #define TYPE2_MASK (3 << TYPE2_SHIFT)
83 static u8 res_config_addrs
[] = {
94 [RES_VINTANA1
] = 0x3f,
95 [RES_VINTANA2
] = 0x43,
100 [RES_VUSB_1V5
] = 0x71,
101 [RES_VUSB_1V8
] = 0x74,
102 [RES_VUSB_3V1
] = 0x77,
105 [RES_NRES_PWRON
] = 0x82,
108 [RES_HFCLKOUT
] = 0x8b,
109 [RES_32KCLKOUT
] = 0x8e,
111 [RES_Main_Ref
] = 0x94,
114 static int __init
twl4030_write_script_byte(u8 address
, u8 byte
)
118 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, address
,
122 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, byte
,
128 static int __init
twl4030_write_script_ins(u8 address
, u16 pmb_message
,
134 err
= twl4030_write_script_byte(address
++, pmb_message
>> 8);
137 err
= twl4030_write_script_byte(address
++, pmb_message
& 0xff);
140 err
= twl4030_write_script_byte(address
++, delay
);
143 err
= twl4030_write_script_byte(address
++, next
);
148 static int __init
twl4030_write_script(u8 address
, struct twl4030_ins
*script
,
153 for (; len
; len
--, address
++, script
++) {
155 err
= twl4030_write_script_ins(address
,
162 err
= twl4030_write_script_ins(address
,
173 static int __init
twl4030_config_wakeup3_sequence(u8 address
)
178 /* Set SLEEP to ACTIVE SEQ address for P3 */
179 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, address
,
184 /* P3 LVL_WAKEUP should be on LEVEL */
185 err
= twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER
, &data
,
190 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, data
,
194 pr_err("TWL4030 wakeup sequence for P3 config error\n");
198 static int __init
twl4030_config_wakeup12_sequence(u8 address
)
203 /* Set SLEEP to ACTIVE SEQ address for P1 and P2 */
204 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, address
,
209 /* P1/P2 LVL_WAKEUP should be on LEVEL */
210 err
= twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER
, &data
,
216 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, data
,
221 err
= twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER
, &data
,
227 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, data
,
232 if (machine_is_omap_3430sdp() || machine_is_omap_ldp()) {
233 /* Disabling AC charger effect on sleep-active transitions */
234 err
= twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER
, &data
,
235 R_CFG_P1_TRANSITION
);
239 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, data
,
240 R_CFG_P1_TRANSITION
);
247 pr_err("TWL4030 wakeup sequence for P1 and P2" \
252 static int __init
twl4030_config_sleep_sequence(u8 address
)
256 /* Set ACTIVE to SLEEP SEQ address in T2 memory*/
257 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, address
,
261 pr_err("TWL4030 sleep sequence config error\n");
266 static int __init
twl4030_config_warmreset_sequence(u8 address
)
271 /* Set WARM RESET SEQ address for P1 */
272 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, address
,
277 /* P1/P2/P3 enable WARMRESET */
278 err
= twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER
, &rd_data
,
283 rd_data
|= ENABLE_WARMRESET
;
284 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, rd_data
,
289 err
= twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER
, &rd_data
,
294 rd_data
|= ENABLE_WARMRESET
;
295 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, rd_data
,
300 err
= twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER
, &rd_data
,
305 rd_data
|= ENABLE_WARMRESET
;
306 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, rd_data
,
310 pr_err("TWL4030 warmreset seq config error\n");
314 static int __init
twl4030_configure_resource(struct twl4030_resconfig
*rconfig
)
321 if (rconfig
->resource
> TOTAL_RESOURCES
) {
322 pr_err("TWL4030 Resource %d does not exist\n",
327 rconfig_addr
= res_config_addrs
[rconfig
->resource
];
329 /* Set resource group */
330 err
= twl4030_i2c_read_u8(TWL4030_MODULE_PM_RECEIVER
, &grp
,
331 rconfig_addr
+ DEVGROUP_OFFSET
);
333 pr_err("TWL4030 Resource %d group could not be read\n",
338 if (rconfig
->devgroup
>= 0) {
339 grp
&= ~DEVGROUP_MASK
;
340 grp
|= rconfig
->devgroup
<< DEVGROUP_SHIFT
;
341 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER
,
342 grp
, rconfig_addr
+ DEVGROUP_OFFSET
);
344 pr_err("TWL4030 failed to program devgroup\n");
349 /* Set resource types */
350 err
= twl4030_i2c_read_u8(TWL4030_MODULE_PM_RECEIVER
, &type
,
351 rconfig_addr
+ TYPE_OFFSET
);
353 pr_err("TWL4030 Resource %d type could not be read\n",
358 if (rconfig
->type
>= 0) {
360 type
|= rconfig
->type
<< TYPE_SHIFT
;
363 if (rconfig
->type2
>= 0) {
365 type
|= rconfig
->type2
<< TYPE2_SHIFT
;
368 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER
,
369 type
, rconfig_addr
+ TYPE_OFFSET
);
371 pr_err("TWL4030 failed to program resource type\n");
378 static int __init
load_twl4030_script(struct twl4030_script
*tscript
,
384 /* Make sure the script isn't going beyond last valid address (0x3f) */
385 if ((address
+ tscript
->size
) > END_OF_SCRIPT
) {
386 pr_err("TWL4030 scripts too big error\n");
390 err
= twl4030_write_script(address
, tscript
->script
, tscript
->size
);
394 if (tscript
->flags
& TWL4030_WRST_SCRIPT
) {
395 err
= twl4030_config_warmreset_sequence(address
);
399 if (tscript
->flags
& TWL4030_WAKEUP12_SCRIPT
) {
400 err
= twl4030_config_wakeup12_sequence(address
);
405 if (tscript
->flags
& TWL4030_WAKEUP3_SCRIPT
) {
406 err
= twl4030_config_wakeup3_sequence(address
);
410 if (tscript
->flags
& TWL4030_SLEEP_SCRIPT
)
412 pr_warning("TWL4030: Bad order of scripts (sleep "\
413 "script before wakeup) Leads to boot"\
414 "failure on some boards\n");
415 err
= twl4030_config_sleep_sequence(address
);
420 void __init
twl4030_power_init(struct twl4030_power_data
*twl4030_scripts
)
424 struct twl4030_resconfig
*resconfig
;
425 u8 address
= twl4030_start_script_address
;
427 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, R_KEY_1
,
432 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, R_KEY_2
,
437 for (i
= 0; i
< twl4030_scripts
->num
; i
++) {
438 err
= load_twl4030_script(twl4030_scripts
->scripts
[i
], address
);
441 address
+= twl4030_scripts
->scripts
[i
]->size
;
444 resconfig
= twl4030_scripts
->resource_config
;
446 while (resconfig
->resource
) {
447 err
= twl4030_configure_resource(resconfig
);
455 err
= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER
, 0, R_PROTECT_KEY
);
457 pr_err("TWL4030 Unable to relock registers\n");
462 pr_err("TWL4030 Unable to unlock registers\n");
466 pr_err("TWL4030 failed to load scripts\n");
470 pr_err("TWL4030 failed to configure resource\n");