tps65217-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * tps65217-regulator.c
  3. *
  4. * Regulator driver for TPS65217 PMIC
  5. *
  6. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/init.h>
  21. #include <linux/err.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regulator/driver.h>
  24. #include <linux/regulator/machine.h>
  25. #include <linux/mfd/tps65217.h>
  26. #define TPS65217_REGULATOR(_name, _id, _ops, _n) \
  27. { \
  28. .name = _name, \
  29. .id = _id, \
  30. .ops = &_ops, \
  31. .n_voltages = _n, \
  32. .type = REGULATOR_VOLTAGE, \
  33. .owner = THIS_MODULE, \
  34. } \
  35. #define TPS65217_INFO(_nm, _min, _max, _f1, _f2, _t, _n, _em, _vr, _vm) \
  36. { \
  37. .name = _nm, \
  38. .min_uV = _min, \
  39. .max_uV = _max, \
  40. .vsel_to_uv = _f1, \
  41. .uv_to_vsel = _f2, \
  42. .table = _t, \
  43. .table_len = _n, \
  44. .enable_mask = _em, \
  45. .set_vout_reg = _vr, \
  46. .set_vout_mask = _vm, \
  47. }
  48. static const int LDO1_VSEL_table[] = {
  49. 1000000, 1100000, 1200000, 1250000,
  50. 1300000, 1350000, 1400000, 1500000,
  51. 1600000, 1800000, 2500000, 2750000,
  52. 2800000, 3000000, 3100000, 3300000,
  53. };
  54. static int tps65217_vsel_to_uv1(unsigned int vsel)
  55. {
  56. int uV = 0;
  57. if (vsel > 63)
  58. return -EINVAL;
  59. if (vsel <= 24)
  60. uV = vsel * 25000 + 900000;
  61. else if (vsel <= 52)
  62. uV = (vsel - 24) * 50000 + 1500000;
  63. else if (vsel < 56)
  64. uV = (vsel - 52) * 100000 + 2900000;
  65. else
  66. uV = 3300000;
  67. return uV;
  68. }
  69. static int tps65217_uv_to_vsel1(int uV, unsigned int *vsel)
  70. {
  71. if ((uV < 0) && (uV > 3300000))
  72. return -EINVAL;
  73. if (uV <= 1500000)
  74. *vsel = DIV_ROUND_UP(uV - 900000, 25000);
  75. else if (uV <= 2900000)
  76. *vsel = 24 + DIV_ROUND_UP(uV - 1500000, 50000);
  77. else if (uV < 3300000)
  78. *vsel = 52 + DIV_ROUND_UP(uV - 2900000, 100000);
  79. else
  80. *vsel = 56;
  81. return 0;
  82. }
  83. static int tps65217_vsel_to_uv2(unsigned int vsel)
  84. {
  85. int uV = 0;
  86. if (vsel > 31)
  87. return -EINVAL;
  88. if (vsel <= 8)
  89. uV = vsel * 50000 + 1500000;
  90. else if (vsel <= 13)
  91. uV = (vsel - 8) * 100000 + 1900000;
  92. else
  93. uV = (vsel - 13) * 50000 + 2400000;
  94. return uV;
  95. }
  96. static int tps65217_uv_to_vsel2(int uV, unsigned int *vsel)
  97. {
  98. if ((uV < 0) && (uV > 3300000))
  99. return -EINVAL;
  100. if (uV <= 1900000)
  101. *vsel = DIV_ROUND_UP(uV - 1500000, 50000);
  102. else if (uV <= 2400000)
  103. *vsel = 8 + DIV_ROUND_UP(uV - 1900000, 100000);
  104. else
  105. *vsel = 13 + DIV_ROUND_UP(uV - 2400000, 50000);
  106. return 0;
  107. }
  108. static struct tps_info tps65217_pmic_regs[] = {
  109. TPS65217_INFO("DCDC1", 900000, 1800000, tps65217_vsel_to_uv1,
  110. tps65217_uv_to_vsel1, NULL, 64, TPS65217_ENABLE_DC1_EN,
  111. TPS65217_REG_DEFDCDC1, TPS65217_DEFDCDCX_DCDC_MASK),
  112. TPS65217_INFO("DCDC2", 900000, 3300000, tps65217_vsel_to_uv1,
  113. tps65217_uv_to_vsel1, NULL, 64, TPS65217_ENABLE_DC2_EN,
  114. TPS65217_REG_DEFDCDC2, TPS65217_DEFDCDCX_DCDC_MASK),
  115. TPS65217_INFO("DCDC3", 900000, 1500000, tps65217_vsel_to_uv1,
  116. tps65217_uv_to_vsel1, NULL, 64, TPS65217_ENABLE_DC3_EN,
  117. TPS65217_REG_DEFDCDC3, TPS65217_DEFDCDCX_DCDC_MASK),
  118. TPS65217_INFO("LDO1", 1000000, 3300000, NULL, NULL, LDO1_VSEL_table,
  119. 16, TPS65217_ENABLE_LDO1_EN, TPS65217_REG_DEFLDO1,
  120. TPS65217_DEFLDO1_LDO1_MASK),
  121. TPS65217_INFO("LDO2", 900000, 3300000, tps65217_vsel_to_uv1,
  122. tps65217_uv_to_vsel1, NULL, 64, TPS65217_ENABLE_LDO2_EN,
  123. TPS65217_REG_DEFLDO2, TPS65217_DEFLDO2_LDO2_MASK),
  124. TPS65217_INFO("LDO3", 1800000, 3300000, tps65217_vsel_to_uv2,
  125. tps65217_uv_to_vsel2, NULL, 32,
  126. TPS65217_ENABLE_LS1_EN | TPS65217_DEFLDO3_LDO3_EN,
  127. TPS65217_REG_DEFLS1, TPS65217_DEFLDO3_LDO3_MASK),
  128. TPS65217_INFO("LDO4", 1800000, 3300000, tps65217_vsel_to_uv2,
  129. tps65217_uv_to_vsel2, NULL, 32,
  130. TPS65217_ENABLE_LS2_EN | TPS65217_DEFLDO4_LDO4_EN,
  131. TPS65217_REG_DEFLS2, TPS65217_DEFLDO4_LDO4_MASK),
  132. };
  133. static int tps65217_pmic_dcdc_is_enabled(struct regulator_dev *dev)
  134. {
  135. int ret;
  136. struct tps65217 *tps = rdev_get_drvdata(dev);
  137. unsigned int data, dcdc = rdev_get_id(dev);
  138. if (dcdc < TPS65217_DCDC_1 || dcdc > TPS65217_DCDC_3)
  139. return -EINVAL;
  140. ret = tps65217_reg_read(tps, TPS65217_REG_ENABLE, &data);
  141. if (ret)
  142. return ret;
  143. return (data & tps->info[dcdc]->enable_mask) ? 1 : 0;
  144. }
  145. static int tps65217_pmic_ldo_is_enabled(struct regulator_dev *dev)
  146. {
  147. int ret;
  148. struct tps65217 *tps = rdev_get_drvdata(dev);
  149. unsigned int data, ldo = rdev_get_id(dev);
  150. if (ldo < TPS65217_LDO_1 || ldo > TPS65217_LDO_4)
  151. return -EINVAL;
  152. ret = tps65217_reg_read(tps, TPS65217_REG_ENABLE, &data);
  153. if (ret)
  154. return ret;
  155. return (data & tps->info[ldo]->enable_mask) ? 1 : 0;
  156. }
  157. static int tps65217_pmic_dcdc_enable(struct regulator_dev *dev)
  158. {
  159. struct tps65217 *tps = rdev_get_drvdata(dev);
  160. unsigned int dcdc = rdev_get_id(dev);
  161. if (dcdc < TPS65217_DCDC_1 || dcdc > TPS65217_DCDC_3)
  162. return -EINVAL;
  163. /* Enable the regulator and password protection is level 1 */
  164. return tps65217_set_bits(tps, TPS65217_REG_ENABLE,
  165. tps->info[dcdc]->enable_mask,
  166. tps->info[dcdc]->enable_mask,
  167. TPS65217_PROTECT_L1);
  168. }
  169. static int tps65217_pmic_dcdc_disable(struct regulator_dev *dev)
  170. {
  171. struct tps65217 *tps = rdev_get_drvdata(dev);
  172. unsigned int dcdc = rdev_get_id(dev);
  173. if (dcdc < TPS65217_DCDC_1 || dcdc > TPS65217_DCDC_3)
  174. return -EINVAL;
  175. /* Disable the regulator and password protection is level 1 */
  176. return tps65217_clear_bits(tps, TPS65217_REG_ENABLE,
  177. tps->info[dcdc]->enable_mask, TPS65217_PROTECT_L1);
  178. }
  179. static int tps65217_pmic_ldo_enable(struct regulator_dev *dev)
  180. {
  181. struct tps65217 *tps = rdev_get_drvdata(dev);
  182. unsigned int ldo = rdev_get_id(dev);
  183. if (ldo < TPS65217_LDO_1 || ldo > TPS65217_LDO_4)
  184. return -EINVAL;
  185. /* Enable the regulator and password protection is level 1 */
  186. return tps65217_set_bits(tps, TPS65217_REG_ENABLE,
  187. tps->info[ldo]->enable_mask,
  188. tps->info[ldo]->enable_mask,
  189. TPS65217_PROTECT_L1);
  190. }
  191. static int tps65217_pmic_ldo_disable(struct regulator_dev *dev)
  192. {
  193. struct tps65217 *tps = rdev_get_drvdata(dev);
  194. unsigned int ldo = rdev_get_id(dev);
  195. if (ldo < TPS65217_LDO_1 || ldo > TPS65217_LDO_4)
  196. return -EINVAL;
  197. /* Disable the regulator and password protection is level 1 */
  198. return tps65217_clear_bits(tps, TPS65217_REG_ENABLE,
  199. tps->info[ldo]->enable_mask, TPS65217_PROTECT_L1);
  200. }
  201. static int tps65217_pmic_dcdc_get_voltage_sel(struct regulator_dev *dev)
  202. {
  203. int ret;
  204. struct tps65217 *tps = rdev_get_drvdata(dev);
  205. unsigned int selector, dcdc = rdev_get_id(dev);
  206. if (dcdc < TPS65217_DCDC_1 || dcdc > TPS65217_DCDC_3)
  207. return -EINVAL;
  208. ret = tps65217_reg_read(tps, tps->info[dcdc]->set_vout_reg, &selector);
  209. if (ret)
  210. return ret;
  211. selector &= tps->info[dcdc]->set_vout_mask;
  212. return selector;
  213. }
  214. static int tps65217_pmic_dcdc_set_voltage(struct regulator_dev *dev,
  215. int min_uV, int max_uV, unsigned *selector)
  216. {
  217. int ret;
  218. struct tps65217 *tps = rdev_get_drvdata(dev);
  219. unsigned int dcdc = rdev_get_id(dev);
  220. if (dcdc < TPS65217_DCDC_1 || dcdc > TPS65217_DCDC_3)
  221. return -EINVAL;
  222. if (min_uV < tps->info[dcdc]->min_uV
  223. || min_uV > tps->info[dcdc]->max_uV)
  224. return -EINVAL;
  225. if (max_uV < tps->info[dcdc]->min_uV
  226. || max_uV > tps->info[dcdc]->max_uV)
  227. return -EINVAL;
  228. ret = tps->info[dcdc]->uv_to_vsel(min_uV, selector);
  229. if (ret)
  230. return ret;
  231. /* Set the voltage based on vsel value and write protect level is 2 */
  232. ret = tps65217_set_bits(tps, tps->info[dcdc]->set_vout_reg,
  233. tps->info[dcdc]->set_vout_mask,
  234. *selector, TPS65217_PROTECT_L2);
  235. if (ret)
  236. return ret;
  237. /* Set GO bit to initiate voltage transistion */
  238. return tps65217_set_bits(tps, TPS65217_REG_DEFSLEW,
  239. TPS65217_DEFSLEW_GO, TPS65217_DEFSLEW_GO,
  240. TPS65217_PROTECT_L2);
  241. }
  242. static int tps65217_pmic_ldo_get_voltage_sel(struct regulator_dev *dev)
  243. {
  244. int ret;
  245. struct tps65217 *tps = rdev_get_drvdata(dev);
  246. unsigned int selector, ldo = rdev_get_id(dev);
  247. if (ldo < TPS65217_LDO_1 || ldo > TPS65217_LDO_4)
  248. return -EINVAL;
  249. ret = tps65217_reg_read(tps, tps->info[ldo]->set_vout_reg, &selector);
  250. if (ret)
  251. return ret;
  252. selector &= tps->info[ldo]->set_vout_mask;
  253. return selector;
  254. }
  255. static int tps65217_pmic_ldo_set_voltage_sel(struct regulator_dev *dev,
  256. unsigned selector)
  257. {
  258. struct tps65217 *tps = rdev_get_drvdata(dev);
  259. int ldo = rdev_get_id(dev);
  260. if (ldo != TPS65217_LDO_1)
  261. return -EINVAL;
  262. if (selector >= tps->info[ldo]->table_len)
  263. return -EINVAL;
  264. /* Set the voltage based on vsel value and write protect level is 2 */
  265. return tps65217_set_bits(tps, tps->info[ldo]->set_vout_reg,
  266. tps->info[ldo]->set_vout_mask,
  267. selector, TPS65217_PROTECT_L2);
  268. }
  269. static int tps65217_pmic_ldo_set_voltage(struct regulator_dev *dev,
  270. int min_uV, int max_uV, unsigned *selector)
  271. {
  272. int ret;
  273. struct tps65217 *tps = rdev_get_drvdata(dev);
  274. unsigned int ldo = rdev_get_id(dev);
  275. if (ldo < TPS65217_LDO_2 || ldo > TPS65217_LDO_4)
  276. return -EINVAL;
  277. if (min_uV < tps->info[ldo]->min_uV
  278. || min_uV > tps->info[ldo]->max_uV)
  279. return -EINVAL;
  280. if (max_uV < tps->info[ldo]->min_uV
  281. || max_uV > tps->info[ldo]->max_uV)
  282. return -EINVAL;
  283. ret = tps->info[ldo]->uv_to_vsel(min_uV, selector);
  284. if (ret)
  285. return ret;
  286. /* Set the voltage based on vsel value and write protect level is 2 */
  287. return tps65217_set_bits(tps, tps->info[ldo]->set_vout_reg,
  288. tps->info[ldo]->set_vout_mask,
  289. *selector, TPS65217_PROTECT_L2);
  290. }
  291. static int tps65217_pmic_dcdc_list_voltage(struct regulator_dev *dev,
  292. unsigned selector)
  293. {
  294. struct tps65217 *tps = rdev_get_drvdata(dev);
  295. unsigned int dcdc = rdev_get_id(dev);
  296. if (dcdc < TPS65217_DCDC_1 || dcdc > TPS65217_DCDC_3)
  297. return -EINVAL;
  298. if (selector >= tps->info[dcdc]->table_len)
  299. return -EINVAL;
  300. return tps->info[dcdc]->vsel_to_uv(selector);
  301. }
  302. static int tps65217_pmic_ldo_list_voltage(struct regulator_dev *dev,
  303. unsigned selector)
  304. {
  305. struct tps65217 *tps = rdev_get_drvdata(dev);
  306. unsigned int ldo = rdev_get_id(dev);
  307. if (ldo < TPS65217_LDO_1 || ldo > TPS65217_LDO_4)
  308. return -EINVAL;
  309. if (selector >= tps->info[ldo]->table_len)
  310. return -EINVAL;
  311. if (tps->info[ldo]->table)
  312. return tps->info[ldo]->table[selector];
  313. return tps->info[ldo]->vsel_to_uv(selector);
  314. }
  315. /* Operations permitted on DCDCx */
  316. static struct regulator_ops tps65217_pmic_dcdc_ops = {
  317. .is_enabled = tps65217_pmic_dcdc_is_enabled,
  318. .enable = tps65217_pmic_dcdc_enable,
  319. .disable = tps65217_pmic_dcdc_disable,
  320. .get_voltage_sel = tps65217_pmic_dcdc_get_voltage_sel,
  321. .set_voltage = tps65217_pmic_dcdc_set_voltage,
  322. .list_voltage = tps65217_pmic_dcdc_list_voltage,
  323. };
  324. /* Operations permitted on LDO1 */
  325. static struct regulator_ops tps65217_pmic_ldo1_ops = {
  326. .is_enabled = tps65217_pmic_ldo_is_enabled,
  327. .enable = tps65217_pmic_ldo_enable,
  328. .disable = tps65217_pmic_ldo_disable,
  329. .get_voltage_sel = tps65217_pmic_ldo_get_voltage_sel,
  330. .set_voltage_sel = tps65217_pmic_ldo_set_voltage_sel,
  331. .list_voltage = tps65217_pmic_ldo_list_voltage,
  332. };
  333. /* Operations permitted on LDO2, LDO3 and LDO4 */
  334. static struct regulator_ops tps65217_pmic_ldo234_ops = {
  335. .is_enabled = tps65217_pmic_ldo_is_enabled,
  336. .enable = tps65217_pmic_ldo_enable,
  337. .disable = tps65217_pmic_ldo_disable,
  338. .get_voltage_sel = tps65217_pmic_ldo_get_voltage_sel,
  339. .set_voltage = tps65217_pmic_ldo_set_voltage,
  340. .list_voltage = tps65217_pmic_ldo_list_voltage,
  341. };
  342. static struct regulator_desc regulators[] = {
  343. TPS65217_REGULATOR("DCDC1", TPS65217_DCDC_1,
  344. tps65217_pmic_dcdc_ops, 64),
  345. TPS65217_REGULATOR("DCDC2",TPS65217_DCDC_2,
  346. tps65217_pmic_dcdc_ops, 64),
  347. TPS65217_REGULATOR("DCDC3", TPS65217_DCDC_3,
  348. tps65217_pmic_dcdc_ops, 64),
  349. TPS65217_REGULATOR("LDO1", TPS65217_LDO_1,
  350. tps65217_pmic_ldo1_ops, 16),
  351. TPS65217_REGULATOR("LDO2", TPS65217_LDO_2,
  352. tps65217_pmic_ldo234_ops, 64),
  353. TPS65217_REGULATOR("LDO3", TPS65217_LDO_3,
  354. tps65217_pmic_ldo234_ops, 32),
  355. TPS65217_REGULATOR("LDO4", TPS65217_LDO_4,
  356. tps65217_pmic_ldo234_ops, 32),
  357. };
  358. static int __devinit tps65217_regulator_probe(struct platform_device *pdev)
  359. {
  360. struct regulator_dev *rdev;
  361. struct tps65217 *tps;
  362. struct tps_info *info = &tps65217_pmic_regs[pdev->id];
  363. /* Already set by core driver */
  364. tps = dev_to_tps65217(pdev->dev.parent);
  365. tps->info[pdev->id] = info;
  366. rdev = regulator_register(&regulators[pdev->id], &pdev->dev,
  367. pdev->dev.platform_data, tps, NULL);
  368. if (IS_ERR(rdev))
  369. return PTR_ERR(rdev);
  370. platform_set_drvdata(pdev, rdev);
  371. return 0;
  372. }
  373. static int __devexit tps65217_regulator_remove(struct platform_device *pdev)
  374. {
  375. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  376. platform_set_drvdata(pdev, NULL);
  377. regulator_unregister(rdev);
  378. return 0;
  379. }
  380. static struct platform_driver tps65217_regulator_driver = {
  381. .driver = {
  382. .name = "tps65217-pmic",
  383. },
  384. .probe = tps65217_regulator_probe,
  385. .remove = __devexit_p(tps65217_regulator_remove),
  386. };
  387. static int __init tps65217_regulator_init(void)
  388. {
  389. return platform_driver_register(&tps65217_regulator_driver);
  390. }
  391. subsys_initcall(tps65217_regulator_init);
  392. static void __exit tps65217_regulator_exit(void)
  393. {
  394. platform_driver_unregister(&tps65217_regulator_driver);
  395. }
  396. module_exit(tps65217_regulator_exit);
  397. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  398. MODULE_DESCRIPTION("TPS65217 voltage regulator driver");
  399. MODULE_ALIAS("platform:tps65217-pmic");
  400. MODULE_LICENSE("GPL v2");