tps65217-regulator.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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, _vr, _vm, _em, _t) \
  27. { \
  28. .name = _name, \
  29. .id = _id, \
  30. .ops = &_ops, \
  31. .n_voltages = _n, \
  32. .type = REGULATOR_VOLTAGE, \
  33. .owner = THIS_MODULE, \
  34. .vsel_reg = _vr, \
  35. .vsel_mask = _vm, \
  36. .enable_reg = TPS65217_REG_ENABLE, \
  37. .enable_mask = _em, \
  38. .volt_table = _t, \
  39. } \
  40. #define TPS65217_INFO(_nm, _min, _max, _f1, _f2) \
  41. { \
  42. .name = _nm, \
  43. .min_uV = _min, \
  44. .max_uV = _max, \
  45. .vsel_to_uv = _f1, \
  46. .uv_to_vsel = _f2, \
  47. }
  48. static const unsigned 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),
  111. TPS65217_INFO("DCDC2", 900000, 3300000, tps65217_vsel_to_uv1,
  112. tps65217_uv_to_vsel1),
  113. TPS65217_INFO("DCDC3", 900000, 1500000, tps65217_vsel_to_uv1,
  114. tps65217_uv_to_vsel1),
  115. TPS65217_INFO("LDO1", 1000000, 3300000, NULL, NULL),
  116. TPS65217_INFO("LDO2", 900000, 3300000, tps65217_vsel_to_uv1,
  117. tps65217_uv_to_vsel1),
  118. TPS65217_INFO("LDO3", 1800000, 3300000, tps65217_vsel_to_uv2,
  119. tps65217_uv_to_vsel2),
  120. TPS65217_INFO("LDO4", 1800000, 3300000, tps65217_vsel_to_uv2,
  121. tps65217_uv_to_vsel2),
  122. };
  123. static int tps65217_pmic_enable(struct regulator_dev *dev)
  124. {
  125. struct tps65217 *tps = rdev_get_drvdata(dev);
  126. unsigned int rid = rdev_get_id(dev);
  127. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  128. return -EINVAL;
  129. /* Enable the regulator and password protection is level 1 */
  130. return tps65217_set_bits(tps, TPS65217_REG_ENABLE,
  131. dev->desc->enable_mask, dev->desc->enable_mask,
  132. TPS65217_PROTECT_L1);
  133. }
  134. static int tps65217_pmic_disable(struct regulator_dev *dev)
  135. {
  136. struct tps65217 *tps = rdev_get_drvdata(dev);
  137. unsigned int rid = rdev_get_id(dev);
  138. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  139. return -EINVAL;
  140. /* Disable the regulator and password protection is level 1 */
  141. return tps65217_clear_bits(tps, TPS65217_REG_ENABLE,
  142. dev->desc->enable_mask, TPS65217_PROTECT_L1);
  143. }
  144. static int tps65217_pmic_set_voltage_sel(struct regulator_dev *dev,
  145. unsigned selector)
  146. {
  147. int ret;
  148. struct tps65217 *tps = rdev_get_drvdata(dev);
  149. unsigned int rid = rdev_get_id(dev);
  150. /* Set the voltage based on vsel value and write protect level is 2 */
  151. ret = tps65217_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
  152. selector, TPS65217_PROTECT_L2);
  153. /* Set GO bit for DCDCx to initiate voltage transistion */
  154. switch (rid) {
  155. case TPS65217_DCDC_1 ... TPS65217_DCDC_3:
  156. ret = tps65217_set_bits(tps, TPS65217_REG_DEFSLEW,
  157. TPS65217_DEFSLEW_GO, TPS65217_DEFSLEW_GO,
  158. TPS65217_PROTECT_L2);
  159. break;
  160. }
  161. return ret;
  162. }
  163. static int tps65217_pmic_map_voltage(struct regulator_dev *dev,
  164. int min_uV, int max_uV)
  165. {
  166. struct tps65217 *tps = rdev_get_drvdata(dev);
  167. unsigned int sel, rid = rdev_get_id(dev);
  168. int ret;
  169. /* LDO1 uses regulator_map_voltage_iterate() */
  170. if (rid == TPS65217_LDO_1)
  171. return -EINVAL;
  172. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  173. return -EINVAL;
  174. if (min_uV < tps->info[rid]->min_uV)
  175. min_uV = tps->info[rid]->min_uV;
  176. if (max_uV < tps->info[rid]->min_uV || min_uV > tps->info[rid]->max_uV)
  177. return -EINVAL;
  178. ret = tps->info[rid]->uv_to_vsel(min_uV, &sel);
  179. if (ret)
  180. return ret;
  181. return sel;
  182. }
  183. static int tps65217_pmic_list_voltage(struct regulator_dev *dev,
  184. unsigned selector)
  185. {
  186. struct tps65217 *tps = rdev_get_drvdata(dev);
  187. unsigned int rid = rdev_get_id(dev);
  188. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  189. return -EINVAL;
  190. if (selector >= dev->desc->n_voltages)
  191. return -EINVAL;
  192. return tps->info[rid]->vsel_to_uv(selector);
  193. }
  194. /* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */
  195. static struct regulator_ops tps65217_pmic_ops = {
  196. .is_enabled = regulator_is_enabled_regmap,
  197. .enable = tps65217_pmic_enable,
  198. .disable = tps65217_pmic_disable,
  199. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  200. .set_voltage_sel = tps65217_pmic_set_voltage_sel,
  201. .list_voltage = tps65217_pmic_list_voltage,
  202. .map_voltage = tps65217_pmic_map_voltage,
  203. };
  204. /* Operations permitted on LDO1 */
  205. static struct regulator_ops tps65217_pmic_ldo1_ops = {
  206. .is_enabled = regulator_is_enabled_regmap,
  207. .enable = tps65217_pmic_enable,
  208. .disable = tps65217_pmic_disable,
  209. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  210. .set_voltage_sel = tps65217_pmic_set_voltage_sel,
  211. .list_voltage = regulator_list_voltage_table,
  212. };
  213. static const struct regulator_desc regulators[] = {
  214. TPS65217_REGULATOR("DCDC1", TPS65217_DCDC_1, tps65217_pmic_ops, 64,
  215. TPS65217_REG_DEFDCDC1, TPS65217_DEFDCDCX_DCDC_MASK,
  216. TPS65217_ENABLE_DC1_EN, NULL),
  217. TPS65217_REGULATOR("DCDC2", TPS65217_DCDC_2, tps65217_pmic_ops, 64,
  218. TPS65217_REG_DEFDCDC2, TPS65217_DEFDCDCX_DCDC_MASK,
  219. TPS65217_ENABLE_DC2_EN, NULL),
  220. TPS65217_REGULATOR("DCDC3", TPS65217_DCDC_3, tps65217_pmic_ops, 64,
  221. TPS65217_REG_DEFDCDC3, TPS65217_DEFDCDCX_DCDC_MASK,
  222. TPS65217_ENABLE_DC3_EN, NULL),
  223. TPS65217_REGULATOR("LDO1", TPS65217_LDO_1, tps65217_pmic_ldo1_ops, 16,
  224. TPS65217_REG_DEFLDO1, TPS65217_DEFLDO1_LDO1_MASK,
  225. TPS65217_ENABLE_LDO1_EN, LDO1_VSEL_table),
  226. TPS65217_REGULATOR("LDO2", TPS65217_LDO_2, tps65217_pmic_ops, 64,
  227. TPS65217_REG_DEFLDO2, TPS65217_DEFLDO2_LDO2_MASK,
  228. TPS65217_ENABLE_LDO2_EN, NULL),
  229. TPS65217_REGULATOR("LDO3", TPS65217_LDO_3, tps65217_pmic_ops, 32,
  230. TPS65217_REG_DEFLS1, TPS65217_DEFLDO3_LDO3_MASK,
  231. TPS65217_ENABLE_LS1_EN | TPS65217_DEFLDO3_LDO3_EN,
  232. NULL),
  233. TPS65217_REGULATOR("LDO4", TPS65217_LDO_4, tps65217_pmic_ops, 32,
  234. TPS65217_REG_DEFLS2, TPS65217_DEFLDO4_LDO4_MASK,
  235. TPS65217_ENABLE_LS2_EN | TPS65217_DEFLDO4_LDO4_EN,
  236. NULL),
  237. };
  238. static int __devinit tps65217_regulator_probe(struct platform_device *pdev)
  239. {
  240. struct regulator_dev *rdev;
  241. struct tps65217 *tps;
  242. struct tps_info *info = &tps65217_pmic_regs[pdev->id];
  243. struct regulator_config config = { };
  244. /* Already set by core driver */
  245. tps = dev_to_tps65217(pdev->dev.parent);
  246. tps->info[pdev->id] = info;
  247. config.dev = &pdev->dev;
  248. config.of_node = pdev->dev.of_node;
  249. config.init_data = pdev->dev.platform_data;
  250. config.driver_data = tps;
  251. rdev = regulator_register(&regulators[pdev->id], &config);
  252. if (IS_ERR(rdev))
  253. return PTR_ERR(rdev);
  254. platform_set_drvdata(pdev, rdev);
  255. return 0;
  256. }
  257. static int __devexit tps65217_regulator_remove(struct platform_device *pdev)
  258. {
  259. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  260. platform_set_drvdata(pdev, NULL);
  261. regulator_unregister(rdev);
  262. return 0;
  263. }
  264. static struct platform_driver tps65217_regulator_driver = {
  265. .driver = {
  266. .name = "tps65217-pmic",
  267. },
  268. .probe = tps65217_regulator_probe,
  269. .remove = __devexit_p(tps65217_regulator_remove),
  270. };
  271. static int __init tps65217_regulator_init(void)
  272. {
  273. return platform_driver_register(&tps65217_regulator_driver);
  274. }
  275. subsys_initcall(tps65217_regulator_init);
  276. static void __exit tps65217_regulator_exit(void)
  277. {
  278. platform_driver_unregister(&tps65217_regulator_driver);
  279. }
  280. module_exit(tps65217_regulator_exit);
  281. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  282. MODULE_DESCRIPTION("TPS65217 voltage regulator driver");
  283. MODULE_ALIAS("platform:tps65217-pmic");
  284. MODULE_LICENSE("GPL v2");