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