lp87565-regulator.c 6.9 KB

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