tps6287x-regulator.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 bool tps6287x_volatile_reg(struct device *dev, unsigned int reg)
  28. {
  29. return reg == TPS6287X_STATUS;
  30. }
  31. static const struct regmap_config tps6287x_regmap_config = {
  32. .reg_bits = 8,
  33. .val_bits = 8,
  34. .max_register = TPS6287X_STATUS,
  35. .cache_type = REGCACHE_MAPLE,
  36. .volatile_reg = tps6287x_volatile_reg,
  37. };
  38. static const struct linear_range tps6287x_voltage_ranges[] = {
  39. LINEAR_RANGE(400000, 0, 0xFF, 1250),
  40. LINEAR_RANGE(400000, 0, 0xFF, 2500),
  41. LINEAR_RANGE(400000, 0, 0xFF, 5000),
  42. LINEAR_RANGE(800000, 0, 0xFF, 10000),
  43. };
  44. static const unsigned int tps6287x_voltage_range_sel[] = {
  45. 0x0, 0x1, 0x2, 0x3
  46. };
  47. static const unsigned int tps6287x_voltage_range_prefix[] = {
  48. 0x000, 0x100, 0x200, 0x300
  49. };
  50. static const unsigned int tps6287x_ramp_table[] = {
  51. 10000, 5000, 1250, 500
  52. };
  53. struct tps6287x_reg_data {
  54. int range;
  55. };
  56. static int tps6287x_best_range(struct regulator_config *config, const struct regulator_desc *desc)
  57. {
  58. const struct linear_range *r;
  59. int i;
  60. if (!config->init_data->constraints.apply_uV)
  61. return -1;
  62. for (i = 0; i < desc->n_linear_ranges; i++) {
  63. r = &desc->linear_ranges[i];
  64. if (r->min <= config->init_data->constraints.min_uV &&
  65. config->init_data->constraints.max_uV <= linear_range_get_max_value(r))
  66. return i;
  67. }
  68. return -1;
  69. }
  70. static int tps6287x_set_mode(struct regulator_dev *rdev, unsigned int mode)
  71. {
  72. unsigned int val;
  73. switch (mode) {
  74. case REGULATOR_MODE_NORMAL:
  75. val = 0;
  76. break;
  77. case REGULATOR_MODE_FAST:
  78. val = TPS6287X_CTRL1_FPWMEN;
  79. break;
  80. default:
  81. return -EINVAL;
  82. }
  83. return regmap_update_bits(rdev->regmap, TPS6287X_CTRL1,
  84. TPS6287X_CTRL1_FPWMEN, val);
  85. }
  86. static unsigned int tps6287x_get_mode(struct regulator_dev *rdev)
  87. {
  88. unsigned int val;
  89. int ret;
  90. ret = regmap_read(rdev->regmap, TPS6287X_CTRL1, &val);
  91. if (ret < 0)
  92. return 0;
  93. return (val & TPS6287X_CTRL1_FPWMEN) ? REGULATOR_MODE_FAST :
  94. REGULATOR_MODE_NORMAL;
  95. }
  96. static unsigned int tps6287x_of_map_mode(unsigned int mode)
  97. {
  98. switch (mode) {
  99. case REGULATOR_MODE_NORMAL:
  100. case REGULATOR_MODE_FAST:
  101. return mode;
  102. default:
  103. return REGULATOR_MODE_INVALID;
  104. }
  105. }
  106. static int tps6287x_map_voltage(struct regulator_dev *rdev, int min_uV, int max_uV)
  107. {
  108. struct tps6287x_reg_data *data = (struct tps6287x_reg_data *)rdev->reg_data;
  109. struct linear_range selected_range;
  110. int selector, voltage;
  111. if (!data || data->range == -1)
  112. return regulator_map_voltage_pickable_linear_range(rdev, min_uV, max_uV);
  113. selected_range = rdev->desc->linear_ranges[data->range];
  114. selector = DIV_ROUND_UP(min_uV - selected_range.min, selected_range.step);
  115. if (selector < selected_range.min_sel || selector > selected_range.max_sel)
  116. return -EINVAL;
  117. selector |= tps6287x_voltage_range_prefix[data->range];
  118. voltage = rdev->desc->ops->list_voltage(rdev, selector);
  119. if (voltage < min_uV || voltage > max_uV)
  120. return -EINVAL;
  121. return selector;
  122. }
  123. static const struct regulator_ops tps6287x_regulator_ops = {
  124. .enable = regulator_enable_regmap,
  125. .disable = regulator_disable_regmap,
  126. .set_mode = tps6287x_set_mode,
  127. .get_mode = tps6287x_get_mode,
  128. .is_enabled = regulator_is_enabled_regmap,
  129. .get_voltage_sel = regulator_get_voltage_sel_pickable_regmap,
  130. .set_voltage_sel = regulator_set_voltage_sel_pickable_regmap,
  131. .list_voltage = regulator_list_voltage_pickable_linear_range,
  132. .map_voltage = tps6287x_map_voltage,
  133. .set_ramp_delay = regulator_set_ramp_delay_regmap,
  134. };
  135. static const struct regulator_desc tps6287x_reg = {
  136. .name = "tps6287x",
  137. .owner = THIS_MODULE,
  138. .ops = &tps6287x_regulator_ops,
  139. .of_map_mode = tps6287x_of_map_mode,
  140. .type = REGULATOR_VOLTAGE,
  141. .enable_reg = TPS6287X_CTRL1,
  142. .enable_mask = TPS6287X_CTRL1_SWEN,
  143. .vsel_reg = TPS6287X_VSET,
  144. .vsel_mask = 0xFF,
  145. .vsel_range_reg = TPS6287X_CTRL2,
  146. .vsel_range_mask = TPS6287X_CTRL2_VRANGE,
  147. .range_applied_by_vsel = true,
  148. .ramp_reg = TPS6287X_CTRL1,
  149. .ramp_mask = TPS6287X_CTRL1_VRAMP,
  150. .ramp_delay_table = tps6287x_ramp_table,
  151. .n_ramp_values = ARRAY_SIZE(tps6287x_ramp_table),
  152. .n_voltages = 256 * ARRAY_SIZE(tps6287x_voltage_ranges),
  153. .linear_ranges = tps6287x_voltage_ranges,
  154. .n_linear_ranges = ARRAY_SIZE(tps6287x_voltage_ranges),
  155. .linear_range_selectors_bitfield = tps6287x_voltage_range_sel,
  156. };
  157. static int tps6287x_i2c_probe(struct i2c_client *i2c)
  158. {
  159. struct device *dev = &i2c->dev;
  160. struct regulator_config config = {};
  161. struct tps6287x_reg_data *reg_data;
  162. struct regulator_dev *rdev;
  163. reg_data = devm_kzalloc(dev, sizeof(struct tps6287x_reg_data), GFP_KERNEL);
  164. if (!reg_data)
  165. return -ENOMEM;
  166. config.regmap = devm_regmap_init_i2c(i2c, &tps6287x_regmap_config);
  167. if (IS_ERR(config.regmap)) {
  168. dev_err(dev, "Failed to init i2c\n");
  169. return PTR_ERR(config.regmap);
  170. }
  171. config.dev = dev;
  172. config.of_node = dev->of_node;
  173. config.init_data = of_get_regulator_init_data(dev, dev->of_node,
  174. &tps6287x_reg);
  175. reg_data->range = tps6287x_best_range(&config, &tps6287x_reg);
  176. rdev = devm_regulator_register(dev, &tps6287x_reg, &config);
  177. if (IS_ERR(rdev)) {
  178. dev_err(dev, "Failed to register regulator\n");
  179. return PTR_ERR(rdev);
  180. }
  181. rdev->reg_data = (void *)reg_data;
  182. dev_dbg(dev, "Probed regulator\n");
  183. return 0;
  184. }
  185. static const struct of_device_id tps6287x_dt_ids[] = {
  186. { .compatible = "ti,tps62870", },
  187. { .compatible = "ti,tps62871", },
  188. { .compatible = "ti,tps62872", },
  189. { .compatible = "ti,tps62873", },
  190. { }
  191. };
  192. MODULE_DEVICE_TABLE(of, tps6287x_dt_ids);
  193. static const struct i2c_device_id tps6287x_i2c_id[] = {
  194. { "tps62870" },
  195. { "tps62871" },
  196. { "tps62872" },
  197. { "tps62873" },
  198. {}
  199. };
  200. MODULE_DEVICE_TABLE(i2c, tps6287x_i2c_id);
  201. static struct i2c_driver tps6287x_regulator_driver = {
  202. .driver = {
  203. .name = "tps6287x",
  204. .of_match_table = tps6287x_dt_ids,
  205. },
  206. .probe = tps6287x_i2c_probe,
  207. .id_table = tps6287x_i2c_id,
  208. };
  209. module_i2c_driver(tps6287x_regulator_driver);
  210. MODULE_AUTHOR("Mårten Lindahl <marten.lindahl@axis.com>");
  211. MODULE_DESCRIPTION("Regulator driver for TI TPS6287X PMIC");
  212. MODULE_LICENSE("GPL");