tps65217-regulator.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/of_regulator.h>
  24. #include <linux/regulator/driver.h>
  25. #include <linux/regulator/machine.h>
  26. #include <linux/mfd/tps65217.h>
  27. #define TPS65217_REGULATOR(_name, _id, _of_match, _ops, _n, _vr, _vm, _em, \
  28. _t, _lr, _nlr, _sr, _sm) \
  29. { \
  30. .name = _name, \
  31. .id = _id, \
  32. .of_match = of_match_ptr(_of_match), \
  33. .regulators_node= of_match_ptr("regulators"), \
  34. .ops = &_ops, \
  35. .n_voltages = _n, \
  36. .type = REGULATOR_VOLTAGE, \
  37. .owner = THIS_MODULE, \
  38. .vsel_reg = _vr, \
  39. .vsel_mask = _vm, \
  40. .enable_reg = TPS65217_REG_ENABLE, \
  41. .enable_mask = _em, \
  42. .volt_table = _t, \
  43. .linear_ranges = _lr, \
  44. .n_linear_ranges = _nlr, \
  45. .bypass_reg = _sr, \
  46. .bypass_mask = _sm, \
  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 const struct regulator_linear_range tps65217_uv1_ranges[] = {
  55. REGULATOR_LINEAR_RANGE(900000, 0, 24, 25000),
  56. REGULATOR_LINEAR_RANGE(1550000, 25, 52, 50000),
  57. REGULATOR_LINEAR_RANGE(3000000, 53, 55, 100000),
  58. REGULATOR_LINEAR_RANGE(3300000, 56, 63, 0),
  59. };
  60. static const struct regulator_linear_range tps65217_uv2_ranges[] = {
  61. REGULATOR_LINEAR_RANGE(1500000, 0, 8, 50000),
  62. REGULATOR_LINEAR_RANGE(2000000, 9, 13, 100000),
  63. REGULATOR_LINEAR_RANGE(2450000, 14, 31, 50000),
  64. };
  65. static int tps65217_pmic_enable(struct regulator_dev *dev)
  66. {
  67. struct tps65217 *tps = rdev_get_drvdata(dev);
  68. int rid = rdev_get_id(dev);
  69. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  70. return -EINVAL;
  71. /* Enable the regulator and password protection is level 1 */
  72. return tps65217_set_bits(tps, TPS65217_REG_ENABLE,
  73. dev->desc->enable_mask, dev->desc->enable_mask,
  74. TPS65217_PROTECT_L1);
  75. }
  76. static int tps65217_pmic_disable(struct regulator_dev *dev)
  77. {
  78. struct tps65217 *tps = rdev_get_drvdata(dev);
  79. int rid = rdev_get_id(dev);
  80. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  81. return -EINVAL;
  82. /* Disable the regulator and password protection is level 1 */
  83. return tps65217_clear_bits(tps, TPS65217_REG_ENABLE,
  84. dev->desc->enable_mask, TPS65217_PROTECT_L1);
  85. }
  86. static int tps65217_pmic_set_voltage_sel(struct regulator_dev *dev,
  87. unsigned selector)
  88. {
  89. int ret;
  90. struct tps65217 *tps = rdev_get_drvdata(dev);
  91. unsigned int rid = rdev_get_id(dev);
  92. /* Set the voltage based on vsel value and write protect level is 2 */
  93. ret = tps65217_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
  94. selector, TPS65217_PROTECT_L2);
  95. /* Set GO bit for DCDCx to initiate voltage transistion */
  96. switch (rid) {
  97. case TPS65217_DCDC_1 ... TPS65217_DCDC_3:
  98. ret = tps65217_set_bits(tps, TPS65217_REG_DEFSLEW,
  99. TPS65217_DEFSLEW_GO, TPS65217_DEFSLEW_GO,
  100. TPS65217_PROTECT_L2);
  101. break;
  102. }
  103. return ret;
  104. }
  105. static int tps65217_pmic_set_suspend_enable(struct regulator_dev *dev)
  106. {
  107. struct tps65217 *tps = rdev_get_drvdata(dev);
  108. unsigned int rid = rdev_get_id(dev);
  109. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  110. return -EINVAL;
  111. return tps65217_clear_bits(tps, dev->desc->bypass_reg,
  112. dev->desc->bypass_mask,
  113. TPS65217_PROTECT_L1);
  114. }
  115. static int tps65217_pmic_set_suspend_disable(struct regulator_dev *dev)
  116. {
  117. struct tps65217 *tps = rdev_get_drvdata(dev);
  118. unsigned int rid = rdev_get_id(dev);
  119. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  120. return -EINVAL;
  121. if (!tps->strobes[rid])
  122. return -EINVAL;
  123. return tps65217_set_bits(tps, dev->desc->bypass_reg,
  124. dev->desc->bypass_mask,
  125. tps->strobes[rid], TPS65217_PROTECT_L1);
  126. }
  127. /* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */
  128. static const struct regulator_ops tps65217_pmic_ops = {
  129. .is_enabled = regulator_is_enabled_regmap,
  130. .enable = tps65217_pmic_enable,
  131. .disable = tps65217_pmic_disable,
  132. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  133. .set_voltage_sel = tps65217_pmic_set_voltage_sel,
  134. .list_voltage = regulator_list_voltage_linear_range,
  135. .map_voltage = regulator_map_voltage_linear_range,
  136. .set_suspend_enable = tps65217_pmic_set_suspend_enable,
  137. .set_suspend_disable = tps65217_pmic_set_suspend_disable,
  138. };
  139. /* Operations permitted on LDO1 */
  140. static const struct regulator_ops tps65217_pmic_ldo1_ops = {
  141. .is_enabled = regulator_is_enabled_regmap,
  142. .enable = tps65217_pmic_enable,
  143. .disable = tps65217_pmic_disable,
  144. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  145. .set_voltage_sel = tps65217_pmic_set_voltage_sel,
  146. .list_voltage = regulator_list_voltage_table,
  147. .map_voltage = regulator_map_voltage_ascend,
  148. .set_suspend_enable = tps65217_pmic_set_suspend_enable,
  149. .set_suspend_disable = tps65217_pmic_set_suspend_disable,
  150. };
  151. static const struct regulator_desc regulators[] = {
  152. TPS65217_REGULATOR("DCDC1", TPS65217_DCDC_1, "dcdc1",
  153. tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC1,
  154. TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC1_EN,
  155. NULL, tps65217_uv1_ranges,
  156. ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ1,
  157. TPS65217_SEQ1_DC1_SEQ_MASK),
  158. TPS65217_REGULATOR("DCDC2", TPS65217_DCDC_2, "dcdc2",
  159. tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC2,
  160. TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC2_EN,
  161. NULL, tps65217_uv1_ranges,
  162. ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ1,
  163. TPS65217_SEQ1_DC2_SEQ_MASK),
  164. TPS65217_REGULATOR("DCDC3", TPS65217_DCDC_3, "dcdc3",
  165. tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC3,
  166. TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC3_EN,
  167. NULL, tps65217_uv1_ranges,
  168. ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ2,
  169. TPS65217_SEQ2_DC3_SEQ_MASK),
  170. TPS65217_REGULATOR("LDO1", TPS65217_LDO_1, "ldo1",
  171. tps65217_pmic_ldo1_ops, 16, TPS65217_REG_DEFLDO1,
  172. TPS65217_DEFLDO1_LDO1_MASK, TPS65217_ENABLE_LDO1_EN,
  173. LDO1_VSEL_table, NULL, 0, TPS65217_REG_SEQ2,
  174. TPS65217_SEQ2_LDO1_SEQ_MASK),
  175. TPS65217_REGULATOR("LDO2", TPS65217_LDO_2, "ldo2", tps65217_pmic_ops,
  176. 64, TPS65217_REG_DEFLDO2,
  177. TPS65217_DEFLDO2_LDO2_MASK, TPS65217_ENABLE_LDO2_EN,
  178. NULL, tps65217_uv1_ranges,
  179. ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ3,
  180. TPS65217_SEQ3_LDO2_SEQ_MASK),
  181. TPS65217_REGULATOR("LDO3", TPS65217_LDO_3, "ldo3", tps65217_pmic_ops,
  182. 32, TPS65217_REG_DEFLS1, TPS65217_DEFLDO3_LDO3_MASK,
  183. TPS65217_ENABLE_LS1_EN | TPS65217_DEFLDO3_LDO3_EN,
  184. NULL, tps65217_uv2_ranges,
  185. ARRAY_SIZE(tps65217_uv2_ranges), TPS65217_REG_SEQ3,
  186. TPS65217_SEQ3_LDO3_SEQ_MASK),
  187. TPS65217_REGULATOR("LDO4", TPS65217_LDO_4, "ldo4", tps65217_pmic_ops,
  188. 32, TPS65217_REG_DEFLS2, TPS65217_DEFLDO4_LDO4_MASK,
  189. TPS65217_ENABLE_LS2_EN | TPS65217_DEFLDO4_LDO4_EN,
  190. NULL, tps65217_uv2_ranges,
  191. ARRAY_SIZE(tps65217_uv2_ranges), TPS65217_REG_SEQ4,
  192. TPS65217_SEQ4_LDO4_SEQ_MASK),
  193. };
  194. static int tps65217_regulator_probe(struct platform_device *pdev)
  195. {
  196. struct tps65217 *tps = dev_get_drvdata(pdev->dev.parent);
  197. struct tps65217_board *pdata = dev_get_platdata(tps->dev);
  198. struct regulator_dev *rdev;
  199. struct regulator_config config = { };
  200. int i, ret;
  201. unsigned int val;
  202. /* Allocate memory for strobes */
  203. tps->strobes = devm_kcalloc(&pdev->dev,
  204. TPS65217_NUM_REGULATOR, sizeof(u8),
  205. GFP_KERNEL);
  206. if (!tps->strobes)
  207. return -ENOMEM;
  208. platform_set_drvdata(pdev, tps);
  209. for (i = 0; i < TPS65217_NUM_REGULATOR; i++) {
  210. /* Register the regulators */
  211. config.dev = tps->dev;
  212. if (pdata)
  213. config.init_data = pdata->tps65217_init_data[i];
  214. config.driver_data = tps;
  215. config.regmap = tps->regmap;
  216. rdev = devm_regulator_register(&pdev->dev, &regulators[i],
  217. &config);
  218. if (IS_ERR(rdev)) {
  219. dev_err(tps->dev, "failed to register %s regulator\n",
  220. pdev->name);
  221. return PTR_ERR(rdev);
  222. }
  223. /* Store default strobe info */
  224. ret = tps65217_reg_read(tps, regulators[i].bypass_reg, &val);
  225. tps->strobes[i] = val & regulators[i].bypass_mask;
  226. }
  227. return 0;
  228. }
  229. static struct platform_driver tps65217_regulator_driver = {
  230. .driver = {
  231. .name = "tps65217-pmic",
  232. },
  233. .probe = tps65217_regulator_probe,
  234. };
  235. static int __init tps65217_regulator_init(void)
  236. {
  237. return platform_driver_register(&tps65217_regulator_driver);
  238. }
  239. subsys_initcall(tps65217_regulator_init);
  240. static void __exit tps65217_regulator_exit(void)
  241. {
  242. platform_driver_unregister(&tps65217_regulator_driver);
  243. }
  244. module_exit(tps65217_regulator_exit);
  245. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  246. MODULE_DESCRIPTION("TPS65217 voltage regulator driver");
  247. MODULE_ALIAS("platform:tps65217-pmic");
  248. MODULE_LICENSE("GPL v2");