sm5703-regulator.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/mfd/sm5703.h>
  3. #include <linux/module.h>
  4. #include <linux/mod_devicetable.h>
  5. #include <linux/platform_device.h>
  6. #include <linux/regmap.h>
  7. #include <linux/regulator/driver.h>
  8. #include <linux/regulator/of_regulator.h>
  9. enum sm5703_regulators {
  10. SM5703_BUCK,
  11. SM5703_LDO1,
  12. SM5703_LDO2,
  13. SM5703_LDO3,
  14. SM5703_USBLDO1,
  15. SM5703_USBLDO2,
  16. SM5703_VBUS,
  17. SM5703_MAX_REGULATORS,
  18. };
  19. static const int sm5703_ldo_voltagemap[] = {
  20. 1500000, 1800000, 2600000, 2800000, 3000000, 3300000,
  21. };
  22. static const int sm5703_buck_voltagemap[] = {
  23. 1000000, 1000000, 1000000, 1000000,
  24. 1000000, 1000000, 1000000, 1000000,
  25. 1000000, 1000000, 1000000, 1100000,
  26. 1200000, 1300000, 1400000, 1500000,
  27. 1600000, 1700000, 1800000, 1900000,
  28. 2000000, 2100000, 2200000, 2300000,
  29. 2400000, 2500000, 2600000, 2700000,
  30. 2800000, 2900000, 3000000, 3000000,
  31. };
  32. #define SM5703USBLDO(_name, _id) \
  33. [SM5703_USBLDO ## _id] = { \
  34. .name = _name, \
  35. .of_match = _name, \
  36. .regulators_node = "regulators", \
  37. .type = REGULATOR_VOLTAGE, \
  38. .id = SM5703_USBLDO ## _id, \
  39. .ops = &sm5703_regulator_ops_fixed, \
  40. .fixed_uV = SM5703_USBLDO_MICROVOLT, \
  41. .enable_reg = SM5703_REG_USBLDO12, \
  42. .enable_mask = SM5703_REG_EN_USBLDO ##_id, \
  43. .owner = THIS_MODULE, \
  44. }
  45. #define SM5703VBUS(_name) \
  46. [SM5703_VBUS] = { \
  47. .name = _name, \
  48. .of_match = _name, \
  49. .regulators_node = "regulators", \
  50. .type = REGULATOR_VOLTAGE, \
  51. .id = SM5703_VBUS, \
  52. .ops = &sm5703_regulator_ops_fixed, \
  53. .fixed_uV = SM5703_VBUS_MICROVOLT, \
  54. .enable_reg = SM5703_REG_CNTL, \
  55. .enable_mask = SM5703_OPERATION_MODE_MASK, \
  56. .enable_val = SM5703_OPERATION_MODE_USB_OTG_MODE, \
  57. .disable_val = SM5703_OPERATION_MODE_CHARGING_ON, \
  58. .owner = THIS_MODULE, \
  59. }
  60. #define SM5703BUCK(_name) \
  61. [SM5703_BUCK] = { \
  62. .name = _name, \
  63. .of_match = _name, \
  64. .regulators_node = "regulators", \
  65. .type = REGULATOR_VOLTAGE, \
  66. .id = SM5703_BUCK, \
  67. .ops = &sm5703_regulator_ops, \
  68. .n_voltages = ARRAY_SIZE(sm5703_buck_voltagemap), \
  69. .volt_table = sm5703_buck_voltagemap, \
  70. .vsel_reg = SM5703_REG_BUCK, \
  71. .vsel_mask = SM5703_BUCK_VOLT_MASK, \
  72. .enable_reg = SM5703_REG_BUCK, \
  73. .enable_mask = SM5703_REG_EN_BUCK, \
  74. .owner = THIS_MODULE, \
  75. }
  76. #define SM5703LDO(_name, _id) \
  77. [SM5703_LDO ## _id] = { \
  78. .name = _name, \
  79. .of_match = _name, \
  80. .regulators_node = "regulators", \
  81. .type = REGULATOR_VOLTAGE, \
  82. .id = SM5703_LDO ## _id, \
  83. .ops = &sm5703_regulator_ops, \
  84. .n_voltages = ARRAY_SIZE(sm5703_ldo_voltagemap), \
  85. .volt_table = sm5703_ldo_voltagemap, \
  86. .vsel_reg = SM5703_REG_LDO ##_id, \
  87. .vsel_mask = SM5703_LDO_VOLT_MASK, \
  88. .enable_reg = SM5703_REG_LDO ##_id, \
  89. .enable_mask = SM5703_LDO_EN, \
  90. .owner = THIS_MODULE, \
  91. }
  92. static const struct regulator_ops sm5703_regulator_ops = {
  93. .enable = regulator_enable_regmap,
  94. .disable = regulator_disable_regmap,
  95. .is_enabled = regulator_is_enabled_regmap,
  96. .list_voltage = regulator_list_voltage_table,
  97. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  98. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  99. };
  100. static const struct regulator_ops sm5703_regulator_ops_fixed = {
  101. .enable = regulator_enable_regmap,
  102. .disable = regulator_disable_regmap,
  103. .is_enabled = regulator_is_enabled_regmap,
  104. };
  105. static struct regulator_desc sm5703_regulators_desc[SM5703_MAX_REGULATORS] = {
  106. SM5703BUCK("buck"),
  107. SM5703LDO("ldo1", 1),
  108. SM5703LDO("ldo2", 2),
  109. SM5703LDO("ldo3", 3),
  110. SM5703USBLDO("usbldo1", 1),
  111. SM5703USBLDO("usbldo2", 2),
  112. SM5703VBUS("vbus"),
  113. };
  114. static int sm5703_regulator_probe(struct platform_device *pdev)
  115. {
  116. struct device *dev = &pdev->dev;
  117. struct regulator_config config = { NULL, };
  118. struct regulator_dev *rdev;
  119. struct sm5703_dev *sm5703 = dev_get_drvdata(pdev->dev.parent);
  120. int i;
  121. config.dev = dev->parent;
  122. config.regmap = sm5703->regmap;
  123. for (i = 0; i < SM5703_MAX_REGULATORS; i++) {
  124. rdev = devm_regulator_register(dev,
  125. &sm5703_regulators_desc[i],
  126. &config);
  127. if (IS_ERR(rdev))
  128. return dev_err_probe(dev, PTR_ERR(rdev),
  129. "Failed to register a regulator\n");
  130. }
  131. return 0;
  132. }
  133. static const struct platform_device_id sm5703_regulator_id[] = {
  134. { "sm5703-regulator", 0 },
  135. {}
  136. };
  137. MODULE_DEVICE_TABLE(platform, sm5703_regulator_id);
  138. static struct platform_driver sm5703_regulator_driver = {
  139. .driver = {
  140. .name = "sm5703-regulator",
  141. },
  142. .probe = sm5703_regulator_probe,
  143. .id_table = sm5703_regulator_id,
  144. };
  145. module_platform_driver(sm5703_regulator_driver);
  146. MODULE_DESCRIPTION("Silicon Mitus SM5703 LDO/Buck/USB regulator driver");
  147. MODULE_AUTHOR("Markuss Broks <[email protected]>");
  148. MODULE_LICENSE("GPL");