tps65217-regulator.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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_is_enabled(struct regulator_dev *dev)
  134. {
  135. int ret;
  136. struct tps65217 *tps = rdev_get_drvdata(dev);
  137. unsigned int data, rid = rdev_get_id(dev);
  138. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  139. return -EINVAL;
  140. ret = tps65217_reg_read(tps, TPS65217_REG_ENABLE, &data);
  141. if (ret)
  142. return ret;
  143. return (data & tps->info[rid]->enable_mask) ? 1 : 0;
  144. }
  145. static int tps65217_pmic_enable(struct regulator_dev *dev)
  146. {
  147. struct tps65217 *tps = rdev_get_drvdata(dev);
  148. unsigned int rid = rdev_get_id(dev);
  149. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  150. return -EINVAL;
  151. /* Enable the regulator and password protection is level 1 */
  152. return tps65217_set_bits(tps, TPS65217_REG_ENABLE,
  153. tps->info[rid]->enable_mask,
  154. tps->info[rid]->enable_mask,
  155. TPS65217_PROTECT_L1);
  156. }
  157. static int tps65217_pmic_disable(struct regulator_dev *dev)
  158. {
  159. struct tps65217 *tps = rdev_get_drvdata(dev);
  160. unsigned int rid = rdev_get_id(dev);
  161. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  162. return -EINVAL;
  163. /* Disable the regulator and password protection is level 1 */
  164. return tps65217_clear_bits(tps, TPS65217_REG_ENABLE,
  165. tps->info[rid]->enable_mask, TPS65217_PROTECT_L1);
  166. }
  167. static int tps65217_pmic_get_voltage_sel(struct regulator_dev *dev)
  168. {
  169. int ret;
  170. struct tps65217 *tps = rdev_get_drvdata(dev);
  171. unsigned int selector, rid = rdev_get_id(dev);
  172. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  173. return -EINVAL;
  174. ret = tps65217_reg_read(tps, tps->info[rid]->set_vout_reg, &selector);
  175. if (ret)
  176. return ret;
  177. selector &= tps->info[rid]->set_vout_mask;
  178. return selector;
  179. }
  180. static int tps65217_pmic_ldo1_set_voltage_sel(struct regulator_dev *dev,
  181. unsigned selector)
  182. {
  183. struct tps65217 *tps = rdev_get_drvdata(dev);
  184. int ldo = rdev_get_id(dev);
  185. if (ldo != TPS65217_LDO_1)
  186. return -EINVAL;
  187. if (selector >= tps->info[ldo]->table_len)
  188. return -EINVAL;
  189. /* Set the voltage based on vsel value and write protect level is 2 */
  190. return tps65217_set_bits(tps, tps->info[ldo]->set_vout_reg,
  191. tps->info[ldo]->set_vout_mask,
  192. selector, TPS65217_PROTECT_L2);
  193. }
  194. static int tps65217_pmic_set_voltage(struct regulator_dev *dev,
  195. int min_uV, int max_uV, unsigned *selector)
  196. {
  197. int ret;
  198. struct tps65217 *tps = rdev_get_drvdata(dev);
  199. unsigned int rid = rdev_get_id(dev);
  200. /* LDO1 implements set_voltage_sel callback */
  201. if (rid == TPS65217_LDO_1)
  202. return -EINVAL;
  203. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  204. return -EINVAL;
  205. if (min_uV < tps->info[rid]->min_uV
  206. || min_uV > tps->info[rid]->max_uV)
  207. return -EINVAL;
  208. if (max_uV < tps->info[rid]->min_uV
  209. || max_uV > tps->info[rid]->max_uV)
  210. return -EINVAL;
  211. ret = tps->info[rid]->uv_to_vsel(min_uV, selector);
  212. if (ret)
  213. return ret;
  214. /* Set the voltage based on vsel value and write protect level is 2 */
  215. ret = tps65217_set_bits(tps, tps->info[rid]->set_vout_reg,
  216. tps->info[rid]->set_vout_mask,
  217. *selector, TPS65217_PROTECT_L2);
  218. /* Set GO bit for DCDCx to initiate voltage transistion */
  219. switch (rid) {
  220. case TPS65217_DCDC_1 ... TPS65217_DCDC_3:
  221. ret = tps65217_set_bits(tps, TPS65217_REG_DEFSLEW,
  222. TPS65217_DEFSLEW_GO, TPS65217_DEFSLEW_GO,
  223. TPS65217_PROTECT_L2);
  224. break;
  225. }
  226. return ret;
  227. }
  228. static int tps65217_pmic_list_voltage(struct regulator_dev *dev,
  229. unsigned selector)
  230. {
  231. struct tps65217 *tps = rdev_get_drvdata(dev);
  232. unsigned int rid = rdev_get_id(dev);
  233. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  234. return -EINVAL;
  235. if (selector >= tps->info[rid]->table_len)
  236. return -EINVAL;
  237. if (tps->info[rid]->table)
  238. return tps->info[rid]->table[selector];
  239. return tps->info[rid]->vsel_to_uv(selector);
  240. }
  241. /* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */
  242. static struct regulator_ops tps65217_pmic_ops = {
  243. .is_enabled = tps65217_pmic_is_enabled,
  244. .enable = tps65217_pmic_enable,
  245. .disable = tps65217_pmic_disable,
  246. .get_voltage_sel = tps65217_pmic_get_voltage_sel,
  247. .set_voltage = tps65217_pmic_set_voltage,
  248. .list_voltage = tps65217_pmic_list_voltage,
  249. };
  250. /* Operations permitted on LDO1 */
  251. static struct regulator_ops tps65217_pmic_ldo1_ops = {
  252. .is_enabled = tps65217_pmic_is_enabled,
  253. .enable = tps65217_pmic_enable,
  254. .disable = tps65217_pmic_disable,
  255. .get_voltage_sel = tps65217_pmic_get_voltage_sel,
  256. .set_voltage_sel = tps65217_pmic_ldo1_set_voltage_sel,
  257. .list_voltage = tps65217_pmic_list_voltage,
  258. };
  259. static struct regulator_desc regulators[] = {
  260. TPS65217_REGULATOR("DCDC1", TPS65217_DCDC_1, tps65217_pmic_ops, 64),
  261. TPS65217_REGULATOR("DCDC2", TPS65217_DCDC_2, tps65217_pmic_ops, 64),
  262. TPS65217_REGULATOR("DCDC3", TPS65217_DCDC_3, tps65217_pmic_ops, 64),
  263. TPS65217_REGULATOR("LDO1", TPS65217_LDO_1, tps65217_pmic_ldo1_ops, 16),
  264. TPS65217_REGULATOR("LDO2", TPS65217_LDO_2, tps65217_pmic_ops, 64),
  265. TPS65217_REGULATOR("LDO3", TPS65217_LDO_3, tps65217_pmic_ops, 32),
  266. TPS65217_REGULATOR("LDO4", TPS65217_LDO_4, tps65217_pmic_ops, 32),
  267. };
  268. static int __devinit tps65217_regulator_probe(struct platform_device *pdev)
  269. {
  270. struct regulator_dev *rdev;
  271. struct tps65217 *tps;
  272. struct tps_info *info = &tps65217_pmic_regs[pdev->id];
  273. /* Already set by core driver */
  274. tps = dev_to_tps65217(pdev->dev.parent);
  275. tps->info[pdev->id] = info;
  276. rdev = regulator_register(&regulators[pdev->id], &pdev->dev,
  277. pdev->dev.platform_data, tps, NULL);
  278. if (IS_ERR(rdev))
  279. return PTR_ERR(rdev);
  280. platform_set_drvdata(pdev, rdev);
  281. return 0;
  282. }
  283. static int __devexit tps65217_regulator_remove(struct platform_device *pdev)
  284. {
  285. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  286. platform_set_drvdata(pdev, NULL);
  287. regulator_unregister(rdev);
  288. return 0;
  289. }
  290. static struct platform_driver tps65217_regulator_driver = {
  291. .driver = {
  292. .name = "tps65217-pmic",
  293. },
  294. .probe = tps65217_regulator_probe,
  295. .remove = __devexit_p(tps65217_regulator_remove),
  296. };
  297. static int __init tps65217_regulator_init(void)
  298. {
  299. return platform_driver_register(&tps65217_regulator_driver);
  300. }
  301. subsys_initcall(tps65217_regulator_init);
  302. static void __exit tps65217_regulator_exit(void)
  303. {
  304. platform_driver_unregister(&tps65217_regulator_driver);
  305. }
  306. module_exit(tps65217_regulator_exit);
  307. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  308. MODULE_DESCRIPTION("TPS65217 voltage regulator driver");
  309. MODULE_ALIAS("platform:tps65217-pmic");
  310. MODULE_LICENSE("GPL v2");