aw37503-regulator.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // AWINIC AW37503 Regulator Driver
  4. //
  5. // Copyright (C) 2023 awinic. All Rights Reserved
  6. //
  7. // Author: <like@awinic.com>
  8. #include <linux/err.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/i2c.h>
  11. #include <linux/module.h>
  12. #include <linux/regmap.h>
  13. #include <linux/regulator/driver.h>
  14. #include <linux/regulator/machine.h>
  15. #define AW37503_REG_VPOS 0x00
  16. #define AW37503_REG_VNEG 0x01
  17. #define AW37503_REG_APPS 0x03
  18. #define AW37503_REG_CONTROL 0x04
  19. #define AW37503_REG_WPRTEN 0x21
  20. #define AW37503_VOUT_MASK 0x1F
  21. #define AW37503_VOUT_N_VOLTAGE 0x15
  22. #define AW37503_VOUT_VMIN 4000000
  23. #define AW37503_VOUT_VMAX 6000000
  24. #define AW37503_VOUT_STEP 100000
  25. #define AW37503_REG_APPS_DIS_VPOS BIT(1)
  26. #define AW37503_REG_APPS_DIS_VNEG BIT(0)
  27. #define AW37503_REGULATOR_ID_VPOS 0
  28. #define AW37503_REGULATOR_ID_VNEG 1
  29. #define AW37503_MAX_REGULATORS 2
  30. struct aw37503_reg_pdata {
  31. struct gpio_desc *en_gpiod;
  32. int ena_gpio_state;
  33. };
  34. struct aw37503_regulator {
  35. struct device *dev;
  36. struct aw37503_reg_pdata reg_pdata[AW37503_MAX_REGULATORS];
  37. };
  38. static int aw37503_regulator_enable(struct regulator_dev *rdev)
  39. {
  40. struct aw37503_regulator *chip = rdev_get_drvdata(rdev);
  41. int id = rdev_get_id(rdev);
  42. struct aw37503_reg_pdata *rpdata = &chip->reg_pdata[id];
  43. int ret;
  44. if (!IS_ERR(rpdata->en_gpiod)) {
  45. gpiod_set_value_cansleep(rpdata->en_gpiod, 1);
  46. rpdata->ena_gpio_state = 1;
  47. }
  48. /* Hardware automatically enable discharge bit in enable */
  49. if (rdev->constraints->active_discharge ==
  50. REGULATOR_ACTIVE_DISCHARGE_DISABLE) {
  51. ret = regulator_set_active_discharge_regmap(rdev, false);
  52. if (ret < 0) {
  53. dev_err(chip->dev, "Failed to disable active discharge: %d\n",
  54. ret);
  55. return ret;
  56. }
  57. }
  58. return 0;
  59. }
  60. static int aw37503_regulator_disable(struct regulator_dev *rdev)
  61. {
  62. struct aw37503_regulator *chip = rdev_get_drvdata(rdev);
  63. int id = rdev_get_id(rdev);
  64. struct aw37503_reg_pdata *rpdata = &chip->reg_pdata[id];
  65. if (!IS_ERR(rpdata->en_gpiod)) {
  66. gpiod_set_value_cansleep(rpdata->en_gpiod, 0);
  67. rpdata->ena_gpio_state = 0;
  68. }
  69. return 0;
  70. }
  71. static int aw37503_regulator_is_enabled(struct regulator_dev *rdev)
  72. {
  73. struct aw37503_regulator *chip = rdev_get_drvdata(rdev);
  74. int id = rdev_get_id(rdev);
  75. struct aw37503_reg_pdata *rpdata = &chip->reg_pdata[id];
  76. if (!IS_ERR(rpdata->en_gpiod))
  77. return rpdata->ena_gpio_state;
  78. return 1;
  79. }
  80. static const struct regulator_ops aw37503_regulator_ops = {
  81. .enable = aw37503_regulator_enable,
  82. .disable = aw37503_regulator_disable,
  83. .is_enabled = aw37503_regulator_is_enabled,
  84. .list_voltage = regulator_list_voltage_linear,
  85. .map_voltage = regulator_map_voltage_linear,
  86. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  87. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  88. .set_active_discharge = regulator_set_active_discharge_regmap,
  89. };
  90. static int aw37503_of_parse_cb(struct device_node *np,
  91. const struct regulator_desc *desc,
  92. struct regulator_config *config)
  93. {
  94. struct aw37503_regulator *chip = config->driver_data;
  95. struct aw37503_reg_pdata *rpdata = &chip->reg_pdata[desc->id];
  96. int ret;
  97. rpdata->en_gpiod = devm_fwnode_gpiod_get(chip->dev, of_fwnode_handle(np),
  98. "enable", GPIOD_OUT_LOW,
  99. "enable");
  100. if (IS_ERR(rpdata->en_gpiod)) {
  101. ret = PTR_ERR(rpdata->en_gpiod);
  102. /* Ignore the error other than probe defer */
  103. if (ret == -EPROBE_DEFER)
  104. return ret;
  105. return 0;
  106. }
  107. return 0;
  108. }
  109. #define AW37503_REGULATOR_DESC(_id, _name) \
  110. [AW37503_REGULATOR_ID_##_id] = { \
  111. .name = "aw37503-"#_name, \
  112. .supply_name = "vin", \
  113. .id = AW37503_REGULATOR_ID_##_id, \
  114. .of_match = of_match_ptr(#_name), \
  115. .of_parse_cb = aw37503_of_parse_cb, \
  116. .ops = &aw37503_regulator_ops, \
  117. .n_voltages = AW37503_VOUT_N_VOLTAGE, \
  118. .min_uV = AW37503_VOUT_VMIN, \
  119. .uV_step = AW37503_VOUT_STEP, \
  120. .enable_time = 500, \
  121. .vsel_mask = AW37503_VOUT_MASK, \
  122. .vsel_reg = AW37503_REG_##_id, \
  123. .active_discharge_off = 0, \
  124. .active_discharge_on = AW37503_REG_APPS_DIS_##_id, \
  125. .active_discharge_mask = AW37503_REG_APPS_DIS_##_id, \
  126. .active_discharge_reg = AW37503_REG_APPS, \
  127. .type = REGULATOR_VOLTAGE, \
  128. .owner = THIS_MODULE, \
  129. }
  130. static const struct regulator_desc aw_regs_desc[AW37503_MAX_REGULATORS] = {
  131. AW37503_REGULATOR_DESC(VPOS, outp),
  132. AW37503_REGULATOR_DESC(VNEG, outn),
  133. };
  134. static const struct regmap_range aw37503_no_reg_ranges[] = {
  135. regmap_reg_range(AW37503_REG_CONTROL + 1,
  136. AW37503_REG_WPRTEN - 1),
  137. };
  138. static const struct regmap_access_table aw37503_no_reg_table = {
  139. .no_ranges = aw37503_no_reg_ranges,
  140. .n_no_ranges = ARRAY_SIZE(aw37503_no_reg_ranges),
  141. };
  142. static const struct regmap_config aw37503_regmap_config = {
  143. .reg_bits = 8,
  144. .val_bits = 8,
  145. .max_register = AW37503_REG_WPRTEN,
  146. .rd_table = &aw37503_no_reg_table,
  147. .wr_table = &aw37503_no_reg_table,
  148. };
  149. static int aw37503_probe(struct i2c_client *client)
  150. {
  151. struct device *dev = &client->dev;
  152. struct aw37503_regulator *chip;
  153. struct regulator_dev *rdev;
  154. struct regmap *regmap;
  155. struct regulator_config config = { };
  156. int id;
  157. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  158. if (!chip)
  159. return -ENOMEM;
  160. regmap = devm_regmap_init_i2c(client, &aw37503_regmap_config);
  161. if (IS_ERR(regmap))
  162. return dev_err_probe(dev, PTR_ERR(regmap),
  163. "Failed to init regmap\n");
  164. i2c_set_clientdata(client, chip);
  165. chip->dev = dev;
  166. config.regmap = regmap;
  167. config.dev = dev;
  168. config.driver_data = chip;
  169. for (id = 0; id < AW37503_MAX_REGULATORS; ++id) {
  170. rdev = devm_regulator_register(dev, &aw_regs_desc[id],
  171. &config);
  172. if (IS_ERR(rdev))
  173. return dev_err_probe(dev, PTR_ERR(rdev),
  174. "Failed to register regulator %s\n",
  175. aw_regs_desc[id].name);
  176. }
  177. return 0;
  178. }
  179. static const struct i2c_device_id aw37503_id[] = {
  180. {.name = "aw37503",},
  181. {},
  182. };
  183. MODULE_DEVICE_TABLE(i2c, aw37503_id);
  184. static const struct of_device_id aw37503_of_match[] = {
  185. {.compatible = "awinic,aw37503",},
  186. { /* Sentinel */ },
  187. };
  188. MODULE_DEVICE_TABLE(of, aw37503_of_match);
  189. static struct i2c_driver aw37503_i2c_driver = {
  190. .driver = {
  191. .name = "aw37503",
  192. .of_match_table = aw37503_of_match,
  193. },
  194. .probe = aw37503_probe,
  195. .id_table = aw37503_id,
  196. };
  197. module_i2c_driver(aw37503_i2c_driver);
  198. MODULE_DESCRIPTION("aw37503 regulator driver");
  199. MODULE_AUTHOR("Alec Li <like@awinic.com>");
  200. MODULE_LICENSE("GPL");