tps65217-regulator.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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) \
  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. } \
  46. static const unsigned int LDO1_VSEL_table[] = {
  47. 1000000, 1100000, 1200000, 1250000,
  48. 1300000, 1350000, 1400000, 1500000,
  49. 1600000, 1800000, 2500000, 2750000,
  50. 2800000, 3000000, 3100000, 3300000,
  51. };
  52. static const struct regulator_linear_range tps65217_uv1_ranges[] = {
  53. REGULATOR_LINEAR_RANGE(900000, 0, 24, 25000),
  54. REGULATOR_LINEAR_RANGE(1550000, 25, 30, 50000),
  55. REGULATOR_LINEAR_RANGE(1850000, 31, 52, 50000),
  56. REGULATOR_LINEAR_RANGE(3000000, 53, 55, 100000),
  57. REGULATOR_LINEAR_RANGE(3300000, 56, 62, 0),
  58. };
  59. static const struct regulator_linear_range tps65217_uv2_ranges[] = {
  60. REGULATOR_LINEAR_RANGE(1500000, 0, 8, 50000),
  61. REGULATOR_LINEAR_RANGE(2000000, 9, 13, 100000),
  62. REGULATOR_LINEAR_RANGE(2450000, 14, 31, 50000),
  63. };
  64. static int tps65217_pmic_enable(struct regulator_dev *dev)
  65. {
  66. struct tps65217 *tps = rdev_get_drvdata(dev);
  67. int rid = rdev_get_id(dev);
  68. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  69. return -EINVAL;
  70. /* Enable the regulator and password protection is level 1 */
  71. return tps65217_set_bits(tps, TPS65217_REG_ENABLE,
  72. dev->desc->enable_mask, dev->desc->enable_mask,
  73. TPS65217_PROTECT_L1);
  74. }
  75. static int tps65217_pmic_disable(struct regulator_dev *dev)
  76. {
  77. struct tps65217 *tps = rdev_get_drvdata(dev);
  78. int rid = rdev_get_id(dev);
  79. if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
  80. return -EINVAL;
  81. /* Disable the regulator and password protection is level 1 */
  82. return tps65217_clear_bits(tps, TPS65217_REG_ENABLE,
  83. dev->desc->enable_mask, TPS65217_PROTECT_L1);
  84. }
  85. static int tps65217_pmic_set_voltage_sel(struct regulator_dev *dev,
  86. unsigned selector)
  87. {
  88. int ret;
  89. struct tps65217 *tps = rdev_get_drvdata(dev);
  90. unsigned int rid = rdev_get_id(dev);
  91. /* Set the voltage based on vsel value and write protect level is 2 */
  92. ret = tps65217_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
  93. selector, TPS65217_PROTECT_L2);
  94. /* Set GO bit for DCDCx to initiate voltage transistion */
  95. switch (rid) {
  96. case TPS65217_DCDC_1 ... TPS65217_DCDC_3:
  97. ret = tps65217_set_bits(tps, TPS65217_REG_DEFSLEW,
  98. TPS65217_DEFSLEW_GO, TPS65217_DEFSLEW_GO,
  99. TPS65217_PROTECT_L2);
  100. break;
  101. }
  102. return ret;
  103. }
  104. /* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */
  105. static struct regulator_ops tps65217_pmic_ops = {
  106. .is_enabled = regulator_is_enabled_regmap,
  107. .enable = tps65217_pmic_enable,
  108. .disable = tps65217_pmic_disable,
  109. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  110. .set_voltage_sel = tps65217_pmic_set_voltage_sel,
  111. .list_voltage = regulator_list_voltage_linear_range,
  112. .map_voltage = regulator_map_voltage_linear_range,
  113. };
  114. /* Operations permitted on LDO1 */
  115. static struct regulator_ops tps65217_pmic_ldo1_ops = {
  116. .is_enabled = regulator_is_enabled_regmap,
  117. .enable = tps65217_pmic_enable,
  118. .disable = tps65217_pmic_disable,
  119. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  120. .set_voltage_sel = tps65217_pmic_set_voltage_sel,
  121. .list_voltage = regulator_list_voltage_table,
  122. .map_voltage = regulator_map_voltage_ascend,
  123. };
  124. static const struct regulator_desc regulators[] = {
  125. TPS65217_REGULATOR("DCDC1", TPS65217_DCDC_1, "dcdc1",
  126. tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC1,
  127. TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC1_EN,
  128. NULL, tps65217_uv1_ranges, 2),
  129. TPS65217_REGULATOR("DCDC2", TPS65217_DCDC_2, "dcdc2",
  130. tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC2,
  131. TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC2_EN,
  132. NULL, tps65217_uv1_ranges,
  133. ARRAY_SIZE(tps65217_uv1_ranges)),
  134. TPS65217_REGULATOR("DCDC3", TPS65217_DCDC_3, "dcdc3",
  135. tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC3,
  136. TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC3_EN,
  137. NULL, tps65217_uv1_ranges, 1),
  138. TPS65217_REGULATOR("LDO1", TPS65217_LDO_1, "ldo1",
  139. tps65217_pmic_ldo1_ops, 16, TPS65217_REG_DEFLDO1,
  140. TPS65217_DEFLDO1_LDO1_MASK, TPS65217_ENABLE_LDO1_EN,
  141. LDO1_VSEL_table, NULL, 0),
  142. TPS65217_REGULATOR("LDO2", TPS65217_LDO_2, "ldo2", tps65217_pmic_ops,
  143. 64, TPS65217_REG_DEFLDO2,
  144. TPS65217_DEFLDO2_LDO2_MASK, TPS65217_ENABLE_LDO2_EN,
  145. NULL, tps65217_uv1_ranges,
  146. ARRAY_SIZE(tps65217_uv1_ranges)),
  147. TPS65217_REGULATOR("LDO3", TPS65217_LDO_3, "ldo3", tps65217_pmic_ops,
  148. 32, TPS65217_REG_DEFLS1, TPS65217_DEFLDO3_LDO3_MASK,
  149. TPS65217_ENABLE_LS1_EN | TPS65217_DEFLDO3_LDO3_EN,
  150. NULL, tps65217_uv2_ranges,
  151. ARRAY_SIZE(tps65217_uv2_ranges)),
  152. TPS65217_REGULATOR("LDO4", TPS65217_LDO_4, "ldo4", tps65217_pmic_ops,
  153. 32, TPS65217_REG_DEFLS2, TPS65217_DEFLDO4_LDO4_MASK,
  154. TPS65217_ENABLE_LS2_EN | TPS65217_DEFLDO4_LDO4_EN,
  155. NULL, tps65217_uv2_ranges,
  156. ARRAY_SIZE(tps65217_uv2_ranges)),
  157. };
  158. static int tps65217_regulator_probe(struct platform_device *pdev)
  159. {
  160. struct tps65217 *tps = dev_get_drvdata(pdev->dev.parent);
  161. struct tps65217_board *pdata = dev_get_platdata(tps->dev);
  162. struct regulator_dev *rdev;
  163. struct regulator_config config = { };
  164. int i;
  165. if (tps65217_chip_id(tps) != TPS65217) {
  166. dev_err(&pdev->dev, "Invalid tps chip version\n");
  167. return -ENODEV;
  168. }
  169. platform_set_drvdata(pdev, tps);
  170. for (i = 0; i < TPS65217_NUM_REGULATOR; i++) {
  171. /* Register the regulators */
  172. config.dev = tps->dev;
  173. if (pdata)
  174. config.init_data = pdata->tps65217_init_data[i];
  175. config.driver_data = tps;
  176. config.regmap = tps->regmap;
  177. rdev = devm_regulator_register(&pdev->dev, &regulators[i],
  178. &config);
  179. if (IS_ERR(rdev)) {
  180. dev_err(tps->dev, "failed to register %s regulator\n",
  181. pdev->name);
  182. return PTR_ERR(rdev);
  183. }
  184. }
  185. return 0;
  186. }
  187. static struct platform_driver tps65217_regulator_driver = {
  188. .driver = {
  189. .name = "tps65217-pmic",
  190. },
  191. .probe = tps65217_regulator_probe,
  192. };
  193. static int __init tps65217_regulator_init(void)
  194. {
  195. return platform_driver_register(&tps65217_regulator_driver);
  196. }
  197. subsys_initcall(tps65217_regulator_init);
  198. static void __exit tps65217_regulator_exit(void)
  199. {
  200. platform_driver_unregister(&tps65217_regulator_driver);
  201. }
  202. module_exit(tps65217_regulator_exit);
  203. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  204. MODULE_DESCRIPTION("TPS65217 voltage regulator driver");
  205. MODULE_ALIAS("platform:tps65217-pmic");
  206. MODULE_LICENSE("GPL v2");