lp873x-regulator.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Regulator driver for LP873X PMIC
  3. *
  4. * Copyright (C) 2016 Texas Instruments Incorporated - http://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. }, \
  40. .ctrl2_reg = _cr, \
  41. }
  42. struct lp873x_regulator {
  43. struct regulator_desc desc;
  44. unsigned int ctrl2_reg;
  45. };
  46. static const struct lp873x_regulator regulators[];
  47. static const struct regulator_linear_range buck0_buck1_ranges[] = {
  48. REGULATOR_LINEAR_RANGE(0, 0x0, 0x13, 0),
  49. REGULATOR_LINEAR_RANGE(700000, 0x14, 0x17, 10000),
  50. REGULATOR_LINEAR_RANGE(735000, 0x18, 0x9d, 5000),
  51. REGULATOR_LINEAR_RANGE(1420000, 0x9e, 0xff, 20000),
  52. };
  53. static const struct regulator_linear_range ldo0_ldo1_ranges[] = {
  54. REGULATOR_LINEAR_RANGE(800000, 0x0, 0x19, 100000),
  55. };
  56. static unsigned int lp873x_buck_ramp_delay[] = {
  57. 30000, 15000, 10000, 7500, 3800, 1900, 940, 470
  58. };
  59. /* LP873X BUCK current limit */
  60. static const unsigned int lp873x_buck_uA[] = {
  61. 1500000, 2000000, 2500000, 3000000, 3500000, 4000000,
  62. };
  63. static int lp873x_buck_set_ramp_delay(struct regulator_dev *rdev,
  64. int ramp_delay)
  65. {
  66. int id = rdev_get_id(rdev);
  67. struct lp873x *lp873 = rdev_get_drvdata(rdev);
  68. unsigned int reg;
  69. int ret;
  70. if (ramp_delay <= 470)
  71. reg = 7;
  72. else if (ramp_delay <= 940)
  73. reg = 6;
  74. else if (ramp_delay <= 1900)
  75. reg = 5;
  76. else if (ramp_delay <= 3800)
  77. reg = 4;
  78. else if (ramp_delay <= 7500)
  79. reg = 3;
  80. else if (ramp_delay <= 10000)
  81. reg = 2;
  82. else if (ramp_delay <= 15000)
  83. reg = 1;
  84. else
  85. reg = 0;
  86. ret = regmap_update_bits(lp873->regmap, regulators[id].ctrl2_reg,
  87. LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE,
  88. reg << __ffs(LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE));
  89. if (ret) {
  90. dev_err(lp873->dev, "SLEW RATE write failed: %d\n", ret);
  91. return ret;
  92. }
  93. rdev->constraints->ramp_delay = lp873x_buck_ramp_delay[reg];
  94. return 0;
  95. }
  96. static int lp873x_buck_set_current_limit(struct regulator_dev *rdev,
  97. int min_uA, int max_uA)
  98. {
  99. int id = rdev_get_id(rdev);
  100. struct lp873x *lp873 = rdev_get_drvdata(rdev);
  101. int i;
  102. for (i = ARRAY_SIZE(lp873x_buck_uA) - 1; i >= 0; i--) {
  103. if (lp873x_buck_uA[i] >= min_uA &&
  104. lp873x_buck_uA[i] <= max_uA)
  105. return regmap_update_bits(lp873->regmap,
  106. regulators[id].ctrl2_reg,
  107. LP873X_BUCK0_CTRL_2_BUCK0_ILIM,
  108. i << __ffs(LP873X_BUCK0_CTRL_2_BUCK0_ILIM));
  109. }
  110. return -EINVAL;
  111. }
  112. static int lp873x_buck_get_current_limit(struct regulator_dev *rdev)
  113. {
  114. int id = rdev_get_id(rdev);
  115. struct lp873x *lp873 = rdev_get_drvdata(rdev);
  116. int ret;
  117. unsigned int val;
  118. ret = regmap_read(lp873->regmap, regulators[id].ctrl2_reg, &val);
  119. if (ret)
  120. return ret;
  121. val = (val & LP873X_BUCK0_CTRL_2_BUCK0_ILIM) >>
  122. __ffs(LP873X_BUCK0_CTRL_2_BUCK0_ILIM);
  123. return (val < ARRAY_SIZE(lp873x_buck_uA)) ?
  124. lp873x_buck_uA[val] : -EINVAL;
  125. }
  126. /* Operations permitted on BUCK0, BUCK1 */
  127. static struct regulator_ops lp873x_buck01_ops = {
  128. .is_enabled = regulator_is_enabled_regmap,
  129. .enable = regulator_enable_regmap,
  130. .disable = regulator_disable_regmap,
  131. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  132. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  133. .list_voltage = regulator_list_voltage_linear_range,
  134. .map_voltage = regulator_map_voltage_linear_range,
  135. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  136. .set_ramp_delay = lp873x_buck_set_ramp_delay,
  137. .set_current_limit = lp873x_buck_set_current_limit,
  138. .get_current_limit = lp873x_buck_get_current_limit,
  139. };
  140. /* Operations permitted on LDO0 and LDO1 */
  141. static struct regulator_ops lp873x_ldo01_ops = {
  142. .is_enabled = regulator_is_enabled_regmap,
  143. .enable = regulator_enable_regmap,
  144. .disable = regulator_disable_regmap,
  145. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  146. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  147. .list_voltage = regulator_list_voltage_linear_range,
  148. .map_voltage = regulator_map_voltage_linear_range,
  149. };
  150. static const struct lp873x_regulator regulators[] = {
  151. LP873X_REGULATOR("BUCK0", LP873X_BUCK_0, "buck0", lp873x_buck01_ops,
  152. 256, LP873X_REG_BUCK0_VOUT,
  153. LP873X_BUCK0_VOUT_BUCK0_VSET, LP873X_REG_BUCK0_CTRL_1,
  154. LP873X_BUCK0_CTRL_1_BUCK0_EN, 10000,
  155. buck0_buck1_ranges, LP873X_REG_BUCK0_CTRL_2),
  156. LP873X_REGULATOR("BUCK1", LP873X_BUCK_1, "buck1", lp873x_buck01_ops,
  157. 256, LP873X_REG_BUCK1_VOUT,
  158. LP873X_BUCK1_VOUT_BUCK1_VSET, LP873X_REG_BUCK1_CTRL_1,
  159. LP873X_BUCK1_CTRL_1_BUCK1_EN, 10000,
  160. buck0_buck1_ranges, LP873X_REG_BUCK1_CTRL_2),
  161. LP873X_REGULATOR("LDO0", LP873X_LDO_0, "ldo0", lp873x_ldo01_ops, 26,
  162. LP873X_REG_LDO0_VOUT, LP873X_LDO0_VOUT_LDO0_VSET,
  163. LP873X_REG_LDO0_CTRL,
  164. LP873X_LDO0_CTRL_LDO0_EN, 0, ldo0_ldo1_ranges, 0xFF),
  165. LP873X_REGULATOR("LDO1", LP873X_LDO_1, "ldo1", lp873x_ldo01_ops, 26,
  166. LP873X_REG_LDO1_VOUT, LP873X_LDO1_VOUT_LDO1_VSET,
  167. LP873X_REG_LDO1_CTRL,
  168. LP873X_LDO1_CTRL_LDO1_EN, 0, ldo0_ldo1_ranges, 0xFF),
  169. };
  170. static int lp873x_regulator_probe(struct platform_device *pdev)
  171. {
  172. struct lp873x *lp873 = dev_get_drvdata(pdev->dev.parent);
  173. struct regulator_config config = { };
  174. struct regulator_dev *rdev;
  175. int i;
  176. platform_set_drvdata(pdev, lp873);
  177. config.dev = &pdev->dev;
  178. config.dev->of_node = lp873->dev->of_node;
  179. config.driver_data = lp873;
  180. config.regmap = lp873->regmap;
  181. for (i = 0; i < ARRAY_SIZE(regulators); i++) {
  182. rdev = devm_regulator_register(&pdev->dev, &regulators[i].desc,
  183. &config);
  184. if (IS_ERR(rdev)) {
  185. dev_err(lp873->dev, "failed to register %s regulator\n",
  186. pdev->name);
  187. return PTR_ERR(rdev);
  188. }
  189. }
  190. return 0;
  191. }
  192. static const struct platform_device_id lp873x_regulator_id_table[] = {
  193. { "lp873x-regulator", },
  194. { /* sentinel */ }
  195. };
  196. MODULE_DEVICE_TABLE(platform, lp873x_regulator_id_table);
  197. static struct platform_driver lp873x_regulator_driver = {
  198. .driver = {
  199. .name = "lp873x-pmic",
  200. },
  201. .probe = lp873x_regulator_probe,
  202. .id_table = lp873x_regulator_id_table,
  203. };
  204. module_platform_driver(lp873x_regulator_driver);
  205. MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
  206. MODULE_DESCRIPTION("LP873X voltage regulator driver");
  207. MODULE_ALIAS("platform:lp873x-pmic");
  208. MODULE_LICENSE("GPL v2");