sy8106a-regulator.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // sy8106a-regulator.c - Regulator device driver for SY8106A
  4. //
  5. // Copyright (C) 2016 Ondřej Jirman <megous@megous.com>
  6. // Copyright (c) 2017-2018 Icenowy Zheng <icenowy@aosc.io>
  7. #include <linux/err.h>
  8. #include <linux/i2c.h>
  9. #include <linux/module.h>
  10. #include <linux/regmap.h>
  11. #include <linux/regulator/driver.h>
  12. #include <linux/regulator/of_regulator.h>
  13. #define SY8106A_REG_VOUT1_SEL 0x01
  14. #define SY8106A_REG_VOUT_COM 0x02
  15. #define SY8106A_REG_VOUT1_SEL_MASK 0x7f
  16. #define SY8106A_DISABLE_REG BIT(0)
  17. /*
  18. * The I2C controlled voltage will only work when this bit is set; otherwise
  19. * it will behave like a fixed regulator.
  20. */
  21. #define SY8106A_GO_BIT BIT(7)
  22. static const struct regmap_config sy8106a_regmap_config = {
  23. .reg_bits = 8,
  24. .val_bits = 8,
  25. };
  26. static const struct regulator_ops sy8106a_ops = {
  27. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  28. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  29. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  30. .list_voltage = regulator_list_voltage_linear,
  31. /* Enabling/disabling the regulator is not yet implemented */
  32. };
  33. /* Default limits measured in millivolts */
  34. #define SY8106A_MIN_MV 680
  35. #define SY8106A_MAX_MV 1950
  36. #define SY8106A_STEP_MV 10
  37. static const struct regulator_desc sy8106a_reg = {
  38. .name = "SY8106A",
  39. .id = 0,
  40. .ops = &sy8106a_ops,
  41. .type = REGULATOR_VOLTAGE,
  42. .n_voltages = ((SY8106A_MAX_MV - SY8106A_MIN_MV) / SY8106A_STEP_MV) + 1,
  43. .min_uV = (SY8106A_MIN_MV * 1000),
  44. .uV_step = (SY8106A_STEP_MV * 1000),
  45. .vsel_reg = SY8106A_REG_VOUT1_SEL,
  46. .vsel_mask = SY8106A_REG_VOUT1_SEL_MASK,
  47. /*
  48. * This ramp_delay is a conservative default value which works on
  49. * H3/H5 boards VDD-CPUX situations.
  50. */
  51. .ramp_delay = 200,
  52. .owner = THIS_MODULE,
  53. };
  54. /*
  55. * I2C driver interface functions
  56. */
  57. static int sy8106a_i2c_probe(struct i2c_client *i2c,
  58. const struct i2c_device_id *id)
  59. {
  60. struct device *dev = &i2c->dev;
  61. struct regulator_dev *rdev;
  62. struct regulator_config config = { };
  63. struct regmap *regmap;
  64. unsigned int reg, vsel;
  65. u32 fixed_voltage;
  66. int error;
  67. error = of_property_read_u32(dev->of_node, "silergy,fixed-microvolt",
  68. &fixed_voltage);
  69. if (error)
  70. return error;
  71. if (fixed_voltage < SY8106A_MIN_MV * 1000 ||
  72. fixed_voltage > SY8106A_MAX_MV * 1000)
  73. return -EINVAL;
  74. regmap = devm_regmap_init_i2c(i2c, &sy8106a_regmap_config);
  75. if (IS_ERR(regmap)) {
  76. error = PTR_ERR(regmap);
  77. dev_err(dev, "Failed to allocate register map: %d\n", error);
  78. return error;
  79. }
  80. config.dev = &i2c->dev;
  81. config.regmap = regmap;
  82. config.of_node = dev->of_node;
  83. config.init_data = of_get_regulator_init_data(dev, dev->of_node,
  84. &sy8106a_reg);
  85. if (!config.init_data)
  86. return -ENOMEM;
  87. /* Ensure GO_BIT is enabled when probing */
  88. error = regmap_read(regmap, SY8106A_REG_VOUT1_SEL, &reg);
  89. if (error)
  90. return error;
  91. if (!(reg & SY8106A_GO_BIT)) {
  92. vsel = (fixed_voltage / 1000 - SY8106A_MIN_MV) /
  93. SY8106A_STEP_MV;
  94. error = regmap_write(regmap, SY8106A_REG_VOUT1_SEL,
  95. vsel | SY8106A_GO_BIT);
  96. if (error)
  97. return error;
  98. }
  99. /* Probe regulator */
  100. rdev = devm_regulator_register(&i2c->dev, &sy8106a_reg, &config);
  101. if (IS_ERR(rdev)) {
  102. error = PTR_ERR(rdev);
  103. dev_err(&i2c->dev, "Failed to register SY8106A regulator: %d\n", error);
  104. return error;
  105. }
  106. return 0;
  107. }
  108. static const struct of_device_id sy8106a_i2c_of_match[] = {
  109. { .compatible = "silergy,sy8106a" },
  110. { },
  111. };
  112. MODULE_DEVICE_TABLE(of, sy8106a_i2c_of_match);
  113. static const struct i2c_device_id sy8106a_i2c_id[] = {
  114. { "sy8106a", 0 },
  115. { },
  116. };
  117. MODULE_DEVICE_TABLE(i2c, sy8106a_i2c_id);
  118. static struct i2c_driver sy8106a_regulator_driver = {
  119. .driver = {
  120. .name = "sy8106a",
  121. .of_match_table = of_match_ptr(sy8106a_i2c_of_match),
  122. },
  123. .probe = sy8106a_i2c_probe,
  124. .id_table = sy8106a_i2c_id,
  125. };
  126. module_i2c_driver(sy8106a_regulator_driver);
  127. MODULE_AUTHOR("Ondřej Jirman <megous@megous.com>");
  128. MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
  129. MODULE_DESCRIPTION("Regulator device driver for Silergy SY8106A");
  130. MODULE_LICENSE("GPL");