lp87565-regulator.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Regulator driver for LP87565 PMIC
  4. *
  5. * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
  6. */
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/regmap.h>
  10. #include <linux/mfd/lp87565.h>
  11. #define LP87565_REGULATOR(_name, _id, _of, _ops, _n, _vr, _vm, _er, _em, \
  12. _delay, _lr, _cr) \
  13. [_id] = { \
  14. .desc = { \
  15. .name = _name, \
  16. .supply_name = _of "-in", \
  17. .id = _id, \
  18. .of_match = of_match_ptr(_of), \
  19. .regulators_node = of_match_ptr("regulators"),\
  20. .ops = &_ops, \
  21. .n_voltages = _n, \
  22. .type = REGULATOR_VOLTAGE, \
  23. .owner = THIS_MODULE, \
  24. .vsel_reg = _vr, \
  25. .vsel_mask = _vm, \
  26. .enable_reg = _er, \
  27. .enable_mask = _em, \
  28. .ramp_delay = _delay, \
  29. .linear_ranges = _lr, \
  30. .n_linear_ranges = ARRAY_SIZE(_lr), \
  31. .curr_table = lp87565_buck_uA, \
  32. .n_current_limits = ARRAY_SIZE(lp87565_buck_uA),\
  33. .csel_reg = (_cr), \
  34. .csel_mask = LP87565_BUCK_CTRL_2_ILIM, \
  35. }, \
  36. .ctrl2_reg = _cr, \
  37. }
  38. struct lp87565_regulator {
  39. struct regulator_desc desc;
  40. unsigned int ctrl2_reg;
  41. };
  42. static const struct lp87565_regulator regulators[];
  43. static const struct regulator_linear_range buck0_1_2_3_ranges[] = {
  44. REGULATOR_LINEAR_RANGE(600000, 0xA, 0x17, 10000),
  45. REGULATOR_LINEAR_RANGE(735000, 0x18, 0x9d, 5000),
  46. REGULATOR_LINEAR_RANGE(1420000, 0x9e, 0xff, 20000),
  47. };
  48. static const unsigned int lp87565_buck_ramp_delay[] = {
  49. 30000, 15000, 10000, 7500, 3800, 1900, 940, 470
  50. };
  51. /* LP87565 BUCK current limit */
  52. static const unsigned int lp87565_buck_uA[] = {
  53. 1500000, 2000000, 2500000, 3000000, 3500000, 4000000, 4500000, 5000000,
  54. };
  55. static int lp87565_buck_set_ramp_delay(struct regulator_dev *rdev,
  56. int ramp_delay)
  57. {
  58. int id = rdev_get_id(rdev);
  59. unsigned int reg;
  60. int ret;
  61. if (ramp_delay <= 470)
  62. reg = 7;
  63. else if (ramp_delay <= 940)
  64. reg = 6;
  65. else if (ramp_delay <= 1900)
  66. reg = 5;
  67. else if (ramp_delay <= 3800)
  68. reg = 4;
  69. else if (ramp_delay <= 7500)
  70. reg = 3;
  71. else if (ramp_delay <= 10000)
  72. reg = 2;
  73. else if (ramp_delay <= 15000)
  74. reg = 1;
  75. else
  76. reg = 0;
  77. ret = regmap_update_bits(rdev->regmap, regulators[id].ctrl2_reg,
  78. LP87565_BUCK_CTRL_2_SLEW_RATE,
  79. reg << __ffs(LP87565_BUCK_CTRL_2_SLEW_RATE));
  80. if (ret) {
  81. dev_err(&rdev->dev, "SLEW RATE write failed: %d\n", ret);
  82. return ret;
  83. }
  84. rdev->constraints->ramp_delay = lp87565_buck_ramp_delay[reg];
  85. /* Conservatively give a 15% margin */
  86. rdev->constraints->ramp_delay =
  87. rdev->constraints->ramp_delay * 85 / 100;
  88. return 0;
  89. }
  90. /* Operations permitted on BUCKs */
  91. static const struct regulator_ops lp87565_buck_ops = {
  92. .is_enabled = regulator_is_enabled_regmap,
  93. .enable = regulator_enable_regmap,
  94. .disable = regulator_disable_regmap,
  95. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  96. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  97. .list_voltage = regulator_list_voltage_linear_range,
  98. .map_voltage = regulator_map_voltage_linear_range,
  99. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  100. .set_ramp_delay = lp87565_buck_set_ramp_delay,
  101. .set_current_limit = regulator_set_current_limit_regmap,
  102. .get_current_limit = regulator_get_current_limit_regmap,
  103. };
  104. static const struct lp87565_regulator regulators[] = {
  105. LP87565_REGULATOR("BUCK0", LP87565_BUCK_0, "buck0", lp87565_buck_ops,
  106. 256, LP87565_REG_BUCK0_VOUT, LP87565_BUCK_VSET,
  107. LP87565_REG_BUCK0_CTRL_1,
  108. LP87565_BUCK_CTRL_1_EN, 3230,
  109. buck0_1_2_3_ranges, LP87565_REG_BUCK0_CTRL_2),
  110. LP87565_REGULATOR("BUCK1", LP87565_BUCK_1, "buck1", lp87565_buck_ops,
  111. 256, LP87565_REG_BUCK1_VOUT, LP87565_BUCK_VSET,
  112. LP87565_REG_BUCK1_CTRL_1,
  113. LP87565_BUCK_CTRL_1_EN, 3230,
  114. buck0_1_2_3_ranges, LP87565_REG_BUCK1_CTRL_2),
  115. LP87565_REGULATOR("BUCK2", LP87565_BUCK_2, "buck2", lp87565_buck_ops,
  116. 256, LP87565_REG_BUCK2_VOUT, LP87565_BUCK_VSET,
  117. LP87565_REG_BUCK2_CTRL_1,
  118. LP87565_BUCK_CTRL_1_EN, 3230,
  119. buck0_1_2_3_ranges, LP87565_REG_BUCK2_CTRL_2),
  120. LP87565_REGULATOR("BUCK3", LP87565_BUCK_3, "buck3", lp87565_buck_ops,
  121. 256, LP87565_REG_BUCK3_VOUT, LP87565_BUCK_VSET,
  122. LP87565_REG_BUCK3_CTRL_1,
  123. LP87565_BUCK_CTRL_1_EN, 3230,
  124. buck0_1_2_3_ranges, LP87565_REG_BUCK3_CTRL_2),
  125. LP87565_REGULATOR("BUCK10", LP87565_BUCK_10, "buck10", lp87565_buck_ops,
  126. 256, LP87565_REG_BUCK0_VOUT, LP87565_BUCK_VSET,
  127. LP87565_REG_BUCK0_CTRL_1,
  128. LP87565_BUCK_CTRL_1_EN |
  129. LP87565_BUCK_CTRL_1_FPWM_MP_0_2, 3230,
  130. buck0_1_2_3_ranges, LP87565_REG_BUCK0_CTRL_2),
  131. LP87565_REGULATOR("BUCK23", LP87565_BUCK_23, "buck23", lp87565_buck_ops,
  132. 256, LP87565_REG_BUCK2_VOUT, LP87565_BUCK_VSET,
  133. LP87565_REG_BUCK2_CTRL_1,
  134. LP87565_BUCK_CTRL_1_EN, 3230,
  135. buck0_1_2_3_ranges, LP87565_REG_BUCK2_CTRL_2),
  136. LP87565_REGULATOR("BUCK3210", LP87565_BUCK_3210, "buck3210",
  137. lp87565_buck_ops, 256, LP87565_REG_BUCK0_VOUT,
  138. LP87565_BUCK_VSET, LP87565_REG_BUCK0_CTRL_1,
  139. LP87565_BUCK_CTRL_1_EN |
  140. LP87565_BUCK_CTRL_1_FPWM_MP_0_2, 3230,
  141. buck0_1_2_3_ranges, LP87565_REG_BUCK0_CTRL_2),
  142. };
  143. static int lp87565_regulator_probe(struct platform_device *pdev)
  144. {
  145. struct lp87565 *lp87565 = dev_get_drvdata(pdev->dev.parent);
  146. struct regulator_config config = { };
  147. struct regulator_dev *rdev;
  148. int i, min_idx, max_idx;
  149. platform_set_drvdata(pdev, lp87565);
  150. config.dev = &pdev->dev;
  151. config.dev->of_node = lp87565->dev->of_node;
  152. config.driver_data = lp87565;
  153. config.regmap = lp87565->regmap;
  154. switch (lp87565->dev_type) {
  155. case LP87565_DEVICE_TYPE_LP87565_Q1:
  156. min_idx = LP87565_BUCK_10;
  157. max_idx = LP87565_BUCK_23;
  158. break;
  159. case LP87565_DEVICE_TYPE_LP87561_Q1:
  160. min_idx = LP87565_BUCK_3210;
  161. max_idx = LP87565_BUCK_3210;
  162. break;
  163. default:
  164. min_idx = LP87565_BUCK_0;
  165. max_idx = LP87565_BUCK_3;
  166. break;
  167. }
  168. for (i = min_idx; i <= max_idx; i++) {
  169. rdev = devm_regulator_register(&pdev->dev, &regulators[i].desc,
  170. &config);
  171. if (IS_ERR(rdev)) {
  172. dev_err(lp87565->dev, "failed to register %s regulator\n",
  173. pdev->name);
  174. return PTR_ERR(rdev);
  175. }
  176. }
  177. return 0;
  178. }
  179. static const struct platform_device_id lp87565_regulator_id_table[] = {
  180. { "lp87565-regulator", },
  181. { "lp87565-q1-regulator", },
  182. { /* sentinel */ }
  183. };
  184. MODULE_DEVICE_TABLE(platform, lp87565_regulator_id_table);
  185. static struct platform_driver lp87565_regulator_driver = {
  186. .driver = {
  187. .name = "lp87565-pmic",
  188. },
  189. .probe = lp87565_regulator_probe,
  190. .id_table = lp87565_regulator_id_table,
  191. };
  192. module_platform_driver(lp87565_regulator_driver);
  193. MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
  194. MODULE_DESCRIPTION("LP87565 voltage regulator driver");
  195. MODULE_LICENSE("GPL v2");