mp5416.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // mp5416.c - regulator driver for mps mp5416
  4. //
  5. // Copyright 2020 Monolithic Power Systems, Inc
  6. //
  7. // Author: Saravanan Sekar <sravanhome@gmail.com>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/err.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/regulator/driver.h>
  15. #include <linux/i2c.h>
  16. #define MP5416_REG_CTL0 0x00
  17. #define MP5416_REG_CTL1 0x01
  18. #define MP5416_REG_CTL2 0x02
  19. #define MP5416_REG_ILIM 0x03
  20. #define MP5416_REG_BUCK1 0x04
  21. #define MP5416_REG_BUCK2 0x05
  22. #define MP5416_REG_BUCK3 0x06
  23. #define MP5416_REG_BUCK4 0x07
  24. #define MP5416_REG_LDO1 0x08
  25. #define MP5416_REG_LDO2 0x09
  26. #define MP5416_REG_LDO3 0x0a
  27. #define MP5416_REG_LDO4 0x0b
  28. #define MP5416_REGULATOR_EN BIT(7)
  29. #define MP5416_MASK_VSET 0x7f
  30. #define MP5416_MASK_BUCK1_ILIM 0xc0
  31. #define MP5416_MASK_BUCK2_ILIM 0x0c
  32. #define MP5416_MASK_BUCK3_ILIM 0x30
  33. #define MP5416_MASK_BUCK4_ILIM 0x03
  34. #define MP5416_MASK_DVS_SLEWRATE 0xc0
  35. /* values in uV */
  36. #define MP5416_VOLT1_MIN 600000
  37. #define MP5416_VOLT1_MAX 2187500
  38. #define MP5416_VOLT1_STEP 12500
  39. #define MP5416_VOLT2_MIN 800000
  40. #define MP5416_VOLT2_MAX 3975000
  41. #define MP5416_VOLT2_STEP 25000
  42. #define MP5416_VOLT1_RANGE \
  43. ((MP5416_VOLT1_MAX - MP5416_VOLT1_MIN)/MP5416_VOLT1_STEP + 1)
  44. #define MP5416_VOLT2_RANGE \
  45. ((MP5416_VOLT2_MAX - MP5416_VOLT2_MIN)/MP5416_VOLT2_STEP + 1)
  46. #define MP5416BUCK(_name, _id, _ilim, _dreg, _dval, _vsel) \
  47. [MP5416_BUCK ## _id] = { \
  48. .id = MP5416_BUCK ## _id, \
  49. .name = _name, \
  50. .of_match = _name, \
  51. .regulators_node = "regulators", \
  52. .ops = &mp5416_buck_ops, \
  53. .min_uV = MP5416_VOLT ##_vsel## _MIN, \
  54. .uV_step = MP5416_VOLT ##_vsel## _STEP, \
  55. .n_voltages = MP5416_VOLT ##_vsel## _RANGE, \
  56. .curr_table = _ilim, \
  57. .n_current_limits = ARRAY_SIZE(_ilim), \
  58. .csel_reg = MP5416_REG_ILIM, \
  59. .csel_mask = MP5416_MASK_BUCK ## _id ##_ILIM, \
  60. .vsel_reg = MP5416_REG_BUCK ## _id, \
  61. .vsel_mask = MP5416_MASK_VSET, \
  62. .enable_reg = MP5416_REG_BUCK ## _id, \
  63. .enable_mask = MP5416_REGULATOR_EN, \
  64. .active_discharge_on = _dval, \
  65. .active_discharge_reg = _dreg, \
  66. .active_discharge_mask = _dval, \
  67. .owner = THIS_MODULE, \
  68. }
  69. #define MP5416LDO(_name, _id, _dval) \
  70. [MP5416_LDO ## _id] = { \
  71. .id = MP5416_LDO ## _id, \
  72. .name = _name, \
  73. .of_match = _name, \
  74. .regulators_node = "regulators", \
  75. .ops = &mp5416_ldo_ops, \
  76. .min_uV = MP5416_VOLT2_MIN, \
  77. .uV_step = MP5416_VOLT2_STEP, \
  78. .n_voltages = MP5416_VOLT2_RANGE, \
  79. .vsel_reg = MP5416_REG_LDO ##_id, \
  80. .vsel_mask = MP5416_MASK_VSET, \
  81. .enable_reg = MP5416_REG_LDO ##_id, \
  82. .enable_mask = MP5416_REGULATOR_EN, \
  83. .active_discharge_on = _dval, \
  84. .active_discharge_reg = MP5416_REG_CTL2, \
  85. .active_discharge_mask = _dval, \
  86. .owner = THIS_MODULE, \
  87. }
  88. enum mp5416_regulators {
  89. MP5416_BUCK1,
  90. MP5416_BUCK2,
  91. MP5416_BUCK3,
  92. MP5416_BUCK4,
  93. MP5416_LDO1,
  94. MP5416_LDO2,
  95. MP5416_LDO3,
  96. MP5416_LDO4,
  97. MP5416_MAX_REGULATORS,
  98. };
  99. static const struct regmap_config mp5416_regmap_config = {
  100. .reg_bits = 8,
  101. .val_bits = 8,
  102. .max_register = 0x0d,
  103. };
  104. /* Current limits array (in uA)
  105. * ILIM1 & ILIM3
  106. */
  107. static const unsigned int mp5416_I_limits1[] = {
  108. 3800000, 4600000, 5600000, 6800000
  109. };
  110. /* ILIM2 & ILIM4 */
  111. static const unsigned int mp5416_I_limits2[] = {
  112. 2200000, 3200000, 4200000, 5200000
  113. };
  114. static int mp5416_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay);
  115. static const struct regulator_ops mp5416_ldo_ops = {
  116. .enable = regulator_enable_regmap,
  117. .disable = regulator_disable_regmap,
  118. .is_enabled = regulator_is_enabled_regmap,
  119. .list_voltage = regulator_list_voltage_linear,
  120. .map_voltage = regulator_map_voltage_linear,
  121. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  122. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  123. .set_active_discharge = regulator_set_active_discharge_regmap,
  124. };
  125. static const struct regulator_ops mp5416_buck_ops = {
  126. .enable = regulator_enable_regmap,
  127. .disable = regulator_disable_regmap,
  128. .is_enabled = regulator_is_enabled_regmap,
  129. .list_voltage = regulator_list_voltage_linear,
  130. .map_voltage = regulator_map_voltage_linear,
  131. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  132. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  133. .set_active_discharge = regulator_set_active_discharge_regmap,
  134. .get_current_limit = regulator_get_current_limit_regmap,
  135. .set_current_limit = regulator_set_current_limit_regmap,
  136. .set_ramp_delay = mp5416_set_ramp_delay,
  137. };
  138. static struct regulator_desc mp5416_regulators_desc[MP5416_MAX_REGULATORS] = {
  139. MP5416BUCK("buck1", 1, mp5416_I_limits1, MP5416_REG_CTL1, BIT(0), 1),
  140. MP5416BUCK("buck2", 2, mp5416_I_limits2, MP5416_REG_CTL1, BIT(1), 2),
  141. MP5416BUCK("buck3", 3, mp5416_I_limits1, MP5416_REG_CTL1, BIT(2), 1),
  142. MP5416BUCK("buck4", 4, mp5416_I_limits2, MP5416_REG_CTL2, BIT(5), 2),
  143. MP5416LDO("ldo1", 1, BIT(4)),
  144. MP5416LDO("ldo2", 2, BIT(3)),
  145. MP5416LDO("ldo3", 3, BIT(2)),
  146. MP5416LDO("ldo4", 4, BIT(1)),
  147. };
  148. /*
  149. * DVS ramp rate BUCK1 to BUCK4
  150. * 00: 32mV/us
  151. * 01: 16mV/us
  152. * 10: 8mV/us
  153. * 11: 4mV/us
  154. */
  155. static int mp5416_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  156. {
  157. unsigned int ramp_val;
  158. if (ramp_delay > 32000 || ramp_delay < 0)
  159. return -EINVAL;
  160. if (ramp_delay <= 4000)
  161. ramp_val = 3;
  162. else if (ramp_delay <= 8000)
  163. ramp_val = 2;
  164. else if (ramp_delay <= 16000)
  165. ramp_val = 1;
  166. else
  167. ramp_val = 0;
  168. return regmap_update_bits(rdev->regmap, MP5416_REG_CTL2,
  169. MP5416_MASK_DVS_SLEWRATE, ramp_val << 6);
  170. }
  171. static int mp5416_i2c_probe(struct i2c_client *client)
  172. {
  173. struct device *dev = &client->dev;
  174. struct regulator_config config = { NULL, };
  175. struct regulator_dev *rdev;
  176. struct regmap *regmap;
  177. int i;
  178. regmap = devm_regmap_init_i2c(client, &mp5416_regmap_config);
  179. if (IS_ERR(regmap)) {
  180. dev_err(dev, "Failed to allocate regmap!\n");
  181. return PTR_ERR(regmap);
  182. }
  183. config.dev = dev;
  184. config.regmap = regmap;
  185. for (i = 0; i < MP5416_MAX_REGULATORS; i++) {
  186. rdev = devm_regulator_register(dev,
  187. &mp5416_regulators_desc[i],
  188. &config);
  189. if (IS_ERR(rdev)) {
  190. dev_err(dev, "Failed to register regulator!\n");
  191. return PTR_ERR(rdev);
  192. }
  193. }
  194. return 0;
  195. }
  196. static const struct of_device_id mp5416_of_match[] = {
  197. { .compatible = "mps,mp5416" },
  198. {},
  199. };
  200. MODULE_DEVICE_TABLE(of, mp5416_of_match);
  201. static const struct i2c_device_id mp5416_id[] = {
  202. { "mp5416", },
  203. { },
  204. };
  205. MODULE_DEVICE_TABLE(i2c, mp5416_id);
  206. static struct i2c_driver mp5416_regulator_driver = {
  207. .driver = {
  208. .name = "mp5416",
  209. .of_match_table = of_match_ptr(mp5416_of_match),
  210. },
  211. .probe_new = mp5416_i2c_probe,
  212. .id_table = mp5416_id,
  213. };
  214. module_i2c_driver(mp5416_regulator_driver);
  215. MODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>");
  216. MODULE_DESCRIPTION("MP5416 PMIC regulator driver");
  217. MODULE_LICENSE("GPL");