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