tps65086-regulator.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
  3. *
  4. * Author: Andrew F. Davis <afd@ti.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether expressed or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License version 2 for more details.
  14. *
  15. * Based on the TPS65912 driver
  16. */
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/mfd/tps65086.h>
  22. enum tps65086_regulators { BUCK1, BUCK2, BUCK3, BUCK4, BUCK5, BUCK6, LDOA1,
  23. LDOA2, LDOA3, SWA1, SWB1, SWB2, VTT };
  24. #define TPS65086_REGULATOR(_name, _of, _id, _nv, _vr, _vm, _er, _em, _lr, _dr, _dm) \
  25. [_id] = { \
  26. .desc = { \
  27. .name = _name, \
  28. .of_match = of_match_ptr(_of), \
  29. .regulators_node = "regulators", \
  30. .of_parse_cb = tps65086_of_parse_cb, \
  31. .id = _id, \
  32. .ops = &reg_ops, \
  33. .n_voltages = _nv, \
  34. .type = REGULATOR_VOLTAGE, \
  35. .owner = THIS_MODULE, \
  36. .vsel_reg = _vr, \
  37. .vsel_mask = _vm, \
  38. .enable_reg = _er, \
  39. .enable_mask = _em, \
  40. .volt_table = NULL, \
  41. .linear_ranges = _lr, \
  42. .n_linear_ranges = ARRAY_SIZE(_lr), \
  43. }, \
  44. .decay_reg = _dr, \
  45. .decay_mask = _dm, \
  46. }
  47. #define TPS65086_SWITCH(_name, _of, _id, _er, _em) \
  48. [_id] = { \
  49. .desc = { \
  50. .name = _name, \
  51. .of_match = of_match_ptr(_of), \
  52. .regulators_node = "regulators", \
  53. .of_parse_cb = tps65086_of_parse_cb, \
  54. .id = _id, \
  55. .ops = &switch_ops, \
  56. .type = REGULATOR_VOLTAGE, \
  57. .owner = THIS_MODULE, \
  58. .enable_reg = _er, \
  59. .enable_mask = _em, \
  60. }, \
  61. }
  62. struct tps65086_regulator {
  63. struct regulator_desc desc;
  64. unsigned int decay_reg;
  65. unsigned int decay_mask;
  66. };
  67. static const struct regulator_linear_range tps65086_10mv_ranges[] = {
  68. REGULATOR_LINEAR_RANGE(0, 0x0, 0x0, 0),
  69. REGULATOR_LINEAR_RANGE(410000, 0x1, 0x7F, 10000),
  70. };
  71. static const struct regulator_linear_range tps65086_buck126_25mv_ranges[] = {
  72. REGULATOR_LINEAR_RANGE(0, 0x0, 0x0, 0),
  73. REGULATOR_LINEAR_RANGE(1000000, 0x1, 0x18, 0),
  74. REGULATOR_LINEAR_RANGE(1025000, 0x19, 0x7F, 25000),
  75. };
  76. static const struct regulator_linear_range tps65086_buck345_25mv_ranges[] = {
  77. REGULATOR_LINEAR_RANGE(0, 0x0, 0x0, 0),
  78. REGULATOR_LINEAR_RANGE(425000, 0x1, 0x7F, 25000),
  79. };
  80. static const struct regulator_linear_range tps65086_ldoa1_ranges[] = {
  81. REGULATOR_LINEAR_RANGE(1350000, 0x0, 0x0, 0),
  82. REGULATOR_LINEAR_RANGE(1500000, 0x1, 0x7, 100000),
  83. REGULATOR_LINEAR_RANGE(2300000, 0x8, 0xB, 100000),
  84. REGULATOR_LINEAR_RANGE(2850000, 0xC, 0xD, 150000),
  85. REGULATOR_LINEAR_RANGE(3300000, 0xE, 0xE, 0),
  86. };
  87. static const struct regulator_linear_range tps65086_ldoa23_ranges[] = {
  88. REGULATOR_LINEAR_RANGE(700000, 0x0, 0xD, 50000),
  89. REGULATOR_LINEAR_RANGE(1400000, 0xE, 0xF, 100000),
  90. };
  91. /* Operations permitted on regulators */
  92. static struct regulator_ops reg_ops = {
  93. .enable = regulator_enable_regmap,
  94. .disable = regulator_disable_regmap,
  95. .is_enabled = regulator_is_enabled_regmap,
  96. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  97. .map_voltage = regulator_map_voltage_linear_range,
  98. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  99. .list_voltage = regulator_list_voltage_linear_range,
  100. };
  101. /* Operations permitted on load switches */
  102. static struct regulator_ops switch_ops = {
  103. .enable = regulator_enable_regmap,
  104. .disable = regulator_disable_regmap,
  105. .is_enabled = regulator_is_enabled_regmap,
  106. };
  107. static int tps65086_of_parse_cb(struct device_node *dev,
  108. const struct regulator_desc *desc,
  109. struct regulator_config *config);
  110. static struct tps65086_regulator regulators[] = {
  111. TPS65086_REGULATOR("BUCK1", "buck1", BUCK1, 0x80, TPS65086_BUCK1CTRL,
  112. BUCK_VID_MASK, TPS65086_BUCK123CTRL, BIT(0),
  113. tps65086_10mv_ranges, TPS65086_BUCK1CTRL,
  114. BIT(0)),
  115. TPS65086_REGULATOR("BUCK2", "buck2", BUCK2, 0x80, TPS65086_BUCK2CTRL,
  116. BUCK_VID_MASK, TPS65086_BUCK123CTRL, BIT(1),
  117. tps65086_10mv_ranges, TPS65086_BUCK2CTRL,
  118. BIT(0)),
  119. TPS65086_REGULATOR("BUCK3", "buck3", BUCK3, 0x80, TPS65086_BUCK3VID,
  120. BUCK_VID_MASK, TPS65086_BUCK123CTRL, BIT(2),
  121. tps65086_10mv_ranges, TPS65086_BUCK3DECAY,
  122. BIT(0)),
  123. TPS65086_REGULATOR("BUCK4", "buck4", BUCK4, 0x80, TPS65086_BUCK4VID,
  124. BUCK_VID_MASK, TPS65086_BUCK4CTRL, BIT(0),
  125. tps65086_10mv_ranges, TPS65086_BUCK4VID,
  126. BIT(0)),
  127. TPS65086_REGULATOR("BUCK5", "buck5", BUCK5, 0x80, TPS65086_BUCK5VID,
  128. BUCK_VID_MASK, TPS65086_BUCK5CTRL, BIT(0),
  129. tps65086_10mv_ranges, TPS65086_BUCK5CTRL,
  130. BIT(0)),
  131. TPS65086_REGULATOR("BUCK6", "buck6", BUCK6, 0x80, TPS65086_BUCK6VID,
  132. BUCK_VID_MASK, TPS65086_BUCK6CTRL, BIT(0),
  133. tps65086_10mv_ranges, TPS65086_BUCK6CTRL,
  134. BIT(0)),
  135. TPS65086_REGULATOR("LDOA1", "ldoa1", LDOA1, 0xF, TPS65086_LDOA1CTRL,
  136. VDOA1_VID_MASK, TPS65086_LDOA1CTRL, BIT(0),
  137. tps65086_ldoa1_ranges, 0, 0),
  138. TPS65086_REGULATOR("LDOA2", "ldoa2", LDOA2, 0x10, TPS65086_LDOA2VID,
  139. VDOA23_VID_MASK, TPS65086_LDOA2CTRL, BIT(0),
  140. tps65086_ldoa23_ranges, 0, 0),
  141. TPS65086_REGULATOR("LDOA3", "ldoa3", LDOA3, 0x10, TPS65086_LDOA3VID,
  142. VDOA23_VID_MASK, TPS65086_LDOA3CTRL, BIT(0),
  143. tps65086_ldoa23_ranges, 0, 0),
  144. TPS65086_SWITCH("SWA1", "swa1", SWA1, TPS65086_SWVTT_EN, BIT(5)),
  145. TPS65086_SWITCH("SWB1", "swb1", SWB1, TPS65086_SWVTT_EN, BIT(6)),
  146. TPS65086_SWITCH("SWB2", "swb2", SWB2, TPS65086_SWVTT_EN, BIT(7)),
  147. TPS65086_SWITCH("VTT", "vtt", VTT, TPS65086_SWVTT_EN, BIT(4)),
  148. };
  149. static int tps65086_of_parse_cb(struct device_node *node,
  150. const struct regulator_desc *desc,
  151. struct regulator_config *config)
  152. {
  153. int ret;
  154. /* Check for 25mV step mode */
  155. if (of_property_read_bool(node, "ti,regulator-step-size-25mv")) {
  156. switch (desc->id) {
  157. case BUCK1:
  158. case BUCK2:
  159. case BUCK6:
  160. regulators[desc->id].desc.linear_ranges =
  161. tps65086_buck126_25mv_ranges;
  162. regulators[desc->id].desc.n_linear_ranges =
  163. ARRAY_SIZE(tps65086_buck126_25mv_ranges);
  164. break;
  165. case BUCK3:
  166. case BUCK4:
  167. case BUCK5:
  168. regulators[desc->id].desc.linear_ranges =
  169. tps65086_buck345_25mv_ranges;
  170. regulators[desc->id].desc.n_linear_ranges =
  171. ARRAY_SIZE(tps65086_buck345_25mv_ranges);
  172. break;
  173. default:
  174. dev_warn(config->dev, "25mV step mode only valid for BUCK regulators\n");
  175. }
  176. }
  177. /* Check for decay mode */
  178. if (desc->id <= BUCK6 && of_property_read_bool(node, "ti,regulator-decay")) {
  179. ret = regmap_write_bits(config->regmap,
  180. regulators[desc->id].decay_reg,
  181. regulators[desc->id].decay_mask,
  182. regulators[desc->id].decay_mask);
  183. if (ret) {
  184. dev_err(config->dev, "Error setting decay\n");
  185. return ret;
  186. }
  187. }
  188. return 0;
  189. }
  190. static int tps65086_regulator_probe(struct platform_device *pdev)
  191. {
  192. struct tps65086 *tps = dev_get_drvdata(pdev->dev.parent);
  193. struct regulator_config config = { };
  194. struct regulator_dev *rdev;
  195. int i;
  196. platform_set_drvdata(pdev, tps);
  197. config.dev = &pdev->dev;
  198. config.dev->of_node = tps->dev->of_node;
  199. config.driver_data = tps;
  200. config.regmap = tps->regmap;
  201. for (i = 0; i < ARRAY_SIZE(regulators); i++) {
  202. rdev = devm_regulator_register(&pdev->dev, &regulators[i].desc,
  203. &config);
  204. if (IS_ERR(rdev)) {
  205. dev_err(tps->dev, "failed to register %s regulator\n",
  206. pdev->name);
  207. return PTR_ERR(rdev);
  208. }
  209. }
  210. return 0;
  211. }
  212. static const struct platform_device_id tps65086_regulator_id_table[] = {
  213. { "tps65086-regulator", },
  214. { /* sentinel */ }
  215. };
  216. MODULE_DEVICE_TABLE(platform, tps65086_regulator_id_table);
  217. static struct platform_driver tps65086_regulator_driver = {
  218. .driver = {
  219. .name = "tps65086-regulator",
  220. },
  221. .probe = tps65086_regulator_probe,
  222. .id_table = tps65086_regulator_id_table,
  223. };
  224. module_platform_driver(tps65086_regulator_driver);
  225. MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
  226. MODULE_DESCRIPTION("TPS65086 Regulator driver");
  227. MODULE_LICENSE("GPL v2");