fan53880.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/module.h>
  3. #include <linux/i2c.h>
  4. #include <linux/regmap.h>
  5. #include <linux/regulator/driver.h>
  6. enum fan53880_regulator_ids {
  7. FAN53880_LDO1,
  8. FAN53880_LDO2,
  9. FAN53880_LDO3,
  10. FAN53880_LDO4,
  11. FAN53880_BUCK,
  12. FAN53880_BOOST,
  13. };
  14. enum fan53880_registers {
  15. FAN53880_PRODUCT_ID = 0x00,
  16. FAN53880_SILICON_REV,
  17. FAN53880_BUCKVOUT,
  18. FAN53880_BOOSTVOUT,
  19. FAN53880_LDO1VOUT,
  20. FAN53880_LDO2VOUT,
  21. FAN53880_LDO3VOUT,
  22. FAN53880_LDO4VOUT,
  23. FAN53880_IOUT,
  24. FAN53880_ENABLE,
  25. FAN53880_ENABLE_BOOST,
  26. };
  27. #define FAN53880_ID 0x01
  28. static const struct regulator_ops fan53880_ops = {
  29. .list_voltage = regulator_list_voltage_linear_range,
  30. .map_voltage = regulator_map_voltage_linear_range,
  31. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  32. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  33. .enable = regulator_enable_regmap,
  34. .disable = regulator_disable_regmap,
  35. .is_enabled = regulator_is_enabled_regmap,
  36. };
  37. #define FAN53880_LDO(_num, _supply, _default) \
  38. [FAN53880_LDO ## _num] = { \
  39. .name = "LDO"#_num, \
  40. .of_match = of_match_ptr("LDO"#_num), \
  41. .regulators_node = of_match_ptr("regulators"), \
  42. .type = REGULATOR_VOLTAGE, \
  43. .owner = THIS_MODULE, \
  44. .linear_ranges = (struct linear_range[]) { \
  45. REGULATOR_LINEAR_RANGE(_default, 0x0, 0x0, 0), \
  46. REGULATOR_LINEAR_RANGE(800000, 0xf, 0x73, 25000), \
  47. }, \
  48. .n_linear_ranges = 2, \
  49. .n_voltages = 0x74, \
  50. .vsel_reg = FAN53880_LDO ## _num ## VOUT, \
  51. .vsel_mask = 0x7f, \
  52. .enable_reg = FAN53880_ENABLE, \
  53. .enable_mask = BIT(_num - 1), \
  54. .enable_time = 150, \
  55. .supply_name = _supply, \
  56. .ops = &fan53880_ops, \
  57. }
  58. static const struct regulator_desc fan53880_regulators[] = {
  59. FAN53880_LDO(1, "VIN12", 2800000),
  60. FAN53880_LDO(2, "VIN12", 2800000),
  61. FAN53880_LDO(3, "VIN3", 1800000),
  62. FAN53880_LDO(4, "VIN4", 1800000),
  63. [FAN53880_BUCK] = {
  64. .name = "BUCK",
  65. .of_match = of_match_ptr("BUCK"),
  66. .regulators_node = of_match_ptr("regulators"),
  67. .type = REGULATOR_VOLTAGE,
  68. .owner = THIS_MODULE,
  69. .linear_ranges = (struct linear_range[]) {
  70. REGULATOR_LINEAR_RANGE(1100000, 0x0, 0x0, 0),
  71. REGULATOR_LINEAR_RANGE(600000, 0x1f, 0xf7, 12500),
  72. },
  73. .n_linear_ranges = 2,
  74. .n_voltages = 0xf8,
  75. .vsel_reg = FAN53880_BUCKVOUT,
  76. .vsel_mask = 0xff,
  77. .enable_reg = FAN53880_ENABLE,
  78. .enable_mask = 0x10,
  79. .enable_time = 480,
  80. .supply_name = "PVIN",
  81. .ops = &fan53880_ops,
  82. },
  83. [FAN53880_BOOST] = {
  84. .name = "BOOST",
  85. .of_match = of_match_ptr("BOOST"),
  86. .regulators_node = of_match_ptr("regulators"),
  87. .type = REGULATOR_VOLTAGE,
  88. .owner = THIS_MODULE,
  89. .linear_ranges = (struct linear_range[]) {
  90. REGULATOR_LINEAR_RANGE(5000000, 0x0, 0x0, 0),
  91. REGULATOR_LINEAR_RANGE(3000000, 0x4, 0x70, 25000),
  92. },
  93. .n_linear_ranges = 2,
  94. .n_voltages = 0x71,
  95. .vsel_reg = FAN53880_BOOSTVOUT,
  96. .vsel_mask = 0x7f,
  97. .enable_reg = FAN53880_ENABLE_BOOST,
  98. .enable_mask = 0xff,
  99. .enable_time = 580,
  100. .supply_name = "PVIN",
  101. .ops = &fan53880_ops,
  102. },
  103. };
  104. static const struct regmap_config fan53880_regmap = {
  105. .reg_bits = 8,
  106. .val_bits = 8,
  107. .max_register = FAN53880_ENABLE_BOOST,
  108. };
  109. static int fan53880_i2c_probe(struct i2c_client *i2c,
  110. const struct i2c_device_id *id)
  111. {
  112. struct regulator_config config = { };
  113. struct regulator_dev *rdev;
  114. struct regmap *regmap;
  115. int i, ret;
  116. unsigned int data;
  117. regmap = devm_regmap_init_i2c(i2c, &fan53880_regmap);
  118. if (IS_ERR(regmap)) {
  119. ret = PTR_ERR(regmap);
  120. dev_err(&i2c->dev, "Failed to create regmap: %d\n", ret);
  121. return ret;
  122. }
  123. ret = regmap_read(regmap, FAN53880_PRODUCT_ID, &data);
  124. if (ret < 0) {
  125. dev_err(&i2c->dev, "Failed to read PRODUCT_ID: %d\n", ret);
  126. return ret;
  127. }
  128. if (data != FAN53880_ID) {
  129. dev_err(&i2c->dev, "Unsupported device id: 0x%x.\n", data);
  130. return -ENODEV;
  131. }
  132. config.dev = &i2c->dev;
  133. config.init_data = NULL;
  134. for (i = 0; i < ARRAY_SIZE(fan53880_regulators); i++) {
  135. rdev = devm_regulator_register(&i2c->dev,
  136. &fan53880_regulators[i],
  137. &config);
  138. if (IS_ERR(rdev)) {
  139. ret = PTR_ERR(rdev);
  140. dev_err(&i2c->dev, "Failed to register %s: %d\n",
  141. fan53880_regulators[i].name, ret);
  142. return ret;
  143. }
  144. }
  145. return 0;
  146. }
  147. #ifdef CONFIG_OF
  148. static const struct of_device_id fan53880_dt_ids[] = {
  149. { .compatible = "onnn,fan53880", },
  150. {}
  151. };
  152. MODULE_DEVICE_TABLE(of, fan53880_dt_ids);
  153. #endif
  154. static const struct i2c_device_id fan53880_i2c_id[] = {
  155. { "fan53880", },
  156. {}
  157. };
  158. MODULE_DEVICE_TABLE(i2c, fan53880_i2c_id);
  159. static struct i2c_driver fan53880_regulator_driver = {
  160. .driver = {
  161. .name = "fan53880",
  162. .of_match_table = of_match_ptr(fan53880_dt_ids),
  163. },
  164. .probe = fan53880_i2c_probe,
  165. .id_table = fan53880_i2c_id,
  166. };
  167. module_i2c_driver(fan53880_regulator_driver);
  168. MODULE_DESCRIPTION("FAN53880 PMIC voltage regulator driver");
  169. MODULE_AUTHOR("Christoph Fritz <chf.fritz@googlemail.com>");
  170. MODULE_LICENSE("GPL");