tps65217-regulator.c 9.2 KB

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