tps6286x-regulator.c 3.9 KB

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