tps65217-regulator.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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) \
  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. } \
  39. #define TPS65217_INFO(_nm, _min, _max, _f1, _f2, _t, _n)\
  40. { \
  41. .name = _nm, \
  42. .min_uV = _min, \
  43. .max_uV = _max, \
  44. .vsel_to_uv = _f1, \
  45. .uv_to_vsel = _f2, \
  46. .table = _t, \
  47. .table_len = _n, \
  48. }
  49. static const int LDO1_VSEL_table[] = {
  50. 1000000, 1100000, 1200000, 1250000,
  51. 1300000, 1350000, 1400000, 1500000,
  52. 1600000, 1800000, 2500000, 2750000,
  53. 2800000, 3000000, 3100000, 3300000,
  54. };
  55. static int tps65217_vsel_to_uv1(unsigned int vsel)
  56. {
  57. int uV = 0;
  58. if (vsel > 63)
  59. return -EINVAL;
  60. if (vsel <= 24)
  61. uV = vsel * 25000 + 900000;
  62. else if (vsel <= 52)
  63. uV = (vsel - 24) * 50000 + 1500000;
  64. else if (vsel < 56)
  65. uV = (vsel - 52) * 100000 + 2900000;
  66. else
  67. uV = 3300000;
  68. return uV;
  69. }
  70. static int tps65217_uv_to_vsel1(int uV, unsigned int *vsel)
  71. {
  72. if ((uV < 0) && (uV > 3300000))
  73. return -EINVAL;
  74. if (uV <= 1500000)
  75. *vsel = DIV_ROUND_UP(uV - 900000, 25000);
  76. else if (uV <= 2900000)
  77. *vsel = 24 + DIV_ROUND_UP(uV - 1500000, 50000);
  78. else if (uV < 3300000)
  79. *vsel = 52 + DIV_ROUND_UP(uV - 2900000, 100000);
  80. else
  81. *vsel = 56;
  82. return 0;
  83. }
  84. static int tps65217_vsel_to_uv2(unsigned int vsel)
  85. {
  86. int uV = 0;
  87. if (vsel > 31)
  88. return -EINVAL;
  89. if (vsel <= 8)
  90. uV = vsel * 50000 + 1500000;
  91. else if (vsel <= 13)
  92. uV = (vsel - 8) * 100000 + 1900000;
  93. else
  94. uV = (vsel - 13) * 50000 + 2400000;
  95. return uV;
  96. }
  97. static int tps65217_uv_to_vsel2(int uV, unsigned int *vsel)
  98. {
  99. if ((uV < 0) && (uV > 3300000))
  100. return -EINVAL;
  101. if (uV <= 1900000)
  102. *vsel = DIV_ROUND_UP(uV - 1500000, 50000);
  103. else if (uV <= 2400000)
  104. *vsel = 8 + DIV_ROUND_UP(uV - 1900000, 100000);
  105. else
  106. *vsel = 13 + DIV_ROUND_UP(uV - 2400000, 50000);
  107. return 0;
  108. }
  109. static struct tps_info tps65217_pmic_regs[] = {
  110. TPS65217_INFO("DCDC1", 900000, 1800000, tps65217_vsel_to_uv1,
  111. tps65217_uv_to_vsel1, NULL, 64),
  112. TPS65217_INFO("DCDC2", 900000, 3300000, tps65217_vsel_to_uv1,
  113. tps65217_uv_to_vsel1, NULL, 64),
  114. TPS65217_INFO("DCDC3", 900000, 1500000, tps65217_vsel_to_uv1,
  115. tps65217_uv_to_vsel1, NULL, 64),
  116. TPS65217_INFO("LDO1", 1000000, 3300000, NULL, NULL, LDO1_VSEL_table,
  117. 16),
  118. TPS65217_INFO("LDO2", 900000, 3300000, tps65217_vsel_to_uv1,
  119. tps65217_uv_to_vsel1, NULL, 64),
  120. TPS65217_INFO("LDO3", 1800000, 3300000, tps65217_vsel_to_uv2,
  121. tps65217_uv_to_vsel2, NULL, 32),
  122. TPS65217_INFO("LDO4", 1800000, 3300000, tps65217_vsel_to_uv2,
  123. tps65217_uv_to_vsel2, NULL, 32),
  124. };
  125. static int tps65217_pmic_enable(struct regulator_dev *dev)
  126. {
  127. struct tps65217 *tps = rdev_get_drvdata(dev);
  128. unsigned int rid = rdev_get_id(dev);
  129. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  130. return -EINVAL;
  131. /* Enable the regulator and password protection is level 1 */
  132. return tps65217_set_bits(tps, TPS65217_REG_ENABLE,
  133. dev->desc->enable_mask, dev->desc->enable_mask,
  134. TPS65217_PROTECT_L1);
  135. }
  136. static int tps65217_pmic_disable(struct regulator_dev *dev)
  137. {
  138. struct tps65217 *tps = rdev_get_drvdata(dev);
  139. unsigned int rid = rdev_get_id(dev);
  140. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  141. return -EINVAL;
  142. /* Disable the regulator and password protection is level 1 */
  143. return tps65217_clear_bits(tps, TPS65217_REG_ENABLE,
  144. dev->desc->enable_mask, TPS65217_PROTECT_L1);
  145. }
  146. static int tps65217_pmic_set_voltage_sel(struct regulator_dev *dev,
  147. unsigned selector)
  148. {
  149. int ret;
  150. struct tps65217 *tps = rdev_get_drvdata(dev);
  151. unsigned int rid = rdev_get_id(dev);
  152. /* Set the voltage based on vsel value and write protect level is 2 */
  153. ret = tps65217_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
  154. selector, TPS65217_PROTECT_L2);
  155. /* Set GO bit for DCDCx to initiate voltage transistion */
  156. switch (rid) {
  157. case TPS65217_DCDC_1 ... TPS65217_DCDC_3:
  158. ret = tps65217_set_bits(tps, TPS65217_REG_DEFSLEW,
  159. TPS65217_DEFSLEW_GO, TPS65217_DEFSLEW_GO,
  160. TPS65217_PROTECT_L2);
  161. break;
  162. }
  163. return ret;
  164. }
  165. static int tps65217_pmic_map_voltage(struct regulator_dev *dev,
  166. int min_uV, int max_uV)
  167. {
  168. struct tps65217 *tps = rdev_get_drvdata(dev);
  169. unsigned int sel, rid = rdev_get_id(dev);
  170. int ret;
  171. /* LDO1 uses regulator_map_voltage_iterate() */
  172. if (rid == TPS65217_LDO_1)
  173. return -EINVAL;
  174. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  175. return -EINVAL;
  176. if (min_uV < tps->info[rid]->min_uV || min_uV > tps->info[rid]->max_uV)
  177. return -EINVAL;
  178. if (max_uV < tps->info[rid]->min_uV || max_uV > tps->info[rid]->max_uV)
  179. return -EINVAL;
  180. ret = tps->info[rid]->uv_to_vsel(min_uV, &sel);
  181. if (ret)
  182. return ret;
  183. return sel;
  184. }
  185. static int tps65217_pmic_list_voltage(struct regulator_dev *dev,
  186. unsigned selector)
  187. {
  188. struct tps65217 *tps = rdev_get_drvdata(dev);
  189. unsigned int rid = rdev_get_id(dev);
  190. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  191. return -EINVAL;
  192. if (selector >= tps->info[rid]->table_len)
  193. return -EINVAL;
  194. if (tps->info[rid]->table)
  195. return tps->info[rid]->table[selector];
  196. return tps->info[rid]->vsel_to_uv(selector);
  197. }
  198. /* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */
  199. static struct regulator_ops tps65217_pmic_ops = {
  200. .is_enabled = regulator_is_enabled_regmap,
  201. .enable = tps65217_pmic_enable,
  202. .disable = tps65217_pmic_disable,
  203. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  204. .set_voltage_sel = tps65217_pmic_set_voltage_sel,
  205. .list_voltage = tps65217_pmic_list_voltage,
  206. .map_voltage = tps65217_pmic_map_voltage,
  207. };
  208. /* Operations permitted on LDO1 */
  209. static struct regulator_ops tps65217_pmic_ldo1_ops = {
  210. .is_enabled = regulator_is_enabled_regmap,
  211. .enable = tps65217_pmic_enable,
  212. .disable = tps65217_pmic_disable,
  213. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  214. .set_voltage_sel = tps65217_pmic_set_voltage_sel,
  215. .list_voltage = tps65217_pmic_list_voltage,
  216. };
  217. static const struct regulator_desc regulators[] = {
  218. TPS65217_REGULATOR("DCDC1", TPS65217_DCDC_1, tps65217_pmic_ops, 64,
  219. TPS65217_REG_DEFDCDC1, TPS65217_DEFDCDCX_DCDC_MASK,
  220. TPS65217_ENABLE_DC1_EN),
  221. TPS65217_REGULATOR("DCDC2", TPS65217_DCDC_2, tps65217_pmic_ops, 64,
  222. TPS65217_REG_DEFDCDC2, TPS65217_DEFDCDCX_DCDC_MASK,
  223. TPS65217_ENABLE_DC2_EN),
  224. TPS65217_REGULATOR("DCDC3", TPS65217_DCDC_3, tps65217_pmic_ops, 64,
  225. TPS65217_REG_DEFDCDC3, TPS65217_DEFDCDCX_DCDC_MASK,
  226. TPS65217_ENABLE_DC3_EN),
  227. TPS65217_REGULATOR("LDO1", TPS65217_LDO_1, tps65217_pmic_ldo1_ops, 16,
  228. TPS65217_REG_DEFLDO1, TPS65217_DEFLDO1_LDO1_MASK,
  229. TPS65217_ENABLE_LDO1_EN),
  230. TPS65217_REGULATOR("LDO2", TPS65217_LDO_2, tps65217_pmic_ops, 64,
  231. TPS65217_REG_DEFLDO2, TPS65217_DEFLDO2_LDO2_MASK,
  232. TPS65217_ENABLE_LDO2_EN),
  233. TPS65217_REGULATOR("LDO3", TPS65217_LDO_3, tps65217_pmic_ops, 32,
  234. TPS65217_REG_DEFLS1, TPS65217_DEFLDO3_LDO3_MASK,
  235. TPS65217_ENABLE_LS1_EN | TPS65217_DEFLDO3_LDO3_EN),
  236. TPS65217_REGULATOR("LDO4", TPS65217_LDO_4, tps65217_pmic_ops, 32,
  237. TPS65217_REG_DEFLS2, TPS65217_DEFLDO4_LDO4_MASK,
  238. TPS65217_ENABLE_LS2_EN | TPS65217_DEFLDO4_LDO4_EN),
  239. };
  240. static int __devinit tps65217_regulator_probe(struct platform_device *pdev)
  241. {
  242. struct regulator_dev *rdev;
  243. struct tps65217 *tps;
  244. struct tps_info *info = &tps65217_pmic_regs[pdev->id];
  245. struct regulator_config config = { };
  246. /* Already set by core driver */
  247. tps = dev_to_tps65217(pdev->dev.parent);
  248. tps->info[pdev->id] = info;
  249. config.dev = &pdev->dev;
  250. config.init_data = pdev->dev.platform_data;
  251. config.driver_data = tps;
  252. rdev = regulator_register(&regulators[pdev->id], &config);
  253. if (IS_ERR(rdev))
  254. return PTR_ERR(rdev);
  255. platform_set_drvdata(pdev, rdev);
  256. return 0;
  257. }
  258. static int __devexit tps65217_regulator_remove(struct platform_device *pdev)
  259. {
  260. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  261. platform_set_drvdata(pdev, NULL);
  262. regulator_unregister(rdev);
  263. return 0;
  264. }
  265. static struct platform_driver tps65217_regulator_driver = {
  266. .driver = {
  267. .name = "tps65217-pmic",
  268. },
  269. .probe = tps65217_regulator_probe,
  270. .remove = __devexit_p(tps65217_regulator_remove),
  271. };
  272. static int __init tps65217_regulator_init(void)
  273. {
  274. return platform_driver_register(&tps65217_regulator_driver);
  275. }
  276. subsys_initcall(tps65217_regulator_init);
  277. static void __exit tps65217_regulator_exit(void)
  278. {
  279. platform_driver_unregister(&tps65217_regulator_driver);
  280. }
  281. module_exit(tps65217_regulator_exit);
  282. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  283. MODULE_DESCRIPTION("TPS65217 voltage regulator driver");
  284. MODULE_ALIAS("platform:tps65217-pmic");
  285. MODULE_LICENSE("GPL v2");