twl4030-power.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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
  239. twl4030_config_wakeup12_sequence(const struct twl4030_power_data *pdata,
  240. u8 address)
  241. {
  242. int err = 0;
  243. u8 data;
  244. /* Set SLEEP to ACTIVE SEQ address for P1 and P2 */
  245. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_S2A12);
  246. if (err)
  247. goto out;
  248. /* P1/P2 LVL_WAKEUP should be on LEVEL */
  249. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data, R_P1_SW_EVENTS);
  250. if (err)
  251. goto out;
  252. data |= PWR_LVL_WAKEUP;
  253. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P1_SW_EVENTS);
  254. if (err)
  255. goto out;
  256. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data, R_P2_SW_EVENTS);
  257. if (err)
  258. goto out;
  259. data |= PWR_LVL_WAKEUP;
  260. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P2_SW_EVENTS);
  261. if (err)
  262. goto out;
  263. if (pdata->ac_charger_quirk || machine_is_omap_3430sdp() ||
  264. machine_is_omap_ldp()) {
  265. /* Disabling AC charger effect on sleep-active transitions */
  266. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data,
  267. R_CFG_P1_TRANSITION);
  268. if (err)
  269. goto out;
  270. data &= ~STARTON_CHG;
  271. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data,
  272. R_CFG_P1_TRANSITION);
  273. if (err)
  274. goto out;
  275. }
  276. out:
  277. if (err)
  278. pr_err("TWL4030 wakeup sequence for P1 and P2" \
  279. "config error\n");
  280. return err;
  281. }
  282. static int twl4030_config_sleep_sequence(u8 address)
  283. {
  284. int err;
  285. /* Set ACTIVE to SLEEP SEQ address in T2 memory*/
  286. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_A2S);
  287. if (err)
  288. pr_err("TWL4030 sleep sequence config error\n");
  289. return err;
  290. }
  291. static int twl4030_config_warmreset_sequence(u8 address)
  292. {
  293. int err;
  294. u8 rd_data;
  295. /* Set WARM RESET SEQ address for P1 */
  296. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_WARM);
  297. if (err)
  298. goto out;
  299. /* P1/P2/P3 enable WARMRESET */
  300. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &rd_data, R_P1_SW_EVENTS);
  301. if (err)
  302. goto out;
  303. rd_data |= PWR_ENABLE_WARMRESET;
  304. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P1_SW_EVENTS);
  305. if (err)
  306. goto out;
  307. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &rd_data, R_P2_SW_EVENTS);
  308. if (err)
  309. goto out;
  310. rd_data |= PWR_ENABLE_WARMRESET;
  311. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P2_SW_EVENTS);
  312. if (err)
  313. goto out;
  314. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &rd_data, R_P3_SW_EVENTS);
  315. if (err)
  316. goto out;
  317. rd_data |= PWR_ENABLE_WARMRESET;
  318. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P3_SW_EVENTS);
  319. out:
  320. if (err)
  321. pr_err("TWL4030 warmreset seq config error\n");
  322. return err;
  323. }
  324. static int twl4030_configure_resource(struct twl4030_resconfig *rconfig)
  325. {
  326. int rconfig_addr;
  327. int err;
  328. u8 type;
  329. u8 grp;
  330. u8 remap;
  331. if (rconfig->resource > TOTAL_RESOURCES) {
  332. pr_err("TWL4030 Resource %d does not exist\n",
  333. rconfig->resource);
  334. return -EINVAL;
  335. }
  336. rconfig_addr = res_config_addrs[rconfig->resource];
  337. /* Set resource group */
  338. err = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &grp,
  339. rconfig_addr + DEV_GRP_OFFSET);
  340. if (err) {
  341. pr_err("TWL4030 Resource %d group could not be read\n",
  342. rconfig->resource);
  343. return err;
  344. }
  345. if (rconfig->devgroup != TWL4030_RESCONFIG_UNDEF) {
  346. grp &= ~DEV_GRP_MASK;
  347. grp |= rconfig->devgroup << DEV_GRP_SHIFT;
  348. err = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
  349. grp, rconfig_addr + DEV_GRP_OFFSET);
  350. if (err < 0) {
  351. pr_err("TWL4030 failed to program devgroup\n");
  352. return err;
  353. }
  354. }
  355. /* Set resource types */
  356. err = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &type,
  357. rconfig_addr + TYPE_OFFSET);
  358. if (err < 0) {
  359. pr_err("TWL4030 Resource %d type could not be read\n",
  360. rconfig->resource);
  361. return err;
  362. }
  363. if (rconfig->type != TWL4030_RESCONFIG_UNDEF) {
  364. type &= ~TYPE_MASK;
  365. type |= rconfig->type << TYPE_SHIFT;
  366. }
  367. if (rconfig->type2 != TWL4030_RESCONFIG_UNDEF) {
  368. type &= ~TYPE2_MASK;
  369. type |= rconfig->type2 << TYPE2_SHIFT;
  370. }
  371. err = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
  372. type, rconfig_addr + TYPE_OFFSET);
  373. if (err < 0) {
  374. pr_err("TWL4030 failed to program resource type\n");
  375. return err;
  376. }
  377. /* Set remap states */
  378. err = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &remap,
  379. rconfig_addr + REMAP_OFFSET);
  380. if (err < 0) {
  381. pr_err("TWL4030 Resource %d remap could not be read\n",
  382. rconfig->resource);
  383. return err;
  384. }
  385. if (rconfig->remap_off != TWL4030_RESCONFIG_UNDEF) {
  386. remap &= ~OFF_STATE_MASK;
  387. remap |= rconfig->remap_off << OFF_STATE_SHIFT;
  388. }
  389. if (rconfig->remap_sleep != TWL4030_RESCONFIG_UNDEF) {
  390. remap &= ~SLEEP_STATE_MASK;
  391. remap |= rconfig->remap_sleep << SLEEP_STATE_SHIFT;
  392. }
  393. err = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
  394. remap,
  395. rconfig_addr + REMAP_OFFSET);
  396. if (err < 0) {
  397. pr_err("TWL4030 failed to program remap\n");
  398. return err;
  399. }
  400. return 0;
  401. }
  402. static int load_twl4030_script(const struct twl4030_power_data *pdata,
  403. struct twl4030_script *tscript,
  404. u8 address)
  405. {
  406. int err;
  407. static int order;
  408. /* Make sure the script isn't going beyond last valid address (0x3f) */
  409. if ((address + tscript->size) > END_OF_SCRIPT) {
  410. pr_err("TWL4030 scripts too big error\n");
  411. return -EINVAL;
  412. }
  413. err = twl4030_write_script(address, tscript->script, tscript->size);
  414. if (err)
  415. goto out;
  416. if (tscript->flags & TWL4030_WRST_SCRIPT) {
  417. err = twl4030_config_warmreset_sequence(address);
  418. if (err)
  419. goto out;
  420. }
  421. if (tscript->flags & TWL4030_WAKEUP12_SCRIPT) {
  422. /* Reset any existing sleep script to avoid hangs on reboot */
  423. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
  424. R_SEQ_ADD_A2S);
  425. if (err)
  426. goto out;
  427. err = twl4030_config_wakeup12_sequence(pdata, address);
  428. if (err)
  429. goto out;
  430. order = 1;
  431. }
  432. if (tscript->flags & TWL4030_WAKEUP3_SCRIPT) {
  433. err = twl4030_config_wakeup3_sequence(address);
  434. if (err)
  435. goto out;
  436. }
  437. if (tscript->flags & TWL4030_SLEEP_SCRIPT) {
  438. if (!order)
  439. pr_warning("TWL4030: Bad order of scripts (sleep "\
  440. "script before wakeup) Leads to boot"\
  441. "failure on some boards\n");
  442. err = twl4030_config_sleep_sequence(address);
  443. }
  444. out:
  445. return err;
  446. }
  447. int twl4030_remove_script(u8 flags)
  448. {
  449. int err = 0;
  450. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
  451. TWL4030_PM_MASTER_PROTECT_KEY);
  452. if (err) {
  453. pr_err("twl4030: unable to unlock PROTECT_KEY\n");
  454. return err;
  455. }
  456. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG2,
  457. TWL4030_PM_MASTER_PROTECT_KEY);
  458. if (err) {
  459. pr_err("twl4030: unable to unlock PROTECT_KEY\n");
  460. return err;
  461. }
  462. if (flags & TWL4030_WRST_SCRIPT) {
  463. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
  464. R_SEQ_ADD_WARM);
  465. if (err)
  466. return err;
  467. }
  468. if (flags & TWL4030_WAKEUP12_SCRIPT) {
  469. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
  470. R_SEQ_ADD_S2A12);
  471. if (err)
  472. return err;
  473. }
  474. if (flags & TWL4030_WAKEUP3_SCRIPT) {
  475. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
  476. R_SEQ_ADD_S2A3);
  477. if (err)
  478. return err;
  479. }
  480. if (flags & TWL4030_SLEEP_SCRIPT) {
  481. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
  482. R_SEQ_ADD_A2S);
  483. if (err)
  484. return err;
  485. }
  486. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
  487. TWL4030_PM_MASTER_PROTECT_KEY);
  488. if (err)
  489. pr_err("TWL4030 Unable to relock registers\n");
  490. return err;
  491. }
  492. static int
  493. twl4030_power_configure_scripts(const struct twl4030_power_data *pdata)
  494. {
  495. int err;
  496. int i;
  497. u8 address = twl4030_start_script_address;
  498. for (i = 0; i < pdata->num; i++) {
  499. err = load_twl4030_script(pdata, pdata->scripts[i], address);
  500. if (err)
  501. return err;
  502. address += pdata->scripts[i]->size;
  503. }
  504. return 0;
  505. }
  506. static void twl4030_patch_rconfig(struct twl4030_resconfig *common,
  507. struct twl4030_resconfig *board)
  508. {
  509. while (common->resource) {
  510. struct twl4030_resconfig *b = board;
  511. while (b->resource) {
  512. if (b->resource == common->resource) {
  513. *common = *b;
  514. break;
  515. }
  516. b++;
  517. }
  518. common++;
  519. }
  520. }
  521. static int
  522. twl4030_power_configure_resources(const struct twl4030_power_data *pdata)
  523. {
  524. struct twl4030_resconfig *resconfig = pdata->resource_config;
  525. struct twl4030_resconfig *boardconf = pdata->board_config;
  526. int err;
  527. if (resconfig) {
  528. if (boardconf)
  529. twl4030_patch_rconfig(resconfig, boardconf);
  530. while (resconfig->resource) {
  531. err = twl4030_configure_resource(resconfig);
  532. if (err)
  533. return err;
  534. resconfig++;
  535. }
  536. }
  537. return 0;
  538. }
  539. static int twl4030_starton_mask_and_set(u8 bitmask, u8 bitvalues)
  540. {
  541. u8 regs[3] = { TWL4030_PM_MASTER_CFG_P1_TRANSITION,
  542. TWL4030_PM_MASTER_CFG_P2_TRANSITION,
  543. TWL4030_PM_MASTER_CFG_P3_TRANSITION, };
  544. u8 val;
  545. int i, err;
  546. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
  547. TWL4030_PM_MASTER_PROTECT_KEY);
  548. if (err)
  549. goto relock;
  550. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
  551. TWL4030_PM_MASTER_KEY_CFG2,
  552. TWL4030_PM_MASTER_PROTECT_KEY);
  553. if (err)
  554. goto relock;
  555. for (i = 0; i < sizeof(regs); i++) {
  556. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER,
  557. &val, regs[i]);
  558. if (err)
  559. break;
  560. val = (~bitmask & val) | (bitmask & bitvalues);
  561. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
  562. val, regs[i]);
  563. if (err)
  564. break;
  565. }
  566. if (err)
  567. pr_err("TWL4030 Register access failed: %i\n", err);
  568. relock:
  569. return twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
  570. TWL4030_PM_MASTER_PROTECT_KEY);
  571. }
  572. /*
  573. * In master mode, start the power off sequence.
  574. * After a successful execution, TWL shuts down the power to the SoC
  575. * and all peripherals connected to it.
  576. */
  577. void twl4030_power_off(void)
  578. {
  579. int err;
  580. /* Disable start on charger or VBUS as it can break poweroff */
  581. err = twl4030_starton_mask_and_set(STARTON_VBUS | STARTON_CHG, 0);
  582. if (err)
  583. pr_err("TWL4030 Unable to configure start-up\n");
  584. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, PWR_DEVOFF,
  585. TWL4030_PM_MASTER_P1_SW_EVENTS);
  586. if (err)
  587. pr_err("TWL4030 Unable to power off\n");
  588. }
  589. static bool twl4030_power_use_poweroff(const struct twl4030_power_data *pdata,
  590. struct device_node *node)
  591. {
  592. if (pdata && pdata->use_poweroff)
  593. return true;
  594. if (of_property_read_bool(node, "ti,system-power-controller"))
  595. return true;
  596. if (of_property_read_bool(node, "ti,use_poweroff"))
  597. return true;
  598. return false;
  599. }
  600. #ifdef CONFIG_OF
  601. /* Generic warm reset configuration for omap3 */
  602. static struct twl4030_ins omap3_wrst_seq[] = {
  603. TWL_RESOURCE_OFF(RES_NRES_PWRON),
  604. TWL_RESOURCE_OFF(RES_RESET),
  605. TWL_RESOURCE_RESET(RES_MAIN_REF),
  606. TWL_RESOURCE_GROUP_RESET(RES_GRP_ALL, RES_TYPE_R0, RES_TYPE2_R2),
  607. TWL_RESOURCE_RESET(RES_VUSB_3V1),
  608. TWL_RESOURCE_GROUP_RESET(RES_GRP_ALL, RES_TYPE_R0, RES_TYPE2_R1),
  609. TWL_RESOURCE_GROUP_RESET(RES_GRP_RC, RES_TYPE_ALL, RES_TYPE2_R0),
  610. TWL_RESOURCE_ON(RES_RESET),
  611. TWL_RESOURCE_ON(RES_NRES_PWRON),
  612. };
  613. static struct twl4030_script omap3_wrst_script = {
  614. .script = omap3_wrst_seq,
  615. .size = ARRAY_SIZE(omap3_wrst_seq),
  616. .flags = TWL4030_WRST_SCRIPT,
  617. };
  618. static struct twl4030_script *omap3_reset_scripts[] = {
  619. &omap3_wrst_script,
  620. };
  621. static struct twl4030_resconfig omap3_rconfig[] = {
  622. TWL_REMAP_SLEEP(RES_HFCLKOUT, DEV_GRP_P3, -1, -1),
  623. TWL_REMAP_SLEEP(RES_VDD1, DEV_GRP_P1, -1, -1),
  624. TWL_REMAP_SLEEP(RES_VDD2, DEV_GRP_P1, -1, -1),
  625. { 0, 0 },
  626. };
  627. static struct twl4030_power_data omap3_reset = {
  628. .scripts = omap3_reset_scripts,
  629. .num = ARRAY_SIZE(omap3_reset_scripts),
  630. .resource_config = omap3_rconfig,
  631. };
  632. /* Recommended generic default idle configuration for off-idle */
  633. /* Broadcast message to put res to sleep */
  634. static struct twl4030_ins omap3_idle_sleep_on_seq[] = {
  635. TWL_RESOURCE_GROUP_SLEEP(RES_GRP_ALL, RES_TYPE_ALL, 0),
  636. };
  637. static struct twl4030_script omap3_idle_sleep_on_script = {
  638. .script = omap3_idle_sleep_on_seq,
  639. .size = ARRAY_SIZE(omap3_idle_sleep_on_seq),
  640. .flags = TWL4030_SLEEP_SCRIPT,
  641. };
  642. /* Broadcast message to put res to active */
  643. static struct twl4030_ins omap3_idle_wakeup_p12_seq[] = {
  644. TWL_RESOURCE_GROUP_ACTIVE(RES_GRP_ALL, RES_TYPE_ALL, 0),
  645. };
  646. static struct twl4030_script omap3_idle_wakeup_p12_script = {
  647. .script = omap3_idle_wakeup_p12_seq,
  648. .size = ARRAY_SIZE(omap3_idle_wakeup_p12_seq),
  649. .flags = TWL4030_WAKEUP12_SCRIPT,
  650. };
  651. /* Broadcast message to put res to active */
  652. static struct twl4030_ins omap3_idle_wakeup_p3_seq[] = {
  653. TWL_RESOURCE_SET_ACTIVE(RES_CLKEN, 0x37),
  654. TWL_RESOURCE_GROUP_ACTIVE(RES_GRP_ALL, RES_TYPE_ALL, 0),
  655. };
  656. static struct twl4030_script omap3_idle_wakeup_p3_script = {
  657. .script = omap3_idle_wakeup_p3_seq,
  658. .size = ARRAY_SIZE(omap3_idle_wakeup_p3_seq),
  659. .flags = TWL4030_WAKEUP3_SCRIPT,
  660. };
  661. static struct twl4030_script *omap3_idle_scripts[] = {
  662. &omap3_idle_wakeup_p12_script,
  663. &omap3_idle_wakeup_p3_script,
  664. &omap3_wrst_script,
  665. &omap3_idle_sleep_on_script,
  666. };
  667. /*
  668. * Recommended configuration based on "Recommended Sleep
  669. * Sequences for the Zoom Platform":
  670. * http://omappedia.com/wiki/File:Recommended_Sleep_Sequences_Zoom.pdf
  671. * Note that the type1 and type2 seem to be just the init order number
  672. * for type1 and type2 groups as specified in the document mentioned
  673. * above.
  674. */
  675. static struct twl4030_resconfig omap3_idle_rconfig[] = {
  676. TWL_REMAP_SLEEP(RES_VAUX1, TWL4030_RESCONFIG_UNDEF, 0, 0),
  677. TWL_REMAP_SLEEP(RES_VAUX2, TWL4030_RESCONFIG_UNDEF, 0, 0),
  678. TWL_REMAP_SLEEP(RES_VAUX3, TWL4030_RESCONFIG_UNDEF, 0, 0),
  679. TWL_REMAP_SLEEP(RES_VAUX4, TWL4030_RESCONFIG_UNDEF, 0, 0),
  680. TWL_REMAP_SLEEP(RES_VMMC1, TWL4030_RESCONFIG_UNDEF, 0, 0),
  681. TWL_REMAP_SLEEP(RES_VMMC2, TWL4030_RESCONFIG_UNDEF, 0, 0),
  682. TWL_REMAP_OFF(RES_VPLL1, DEV_GRP_P1, 3, 1),
  683. TWL_REMAP_SLEEP(RES_VPLL2, DEV_GRP_P1, 0, 0),
  684. TWL_REMAP_SLEEP(RES_VSIM, TWL4030_RESCONFIG_UNDEF, 0, 0),
  685. TWL_REMAP_SLEEP(RES_VDAC, TWL4030_RESCONFIG_UNDEF, 0, 0),
  686. TWL_REMAP_SLEEP(RES_VINTANA1, TWL_DEV_GRP_P123, 1, 2),
  687. TWL_REMAP_SLEEP(RES_VINTANA2, TWL_DEV_GRP_P123, 0, 2),
  688. TWL_REMAP_SLEEP(RES_VINTDIG, TWL_DEV_GRP_P123, 1, 2),
  689. TWL_REMAP_SLEEP(RES_VIO, TWL_DEV_GRP_P123, 2, 2),
  690. TWL_REMAP_OFF(RES_VDD1, DEV_GRP_P1, 4, 1),
  691. TWL_REMAP_OFF(RES_VDD2, DEV_GRP_P1, 3, 1),
  692. TWL_REMAP_SLEEP(RES_VUSB_1V5, TWL4030_RESCONFIG_UNDEF, 0, 0),
  693. TWL_REMAP_SLEEP(RES_VUSB_1V8, TWL4030_RESCONFIG_UNDEF, 0, 0),
  694. TWL_REMAP_SLEEP(RES_VUSB_3V1, TWL_DEV_GRP_P123, 0, 0),
  695. /* Resource #20 USB charge pump skipped */
  696. TWL_REMAP_SLEEP(RES_REGEN, TWL_DEV_GRP_P123, 2, 1),
  697. TWL_REMAP_SLEEP(RES_NRES_PWRON, TWL_DEV_GRP_P123, 0, 1),
  698. TWL_REMAP_SLEEP(RES_CLKEN, TWL_DEV_GRP_P123, 3, 2),
  699. TWL_REMAP_SLEEP(RES_SYSEN, TWL_DEV_GRP_P123, 6, 1),
  700. TWL_REMAP_SLEEP(RES_HFCLKOUT, DEV_GRP_P3, 0, 2),
  701. TWL_REMAP_SLEEP(RES_32KCLKOUT, TWL_DEV_GRP_P123, 0, 0),
  702. TWL_REMAP_SLEEP(RES_RESET, TWL_DEV_GRP_P123, 6, 0),
  703. TWL_REMAP_SLEEP(RES_MAIN_REF, TWL_DEV_GRP_P123, 0, 0),
  704. { /* Terminator */ },
  705. };
  706. static struct twl4030_power_data omap3_idle = {
  707. .scripts = omap3_idle_scripts,
  708. .num = ARRAY_SIZE(omap3_idle_scripts),
  709. .resource_config = omap3_idle_rconfig,
  710. };
  711. /* Disable 32 KiHz oscillator during idle */
  712. static struct twl4030_resconfig osc_off_rconfig[] = {
  713. TWL_REMAP_OFF(RES_CLKEN, DEV_GRP_P1 | DEV_GRP_P3, 3, 2),
  714. { /* Terminator */ },
  715. };
  716. static struct twl4030_power_data osc_off_idle = {
  717. .scripts = omap3_idle_scripts,
  718. .num = ARRAY_SIZE(omap3_idle_scripts),
  719. .resource_config = omap3_idle_rconfig,
  720. .board_config = osc_off_rconfig,
  721. };
  722. static struct twl4030_power_data omap3_idle_ac_quirk = {
  723. .scripts = omap3_idle_scripts,
  724. .num = ARRAY_SIZE(omap3_idle_scripts),
  725. .resource_config = omap3_idle_rconfig,
  726. .ac_charger_quirk = true,
  727. };
  728. static struct twl4030_power_data omap3_idle_ac_quirk_osc_off = {
  729. .scripts = omap3_idle_scripts,
  730. .num = ARRAY_SIZE(omap3_idle_scripts),
  731. .resource_config = omap3_idle_rconfig,
  732. .board_config = osc_off_rconfig,
  733. .ac_charger_quirk = true,
  734. };
  735. static const struct of_device_id twl4030_power_of_match[] = {
  736. {
  737. .compatible = "ti,twl4030-power",
  738. },
  739. {
  740. .compatible = "ti,twl4030-power-reset",
  741. .data = &omap3_reset,
  742. },
  743. {
  744. .compatible = "ti,twl4030-power-idle",
  745. .data = &omap3_idle,
  746. },
  747. {
  748. .compatible = "ti,twl4030-power-idle-osc-off",
  749. .data = &osc_off_idle,
  750. },
  751. {
  752. .compatible = "ti,twl4030-power-omap3-sdp",
  753. .data = &omap3_idle_ac_quirk,
  754. },
  755. {
  756. .compatible = "ti,twl4030-power-omap3-ldp",
  757. .data = &omap3_idle_ac_quirk_osc_off,
  758. },
  759. {
  760. .compatible = "ti,twl4030-power-omap3-evm",
  761. .data = &omap3_idle_ac_quirk,
  762. },
  763. { },
  764. };
  765. MODULE_DEVICE_TABLE(of, twl4030_power_of_match);
  766. #endif /* CONFIG_OF */
  767. static int twl4030_power_probe(struct platform_device *pdev)
  768. {
  769. const struct twl4030_power_data *pdata = dev_get_platdata(&pdev->dev);
  770. struct device_node *node = pdev->dev.of_node;
  771. const struct of_device_id *match;
  772. int err = 0;
  773. int err2 = 0;
  774. u8 val;
  775. if (!pdata && !node) {
  776. dev_err(&pdev->dev, "Platform data is missing\n");
  777. return -EINVAL;
  778. }
  779. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
  780. TWL4030_PM_MASTER_PROTECT_KEY);
  781. err |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
  782. TWL4030_PM_MASTER_KEY_CFG2,
  783. TWL4030_PM_MASTER_PROTECT_KEY);
  784. if (err) {
  785. pr_err("TWL4030 Unable to unlock registers\n");
  786. return err;
  787. }
  788. match = of_match_device(of_match_ptr(twl4030_power_of_match),
  789. &pdev->dev);
  790. if (match && match->data)
  791. pdata = match->data;
  792. if (pdata) {
  793. err = twl4030_power_configure_scripts(pdata);
  794. if (err) {
  795. pr_err("TWL4030 failed to load scripts\n");
  796. goto relock;
  797. }
  798. err = twl4030_power_configure_resources(pdata);
  799. if (err) {
  800. pr_err("TWL4030 failed to configure resource\n");
  801. goto relock;
  802. }
  803. }
  804. /* Board has to be wired properly to use this feature */
  805. if (twl4030_power_use_poweroff(pdata, node) && !pm_power_off) {
  806. /* Default for SEQ_OFFSYNC is set, lets ensure this */
  807. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
  808. TWL4030_PM_MASTER_CFG_P123_TRANSITION);
  809. if (err) {
  810. pr_warning("TWL4030 Unable to read registers\n");
  811. } else if (!(val & SEQ_OFFSYNC)) {
  812. val |= SEQ_OFFSYNC;
  813. err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val,
  814. TWL4030_PM_MASTER_CFG_P123_TRANSITION);
  815. if (err) {
  816. pr_err("TWL4030 Unable to setup SEQ_OFFSYNC\n");
  817. goto relock;
  818. }
  819. }
  820. pm_power_off = twl4030_power_off;
  821. }
  822. relock:
  823. err2 = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
  824. TWL4030_PM_MASTER_PROTECT_KEY);
  825. if (err2) {
  826. pr_err("TWL4030 Unable to relock registers\n");
  827. return err2;
  828. }
  829. return err;
  830. }
  831. static int twl4030_power_remove(struct platform_device *pdev)
  832. {
  833. return 0;
  834. }
  835. static struct platform_driver twl4030_power_driver = {
  836. .driver = {
  837. .name = "twl4030_power",
  838. .of_match_table = of_match_ptr(twl4030_power_of_match),
  839. },
  840. .probe = twl4030_power_probe,
  841. .remove = twl4030_power_remove,
  842. };
  843. module_platform_driver(twl4030_power_driver);
  844. MODULE_AUTHOR("Nokia Corporation");
  845. MODULE_AUTHOR("Texas Instruments, Inc.");
  846. MODULE_DESCRIPTION("Power management for TWL4030");
  847. MODULE_LICENSE("GPL");
  848. MODULE_ALIAS("platform:twl4030_power");