lp873x-regulator.c 6.3 KB

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