db8500-prcmu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License v2
  5. * Authors: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
  6. * Bengt Jonsson <bengt.g.jonsson@stericsson.com> for ST-Ericsson
  7. *
  8. * Power domain regulators on DB8500
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/err.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/mfd/db8500-prcmu.h>
  16. #include <linux/regulator/driver.h>
  17. #include <linux/regulator/machine.h>
  18. #include <linux/regulator/db8500-prcmu.h>
  19. /*
  20. * power state reference count
  21. */
  22. static int power_state_active_cnt; /* will initialize to zero */
  23. static DEFINE_SPINLOCK(power_state_active_lock);
  24. static void power_state_active_enable(void)
  25. {
  26. unsigned long flags;
  27. spin_lock_irqsave(&power_state_active_lock, flags);
  28. power_state_active_cnt++;
  29. spin_unlock_irqrestore(&power_state_active_lock, flags);
  30. }
  31. static int power_state_active_disable(void)
  32. {
  33. int ret = 0;
  34. unsigned long flags;
  35. spin_lock_irqsave(&power_state_active_lock, flags);
  36. if (power_state_active_cnt <= 0) {
  37. pr_err("power state: unbalanced enable/disable calls\n");
  38. ret = -EINVAL;
  39. goto out;
  40. }
  41. power_state_active_cnt--;
  42. out:
  43. spin_unlock_irqrestore(&power_state_active_lock, flags);
  44. return ret;
  45. }
  46. /*
  47. * Exported interface for CPUIdle only. This function is called when interrupts
  48. * are turned off. Hence, no locking.
  49. */
  50. int power_state_active_is_enabled(void)
  51. {
  52. return (power_state_active_cnt > 0);
  53. }
  54. /**
  55. * struct db8500_regulator_info - db8500 regulator information
  56. * @dev: device pointer
  57. * @desc: regulator description
  58. * @rdev: regulator device pointer
  59. * @is_enabled: status of the regulator
  60. * @epod_id: id for EPOD (power domain)
  61. * @is_ramret: RAM retention switch for EPOD (power domain)
  62. * @operating_point: operating point (only for vape, to be removed)
  63. *
  64. */
  65. struct db8500_regulator_info {
  66. struct device *dev;
  67. struct regulator_desc desc;
  68. struct regulator_dev *rdev;
  69. bool is_enabled;
  70. u16 epod_id;
  71. bool is_ramret;
  72. bool exclude_from_power_state;
  73. unsigned int operating_point;
  74. };
  75. static int db8500_regulator_enable(struct regulator_dev *rdev)
  76. {
  77. struct db8500_regulator_info *info = rdev_get_drvdata(rdev);
  78. if (info == NULL)
  79. return -EINVAL;
  80. dev_vdbg(rdev_get_dev(rdev), "regulator-%s-enable\n",
  81. info->desc.name);
  82. info->is_enabled = true;
  83. if (!info->exclude_from_power_state)
  84. power_state_active_enable();
  85. return 0;
  86. }
  87. static int db8500_regulator_disable(struct regulator_dev *rdev)
  88. {
  89. struct db8500_regulator_info *info = rdev_get_drvdata(rdev);
  90. int ret = 0;
  91. if (info == NULL)
  92. return -EINVAL;
  93. dev_vdbg(rdev_get_dev(rdev), "regulator-%s-disable\n",
  94. info->desc.name);
  95. info->is_enabled = false;
  96. if (!info->exclude_from_power_state)
  97. ret = power_state_active_disable();
  98. return ret;
  99. }
  100. static int db8500_regulator_is_enabled(struct regulator_dev *rdev)
  101. {
  102. struct db8500_regulator_info *info = rdev_get_drvdata(rdev);
  103. if (info == NULL)
  104. return -EINVAL;
  105. dev_vdbg(rdev_get_dev(rdev), "regulator-%s-is_enabled (is_enabled):"
  106. " %i\n", info->desc.name, info->is_enabled);
  107. return info->is_enabled;
  108. }
  109. /* db8500 regulator operations */
  110. static struct regulator_ops db8500_regulator_ops = {
  111. .enable = db8500_regulator_enable,
  112. .disable = db8500_regulator_disable,
  113. .is_enabled = db8500_regulator_is_enabled,
  114. };
  115. /*
  116. * EPOD control
  117. */
  118. static bool epod_on[NUM_EPOD_ID];
  119. static bool epod_ramret[NUM_EPOD_ID];
  120. static int enable_epod(u16 epod_id, bool ramret)
  121. {
  122. int ret;
  123. if (ramret) {
  124. if (!epod_on[epod_id]) {
  125. ret = prcmu_set_epod(epod_id, EPOD_STATE_RAMRET);
  126. if (ret < 0)
  127. return ret;
  128. }
  129. epod_ramret[epod_id] = true;
  130. } else {
  131. ret = prcmu_set_epod(epod_id, EPOD_STATE_ON);
  132. if (ret < 0)
  133. return ret;
  134. epod_on[epod_id] = true;
  135. }
  136. return 0;
  137. }
  138. static int disable_epod(u16 epod_id, bool ramret)
  139. {
  140. int ret;
  141. if (ramret) {
  142. if (!epod_on[epod_id]) {
  143. ret = prcmu_set_epod(epod_id, EPOD_STATE_OFF);
  144. if (ret < 0)
  145. return ret;
  146. }
  147. epod_ramret[epod_id] = false;
  148. } else {
  149. if (epod_ramret[epod_id]) {
  150. ret = prcmu_set_epod(epod_id, EPOD_STATE_RAMRET);
  151. if (ret < 0)
  152. return ret;
  153. } else {
  154. ret = prcmu_set_epod(epod_id, EPOD_STATE_OFF);
  155. if (ret < 0)
  156. return ret;
  157. }
  158. epod_on[epod_id] = false;
  159. }
  160. return 0;
  161. }
  162. /*
  163. * Regulator switch
  164. */
  165. static int db8500_regulator_switch_enable(struct regulator_dev *rdev)
  166. {
  167. struct db8500_regulator_info *info = rdev_get_drvdata(rdev);
  168. int ret;
  169. if (info == NULL)
  170. return -EINVAL;
  171. dev_vdbg(rdev_get_dev(rdev), "regulator-switch-%s-enable\n",
  172. info->desc.name);
  173. ret = enable_epod(info->epod_id, info->is_ramret);
  174. if (ret < 0) {
  175. dev_err(rdev_get_dev(rdev),
  176. "regulator-switch-%s-enable: prcmu call failed\n",
  177. info->desc.name);
  178. goto out;
  179. }
  180. info->is_enabled = true;
  181. out:
  182. return ret;
  183. }
  184. static int db8500_regulator_switch_disable(struct regulator_dev *rdev)
  185. {
  186. struct db8500_regulator_info *info = rdev_get_drvdata(rdev);
  187. int ret;
  188. if (info == NULL)
  189. return -EINVAL;
  190. dev_vdbg(rdev_get_dev(rdev), "regulator-switch-%s-disable\n",
  191. info->desc.name);
  192. ret = disable_epod(info->epod_id, info->is_ramret);
  193. if (ret < 0) {
  194. dev_err(rdev_get_dev(rdev),
  195. "regulator_switch-%s-disable: prcmu call failed\n",
  196. info->desc.name);
  197. goto out;
  198. }
  199. info->is_enabled = 0;
  200. out:
  201. return ret;
  202. }
  203. static int db8500_regulator_switch_is_enabled(struct regulator_dev *rdev)
  204. {
  205. struct db8500_regulator_info *info = rdev_get_drvdata(rdev);
  206. if (info == NULL)
  207. return -EINVAL;
  208. dev_vdbg(rdev_get_dev(rdev),
  209. "regulator-switch-%s-is_enabled (is_enabled): %i\n",
  210. info->desc.name, info->is_enabled);
  211. return info->is_enabled;
  212. }
  213. static struct regulator_ops db8500_regulator_switch_ops = {
  214. .enable = db8500_regulator_switch_enable,
  215. .disable = db8500_regulator_switch_disable,
  216. .is_enabled = db8500_regulator_switch_is_enabled,
  217. };
  218. /*
  219. * Regulator information
  220. */
  221. static struct db8500_regulator_info
  222. db8500_regulator_info[DB8500_NUM_REGULATORS] = {
  223. [DB8500_REGULATOR_VAPE] = {
  224. .desc = {
  225. .name = "db8500-vape",
  226. .id = DB8500_REGULATOR_VAPE,
  227. .ops = &db8500_regulator_ops,
  228. .type = REGULATOR_VOLTAGE,
  229. .owner = THIS_MODULE,
  230. },
  231. },
  232. [DB8500_REGULATOR_VARM] = {
  233. .desc = {
  234. .name = "db8500-varm",
  235. .id = DB8500_REGULATOR_VARM,
  236. .ops = &db8500_regulator_ops,
  237. .type = REGULATOR_VOLTAGE,
  238. .owner = THIS_MODULE,
  239. },
  240. },
  241. [DB8500_REGULATOR_VMODEM] = {
  242. .desc = {
  243. .name = "db8500-vmodem",
  244. .id = DB8500_REGULATOR_VMODEM,
  245. .ops = &db8500_regulator_ops,
  246. .type = REGULATOR_VOLTAGE,
  247. .owner = THIS_MODULE,
  248. },
  249. },
  250. [DB8500_REGULATOR_VPLL] = {
  251. .desc = {
  252. .name = "db8500-vpll",
  253. .id = DB8500_REGULATOR_VPLL,
  254. .ops = &db8500_regulator_ops,
  255. .type = REGULATOR_VOLTAGE,
  256. .owner = THIS_MODULE,
  257. },
  258. },
  259. [DB8500_REGULATOR_VSMPS1] = {
  260. .desc = {
  261. .name = "db8500-vsmps1",
  262. .id = DB8500_REGULATOR_VSMPS1,
  263. .ops = &db8500_regulator_ops,
  264. .type = REGULATOR_VOLTAGE,
  265. .owner = THIS_MODULE,
  266. },
  267. },
  268. [DB8500_REGULATOR_VSMPS2] = {
  269. .desc = {
  270. .name = "db8500-vsmps2",
  271. .id = DB8500_REGULATOR_VSMPS2,
  272. .ops = &db8500_regulator_ops,
  273. .type = REGULATOR_VOLTAGE,
  274. .owner = THIS_MODULE,
  275. },
  276. .exclude_from_power_state = true,
  277. },
  278. [DB8500_REGULATOR_VSMPS3] = {
  279. .desc = {
  280. .name = "db8500-vsmps3",
  281. .id = DB8500_REGULATOR_VSMPS3,
  282. .ops = &db8500_regulator_ops,
  283. .type = REGULATOR_VOLTAGE,
  284. .owner = THIS_MODULE,
  285. },
  286. },
  287. [DB8500_REGULATOR_VRF1] = {
  288. .desc = {
  289. .name = "db8500-vrf1",
  290. .id = DB8500_REGULATOR_VRF1,
  291. .ops = &db8500_regulator_ops,
  292. .type = REGULATOR_VOLTAGE,
  293. .owner = THIS_MODULE,
  294. },
  295. },
  296. [DB8500_REGULATOR_SWITCH_SVAMMDSP] = {
  297. .desc = {
  298. .name = "db8500-sva-mmdsp",
  299. .id = DB8500_REGULATOR_SWITCH_SVAMMDSP,
  300. .ops = &db8500_regulator_switch_ops,
  301. .type = REGULATOR_VOLTAGE,
  302. .owner = THIS_MODULE,
  303. },
  304. .epod_id = EPOD_ID_SVAMMDSP,
  305. },
  306. [DB8500_REGULATOR_SWITCH_SVAMMDSPRET] = {
  307. .desc = {
  308. .name = "db8500-sva-mmdsp-ret",
  309. .id = DB8500_REGULATOR_SWITCH_SVAMMDSPRET,
  310. .ops = &db8500_regulator_switch_ops,
  311. .type = REGULATOR_VOLTAGE,
  312. .owner = THIS_MODULE,
  313. },
  314. .epod_id = EPOD_ID_SVAMMDSP,
  315. .is_ramret = true,
  316. },
  317. [DB8500_REGULATOR_SWITCH_SVAPIPE] = {
  318. .desc = {
  319. .name = "db8500-sva-pipe",
  320. .id = DB8500_REGULATOR_SWITCH_SVAPIPE,
  321. .ops = &db8500_regulator_switch_ops,
  322. .type = REGULATOR_VOLTAGE,
  323. .owner = THIS_MODULE,
  324. },
  325. .epod_id = EPOD_ID_SVAPIPE,
  326. },
  327. [DB8500_REGULATOR_SWITCH_SIAMMDSP] = {
  328. .desc = {
  329. .name = "db8500-sia-mmdsp",
  330. .id = DB8500_REGULATOR_SWITCH_SIAMMDSP,
  331. .ops = &db8500_regulator_switch_ops,
  332. .type = REGULATOR_VOLTAGE,
  333. .owner = THIS_MODULE,
  334. },
  335. .epod_id = EPOD_ID_SIAMMDSP,
  336. },
  337. [DB8500_REGULATOR_SWITCH_SIAMMDSPRET] = {
  338. .desc = {
  339. .name = "db8500-sia-mmdsp-ret",
  340. .id = DB8500_REGULATOR_SWITCH_SIAMMDSPRET,
  341. .ops = &db8500_regulator_switch_ops,
  342. .type = REGULATOR_VOLTAGE,
  343. .owner = THIS_MODULE,
  344. },
  345. .epod_id = EPOD_ID_SIAMMDSP,
  346. .is_ramret = true,
  347. },
  348. [DB8500_REGULATOR_SWITCH_SIAPIPE] = {
  349. .desc = {
  350. .name = "db8500-sia-pipe",
  351. .id = DB8500_REGULATOR_SWITCH_SIAPIPE,
  352. .ops = &db8500_regulator_switch_ops,
  353. .type = REGULATOR_VOLTAGE,
  354. .owner = THIS_MODULE,
  355. },
  356. .epod_id = EPOD_ID_SIAPIPE,
  357. },
  358. [DB8500_REGULATOR_SWITCH_SGA] = {
  359. .desc = {
  360. .name = "db8500-sga",
  361. .id = DB8500_REGULATOR_SWITCH_SGA,
  362. .ops = &db8500_regulator_switch_ops,
  363. .type = REGULATOR_VOLTAGE,
  364. .owner = THIS_MODULE,
  365. },
  366. .epod_id = EPOD_ID_SGA,
  367. },
  368. [DB8500_REGULATOR_SWITCH_B2R2_MCDE] = {
  369. .desc = {
  370. .name = "db8500-b2r2-mcde",
  371. .id = DB8500_REGULATOR_SWITCH_B2R2_MCDE,
  372. .ops = &db8500_regulator_switch_ops,
  373. .type = REGULATOR_VOLTAGE,
  374. .owner = THIS_MODULE,
  375. },
  376. .epod_id = EPOD_ID_B2R2_MCDE,
  377. },
  378. [DB8500_REGULATOR_SWITCH_ESRAM12] = {
  379. .desc = {
  380. .name = "db8500-esram12",
  381. .id = DB8500_REGULATOR_SWITCH_ESRAM12,
  382. .ops = &db8500_regulator_switch_ops,
  383. .type = REGULATOR_VOLTAGE,
  384. .owner = THIS_MODULE,
  385. },
  386. .epod_id = EPOD_ID_ESRAM12,
  387. .is_enabled = true,
  388. },
  389. [DB8500_REGULATOR_SWITCH_ESRAM12RET] = {
  390. .desc = {
  391. .name = "db8500-esram12-ret",
  392. .id = DB8500_REGULATOR_SWITCH_ESRAM12RET,
  393. .ops = &db8500_regulator_switch_ops,
  394. .type = REGULATOR_VOLTAGE,
  395. .owner = THIS_MODULE,
  396. },
  397. .epod_id = EPOD_ID_ESRAM12,
  398. .is_ramret = true,
  399. },
  400. [DB8500_REGULATOR_SWITCH_ESRAM34] = {
  401. .desc = {
  402. .name = "db8500-esram34",
  403. .id = DB8500_REGULATOR_SWITCH_ESRAM34,
  404. .ops = &db8500_regulator_switch_ops,
  405. .type = REGULATOR_VOLTAGE,
  406. .owner = THIS_MODULE,
  407. },
  408. .epod_id = EPOD_ID_ESRAM34,
  409. .is_enabled = true,
  410. },
  411. [DB8500_REGULATOR_SWITCH_ESRAM34RET] = {
  412. .desc = {
  413. .name = "db8500-esram34-ret",
  414. .id = DB8500_REGULATOR_SWITCH_ESRAM34RET,
  415. .ops = &db8500_regulator_switch_ops,
  416. .type = REGULATOR_VOLTAGE,
  417. .owner = THIS_MODULE,
  418. },
  419. .epod_id = EPOD_ID_ESRAM34,
  420. .is_ramret = true,
  421. },
  422. };
  423. static int __devinit db8500_regulator_probe(struct platform_device *pdev)
  424. {
  425. struct regulator_init_data *db8500_init_data =
  426. dev_get_platdata(&pdev->dev);
  427. int i, err;
  428. /* register all regulators */
  429. for (i = 0; i < ARRAY_SIZE(db8500_regulator_info); i++) {
  430. struct db8500_regulator_info *info;
  431. struct regulator_init_data *init_data = &db8500_init_data[i];
  432. /* assign per-regulator data */
  433. info = &db8500_regulator_info[i];
  434. info->dev = &pdev->dev;
  435. /* register with the regulator framework */
  436. info->rdev = regulator_register(&info->desc, &pdev->dev,
  437. init_data, info);
  438. if (IS_ERR(info->rdev)) {
  439. err = PTR_ERR(info->rdev);
  440. dev_err(&pdev->dev, "failed to register %s: err %i\n",
  441. info->desc.name, err);
  442. /* if failing, unregister all earlier regulators */
  443. while (--i >= 0) {
  444. info = &db8500_regulator_info[i];
  445. regulator_unregister(info->rdev);
  446. }
  447. return err;
  448. }
  449. dev_dbg(rdev_get_dev(info->rdev),
  450. "regulator-%s-probed\n", info->desc.name);
  451. }
  452. return 0;
  453. }
  454. static int __exit db8500_regulator_remove(struct platform_device *pdev)
  455. {
  456. int i;
  457. for (i = 0; i < ARRAY_SIZE(db8500_regulator_info); i++) {
  458. struct db8500_regulator_info *info;
  459. info = &db8500_regulator_info[i];
  460. dev_vdbg(rdev_get_dev(info->rdev),
  461. "regulator-%s-remove\n", info->desc.name);
  462. regulator_unregister(info->rdev);
  463. }
  464. return 0;
  465. }
  466. static struct platform_driver db8500_regulator_driver = {
  467. .driver = {
  468. .name = "db8500-prcmu-regulators",
  469. .owner = THIS_MODULE,
  470. },
  471. .probe = db8500_regulator_probe,
  472. .remove = __exit_p(db8500_regulator_remove),
  473. };
  474. static int __init db8500_regulator_init(void)
  475. {
  476. return platform_driver_register(&db8500_regulator_driver);
  477. }
  478. static void __exit db8500_regulator_exit(void)
  479. {
  480. platform_driver_unregister(&db8500_regulator_driver);
  481. }
  482. arch_initcall(db8500_regulator_init);
  483. module_exit(db8500_regulator_exit);
  484. MODULE_AUTHOR("STMicroelectronics/ST-Ericsson");
  485. MODULE_DESCRIPTION("DB8500 regulator driver");
  486. MODULE_LICENSE("GPL v2");