tps6286x-regulator.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright Axis Communications AB
  3. #include <linux/err.h>
  4. #include <linux/i2c.h>
  5. #include <linux/module.h>
  6. #include <linux/of.h>
  7. #include <linux/regmap.h>
  8. #include <linux/regulator/of_regulator.h>
  9. #include <linux/regulator/machine.h>
  10. #include <linux/regulator/driver.h>
  11. #include <dt-bindings/regulator/ti,tps62864.h>
  12. #define TPS6286X_VOUT1 0x01
  13. #define TPS6286X_VOUT1_VO1_SET GENMASK(7, 0)
  14. #define TPS6286X_CONTROL 0x03
  15. #define TPS6286X_CONTROL_FPWM BIT(4)
  16. #define TPS6286X_CONTROL_SWEN BIT(5)
  17. #define TPS6286X_STATUS 0x05
  18. #define TPS6286X_MIN_MV 400
  19. #define TPS6286X_MAX_MV 1675
  20. #define TPS6286X_STEP_MV 5
  21. static bool tps6286x_volatile_reg(struct device *dev, unsigned int reg)
  22. {
  23. return reg == TPS6286X_STATUS;
  24. }
  25. static const struct regmap_config tps6286x_regmap_config = {
  26. .reg_bits = 8,
  27. .val_bits = 8,
  28. .cache_type = REGCACHE_MAPLE,
  29. .volatile_reg = tps6286x_volatile_reg,
  30. };
  31. static int tps6286x_set_mode(struct regulator_dev *rdev, unsigned int mode)
  32. {
  33. unsigned int val;
  34. switch (mode) {
  35. case REGULATOR_MODE_NORMAL:
  36. val = 0;
  37. break;
  38. case REGULATOR_MODE_FAST:
  39. val = TPS6286X_CONTROL_FPWM;
  40. break;
  41. default:
  42. return -EINVAL;
  43. }
  44. return regmap_update_bits(rdev->regmap, TPS6286X_CONTROL,
  45. TPS6286X_CONTROL_FPWM, val);
  46. }
  47. static unsigned int tps6286x_get_mode(struct regulator_dev *rdev)
  48. {
  49. unsigned int val;
  50. int ret;
  51. ret = regmap_read(rdev->regmap, TPS6286X_CONTROL, &val);
  52. if (ret < 0)
  53. return 0;
  54. return (val & TPS6286X_CONTROL_FPWM) ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
  55. }
  56. static const struct regulator_ops tps6286x_regulator_ops = {
  57. .enable = regulator_enable_regmap,
  58. .disable = regulator_disable_regmap,
  59. .set_mode = tps6286x_set_mode,
  60. .get_mode = tps6286x_get_mode,
  61. .is_enabled = regulator_is_enabled_regmap,
  62. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  63. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  64. .list_voltage = regulator_list_voltage_linear,
  65. };
  66. static unsigned int tps6286x_of_map_mode(unsigned int mode)
  67. {
  68. switch (mode) {
  69. case TPS62864_MODE_NORMAL:
  70. return REGULATOR_MODE_NORMAL;
  71. case TPS62864_MODE_FPWM:
  72. return REGULATOR_MODE_FAST;
  73. default:
  74. return REGULATOR_MODE_INVALID;
  75. }
  76. }
  77. static const struct regulator_desc tps6286x_reg = {
  78. .name = "tps6286x",
  79. .of_match = "SW",
  80. .owner = THIS_MODULE,
  81. .ops = &tps6286x_regulator_ops,
  82. .of_map_mode = tps6286x_of_map_mode,
  83. .regulators_node = "regulators",
  84. .type = REGULATOR_VOLTAGE,
  85. .n_voltages = ((TPS6286X_MAX_MV - TPS6286X_MIN_MV) / TPS6286X_STEP_MV) + 1,
  86. .min_uV = TPS6286X_MIN_MV * 1000,
  87. .uV_step = TPS6286X_STEP_MV * 1000,
  88. .vsel_reg = TPS6286X_VOUT1,
  89. .vsel_mask = TPS6286X_VOUT1_VO1_SET,
  90. .enable_reg = TPS6286X_CONTROL,
  91. .enable_mask = TPS6286X_CONTROL_SWEN,
  92. .ramp_delay = 1000,
  93. /* tDelay + tRamp, rounded up */
  94. .enable_time = 3000,
  95. };
  96. static const struct of_device_id tps6286x_dt_ids[] = {
  97. { .compatible = "ti,tps62864", },
  98. { .compatible = "ti,tps62866", },
  99. { .compatible = "ti,tps62868", },
  100. { .compatible = "ti,tps62869", },
  101. { }
  102. };
  103. MODULE_DEVICE_TABLE(of, tps6286x_dt_ids);
  104. static int tps6286x_i2c_probe(struct i2c_client *i2c)
  105. {
  106. struct device *dev = &i2c->dev;
  107. struct regulator_config config = {};
  108. struct regulator_dev *rdev;
  109. struct regmap *regmap;
  110. regmap = devm_regmap_init_i2c(i2c, &tps6286x_regmap_config);
  111. if (IS_ERR(regmap))
  112. return PTR_ERR(regmap);
  113. config.dev = &i2c->dev;
  114. config.of_node = dev->of_node;
  115. config.regmap = regmap;
  116. rdev = devm_regulator_register(&i2c->dev, &tps6286x_reg, &config);
  117. if (IS_ERR(rdev)) {
  118. dev_err(&i2c->dev, "Failed to register tps6286x regulator\n");
  119. return PTR_ERR(rdev);
  120. }
  121. return 0;
  122. }
  123. static const struct i2c_device_id tps6286x_i2c_id[] = {
  124. { "tps62864" },
  125. { "tps62866" },
  126. { "tps62868" },
  127. { "tps62869" },
  128. {}
  129. };
  130. MODULE_DEVICE_TABLE(i2c, tps6286x_i2c_id);
  131. static struct i2c_driver tps6286x_regulator_driver = {
  132. .driver = {
  133. .name = "tps6286x",
  134. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  135. .of_match_table = tps6286x_dt_ids,
  136. },
  137. .probe = tps6286x_i2c_probe,
  138. .id_table = tps6286x_i2c_id,
  139. };
  140. module_i2c_driver(tps6286x_regulator_driver);
  141. MODULE_DESCRIPTION("TI TPS6286x Power Regulator driver");
  142. MODULE_LICENSE("GPL v2");