tps6287x-regulator.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2023 Axis Communications AB
  4. *
  5. * Driver for Texas Instruments TPS6287x PMIC.
  6. * Datasheet: https://www.ti.com/lit/ds/symlink/tps62873.pdf
  7. */
  8. #include <linux/err.h>
  9. #include <linux/i2c.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/module.h>
  12. #include <linux/regmap.h>
  13. #include <linux/regulator/of_regulator.h>
  14. #include <linux/regulator/machine.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/bitfield.h>
  17. #include <linux/linear_range.h>
  18. #define TPS6287X_VSET 0x00
  19. #define TPS6287X_CTRL1 0x01
  20. #define TPS6287X_CTRL1_VRAMP GENMASK(1, 0)
  21. #define TPS6287X_CTRL1_FPWMEN BIT(4)
  22. #define TPS6287X_CTRL1_SWEN BIT(5)
  23. #define TPS6287X_CTRL2 0x02
  24. #define TPS6287X_CTRL2_VRANGE GENMASK(3, 2)
  25. #define TPS6287X_CTRL3 0x03
  26. #define TPS6287X_STATUS 0x04
  27. static const struct regmap_config tps6287x_regmap_config = {
  28. .reg_bits = 8,
  29. .val_bits = 8,
  30. .max_register = TPS6287X_STATUS,
  31. };
  32. static const struct linear_range tps6287x_voltage_ranges[] = {
  33. LINEAR_RANGE(400000, 0, 0xFF, 1250),
  34. LINEAR_RANGE(400000, 0, 0xFF, 2500),
  35. LINEAR_RANGE(400000, 0, 0xFF, 5000),
  36. LINEAR_RANGE(800000, 0, 0xFF, 10000),
  37. };
  38. static const unsigned int tps6287x_voltage_range_sel[] = {
  39. 0x0, 0x1, 0x2, 0x3
  40. };
  41. static const unsigned int tps6287x_ramp_table[] = {
  42. 10000, 5000, 1250, 500
  43. };
  44. static int tps6287x_set_mode(struct regulator_dev *rdev, unsigned int mode)
  45. {
  46. unsigned int val;
  47. switch (mode) {
  48. case REGULATOR_MODE_NORMAL:
  49. val = 0;
  50. break;
  51. case REGULATOR_MODE_FAST:
  52. val = TPS6287X_CTRL1_FPWMEN;
  53. break;
  54. default:
  55. return -EINVAL;
  56. }
  57. return regmap_update_bits(rdev->regmap, TPS6287X_CTRL1,
  58. TPS6287X_CTRL1_FPWMEN, val);
  59. }
  60. static unsigned int tps6287x_get_mode(struct regulator_dev *rdev)
  61. {
  62. unsigned int val;
  63. int ret;
  64. ret = regmap_read(rdev->regmap, TPS6287X_CTRL1, &val);
  65. if (ret < 0)
  66. return 0;
  67. return (val & TPS6287X_CTRL1_FPWMEN) ? REGULATOR_MODE_FAST :
  68. REGULATOR_MODE_NORMAL;
  69. }
  70. static unsigned int tps6287x_of_map_mode(unsigned int mode)
  71. {
  72. switch (mode) {
  73. case REGULATOR_MODE_NORMAL:
  74. case REGULATOR_MODE_FAST:
  75. return mode;
  76. default:
  77. return REGULATOR_MODE_INVALID;
  78. }
  79. }
  80. static const struct regulator_ops tps6287x_regulator_ops = {
  81. .enable = regulator_enable_regmap,
  82. .disable = regulator_disable_regmap,
  83. .set_mode = tps6287x_set_mode,
  84. .get_mode = tps6287x_get_mode,
  85. .is_enabled = regulator_is_enabled_regmap,
  86. .get_voltage_sel = regulator_get_voltage_sel_pickable_regmap,
  87. .set_voltage_sel = regulator_set_voltage_sel_pickable_regmap,
  88. .list_voltage = regulator_list_voltage_pickable_linear_range,
  89. .set_ramp_delay = regulator_set_ramp_delay_regmap,
  90. };
  91. static const struct regulator_desc tps6287x_reg = {
  92. .name = "tps6287x",
  93. .owner = THIS_MODULE,
  94. .ops = &tps6287x_regulator_ops,
  95. .of_map_mode = tps6287x_of_map_mode,
  96. .type = REGULATOR_VOLTAGE,
  97. .enable_reg = TPS6287X_CTRL1,
  98. .enable_mask = TPS6287X_CTRL1_SWEN,
  99. .vsel_reg = TPS6287X_VSET,
  100. .vsel_mask = 0xFF,
  101. .vsel_range_reg = TPS6287X_CTRL2,
  102. .vsel_range_mask = TPS6287X_CTRL2_VRANGE,
  103. .range_applied_by_vsel = true,
  104. .ramp_reg = TPS6287X_CTRL1,
  105. .ramp_mask = TPS6287X_CTRL1_VRAMP,
  106. .ramp_delay_table = tps6287x_ramp_table,
  107. .n_ramp_values = ARRAY_SIZE(tps6287x_ramp_table),
  108. .n_voltages = 256 * ARRAY_SIZE(tps6287x_voltage_ranges),
  109. .linear_ranges = tps6287x_voltage_ranges,
  110. .n_linear_ranges = ARRAY_SIZE(tps6287x_voltage_ranges),
  111. .linear_range_selectors_bitfield = tps6287x_voltage_range_sel,
  112. };
  113. static int tps6287x_i2c_probe(struct i2c_client *i2c)
  114. {
  115. struct device *dev = &i2c->dev;
  116. struct regulator_config config = {};
  117. struct regulator_dev *rdev;
  118. config.regmap = devm_regmap_init_i2c(i2c, &tps6287x_regmap_config);
  119. if (IS_ERR(config.regmap)) {
  120. dev_err(dev, "Failed to init i2c\n");
  121. return PTR_ERR(config.regmap);
  122. }
  123. config.dev = dev;
  124. config.of_node = dev->of_node;
  125. config.init_data = of_get_regulator_init_data(dev, dev->of_node,
  126. &tps6287x_reg);
  127. rdev = devm_regulator_register(dev, &tps6287x_reg, &config);
  128. if (IS_ERR(rdev)) {
  129. dev_err(dev, "Failed to register regulator\n");
  130. return PTR_ERR(rdev);
  131. }
  132. dev_dbg(dev, "Probed regulator\n");
  133. return 0;
  134. }
  135. static const struct of_device_id tps6287x_dt_ids[] = {
  136. { .compatible = "ti,tps62870", },
  137. { .compatible = "ti,tps62871", },
  138. { .compatible = "ti,tps62872", },
  139. { .compatible = "ti,tps62873", },
  140. { }
  141. };
  142. MODULE_DEVICE_TABLE(of, tps6287x_dt_ids);
  143. static const struct i2c_device_id tps6287x_i2c_id[] = {
  144. { "tps62870" },
  145. { "tps62871" },
  146. { "tps62872" },
  147. { "tps62873" },
  148. {}
  149. };
  150. MODULE_DEVICE_TABLE(i2c, tps6287x_i2c_id);
  151. static struct i2c_driver tps6287x_regulator_driver = {
  152. .driver = {
  153. .name = "tps6287x",
  154. .of_match_table = tps6287x_dt_ids,
  155. },
  156. .probe = tps6287x_i2c_probe,
  157. .id_table = tps6287x_i2c_id,
  158. };
  159. module_i2c_driver(tps6287x_regulator_driver);
  160. MODULE_AUTHOR("Mårten Lindahl <marten.lindahl@axis.com>");
  161. MODULE_DESCRIPTION("Regulator driver for TI TPS6287X PMIC");
  162. MODULE_LICENSE("GPL");