lp873x-regulator.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Regulator driver for LP873X PMIC
  4. *
  5. * Copyright (C) 2016 Texas Instruments Incorporated - https://www.ti.com/
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/regmap.h>
  11. #include <linux/mfd/lp873x.h>
  12. #define LP873X_REGULATOR(_name, _id, _of, _ops, _n, _vr, _vm, _er, _em, \
  13. _delay, _lr, _cr) \
  14. [_id] = { \
  15. .desc = { \
  16. .name = _name, \
  17. .supply_name = _of "-in", \
  18. .id = _id, \
  19. .of_match = of_match_ptr(_of), \
  20. .regulators_node = of_match_ptr("regulators"),\
  21. .ops = &_ops, \
  22. .n_voltages = _n, \
  23. .type = REGULATOR_VOLTAGE, \
  24. .owner = THIS_MODULE, \
  25. .vsel_reg = _vr, \
  26. .vsel_mask = _vm, \
  27. .enable_reg = _er, \
  28. .enable_mask = _em, \
  29. .ramp_delay = _delay, \
  30. .linear_ranges = _lr, \
  31. .n_linear_ranges = ARRAY_SIZE(_lr), \
  32. .curr_table = lp873x_buck_uA, \
  33. .n_current_limits = ARRAY_SIZE(lp873x_buck_uA), \
  34. .csel_reg = (_cr), \
  35. .csel_mask = LP873X_BUCK0_CTRL_2_BUCK0_ILIM,\
  36. }, \
  37. .ctrl2_reg = _cr, \
  38. }
  39. struct lp873x_regulator {
  40. struct regulator_desc desc;
  41. unsigned int ctrl2_reg;
  42. };
  43. static const struct lp873x_regulator regulators[];
  44. static const struct linear_range buck0_buck1_ranges[] = {
  45. REGULATOR_LINEAR_RANGE(0, 0x0, 0x13, 0),
  46. REGULATOR_LINEAR_RANGE(700000, 0x14, 0x17, 10000),
  47. REGULATOR_LINEAR_RANGE(735000, 0x18, 0x9d, 5000),
  48. REGULATOR_LINEAR_RANGE(1420000, 0x9e, 0xff, 20000),
  49. };
  50. static const struct linear_range ldo0_ldo1_ranges[] = {
  51. REGULATOR_LINEAR_RANGE(800000, 0x0, 0x19, 100000),
  52. };
  53. static const unsigned int lp873x_buck_ramp_delay[] = {
  54. 30000, 15000, 10000, 7500, 3800, 1900, 940, 470
  55. };
  56. /* LP873X BUCK current limit */
  57. static const unsigned int lp873x_buck_uA[] = {
  58. 1500000, 2000000, 2500000, 3000000, 3500000, 4000000,
  59. };
  60. static int lp873x_buck_set_ramp_delay(struct regulator_dev *rdev,
  61. int ramp_delay)
  62. {
  63. int id = rdev_get_id(rdev);
  64. struct lp873x *lp873 = rdev_get_drvdata(rdev);
  65. unsigned int reg;
  66. int ret;
  67. if (ramp_delay <= 470)
  68. reg = 7;
  69. else if (ramp_delay <= 940)
  70. reg = 6;
  71. else if (ramp_delay <= 1900)
  72. reg = 5;
  73. else if (ramp_delay <= 3800)
  74. reg = 4;
  75. else if (ramp_delay <= 7500)
  76. reg = 3;
  77. else if (ramp_delay <= 10000)
  78. reg = 2;
  79. else if (ramp_delay <= 15000)
  80. reg = 1;
  81. else
  82. reg = 0;
  83. ret = regmap_update_bits(lp873->regmap, regulators[id].ctrl2_reg,
  84. LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE,
  85. FIELD_PREP(LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE, reg));
  86. if (ret) {
  87. dev_err(lp873->dev, "SLEW RATE write failed: %d\n", ret);
  88. return ret;
  89. }
  90. rdev->constraints->ramp_delay = lp873x_buck_ramp_delay[reg];
  91. return 0;
  92. }
  93. /* Operations permitted on BUCK0, BUCK1 */
  94. static const struct regulator_ops lp873x_buck01_ops = {
  95. .is_enabled = regulator_is_enabled_regmap,
  96. .enable = regulator_enable_regmap,
  97. .disable = regulator_disable_regmap,
  98. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  99. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  100. .list_voltage = regulator_list_voltage_linear_range,
  101. .map_voltage = regulator_map_voltage_linear_range,
  102. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  103. .set_ramp_delay = lp873x_buck_set_ramp_delay,
  104. .set_current_limit = regulator_set_current_limit_regmap,
  105. .get_current_limit = regulator_get_current_limit_regmap,
  106. };
  107. /* Operations permitted on LDO0 and LDO1 */
  108. static const struct regulator_ops lp873x_ldo01_ops = {
  109. .is_enabled = regulator_is_enabled_regmap,
  110. .enable = regulator_enable_regmap,
  111. .disable = regulator_disable_regmap,
  112. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  113. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  114. .list_voltage = regulator_list_voltage_linear_range,
  115. .map_voltage = regulator_map_voltage_linear_range,
  116. };
  117. static const struct lp873x_regulator regulators[] = {
  118. LP873X_REGULATOR("BUCK0", LP873X_BUCK_0, "buck0", lp873x_buck01_ops,
  119. 256, LP873X_REG_BUCK0_VOUT,
  120. LP873X_BUCK0_VOUT_BUCK0_VSET, LP873X_REG_BUCK0_CTRL_1,
  121. LP873X_BUCK0_CTRL_1_BUCK0_EN, 10000,
  122. buck0_buck1_ranges, LP873X_REG_BUCK0_CTRL_2),
  123. LP873X_REGULATOR("BUCK1", LP873X_BUCK_1, "buck1", lp873x_buck01_ops,
  124. 256, LP873X_REG_BUCK1_VOUT,
  125. LP873X_BUCK1_VOUT_BUCK1_VSET, LP873X_REG_BUCK1_CTRL_1,
  126. LP873X_BUCK1_CTRL_1_BUCK1_EN, 10000,
  127. buck0_buck1_ranges, LP873X_REG_BUCK1_CTRL_2),
  128. LP873X_REGULATOR("LDO0", LP873X_LDO_0, "ldo0", lp873x_ldo01_ops, 26,
  129. LP873X_REG_LDO0_VOUT, LP873X_LDO0_VOUT_LDO0_VSET,
  130. LP873X_REG_LDO0_CTRL,
  131. LP873X_LDO0_CTRL_LDO0_EN, 0, ldo0_ldo1_ranges, 0xFF),
  132. LP873X_REGULATOR("LDO1", LP873X_LDO_1, "ldo1", lp873x_ldo01_ops, 26,
  133. LP873X_REG_LDO1_VOUT, LP873X_LDO1_VOUT_LDO1_VSET,
  134. LP873X_REG_LDO1_CTRL,
  135. LP873X_LDO1_CTRL_LDO1_EN, 0, ldo0_ldo1_ranges, 0xFF),
  136. };
  137. static int lp873x_regulator_probe(struct platform_device *pdev)
  138. {
  139. struct lp873x *lp873 = dev_get_drvdata(pdev->dev.parent);
  140. struct regulator_config config = { };
  141. struct regulator_dev *rdev;
  142. int i;
  143. platform_set_drvdata(pdev, lp873);
  144. config.dev = &pdev->dev;
  145. config.dev->of_node = lp873->dev->of_node;
  146. config.driver_data = lp873;
  147. config.regmap = lp873->regmap;
  148. for (i = 0; i < ARRAY_SIZE(regulators); i++) {
  149. rdev = devm_regulator_register(&pdev->dev, &regulators[i].desc,
  150. &config);
  151. if (IS_ERR(rdev)) {
  152. dev_err(lp873->dev, "failed to register %s regulator\n",
  153. pdev->name);
  154. return PTR_ERR(rdev);
  155. }
  156. }
  157. return 0;
  158. }
  159. static const struct platform_device_id lp873x_regulator_id_table[] = {
  160. { "lp873x-regulator", },
  161. { /* sentinel */ }
  162. };
  163. MODULE_DEVICE_TABLE(platform, lp873x_regulator_id_table);
  164. static struct platform_driver lp873x_regulator_driver = {
  165. .driver = {
  166. .name = "lp873x-pmic",
  167. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  168. },
  169. .probe = lp873x_regulator_probe,
  170. .id_table = lp873x_regulator_id_table,
  171. };
  172. module_platform_driver(lp873x_regulator_driver);
  173. MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
  174. MODULE_DESCRIPTION("LP873X voltage regulator driver");
  175. MODULE_ALIAS("platform:lp873x-pmic");
  176. MODULE_LICENSE("GPL v2");