twl4030-power.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * linux/drivers/i2c/chips/twl4030-power.c
  3. *
  4. * Handle TWL4030 Power initialization
  5. *
  6. * Copyright (C) 2008 Nokia Corporation
  7. * Copyright (C) 2006 Texas Instruments, Inc
  8. *
  9. * Written by Kalle Jokiniemi
  10. * Peter De Schrijver <peter.de-schrijver@nokia.com>
  11. * Several fixes by Amit Kucheria <amit.kucheria@verdurent.com>
  12. *
  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.
  16. *
  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.
  21. *
  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
  25. */
  26. #include <linux/module.h>
  27. #include <linux/pm.h>
  28. #include <linux/i2c/twl4030.h>
  29. #include <linux/platform_device.h>
  30. #include <asm/mach-types.h>
  31. static u8 twl4030_start_script_address = 0x2b;
  32. #define PWR_P1_SW_EVENTS 0x10
  33. #define PWR_DEVOFF (1<<0)
  34. #define PHY_TO_OFF_PM_MASTER(p) (p - 0x36)
  35. #define PHY_TO_OFF_PM_RECEIVER(p) (p - 0x5b)
  36. /* resource - hfclk */
  37. #define R_HFCLKOUT_DEV_GRP PHY_TO_OFF_PM_RECEIVER(0xe6)
  38. /* PM events */
  39. #define R_P1_SW_EVENTS PHY_TO_OFF_PM_MASTER(0x46)
  40. #define R_P2_SW_EVENTS PHY_TO_OFF_PM_MASTER(0x47)
  41. #define R_P3_SW_EVENTS PHY_TO_OFF_PM_MASTER(0x48)
  42. #define R_CFG_P1_TRANSITION PHY_TO_OFF_PM_MASTER(0x36)
  43. #define R_CFG_P2_TRANSITION PHY_TO_OFF_PM_MASTER(0x37)
  44. #define R_CFG_P3_TRANSITION PHY_TO_OFF_PM_MASTER(0x38)
  45. #define LVL_WAKEUP 0x08
  46. #define ENABLE_WARMRESET (1<<4)
  47. #define END_OF_SCRIPT 0x3f
  48. #define R_SEQ_ADD_A2S PHY_TO_OFF_PM_MASTER(0x55)
  49. #define R_SEQ_ADD_S2A12 PHY_TO_OFF_PM_MASTER(0x56)
  50. #define R_SEQ_ADD_S2A3 PHY_TO_OFF_PM_MASTER(0x57)
  51. #define R_SEQ_ADD_WARM PHY_TO_OFF_PM_MASTER(0x58)
  52. #define R_MEMORY_ADDRESS PHY_TO_OFF_PM_MASTER(0x59)
  53. #define R_MEMORY_DATA PHY_TO_OFF_PM_MASTER(0x5a)
  54. #define R_PROTECT_KEY 0x0E
  55. #define R_KEY_1 0xC0
  56. #define R_KEY_2 0x0C
  57. /* resource configuration registers
  58. <RESOURCE>_DEV_GRP at address 'n+0'
  59. <RESOURCE>_TYPE at address 'n+1'
  60. <RESOURCE>_REMAP at address 'n+2'
  61. <RESOURCE>_DEDICATED at address 'n+3'
  62. */
  63. #define DEV_GRP_OFFSET 0
  64. #define TYPE_OFFSET 1
  65. /* Bit positions in the registers */
  66. /* <RESOURCE>_DEV_GRP */
  67. #define DEV_GRP_SHIFT 5
  68. #define DEV_GRP_MASK (7 << DEV_GRP_SHIFT)
  69. /* <RESOURCE>_TYPE */
  70. #define TYPE_SHIFT 0
  71. #define TYPE_MASK (7 << TYPE_SHIFT)
  72. #define TYPE2_SHIFT 3
  73. #define TYPE2_MASK (3 << TYPE2_SHIFT)
  74. static u8 res_config_addrs[] = {
  75. [RES_VAUX1] = 0x17,
  76. [RES_VAUX2] = 0x1b,
  77. [RES_VAUX3] = 0x1f,
  78. [RES_VAUX4] = 0x23,
  79. [RES_VMMC1] = 0x27,
  80. [RES_VMMC2] = 0x2b,
  81. [RES_VPLL1] = 0x2f,
  82. [RES_VPLL2] = 0x33,
  83. [RES_VSIM] = 0x37,
  84. [RES_VDAC] = 0x3b,
  85. [RES_VINTANA1] = 0x3f,
  86. [RES_VINTANA2] = 0x43,
  87. [RES_VINTDIG] = 0x47,
  88. [RES_VIO] = 0x4b,
  89. [RES_VDD1] = 0x55,
  90. [RES_VDD2] = 0x63,
  91. [RES_VUSB_1V5] = 0x71,
  92. [RES_VUSB_1V8] = 0x74,
  93. [RES_VUSB_3V1] = 0x77,
  94. [RES_VUSBCP] = 0x7a,
  95. [RES_REGEN] = 0x7f,
  96. [RES_NRES_PWRON] = 0x82,
  97. [RES_CLKEN] = 0x85,
  98. [RES_SYSEN] = 0x88,
  99. [RES_HFCLKOUT] = 0x8b,
  100. [RES_32KCLKOUT] = 0x8e,
  101. [RES_RESET] = 0x91,
  102. [RES_Main_Ref] = 0x94,
  103. };
  104. static int __init twl4030_write_script_byte(u8 address, u8 byte)
  105. {
  106. int err;
  107. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, address,
  108. R_MEMORY_ADDRESS);
  109. if (err)
  110. goto out;
  111. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, byte,
  112. R_MEMORY_DATA);
  113. out:
  114. return err;
  115. }
  116. static int __init twl4030_write_script_ins(u8 address, u16 pmb_message,
  117. u8 delay, u8 next)
  118. {
  119. int err;
  120. address *= 4;
  121. err = twl4030_write_script_byte(address++, pmb_message >> 8);
  122. if (err)
  123. goto out;
  124. err = twl4030_write_script_byte(address++, pmb_message & 0xff);
  125. if (err)
  126. goto out;
  127. err = twl4030_write_script_byte(address++, delay);
  128. if (err)
  129. goto out;
  130. err = twl4030_write_script_byte(address++, next);
  131. out:
  132. return err;
  133. }
  134. static int __init twl4030_write_script(u8 address, struct twl4030_ins *script,
  135. int len)
  136. {
  137. int err;
  138. for (; len; len--, address++, script++) {
  139. if (len == 1) {
  140. err = twl4030_write_script_ins(address,
  141. script->pmb_message,
  142. script->delay,
  143. END_OF_SCRIPT);
  144. if (err)
  145. break;
  146. } else {
  147. err = twl4030_write_script_ins(address,
  148. script->pmb_message,
  149. script->delay,
  150. address + 1);
  151. if (err)
  152. break;
  153. }
  154. }
  155. return err;
  156. }
  157. static int __init twl4030_config_wakeup3_sequence(u8 address)
  158. {
  159. int err;
  160. u8 data;
  161. /* Set SLEEP to ACTIVE SEQ address for P3 */
  162. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, address,
  163. R_SEQ_ADD_S2A3);
  164. if (err)
  165. goto out;
  166. /* P3 LVL_WAKEUP should be on LEVEL */
  167. err = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &data,
  168. R_P3_SW_EVENTS);
  169. if (err)
  170. goto out;
  171. data |= LVL_WAKEUP;
  172. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, data,
  173. R_P3_SW_EVENTS);
  174. out:
  175. if (err)
  176. pr_err("TWL4030 wakeup sequence for P3 config error\n");
  177. return err;
  178. }
  179. static int __init twl4030_config_wakeup12_sequence(u8 address)
  180. {
  181. int err = 0;
  182. u8 data;
  183. /* Set SLEEP to ACTIVE SEQ address for P1 and P2 */
  184. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, address,
  185. R_SEQ_ADD_S2A12);
  186. if (err)
  187. goto out;
  188. /* P1/P2 LVL_WAKEUP should be on LEVEL */
  189. err = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &data,
  190. R_P1_SW_EVENTS);
  191. if (err)
  192. goto out;
  193. data |= LVL_WAKEUP;
  194. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, data,
  195. R_P1_SW_EVENTS);
  196. if (err)
  197. goto out;
  198. err = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &data,
  199. R_P2_SW_EVENTS);
  200. if (err)
  201. goto out;
  202. data |= LVL_WAKEUP;
  203. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, data,
  204. R_P2_SW_EVENTS);
  205. if (err)
  206. goto out;
  207. if (machine_is_omap_3430sdp() || machine_is_omap_ldp()) {
  208. /* Disabling AC charger effect on sleep-active transitions */
  209. err = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &data,
  210. R_CFG_P1_TRANSITION);
  211. if (err)
  212. goto out;
  213. data &= ~(1<<1);
  214. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, data ,
  215. R_CFG_P1_TRANSITION);
  216. if (err)
  217. goto out;
  218. }
  219. out:
  220. if (err)
  221. pr_err("TWL4030 wakeup sequence for P1 and P2" \
  222. "config error\n");
  223. return err;
  224. }
  225. static int __init twl4030_config_sleep_sequence(u8 address)
  226. {
  227. int err;
  228. /* Set ACTIVE to SLEEP SEQ address in T2 memory*/
  229. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, address,
  230. R_SEQ_ADD_A2S);
  231. if (err)
  232. pr_err("TWL4030 sleep sequence config error\n");
  233. return err;
  234. }
  235. static int __init twl4030_config_warmreset_sequence(u8 address)
  236. {
  237. int err;
  238. u8 rd_data;
  239. /* Set WARM RESET SEQ address for P1 */
  240. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, address,
  241. R_SEQ_ADD_WARM);
  242. if (err)
  243. goto out;
  244. /* P1/P2/P3 enable WARMRESET */
  245. err = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &rd_data,
  246. R_P1_SW_EVENTS);
  247. if (err)
  248. goto out;
  249. rd_data |= ENABLE_WARMRESET;
  250. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, rd_data,
  251. R_P1_SW_EVENTS);
  252. if (err)
  253. goto out;
  254. err = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &rd_data,
  255. R_P2_SW_EVENTS);
  256. if (err)
  257. goto out;
  258. rd_data |= ENABLE_WARMRESET;
  259. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, rd_data,
  260. R_P2_SW_EVENTS);
  261. if (err)
  262. goto out;
  263. err = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &rd_data,
  264. R_P3_SW_EVENTS);
  265. if (err)
  266. goto out;
  267. rd_data |= ENABLE_WARMRESET;
  268. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, rd_data,
  269. R_P3_SW_EVENTS);
  270. out:
  271. if (err)
  272. pr_err("TWL4030 warmreset seq config error\n");
  273. return err;
  274. }
  275. static int __init twl4030_configure_resource(struct twl4030_resconfig *rconfig)
  276. {
  277. int rconfig_addr;
  278. int err;
  279. u8 type;
  280. u8 grp;
  281. if (rconfig->resource > TOTAL_RESOURCES) {
  282. pr_err("TWL4030 Resource %d does not exist\n",
  283. rconfig->resource);
  284. return -EINVAL;
  285. }
  286. rconfig_addr = res_config_addrs[rconfig->resource];
  287. /* Set resource group */
  288. err = twl4030_i2c_read_u8(TWL4030_MODULE_PM_RECEIVER, &grp,
  289. rconfig_addr + DEV_GRP_OFFSET);
  290. if (err) {
  291. pr_err("TWL4030 Resource %d group could not be read\n",
  292. rconfig->resource);
  293. return err;
  294. }
  295. if (rconfig->devgroup >= 0) {
  296. grp &= ~DEV_GRP_MASK;
  297. grp |= rconfig->devgroup << DEV_GRP_SHIFT;
  298. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
  299. grp, rconfig_addr + DEV_GRP_OFFSET);
  300. if (err < 0) {
  301. pr_err("TWL4030 failed to program devgroup\n");
  302. return err;
  303. }
  304. }
  305. /* Set resource types */
  306. err = twl4030_i2c_read_u8(TWL4030_MODULE_PM_RECEIVER, &type,
  307. rconfig_addr + TYPE_OFFSET);
  308. if (err < 0) {
  309. pr_err("TWL4030 Resource %d type could not be read\n",
  310. rconfig->resource);
  311. return err;
  312. }
  313. if (rconfig->type >= 0) {
  314. type &= ~TYPE_MASK;
  315. type |= rconfig->type << TYPE_SHIFT;
  316. }
  317. if (rconfig->type2 >= 0) {
  318. type &= ~TYPE2_MASK;
  319. type |= rconfig->type2 << TYPE2_SHIFT;
  320. }
  321. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
  322. type, rconfig_addr + TYPE_OFFSET);
  323. if (err < 0) {
  324. pr_err("TWL4030 failed to program resource type\n");
  325. return err;
  326. }
  327. return 0;
  328. }
  329. static int __init load_twl4030_script(struct twl4030_script *tscript,
  330. u8 address)
  331. {
  332. int err;
  333. static int order;
  334. /* Make sure the script isn't going beyond last valid address (0x3f) */
  335. if ((address + tscript->size) > END_OF_SCRIPT) {
  336. pr_err("TWL4030 scripts too big error\n");
  337. return -EINVAL;
  338. }
  339. err = twl4030_write_script(address, tscript->script, tscript->size);
  340. if (err)
  341. goto out;
  342. if (tscript->flags & TWL4030_WRST_SCRIPT) {
  343. err = twl4030_config_warmreset_sequence(address);
  344. if (err)
  345. goto out;
  346. }
  347. if (tscript->flags & TWL4030_WAKEUP12_SCRIPT) {
  348. err = twl4030_config_wakeup12_sequence(address);
  349. if (err)
  350. goto out;
  351. order = 1;
  352. }
  353. if (tscript->flags & TWL4030_WAKEUP3_SCRIPT) {
  354. err = twl4030_config_wakeup3_sequence(address);
  355. if (err)
  356. goto out;
  357. }
  358. if (tscript->flags & TWL4030_SLEEP_SCRIPT)
  359. if (order)
  360. pr_warning("TWL4030: Bad order of scripts (sleep "\
  361. "script before wakeup) Leads to boot"\
  362. "failure on some boards\n");
  363. err = twl4030_config_sleep_sequence(address);
  364. out:
  365. return err;
  366. }
  367. void __init twl4030_power_init(struct twl4030_power_data *twl4030_scripts)
  368. {
  369. int err = 0;
  370. int i;
  371. struct twl4030_resconfig *resconfig;
  372. u8 address = twl4030_start_script_address;
  373. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, R_KEY_1,
  374. R_PROTECT_KEY);
  375. if (err)
  376. goto unlock;
  377. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, R_KEY_2,
  378. R_PROTECT_KEY);
  379. if (err)
  380. goto unlock;
  381. for (i = 0; i < twl4030_scripts->num; i++) {
  382. err = load_twl4030_script(twl4030_scripts->scripts[i], address);
  383. if (err)
  384. goto load;
  385. address += twl4030_scripts->scripts[i]->size;
  386. }
  387. resconfig = twl4030_scripts->resource_config;
  388. if (resconfig) {
  389. while (resconfig->resource) {
  390. err = twl4030_configure_resource(resconfig);
  391. if (err)
  392. goto resource;
  393. resconfig++;
  394. }
  395. }
  396. err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0, R_PROTECT_KEY);
  397. if (err)
  398. pr_err("TWL4030 Unable to relock registers\n");
  399. return;
  400. unlock:
  401. if (err)
  402. pr_err("TWL4030 Unable to unlock registers\n");
  403. return;
  404. load:
  405. if (err)
  406. pr_err("TWL4030 failed to load scripts\n");
  407. return;
  408. resource:
  409. if (err)
  410. pr_err("TWL4030 failed to configure resource\n");
  411. return;
  412. }