mpq7920.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // mpq7920.c - regulator driver for mps mpq7920
  4. //
  5. // Copyright 2019 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/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/of_regulator.h>
  17. #include <linux/i2c.h>
  18. #include <linux/regmap.h>
  19. #include "mpq7920.h"
  20. #define MPQ7920_BUCK_VOLT_RANGE \
  21. ((MPQ7920_VOLT_MAX - MPQ7920_BUCK_VOLT_MIN)/MPQ7920_VOLT_STEP + 1)
  22. #define MPQ7920_LDO_VOLT_RANGE \
  23. ((MPQ7920_VOLT_MAX - MPQ7920_LDO_VOLT_MIN)/MPQ7920_VOLT_STEP + 1)
  24. #define MPQ7920BUCK(_name, _id, _ilim) \
  25. [MPQ7920_BUCK ## _id] = { \
  26. .id = MPQ7920_BUCK ## _id, \
  27. .name = _name, \
  28. .of_match = _name, \
  29. .regulators_node = "regulators", \
  30. .of_parse_cb = mpq7920_parse_cb, \
  31. .ops = &mpq7920_buck_ops, \
  32. .min_uV = MPQ7920_BUCK_VOLT_MIN, \
  33. .uV_step = MPQ7920_VOLT_STEP, \
  34. .n_voltages = MPQ7920_BUCK_VOLT_RANGE, \
  35. .curr_table = _ilim, \
  36. .n_current_limits = ARRAY_SIZE(_ilim), \
  37. .csel_reg = MPQ7920_BUCK ##_id## _REG_C, \
  38. .csel_mask = MPQ7920_MASK_BUCK_ILIM, \
  39. .enable_reg = MPQ7920_REG_REGULATOR_EN, \
  40. .enable_mask = BIT(MPQ7920_REGULATOR_EN_OFFSET - \
  41. MPQ7920_BUCK ## _id), \
  42. .vsel_reg = MPQ7920_BUCK ##_id## _REG_A, \
  43. .vsel_mask = MPQ7920_MASK_VREF, \
  44. .active_discharge_on = MPQ7920_DISCHARGE_ON, \
  45. .active_discharge_reg = MPQ7920_BUCK ##_id## _REG_B, \
  46. .active_discharge_mask = MPQ7920_MASK_DISCHARGE, \
  47. .soft_start_reg = MPQ7920_BUCK ##_id## _REG_C, \
  48. .soft_start_mask = MPQ7920_MASK_SOFTSTART, \
  49. .owner = THIS_MODULE, \
  50. }
  51. #define MPQ7920LDO(_name, _id, _ops, _ilim, _ilim_sz, _creg, _cmask) \
  52. [MPQ7920_LDO ## _id] = { \
  53. .id = MPQ7920_LDO ## _id, \
  54. .name = _name, \
  55. .of_match = _name, \
  56. .regulators_node = "regulators", \
  57. .ops = _ops, \
  58. .min_uV = MPQ7920_LDO_VOLT_MIN, \
  59. .uV_step = MPQ7920_VOLT_STEP, \
  60. .n_voltages = MPQ7920_LDO_VOLT_RANGE, \
  61. .vsel_reg = MPQ7920_LDO ##_id## _REG_A, \
  62. .vsel_mask = MPQ7920_MASK_VREF, \
  63. .curr_table = _ilim, \
  64. .n_current_limits = _ilim_sz, \
  65. .csel_reg = _creg, \
  66. .csel_mask = _cmask, \
  67. .enable_reg = (_id == 1) ? 0 : MPQ7920_REG_REGULATOR_EN,\
  68. .enable_mask = BIT(MPQ7920_REGULATOR_EN_OFFSET - \
  69. MPQ7920_LDO ##_id + 1), \
  70. .active_discharge_on = MPQ7920_DISCHARGE_ON, \
  71. .active_discharge_mask = MPQ7920_MASK_DISCHARGE, \
  72. .active_discharge_reg = MPQ7920_LDO ##_id## _REG_B, \
  73. .type = REGULATOR_VOLTAGE, \
  74. .owner = THIS_MODULE, \
  75. }
  76. enum mpq7920_regulators {
  77. MPQ7920_BUCK1,
  78. MPQ7920_BUCK2,
  79. MPQ7920_BUCK3,
  80. MPQ7920_BUCK4,
  81. MPQ7920_LDO1, /* LDORTC */
  82. MPQ7920_LDO2,
  83. MPQ7920_LDO3,
  84. MPQ7920_LDO4,
  85. MPQ7920_LDO5,
  86. MPQ7920_MAX_REGULATORS,
  87. };
  88. struct mpq7920_regulator_info {
  89. struct regmap *regmap;
  90. struct regulator_desc *rdesc;
  91. };
  92. static const struct regmap_config mpq7920_regmap_config = {
  93. .reg_bits = 8,
  94. .val_bits = 8,
  95. .max_register = 0x25,
  96. };
  97. /* Current limits array (in uA)
  98. * ILIM1 & ILIM3
  99. */
  100. static const unsigned int mpq7920_I_limits1[] = {
  101. 4600000, 6600000, 7600000, 9300000
  102. };
  103. /* ILIM2 & ILIM4 */
  104. static const unsigned int mpq7920_I_limits2[] = {
  105. 2700000, 3900000, 5100000, 6100000
  106. };
  107. /* LDO4 & LDO5 */
  108. static const unsigned int mpq7920_I_limits3[] = {
  109. 300000, 700000
  110. };
  111. static int mpq7920_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay);
  112. static int mpq7920_parse_cb(struct device_node *np,
  113. const struct regulator_desc *rdesc,
  114. struct regulator_config *config);
  115. /* RTCLDO not controllable, always ON */
  116. static const struct regulator_ops mpq7920_ldortc_ops = {
  117. .list_voltage = regulator_list_voltage_linear,
  118. .map_voltage = regulator_map_voltage_linear,
  119. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  120. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  121. };
  122. static const struct regulator_ops mpq7920_ldo_wo_current_ops = {
  123. .enable = regulator_enable_regmap,
  124. .disable = regulator_disable_regmap,
  125. .is_enabled = regulator_is_enabled_regmap,
  126. .list_voltage = regulator_list_voltage_linear,
  127. .map_voltage = regulator_map_voltage_linear,
  128. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  129. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  130. .set_active_discharge = regulator_set_active_discharge_regmap,
  131. };
  132. static const struct regulator_ops mpq7920_ldo_ops = {
  133. .enable = regulator_enable_regmap,
  134. .disable = regulator_disable_regmap,
  135. .is_enabled = regulator_is_enabled_regmap,
  136. .list_voltage = regulator_list_voltage_linear,
  137. .map_voltage = regulator_map_voltage_linear,
  138. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  139. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  140. .set_active_discharge = regulator_set_active_discharge_regmap,
  141. .get_current_limit = regulator_get_current_limit_regmap,
  142. .set_current_limit = regulator_set_current_limit_regmap,
  143. };
  144. static const struct regulator_ops mpq7920_buck_ops = {
  145. .enable = regulator_enable_regmap,
  146. .disable = regulator_disable_regmap,
  147. .is_enabled = regulator_is_enabled_regmap,
  148. .list_voltage = regulator_list_voltage_linear,
  149. .map_voltage = regulator_map_voltage_linear,
  150. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  151. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  152. .set_active_discharge = regulator_set_active_discharge_regmap,
  153. .set_soft_start = regulator_set_soft_start_regmap,
  154. .set_ramp_delay = mpq7920_set_ramp_delay,
  155. };
  156. static struct regulator_desc mpq7920_regulators_desc[MPQ7920_MAX_REGULATORS] = {
  157. MPQ7920BUCK("buck1", 1, mpq7920_I_limits1),
  158. MPQ7920BUCK("buck2", 2, mpq7920_I_limits2),
  159. MPQ7920BUCK("buck3", 3, mpq7920_I_limits1),
  160. MPQ7920BUCK("buck4", 4, mpq7920_I_limits2),
  161. MPQ7920LDO("ldortc", 1, &mpq7920_ldortc_ops, NULL, 0, 0, 0),
  162. MPQ7920LDO("ldo2", 2, &mpq7920_ldo_wo_current_ops, NULL, 0, 0, 0),
  163. MPQ7920LDO("ldo3", 3, &mpq7920_ldo_wo_current_ops, NULL, 0, 0, 0),
  164. MPQ7920LDO("ldo4", 4, &mpq7920_ldo_ops, mpq7920_I_limits3,
  165. ARRAY_SIZE(mpq7920_I_limits3), MPQ7920_LDO4_REG_B,
  166. MPQ7920_MASK_LDO_ILIM),
  167. MPQ7920LDO("ldo5", 5, &mpq7920_ldo_ops, mpq7920_I_limits3,
  168. ARRAY_SIZE(mpq7920_I_limits3), MPQ7920_LDO5_REG_B,
  169. MPQ7920_MASK_LDO_ILIM),
  170. };
  171. /*
  172. * DVS ramp rate BUCK1 to BUCK4
  173. * 00-01: Reserved
  174. * 10: 8mV/us
  175. * 11: 4mV/us
  176. */
  177. static int mpq7920_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  178. {
  179. unsigned int ramp_val;
  180. if (ramp_delay > 8000 || ramp_delay < 0)
  181. return -EINVAL;
  182. if (ramp_delay <= 4000)
  183. ramp_val = 3;
  184. else
  185. ramp_val = 2;
  186. return regmap_update_bits(rdev->regmap, MPQ7920_REG_CTL0,
  187. MPQ7920_MASK_DVS_SLEWRATE, ramp_val << 6);
  188. }
  189. static int mpq7920_parse_cb(struct device_node *np,
  190. const struct regulator_desc *desc,
  191. struct regulator_config *config)
  192. {
  193. uint8_t val;
  194. int ret;
  195. struct mpq7920_regulator_info *info = config->driver_data;
  196. struct regulator_desc *rdesc = &info->rdesc[desc->id];
  197. if (of_property_read_bool(np, "mps,buck-ovp-disable")) {
  198. regmap_update_bits(config->regmap,
  199. MPQ7920_BUCK1_REG_B + (rdesc->id * 4),
  200. MPQ7920_MASK_OVP, MPQ7920_OVP_DISABLE);
  201. }
  202. ret = of_property_read_u8(np, "mps,buck-phase-delay", &val);
  203. if (!ret) {
  204. regmap_update_bits(config->regmap,
  205. MPQ7920_BUCK1_REG_C + (rdesc->id * 4),
  206. MPQ7920_MASK_BUCK_PHASE_DEALY,
  207. (val & 3) << 4);
  208. }
  209. ret = of_property_read_u8(np, "mps,buck-softstart", &val);
  210. if (!ret)
  211. rdesc->soft_start_val_on = (val & 3) << 2;
  212. return 0;
  213. }
  214. static void mpq7920_parse_dt(struct device *dev,
  215. struct mpq7920_regulator_info *info)
  216. {
  217. int ret;
  218. struct device_node *np = dev->of_node;
  219. uint8_t freq;
  220. np = of_get_child_by_name(np, "regulators");
  221. if (!np) {
  222. dev_err(dev, "missing 'regulators' subnode in DT\n");
  223. return;
  224. }
  225. ret = of_property_read_u8(np, "mps,switch-freq", &freq);
  226. if (!ret) {
  227. regmap_update_bits(info->regmap, MPQ7920_REG_CTL0,
  228. MPQ7920_MASK_SWITCH_FREQ,
  229. (freq & 3) << 4);
  230. }
  231. of_node_put(np);
  232. }
  233. static int mpq7920_i2c_probe(struct i2c_client *client)
  234. {
  235. struct device *dev = &client->dev;
  236. struct mpq7920_regulator_info *info;
  237. struct regulator_config config = { NULL, };
  238. struct regulator_dev *rdev;
  239. struct regmap *regmap;
  240. int i;
  241. info = devm_kzalloc(dev, sizeof(struct mpq7920_regulator_info),
  242. GFP_KERNEL);
  243. if (!info)
  244. return -ENOMEM;
  245. info->rdesc = mpq7920_regulators_desc;
  246. regmap = devm_regmap_init_i2c(client, &mpq7920_regmap_config);
  247. if (IS_ERR(regmap)) {
  248. dev_err(dev, "Failed to allocate regmap!\n");
  249. return PTR_ERR(regmap);
  250. }
  251. i2c_set_clientdata(client, info);
  252. info->regmap = regmap;
  253. if (client->dev.of_node)
  254. mpq7920_parse_dt(&client->dev, info);
  255. config.dev = dev;
  256. config.regmap = regmap;
  257. config.driver_data = info;
  258. for (i = 0; i < MPQ7920_MAX_REGULATORS; i++) {
  259. rdev = devm_regulator_register(dev,
  260. &mpq7920_regulators_desc[i],
  261. &config);
  262. if (IS_ERR(rdev)) {
  263. dev_err(dev, "Failed to register regulator!\n");
  264. return PTR_ERR(rdev);
  265. }
  266. }
  267. return 0;
  268. }
  269. static const struct of_device_id mpq7920_of_match[] = {
  270. { .compatible = "mps,mpq7920"},
  271. {},
  272. };
  273. MODULE_DEVICE_TABLE(of, mpq7920_of_match);
  274. static const struct i2c_device_id mpq7920_id[] = {
  275. { "mpq7920", },
  276. { },
  277. };
  278. MODULE_DEVICE_TABLE(i2c, mpq7920_id);
  279. static struct i2c_driver mpq7920_regulator_driver = {
  280. .driver = {
  281. .name = "mpq7920",
  282. .of_match_table = of_match_ptr(mpq7920_of_match),
  283. },
  284. .probe_new = mpq7920_i2c_probe,
  285. .id_table = mpq7920_id,
  286. };
  287. module_i2c_driver(mpq7920_regulator_driver);
  288. MODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>");
  289. MODULE_DESCRIPTION("MPQ7920 PMIC regulator driver");
  290. MODULE_LICENSE("GPL");