twl4030-power.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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/twl.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/of.h>
  31. #include <linux/of_device.h>
  32. #include <asm/mach-types.h>
  33. static u8 twl4030_start_script_address = 0x2b;
  34. /* Register bits for P1, P2 and P3_SW_EVENTS */
  35. #define PWR_STOPON_PRWON BIT(6)
  36. #define PWR_STOPON_SYSEN BIT(5)
  37. #define PWR_ENABLE_WARMRESET BIT(4)
  38. #define PWR_LVL_WAKEUP BIT(3)
  39. #define PWR_DEVACT BIT(2)
  40. #define PWR_DEVSLP BIT(1)
  41. #define PWR_DEVOFF BIT(0)
  42. /* Register bits for CFG_P1_TRANSITION (also for P2 and P3) */
  43. #define STARTON_SWBUG BIT(7) /* Start on watchdog */
  44. #define STARTON_VBUS BIT(5) /* Start on VBUS */
  45. #define STARTON_VBAT BIT(4) /* Start on battery insert */
  46. #define STARTON_RTC BIT(3) /* Start on RTC */
  47. #define STARTON_USB BIT(2) /* Start on USB host */
  48. #define STARTON_CHG BIT(1) /* Start on charger */
  49. #define STARTON_PWON BIT(0) /* Start on PWRON button */
  50. #define SEQ_OFFSYNC (1 << 0)
  51. #define PHY_TO_OFF_PM_MASTER(p) (p - 0x36)
  52. #define PHY_TO_OFF_PM_RECEIVER(p) (p - 0x5b)
  53. /* resource - hfclk */
  54. #define R_HFCLKOUT_DEV_GRP PHY_TO_OFF_PM_RECEIVER(0xe6)
  55. /* PM events */
  56. #define R_P1_SW_EVENTS PHY_TO_OFF_PM_MASTER(0x46)
  57. #define R_P2_SW_EVENTS PHY_TO_OFF_PM_MASTER(0x47)
  58. #define R_P3_SW_EVENTS PHY_TO_OFF_PM_MASTER(0x48)
  59. #define R_CFG_P1_TRANSITION PHY_TO_OFF_PM_MASTER(0x36)
  60. #define R_CFG_P2_TRANSITION PHY_TO_OFF_PM_MASTER(0x37)
  61. #define R_CFG_P3_TRANSITION PHY_TO_OFF_PM_MASTER(0x38)
  62. #define END_OF_SCRIPT 0x3f
  63. #define R_SEQ_ADD_A2S PHY_TO_OFF_PM_MASTER(0x55)
  64. #define R_SEQ_ADD_S2A12 PHY_TO_OFF_PM_MASTER(0x56)
  65. #define R_SEQ_ADD_S2A3 PHY_TO_OFF_PM_MASTER(0x57)
  66. #define R_SEQ_ADD_WARM PHY_TO_OFF_PM_MASTER(0x58)
  67. #define R_MEMORY_ADDRESS PHY_TO_OFF_PM_MASTER(0x59)
  68. #define R_MEMORY_DATA PHY_TO_OFF_PM_MASTER(0x5a)
  69. /* resource configuration registers
  70. <RESOURCE>_DEV_GRP at address 'n+0'
  71. <RESOURCE>_TYPE at address 'n+1'
  72. <RESOURCE>_REMAP at address 'n+2'
  73. <RESOURCE>_DEDICATED at address 'n+3'
  74. */
  75. #define DEV_GRP_OFFSET 0
  76. #define TYPE_OFFSET 1
  77. #define REMAP_OFFSET 2
  78. #define DEDICATED_OFFSET 3
  79. /* Bit positions in the registers */
  80. /* <RESOURCE>_DEV_GRP */
  81. #define DEV_GRP_SHIFT 5
  82. #define DEV_GRP_MASK (7 << DEV_GRP_SHIFT)
  83. /* <RESOURCE>_TYPE */
  84. #define TYPE_SHIFT 0
  85. #define TYPE_MASK (7 << TYPE_SHIFT)
  86. #define TYPE2_SHIFT 3
  87. #define TYPE2_MASK (3 << TYPE2_SHIFT)
  88. /* <RESOURCE>_REMAP */
  89. #define SLEEP_STATE_SHIFT 0
  90. #define SLEEP_STATE_MASK (0xf << SLEEP_STATE_SHIFT)
  91. #define OFF_STATE_SHIFT 4
  92. #define OFF_STATE_MASK (0xf << OFF_STATE_SHIFT)
  93. static u8 res_config_addrs[] = {
  94. [RES_VAUX1] = 0x17,
  95. [RES_VAUX2] = 0x1b,
  96. [RES_VAUX3] = 0x1f,
  97. [RES_VAUX4] = 0x23,
  98. [RES_VMMC1] = 0x27,
  99. [RES_VMMC2] = 0x2b,
  100. [RES_VPLL1] = 0x2f,
  101. [RES_VPLL2] = 0x33,
  102. [RES_VSIM] = 0x37,
  103. [RES_VDAC] = 0x3b,
  104. [RES_VINTANA1] = 0x3f,
  105. [RES_VINTANA2] = 0x43,
  106. [RES_VINTDIG] = 0x47,
  107. [RES_VIO] = 0x4b,
  108. [RES_VDD1] = 0x55,
  109. [RES_VDD2] = 0x63,
  110. [RES_VUSB_1V5] = 0x71,
  111. [RES_VUSB_1V8] = 0x74,
  112. [RES_VUSB_3V1] = 0x77,
  113. [RES_VUSBCP] = 0x7a,
  114. [RES_REGEN] = 0x7f,
  115. [RES_NRES_PWRON] = 0x82,
  116. [RES_CLKEN] = 0x85,
  117. [RES_SYSEN] = 0x88,
  118. [RES_HFCLKOUT] = 0x8b,
  119. [RES_32KCLKOUT] = 0x8e,
  120. [RES_RESET] = 0x91,
  121. [RES_MAIN_REF] = 0x94,
  122. };
  123. /*
  124. * Usable values for .remap_sleep and .remap_off
  125. * Based on table "5.3.3 Resource Operating modes"
  126. */
  127. enum {
  128. TWL_REMAP_OFF = 0,
  129. TWL_REMAP_SLEEP = 8,
  130. TWL_REMAP_ACTIVE = 9,
  131. };
  132. /*
  133. * Macros to configure the PM register states for various resources.
  134. * Note that we can make MSG_SINGULAR etc private to this driver once
  135. * omap3 has been made DT only.
  136. */
  137. #define TWL_DFLT_DELAY 2 /* typically 2 32 KiHz cycles */
  138. #define TWL_DEV_GRP_P123 (DEV_GRP_P1 | DEV_GRP_P2 | DEV_GRP_P3)
  139. #define TWL_RESOURCE_SET(res, state) \
  140. { MSG_SINGULAR(DEV_GRP_NULL, (res), (state)), TWL_DFLT_DELAY }
  141. #define TWL_RESOURCE_ON(res) TWL_RESOURCE_SET(res, RES_STATE_ACTIVE)
  142. #define TWL_RESOURCE_OFF(res) TWL_RESOURCE_SET(res, RES_STATE_OFF)
  143. #define TWL_RESOURCE_RESET(res) TWL_RESOURCE_SET(res, RES_STATE_WRST)
  144. /*
  145. * It seems that type1 and type2 is just the resource init order
  146. * number for the type1 and type2 group.
  147. */
  148. #define TWL_RESOURCE_SET_ACTIVE(res, state) \
  149. { MSG_SINGULAR(DEV_GRP_NULL, (res), RES_STATE_ACTIVE), (state) }
  150. #define TWL_RESOURCE_GROUP_RESET(group, type1, type2) \
  151. { MSG_BROADCAST(DEV_GRP_NULL, (group), (type1), (type2), \
  152. RES_STATE_WRST), TWL_DFLT_DELAY }
  153. #define TWL_RESOURCE_GROUP_SLEEP(group, type, type2) \
  154. { MSG_BROADCAST(DEV_GRP_NULL, (group), (type), (type2), \
  155. RES_STATE_SLEEP), TWL_DFLT_DELAY }
  156. #define TWL_RESOURCE_GROUP_ACTIVE(group, type, type2) \
  157. { MSG_BROADCAST(DEV_GRP_NULL, (group), (type), (type2), \
  158. RES_STATE_ACTIVE), TWL_DFLT_DELAY }
  159. #define TWL_REMAP_SLEEP(res, devgrp, typ, typ2) \
  160. { .resource = (res), .devgroup = (devgrp), \
  161. .type = (typ), .type2 = (typ2), \
  162. .remap_off = TWL_REMAP_OFF, \
  163. .remap_sleep = TWL_REMAP_SLEEP, }
  164. #define TWL_REMAP_OFF(res, devgrp, typ, typ2) \
  165. { .resource = (res), .devgroup = (devgrp), \
  166. .type = (typ), .type2 = (typ2), \
  167. .remap_off = TWL_REMAP_OFF, .remap_sleep = TWL_REMAP_OFF, }
  168. static int twl4030_write_script_byte(u8 address, u8 byte)
  169. {
  170. int err;
  171. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_MEMORY_ADDRESS);
  172. if (err)
  173. goto out;
  174. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, byte, R_MEMORY_DATA);
  175. out:
  176. return err;
  177. }
  178. static int twl4030_write_script_ins(u8 address, u16 pmb_message,
  179. u8 delay, u8 next)
  180. {
  181. int err;
  182. address *= 4;
  183. err = twl4030_write_script_byte(address++, pmb_message >> 8);
  184. if (err)
  185. goto out;
  186. err = twl4030_write_script_byte(address++, pmb_message & 0xff);
  187. if (err)
  188. goto out;
  189. err = twl4030_write_script_byte(address++, delay);
  190. if (err)
  191. goto out;
  192. err = twl4030_write_script_byte(address++, next);
  193. out:
  194. return err;
  195. }
  196. static int twl4030_write_script(u8 address, struct twl4030_ins *script,
  197. int len)
  198. {
  199. int err = -EINVAL;
  200. for (; len; len--, address++, script++) {
  201. if (len == 1) {
  202. err = twl4030_write_script_ins(address,
  203. script->pmb_message,
  204. script->delay,
  205. END_OF_SCRIPT);
  206. if (err)
  207. break;
  208. } else {
  209. err = twl4030_write_script_ins(address,
  210. script->pmb_message,
  211. script->delay,
  212. address + 1);
  213. if (err)
  214. break;
  215. }
  216. }
  217. return err;
  218. }
  219. static int twl4030_config_wakeup3_sequence(u8 address)
  220. {
  221. int err;
  222. u8 data;
  223. /* Set SLEEP to ACTIVE SEQ address for P3 */
  224. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_S2A3);
  225. if (err)
  226. goto out;
  227. /* P3 LVL_WAKEUP should be on LEVEL */
  228. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data, R_P3_SW_EVENTS);
  229. if (err)
  230. goto out;
  231. data |= PWR_LVL_WAKEUP;
  232. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P3_SW_EVENTS);
  233. out:
  234. if (err)
  235. pr_err("TWL4030 wakeup sequence for P3 config error\n");
  236. return err;
  237. }
  238. static int twl4030_config_wakeup12_sequence(u8 address)
  239. {
  240. int err = 0;
  241. u8 data;
  242. /* Set SLEEP to ACTIVE SEQ address for P1 and P2 */
  243. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_S2A12);
  244. if (err)
  245. goto out;
  246. /* P1/P2 LVL_WAKEUP should be on LEVEL */
  247. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data, R_P1_SW_EVENTS);
  248. if (err)
  249. goto out;
  250. data |= PWR_LVL_WAKEUP;
  251. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P1_SW_EVENTS);
  252. if (err)
  253. goto out;
  254. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data, R_P2_SW_EVENTS);
  255. if (err)
  256. goto out;
  257. data |= PWR_LVL_WAKEUP;
  258. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P2_SW_EVENTS);
  259. if (err)
  260. goto out;
  261. if (machine_is_omap_3430sdp() || machine_is_omap_ldp()) {
  262. /* Disabling AC charger effect on sleep-active transitions */
  263. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data,
  264. R_CFG_P1_TRANSITION);
  265. if (err)
  266. goto out;
  267. data &= ~(1<<1);
  268. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data,
  269. R_CFG_P1_TRANSITION);
  270. if (err)
  271. goto out;
  272. }
  273. out:
  274. if (err)
  275. pr_err("TWL4030 wakeup sequence for P1 and P2" \
  276. "config error\n");
  277. return err;
  278. }
  279. static int twl4030_config_sleep_sequence(u8 address)
  280. {
  281. int err;
  282. /* Set ACTIVE to SLEEP SEQ address in T2 memory*/
  283. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_A2S);
  284. if (err)
  285. pr_err("TWL4030 sleep sequence config error\n");
  286. return err;
  287. }
  288. static int twl4030_config_warmreset_sequence(u8 address)
  289. {
  290. int err;
  291. u8 rd_data;
  292. /* Set WARM RESET SEQ address for P1 */
  293. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_WARM);
  294. if (err)
  295. goto out;
  296. /* P1/P2/P3 enable WARMRESET */
  297. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &rd_data, R_P1_SW_EVENTS);
  298. if (err)
  299. goto out;
  300. rd_data |= PWR_ENABLE_WARMRESET;
  301. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P1_SW_EVENTS);
  302. if (err)
  303. goto out;
  304. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &rd_data, R_P2_SW_EVENTS);
  305. if (err)
  306. goto out;
  307. rd_data |= PWR_ENABLE_WARMRESET;
  308. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P2_SW_EVENTS);
  309. if (err)
  310. goto out;
  311. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &rd_data, R_P3_SW_EVENTS);
  312. if (err)
  313. goto out;
  314. rd_data |= PWR_ENABLE_WARMRESET;
  315. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P3_SW_EVENTS);
  316. out:
  317. if (err)
  318. pr_err("TWL4030 warmreset seq config error\n");
  319. return err;
  320. }
  321. static int twl4030_configure_resource(struct twl4030_resconfig *rconfig)
  322. {
  323. int rconfig_addr;
  324. int err;
  325. u8 type;
  326. u8 grp;
  327. u8 remap;
  328. if (rconfig->resource > TOTAL_RESOURCES) {
  329. pr_err("TWL4030 Resource %d does not exist\n",
  330. rconfig->resource);
  331. return -EINVAL;
  332. }
  333. rconfig_addr = res_config_addrs[rconfig->resource];
  334. /* Set resource group */
  335. err = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &grp,
  336. rconfig_addr + DEV_GRP_OFFSET);
  337. if (err) {
  338. pr_err("TWL4030 Resource %d group could not be read\n",
  339. rconfig->resource);
  340. return err;
  341. }
  342. if (rconfig->devgroup != TWL4030_RESCONFIG_UNDEF) {
  343. grp &= ~DEV_GRP_MASK;
  344. grp |= rconfig->devgroup << DEV_GRP_SHIFT;
  345. err = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
  346. grp, rconfig_addr + DEV_GRP_OFFSET);
  347. if (err < 0) {
  348. pr_err("TWL4030 failed to program devgroup\n");
  349. return err;
  350. }
  351. }
  352. /* Set resource types */
  353. err = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &type,
  354. rconfig_addr + TYPE_OFFSET);
  355. if (err < 0) {
  356. pr_err("TWL4030 Resource %d type could not be read\n",
  357. rconfig->resource);
  358. return err;
  359. }
  360. if (rconfig->type != TWL4030_RESCONFIG_UNDEF) {
  361. type &= ~TYPE_MASK;
  362. type |= rconfig->type << TYPE_SHIFT;
  363. }
  364. if (rconfig->type2 != TWL4030_RESCONFIG_UNDEF) {
  365. type &= ~TYPE2_MASK;
  366. type |= rconfig->type2 << TYPE2_SHIFT;
  367. }
  368. err = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
  369. type, rconfig_addr + TYPE_OFFSET);
  370. if (err < 0) {
  371. pr_err("TWL4030 failed to program resource type\n");
  372. return err;
  373. }
  374. /* Set remap states */
  375. err = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &remap,
  376. rconfig_addr + REMAP_OFFSET);
  377. if (err < 0) {
  378. pr_err("TWL4030 Resource %d remap could not be read\n",
  379. rconfig->resource);
  380. return err;
  381. }
  382. if (rconfig->remap_off != TWL4030_RESCONFIG_UNDEF) {
  383. remap &= ~OFF_STATE_MASK;
  384. remap |= rconfig->remap_off << OFF_STATE_SHIFT;
  385. }
  386. if (rconfig->remap_sleep != TWL4030_RESCONFIG_UNDEF) {
  387. remap &= ~SLEEP_STATE_MASK;
  388. remap |= rconfig->remap_sleep << SLEEP_STATE_SHIFT;
  389. }
  390. err = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
  391. remap,
  392. rconfig_addr + REMAP_OFFSET);
  393. if (err < 0) {
  394. pr_err("TWL4030 failed to program remap\n");
  395. return err;
  396. }
  397. return 0;
  398. }
  399. static int load_twl4030_script(struct twl4030_script *tscript,
  400. u8 address)
  401. {
  402. int err;
  403. static int order;
  404. /* Make sure the script isn't going beyond last valid address (0x3f) */
  405. if ((address + tscript->size) > END_OF_SCRIPT) {
  406. pr_err("TWL4030 scripts too big error\n");
  407. return -EINVAL;
  408. }
  409. err = twl4030_write_script(address, tscript->script, tscript->size);
  410. if (err)
  411. goto out;
  412. if (tscript->flags & TWL4030_WRST_SCRIPT) {
  413. err = twl4030_config_warmreset_sequence(address);
  414. if (err)
  415. goto out;
  416. }
  417. if (tscript->flags & TWL4030_WAKEUP12_SCRIPT) {
  418. /* Reset any existing sleep script to avoid hangs on reboot */
  419. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
  420. R_SEQ_ADD_A2S);
  421. if (err)
  422. goto out;
  423. err = twl4030_config_wakeup12_sequence(address);
  424. if (err)
  425. goto out;
  426. order = 1;
  427. }
  428. if (tscript->flags & TWL4030_WAKEUP3_SCRIPT) {
  429. err = twl4030_config_wakeup3_sequence(address);
  430. if (err)
  431. goto out;
  432. }
  433. if (tscript->flags & TWL4030_SLEEP_SCRIPT) {
  434. if (!order)
  435. pr_warning("TWL4030: Bad order of scripts (sleep "\
  436. "script before wakeup) Leads to boot"\
  437. "failure on some boards\n");
  438. err = twl4030_config_sleep_sequence(address);
  439. }
  440. out:
  441. return err;
  442. }
  443. int twl4030_remove_script(u8 flags)
  444. {
  445. int err = 0;
  446. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
  447. TWL4030_PM_MASTER_PROTECT_KEY);
  448. if (err) {
  449. pr_err("twl4030: unable to unlock PROTECT_KEY\n");
  450. return err;
  451. }
  452. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG2,
  453. TWL4030_PM_MASTER_PROTECT_KEY);
  454. if (err) {
  455. pr_err("twl4030: unable to unlock PROTECT_KEY\n");
  456. return err;
  457. }
  458. if (flags & TWL4030_WRST_SCRIPT) {
  459. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
  460. R_SEQ_ADD_WARM);
  461. if (err)
  462. return err;
  463. }
  464. if (flags & TWL4030_WAKEUP12_SCRIPT) {
  465. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
  466. R_SEQ_ADD_S2A12);
  467. if (err)
  468. return err;
  469. }
  470. if (flags & TWL4030_WAKEUP3_SCRIPT) {
  471. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
  472. R_SEQ_ADD_S2A3);
  473. if (err)
  474. return err;
  475. }
  476. if (flags & TWL4030_SLEEP_SCRIPT) {
  477. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
  478. R_SEQ_ADD_A2S);
  479. if (err)
  480. return err;
  481. }
  482. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
  483. TWL4030_PM_MASTER_PROTECT_KEY);
  484. if (err)
  485. pr_err("TWL4030 Unable to relock registers\n");
  486. return err;
  487. }
  488. static int
  489. twl4030_power_configure_scripts(const struct twl4030_power_data *pdata)
  490. {
  491. int err;
  492. int i;
  493. u8 address = twl4030_start_script_address;
  494. for (i = 0; i < pdata->num; i++) {
  495. err = load_twl4030_script(pdata->scripts[i], address);
  496. if (err)
  497. return err;
  498. address += pdata->scripts[i]->size;
  499. }
  500. return 0;
  501. }
  502. static void twl4030_patch_rconfig(struct twl4030_resconfig *common,
  503. struct twl4030_resconfig *board)
  504. {
  505. while (common->resource) {
  506. struct twl4030_resconfig *b = board;
  507. while (b->resource) {
  508. if (b->resource == common->resource) {
  509. *common = *b;
  510. break;
  511. }
  512. b++;
  513. }
  514. common++;
  515. }
  516. }
  517. static int
  518. twl4030_power_configure_resources(const struct twl4030_power_data *pdata)
  519. {
  520. struct twl4030_resconfig *resconfig = pdata->resource_config;
  521. struct twl4030_resconfig *boardconf = pdata->board_config;
  522. int err;
  523. if (resconfig) {
  524. if (boardconf)
  525. twl4030_patch_rconfig(resconfig, boardconf);
  526. while (resconfig->resource) {
  527. err = twl4030_configure_resource(resconfig);
  528. if (err)
  529. return err;
  530. resconfig++;
  531. }
  532. }
  533. return 0;
  534. }
  535. static int twl4030_starton_mask_and_set(u8 bitmask, u8 bitvalues)
  536. {
  537. u8 regs[3] = { TWL4030_PM_MASTER_CFG_P1_TRANSITION,
  538. TWL4030_PM_MASTER_CFG_P2_TRANSITION,
  539. TWL4030_PM_MASTER_CFG_P3_TRANSITION, };
  540. u8 val;
  541. int i, err;
  542. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
  543. TWL4030_PM_MASTER_PROTECT_KEY);
  544. if (err)
  545. goto relock;
  546. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
  547. TWL4030_PM_MASTER_KEY_CFG2,
  548. TWL4030_PM_MASTER_PROTECT_KEY);
  549. if (err)
  550. goto relock;
  551. for (i = 0; i < sizeof(regs); i++) {
  552. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER,
  553. &val, regs[i]);
  554. if (err)
  555. break;
  556. val = (~bitmask & val) | (bitmask & bitvalues);
  557. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
  558. val, regs[i]);
  559. if (err)
  560. break;
  561. }
  562. if (err)
  563. pr_err("TWL4030 Register access failed: %i\n", err);
  564. relock:
  565. return twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
  566. TWL4030_PM_MASTER_PROTECT_KEY);
  567. }
  568. /*
  569. * In master mode, start the power off sequence.
  570. * After a successful execution, TWL shuts down the power to the SoC
  571. * and all peripherals connected to it.
  572. */
  573. void twl4030_power_off(void)
  574. {
  575. int err;
  576. /* Disable start on charger or VBUS as it can break poweroff */
  577. err = twl4030_starton_mask_and_set(STARTON_VBUS | STARTON_CHG, 0);
  578. if (err)
  579. pr_err("TWL4030 Unable to configure start-up\n");
  580. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, PWR_DEVOFF,
  581. TWL4030_PM_MASTER_P1_SW_EVENTS);
  582. if (err)
  583. pr_err("TWL4030 Unable to power off\n");
  584. }
  585. static bool twl4030_power_use_poweroff(const struct twl4030_power_data *pdata,
  586. struct device_node *node)
  587. {
  588. if (pdata && pdata->use_poweroff)
  589. return true;
  590. if (of_property_read_bool(node, "ti,system-power-controller"))
  591. return true;
  592. if (of_property_read_bool(node, "ti,use_poweroff"))
  593. return true;
  594. return false;
  595. }
  596. #ifdef CONFIG_OF
  597. /* Generic warm reset configuration for omap3 */
  598. static struct twl4030_ins omap3_wrst_seq[] = {
  599. TWL_RESOURCE_OFF(RES_NRES_PWRON),
  600. TWL_RESOURCE_OFF(RES_RESET),
  601. TWL_RESOURCE_RESET(RES_MAIN_REF),
  602. TWL_RESOURCE_GROUP_RESET(RES_GRP_ALL, RES_TYPE_R0, RES_TYPE2_R2),
  603. TWL_RESOURCE_RESET(RES_VUSB_3V1),
  604. TWL_RESOURCE_GROUP_RESET(RES_GRP_ALL, RES_TYPE_R0, RES_TYPE2_R1),
  605. TWL_RESOURCE_GROUP_RESET(RES_GRP_RC, RES_TYPE_ALL, RES_TYPE2_R0),
  606. TWL_RESOURCE_ON(RES_RESET),
  607. TWL_RESOURCE_ON(RES_NRES_PWRON),
  608. };
  609. static struct twl4030_script omap3_wrst_script = {
  610. .script = omap3_wrst_seq,
  611. .size = ARRAY_SIZE(omap3_wrst_seq),
  612. .flags = TWL4030_WRST_SCRIPT,
  613. };
  614. static struct twl4030_script *omap3_reset_scripts[] = {
  615. &omap3_wrst_script,
  616. };
  617. static struct twl4030_resconfig omap3_rconfig[] = {
  618. TWL_REMAP_SLEEP(RES_HFCLKOUT, DEV_GRP_P3, -1, -1),
  619. TWL_REMAP_SLEEP(RES_VDD1, DEV_GRP_P1, -1, -1),
  620. TWL_REMAP_SLEEP(RES_VDD2, DEV_GRP_P1, -1, -1),
  621. { 0, 0 },
  622. };
  623. static struct twl4030_power_data omap3_reset = {
  624. .scripts = omap3_reset_scripts,
  625. .num = ARRAY_SIZE(omap3_reset_scripts),
  626. .resource_config = omap3_rconfig,
  627. };
  628. /* Recommended generic default idle configuration for off-idle */
  629. /* Broadcast message to put res to sleep */
  630. static struct twl4030_ins omap3_idle_sleep_on_seq[] = {
  631. TWL_RESOURCE_GROUP_SLEEP(RES_GRP_ALL, RES_TYPE_ALL, 0),
  632. };
  633. static struct twl4030_script omap3_idle_sleep_on_script = {
  634. .script = omap3_idle_sleep_on_seq,
  635. .size = ARRAY_SIZE(omap3_idle_sleep_on_seq),
  636. .flags = TWL4030_SLEEP_SCRIPT,
  637. };
  638. /* Broadcast message to put res to active */
  639. static struct twl4030_ins omap3_idle_wakeup_p12_seq[] = {
  640. TWL_RESOURCE_GROUP_ACTIVE(RES_GRP_ALL, RES_TYPE_ALL, 0),
  641. };
  642. static struct twl4030_script omap3_idle_wakeup_p12_script = {
  643. .script = omap3_idle_wakeup_p12_seq,
  644. .size = ARRAY_SIZE(omap3_idle_wakeup_p12_seq),
  645. .flags = TWL4030_WAKEUP12_SCRIPT,
  646. };
  647. /* Broadcast message to put res to active */
  648. static struct twl4030_ins omap3_idle_wakeup_p3_seq[] = {
  649. TWL_RESOURCE_SET_ACTIVE(RES_CLKEN, 0x37),
  650. TWL_RESOURCE_GROUP_ACTIVE(RES_GRP_ALL, RES_TYPE_ALL, 0),
  651. };
  652. static struct twl4030_script omap3_idle_wakeup_p3_script = {
  653. .script = omap3_idle_wakeup_p3_seq,
  654. .size = ARRAY_SIZE(omap3_idle_wakeup_p3_seq),
  655. .flags = TWL4030_WAKEUP3_SCRIPT,
  656. };
  657. static struct twl4030_script *omap3_idle_scripts[] = {
  658. &omap3_idle_wakeup_p12_script,
  659. &omap3_idle_wakeup_p3_script,
  660. &omap3_wrst_script,
  661. &omap3_idle_sleep_on_script,
  662. };
  663. /*
  664. * Recommended configuration based on "Recommended Sleep
  665. * Sequences for the Zoom Platform":
  666. * http://omappedia.com/wiki/File:Recommended_Sleep_Sequences_Zoom.pdf
  667. * Note that the type1 and type2 seem to be just the init order number
  668. * for type1 and type2 groups as specified in the document mentioned
  669. * above.
  670. */
  671. static struct twl4030_resconfig omap3_idle_rconfig[] = {
  672. TWL_REMAP_SLEEP(RES_VAUX1, TWL4030_RESCONFIG_UNDEF, 0, 0),
  673. TWL_REMAP_SLEEP(RES_VAUX2, TWL4030_RESCONFIG_UNDEF, 0, 0),
  674. TWL_REMAP_SLEEP(RES_VAUX3, TWL4030_RESCONFIG_UNDEF, 0, 0),
  675. TWL_REMAP_SLEEP(RES_VAUX4, TWL4030_RESCONFIG_UNDEF, 0, 0),
  676. TWL_REMAP_SLEEP(RES_VMMC1, TWL4030_RESCONFIG_UNDEF, 0, 0),
  677. TWL_REMAP_SLEEP(RES_VMMC2, TWL4030_RESCONFIG_UNDEF, 0, 0),
  678. TWL_REMAP_OFF(RES_VPLL1, DEV_GRP_P1, 3, 1),
  679. TWL_REMAP_SLEEP(RES_VPLL2, DEV_GRP_P1, 0, 0),
  680. TWL_REMAP_SLEEP(RES_VSIM, TWL4030_RESCONFIG_UNDEF, 0, 0),
  681. TWL_REMAP_SLEEP(RES_VDAC, TWL4030_RESCONFIG_UNDEF, 0, 0),
  682. TWL_REMAP_SLEEP(RES_VINTANA1, TWL_DEV_GRP_P123, 1, 2),
  683. TWL_REMAP_SLEEP(RES_VINTANA2, TWL_DEV_GRP_P123, 0, 2),
  684. TWL_REMAP_SLEEP(RES_VINTDIG, TWL_DEV_GRP_P123, 1, 2),
  685. TWL_REMAP_SLEEP(RES_VIO, TWL_DEV_GRP_P123, 2, 2),
  686. TWL_REMAP_OFF(RES_VDD1, DEV_GRP_P1, 4, 1),
  687. TWL_REMAP_OFF(RES_VDD2, DEV_GRP_P1, 3, 1),
  688. TWL_REMAP_SLEEP(RES_VUSB_1V5, TWL4030_RESCONFIG_UNDEF, 0, 0),
  689. TWL_REMAP_SLEEP(RES_VUSB_1V8, TWL4030_RESCONFIG_UNDEF, 0, 0),
  690. TWL_REMAP_SLEEP(RES_VUSB_3V1, TWL_DEV_GRP_P123, 0, 0),
  691. /* Resource #20 USB charge pump skipped */
  692. TWL_REMAP_SLEEP(RES_REGEN, TWL_DEV_GRP_P123, 2, 1),
  693. TWL_REMAP_SLEEP(RES_NRES_PWRON, TWL_DEV_GRP_P123, 0, 1),
  694. TWL_REMAP_SLEEP(RES_CLKEN, TWL_DEV_GRP_P123, 3, 2),
  695. TWL_REMAP_SLEEP(RES_SYSEN, TWL_DEV_GRP_P123, 6, 1),
  696. TWL_REMAP_SLEEP(RES_HFCLKOUT, DEV_GRP_P3, 0, 2),
  697. TWL_REMAP_SLEEP(RES_32KCLKOUT, TWL_DEV_GRP_P123, 0, 0),
  698. TWL_REMAP_SLEEP(RES_RESET, TWL_DEV_GRP_P123, 6, 0),
  699. TWL_REMAP_SLEEP(RES_MAIN_REF, TWL_DEV_GRP_P123, 0, 0),
  700. { /* Terminator */ },
  701. };
  702. static struct twl4030_power_data omap3_idle = {
  703. .scripts = omap3_idle_scripts,
  704. .num = ARRAY_SIZE(omap3_idle_scripts),
  705. .resource_config = omap3_idle_rconfig,
  706. };
  707. /* Disable 32 KiHz oscillator during idle */
  708. static struct twl4030_resconfig osc_off_rconfig[] = {
  709. TWL_REMAP_OFF(RES_CLKEN, DEV_GRP_P1 | DEV_GRP_P3, 3, 2),
  710. { /* Terminator */ },
  711. };
  712. static struct twl4030_power_data osc_off_idle = {
  713. .scripts = omap3_idle_scripts,
  714. .num = ARRAY_SIZE(omap3_idle_scripts),
  715. .resource_config = omap3_idle_rconfig,
  716. .board_config = osc_off_rconfig,
  717. };
  718. static struct of_device_id twl4030_power_of_match[] = {
  719. {
  720. .compatible = "ti,twl4030-power",
  721. },
  722. {
  723. .compatible = "ti,twl4030-power-reset",
  724. .data = &omap3_reset,
  725. },
  726. {
  727. .compatible = "ti,twl4030-power-idle",
  728. .data = &omap3_idle,
  729. },
  730. {
  731. .compatible = "ti,twl4030-power-idle-osc-off",
  732. .data = &osc_off_idle,
  733. },
  734. { },
  735. };
  736. MODULE_DEVICE_TABLE(of, twl4030_power_of_match);
  737. #endif /* CONFIG_OF */
  738. static int twl4030_power_probe(struct platform_device *pdev)
  739. {
  740. const struct twl4030_power_data *pdata = dev_get_platdata(&pdev->dev);
  741. struct device_node *node = pdev->dev.of_node;
  742. const struct of_device_id *match;
  743. int err = 0;
  744. int err2 = 0;
  745. u8 val;
  746. if (!pdata && !node) {
  747. dev_err(&pdev->dev, "Platform data is missing\n");
  748. return -EINVAL;
  749. }
  750. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
  751. TWL4030_PM_MASTER_PROTECT_KEY);
  752. err |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
  753. TWL4030_PM_MASTER_KEY_CFG2,
  754. TWL4030_PM_MASTER_PROTECT_KEY);
  755. if (err) {
  756. pr_err("TWL4030 Unable to unlock registers\n");
  757. return err;
  758. }
  759. match = of_match_device(of_match_ptr(twl4030_power_of_match),
  760. &pdev->dev);
  761. if (match && match->data)
  762. pdata = match->data;
  763. if (pdata) {
  764. err = twl4030_power_configure_scripts(pdata);
  765. if (err) {
  766. pr_err("TWL4030 failed to load scripts\n");
  767. goto relock;
  768. }
  769. err = twl4030_power_configure_resources(pdata);
  770. if (err) {
  771. pr_err("TWL4030 failed to configure resource\n");
  772. goto relock;
  773. }
  774. }
  775. /* Board has to be wired properly to use this feature */
  776. if (twl4030_power_use_poweroff(pdata, node) && !pm_power_off) {
  777. /* Default for SEQ_OFFSYNC is set, lets ensure this */
  778. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
  779. TWL4030_PM_MASTER_CFG_P123_TRANSITION);
  780. if (err) {
  781. pr_warning("TWL4030 Unable to read registers\n");
  782. } else if (!(val & SEQ_OFFSYNC)) {
  783. val |= SEQ_OFFSYNC;
  784. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val,
  785. TWL4030_PM_MASTER_CFG_P123_TRANSITION);
  786. if (err) {
  787. pr_err("TWL4030 Unable to setup SEQ_OFFSYNC\n");
  788. goto relock;
  789. }
  790. }
  791. pm_power_off = twl4030_power_off;
  792. }
  793. relock:
  794. err2 = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
  795. TWL4030_PM_MASTER_PROTECT_KEY);
  796. if (err2) {
  797. pr_err("TWL4030 Unable to relock registers\n");
  798. return err2;
  799. }
  800. return err;
  801. }
  802. static int twl4030_power_remove(struct platform_device *pdev)
  803. {
  804. return 0;
  805. }
  806. static struct platform_driver twl4030_power_driver = {
  807. .driver = {
  808. .name = "twl4030_power",
  809. .of_match_table = of_match_ptr(twl4030_power_of_match),
  810. },
  811. .probe = twl4030_power_probe,
  812. .remove = twl4030_power_remove,
  813. };
  814. module_platform_driver(twl4030_power_driver);
  815. MODULE_AUTHOR("Nokia Corporation");
  816. MODULE_AUTHOR("Texas Instruments, Inc.");
  817. MODULE_DESCRIPTION("Power management for TWL4030");
  818. MODULE_LICENSE("GPL");
  819. MODULE_ALIAS("platform:twl4030_power");