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