tps65217-regulator.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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_set_voltage_sel(struct regulator_dev *dev,
  181. unsigned selector)
  182. {
  183. int ret;
  184. struct tps65217 *tps = rdev_get_drvdata(dev);
  185. unsigned int rid = rdev_get_id(dev);
  186. /* Set the voltage based on vsel value and write protect level is 2 */
  187. ret = tps65217_set_bits(tps, tps->info[rid]->set_vout_reg,
  188. tps->info[rid]->set_vout_mask,
  189. selector, TPS65217_PROTECT_L2);
  190. /* Set GO bit for DCDCx to initiate voltage transistion */
  191. switch (rid) {
  192. case TPS65217_DCDC_1 ... TPS65217_DCDC_3:
  193. ret = tps65217_set_bits(tps, TPS65217_REG_DEFSLEW,
  194. TPS65217_DEFSLEW_GO, TPS65217_DEFSLEW_GO,
  195. TPS65217_PROTECT_L2);
  196. break;
  197. }
  198. return ret;
  199. }
  200. static int tps65217_pmic_map_voltage(struct regulator_dev *dev,
  201. int min_uV, int max_uV)
  202. {
  203. struct tps65217 *tps = rdev_get_drvdata(dev);
  204. unsigned int sel, rid = rdev_get_id(dev);
  205. int ret;
  206. /* LDO1 uses regulator_map_voltage_iterate() */
  207. if (rid == TPS65217_LDO_1)
  208. return -EINVAL;
  209. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  210. return -EINVAL;
  211. if (min_uV < tps->info[rid]->min_uV || min_uV > tps->info[rid]->max_uV)
  212. return -EINVAL;
  213. if (max_uV < tps->info[rid]->min_uV || max_uV > tps->info[rid]->max_uV)
  214. return -EINVAL;
  215. ret = tps->info[rid]->uv_to_vsel(min_uV, &sel);
  216. if (ret)
  217. return ret;
  218. return sel;
  219. }
  220. static int tps65217_pmic_list_voltage(struct regulator_dev *dev,
  221. unsigned selector)
  222. {
  223. struct tps65217 *tps = rdev_get_drvdata(dev);
  224. unsigned int rid = rdev_get_id(dev);
  225. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  226. return -EINVAL;
  227. if (selector >= tps->info[rid]->table_len)
  228. return -EINVAL;
  229. if (tps->info[rid]->table)
  230. return tps->info[rid]->table[selector];
  231. return tps->info[rid]->vsel_to_uv(selector);
  232. }
  233. /* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */
  234. static struct regulator_ops tps65217_pmic_ops = {
  235. .is_enabled = tps65217_pmic_is_enabled,
  236. .enable = tps65217_pmic_enable,
  237. .disable = tps65217_pmic_disable,
  238. .get_voltage_sel = tps65217_pmic_get_voltage_sel,
  239. .set_voltage_sel = tps65217_pmic_set_voltage_sel,
  240. .list_voltage = tps65217_pmic_list_voltage,
  241. .map_voltage = tps65217_pmic_map_voltage,
  242. };
  243. /* Operations permitted on LDO1 */
  244. static struct regulator_ops tps65217_pmic_ldo1_ops = {
  245. .is_enabled = tps65217_pmic_is_enabled,
  246. .enable = tps65217_pmic_enable,
  247. .disable = tps65217_pmic_disable,
  248. .get_voltage_sel = tps65217_pmic_get_voltage_sel,
  249. .set_voltage_sel = tps65217_pmic_set_voltage_sel,
  250. .list_voltage = tps65217_pmic_list_voltage,
  251. };
  252. static const struct regulator_desc regulators[] = {
  253. TPS65217_REGULATOR("DCDC1", TPS65217_DCDC_1, tps65217_pmic_ops, 64),
  254. TPS65217_REGULATOR("DCDC2", TPS65217_DCDC_2, tps65217_pmic_ops, 64),
  255. TPS65217_REGULATOR("DCDC3", TPS65217_DCDC_3, tps65217_pmic_ops, 64),
  256. TPS65217_REGULATOR("LDO1", TPS65217_LDO_1, tps65217_pmic_ldo1_ops, 16),
  257. TPS65217_REGULATOR("LDO2", TPS65217_LDO_2, tps65217_pmic_ops, 64),
  258. TPS65217_REGULATOR("LDO3", TPS65217_LDO_3, tps65217_pmic_ops, 32),
  259. TPS65217_REGULATOR("LDO4", TPS65217_LDO_4, tps65217_pmic_ops, 32),
  260. };
  261. static int __devinit tps65217_regulator_probe(struct platform_device *pdev)
  262. {
  263. struct regulator_dev *rdev;
  264. struct tps65217 *tps;
  265. struct tps_info *info = &tps65217_pmic_regs[pdev->id];
  266. struct regulator_config config = { };
  267. /* Already set by core driver */
  268. tps = dev_to_tps65217(pdev->dev.parent);
  269. tps->info[pdev->id] = info;
  270. config.dev = &pdev->dev;
  271. config.init_data = pdev->dev.platform_data;
  272. config.driver_data = tps;
  273. rdev = regulator_register(&regulators[pdev->id], &config);
  274. if (IS_ERR(rdev))
  275. return PTR_ERR(rdev);
  276. platform_set_drvdata(pdev, rdev);
  277. return 0;
  278. }
  279. static int __devexit tps65217_regulator_remove(struct platform_device *pdev)
  280. {
  281. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  282. platform_set_drvdata(pdev, NULL);
  283. regulator_unregister(rdev);
  284. return 0;
  285. }
  286. static struct platform_driver tps65217_regulator_driver = {
  287. .driver = {
  288. .name = "tps65217-pmic",
  289. },
  290. .probe = tps65217_regulator_probe,
  291. .remove = __devexit_p(tps65217_regulator_remove),
  292. };
  293. static int __init tps65217_regulator_init(void)
  294. {
  295. return platform_driver_register(&tps65217_regulator_driver);
  296. }
  297. subsys_initcall(tps65217_regulator_init);
  298. static void __exit tps65217_regulator_exit(void)
  299. {
  300. platform_driver_unregister(&tps65217_regulator_driver);
  301. }
  302. module_exit(tps65217_regulator_exit);
  303. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  304. MODULE_DESCRIPTION("TPS65217 voltage regulator driver");
  305. MODULE_ALIAS("platform:tps65217-pmic");
  306. MODULE_LICENSE("GPL v2");