ap72200-regulator.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved. */
  3. #define pr_fmt(fmt) "ap72200-reg: %s: " fmt, __func__
  4. #include <linux/i2c.h>
  5. #include <linux/module.h>
  6. #include <linux/of_platform.h>
  7. #include <linux/regmap.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/regulator/driver.h>
  10. #include <linux/regulator/debug-regulator.h>
  11. #include <linux/regulator/machine.h>
  12. #include <linux/regulator/of_regulator.h>
  13. #include <linux/regulator/proxy-consumer.h>
  14. #define AP72200_CFG_REG2_ADDR 0x3
  15. #define AP72200_VSEL_REG_ADDR 0x4
  16. #define AP72200_BB_EN_BIT BIT(6)
  17. #define AP72200_MIN_UV 2600000
  18. #define AP72200_MAX_UV 5140000
  19. #define AP72200_STEP_UV 20000
  20. struct ap72200_vreg {
  21. struct device *dev;
  22. struct device_node *of_node;
  23. struct regulator_desc rdesc;
  24. struct regulator_dev *rdev;
  25. struct regmap *regmap;
  26. struct gpio_desc *ena_gpiod;
  27. bool is_enabled;
  28. };
  29. static const struct regmap_config ap27700_regmap_config = {
  30. .reg_bits = 8,
  31. .val_bits = 8,
  32. .max_register = 0x4,
  33. };
  34. static int ap72200_vreg_enable(struct regulator_dev *rdev)
  35. {
  36. struct ap72200_vreg *vreg = rdev_get_drvdata(rdev);
  37. int rc, val;
  38. val = DIV_ROUND_UP(vreg->rdesc.fixed_uV - AP72200_MIN_UV, AP72200_STEP_UV);
  39. /* Set the voltage */
  40. rc = regmap_write(vreg->regmap, AP72200_VSEL_REG_ADDR,
  41. val);
  42. if (rc) {
  43. dev_err(vreg->dev, "Failed to set voltage rc: %d\n", rc);
  44. return rc;
  45. }
  46. /* Enable VOUT */
  47. rc = regmap_update_bits(vreg->regmap, AP72200_CFG_REG2_ADDR,
  48. AP72200_BB_EN_BIT, AP72200_BB_EN_BIT);
  49. if (rc) {
  50. dev_err(vreg->dev, "Failed to enable VOUT rc :%d\n", rc);
  51. return rc;
  52. }
  53. vreg->is_enabled = true;
  54. return rc;
  55. }
  56. static int ap72200_vreg_disable(struct regulator_dev *rdev)
  57. {
  58. struct ap72200_vreg *vreg = rdev_get_drvdata(rdev);
  59. int rc;
  60. /* Disable VOUT */
  61. rc = regmap_update_bits(vreg->regmap, AP72200_CFG_REG2_ADDR,
  62. AP72200_BB_EN_BIT, 0);
  63. if (rc) {
  64. dev_err(vreg->dev, "Failed to disable VOUT rc :%d\n", rc);
  65. return rc;
  66. }
  67. vreg->is_enabled = false;
  68. return rc;
  69. }
  70. static int ap72200_is_enabled(struct regulator_dev *rdev)
  71. {
  72. struct ap72200_vreg *vreg = rdev_get_drvdata(rdev);
  73. return vreg->is_enabled;
  74. }
  75. static const struct regulator_ops ap72200_ops = {
  76. .enable = ap72200_vreg_enable,
  77. .disable = ap72200_vreg_disable,
  78. .is_enabled = ap72200_is_enabled,
  79. };
  80. static int ap72200_probe(struct i2c_client *client,
  81. const struct i2c_device_id *id)
  82. {
  83. struct ap72200_vreg *vreg;
  84. struct regulator_config reg_config = {};
  85. struct regulator_init_data *init_data;
  86. int ret;
  87. vreg = devm_kzalloc(&client->dev, sizeof(*vreg), GFP_KERNEL);
  88. if (!vreg)
  89. return -ENOMEM;
  90. vreg->regmap = devm_regmap_init_i2c(client, &ap27700_regmap_config);
  91. if (IS_ERR(vreg->regmap)) {
  92. ret = PTR_ERR(vreg->regmap);
  93. dev_err(&client->dev, "regmap init failed, err %d\n", ret);
  94. return ret;
  95. }
  96. vreg->dev = &client->dev;
  97. vreg->of_node = client->dev.of_node;
  98. vreg->rdesc.ops = &ap72200_ops;
  99. vreg->rdesc.owner = THIS_MODULE;
  100. vreg->rdesc.type = REGULATOR_VOLTAGE;
  101. init_data = of_get_regulator_init_data(vreg->dev,
  102. vreg->of_node, &vreg->rdesc);
  103. if (init_data == NULL)
  104. return -ENODATA;
  105. if (init_data->constraints.name == NULL) {
  106. dev_err(&client->dev, "%s: regulator name not specified\n", __func__);
  107. return -EINVAL;
  108. }
  109. if (init_data->constraints.min_uV != init_data->constraints.max_uV) {
  110. dev_err(&client->dev,
  111. "Fixed regulator specified with variable voltages\n");
  112. return -EINVAL;
  113. }
  114. init_data->constraints.min_uV = max(init_data->constraints.min_uV, AP72200_MIN_UV);
  115. init_data->constraints.min_uV = min(init_data->constraints.min_uV, AP72200_MAX_UV);
  116. vreg->rdesc.n_voltages = 1;
  117. vreg->rdesc.fixed_uV = init_data->constraints.min_uV;
  118. vreg->rdesc.name = init_data->constraints.name;
  119. reg_config.dev = vreg->dev;
  120. reg_config.init_data = init_data;
  121. reg_config.of_node = vreg->of_node;
  122. reg_config.driver_data = vreg;
  123. vreg->ena_gpiod = gpiod_get(vreg->dev, "enable", GPIOD_OUT_HIGH);
  124. if (IS_ERR(vreg->ena_gpiod)) {
  125. dev_err(&client->dev, "Failed to get enable GPIO\n");
  126. return PTR_ERR(vreg->ena_gpiod);
  127. }
  128. /* Keep the EN pin of this regulator always high */
  129. gpiod_set_value_cansleep(vreg->ena_gpiod, 1);
  130. vreg->rdev = devm_regulator_register(vreg->dev, &vreg->rdesc, &reg_config);
  131. if (IS_ERR(vreg->rdev)) {
  132. ret = PTR_ERR(vreg->rdev);
  133. dev_err(vreg->dev, "Failed to register regulator: %d\n", ret);
  134. return ret;
  135. }
  136. ret = devm_regulator_debug_register(vreg->dev, vreg->rdev);
  137. if (ret)
  138. dev_err(vreg->dev, "Failed to register debug regulator, rc=%d\n", ret);
  139. return 0;
  140. }
  141. static const struct of_device_id ap72200_match_tbl[] = {
  142. { .compatible = "diodes,ap72200", },
  143. { }
  144. };
  145. MODULE_DEVICE_TABLE(of, ap72200_match_tbl);
  146. static const struct i2c_device_id ap72200_id[] = {
  147. { "ap72200", 0 },
  148. { }
  149. };
  150. MODULE_DEVICE_TABLE(i2c, ap72200_id);
  151. static struct i2c_driver ap72200_i2c_driver = {
  152. .driver = {
  153. .name = "ap72200",
  154. .of_match_table = ap72200_match_tbl,
  155. },
  156. .probe = ap72200_probe,
  157. .id_table = ap72200_id,
  158. };
  159. module_i2c_driver(ap72200_i2c_driver);
  160. MODULE_LICENSE("GPL");
  161. MODULE_ALIAS("i2c:ap72200-regulator");