sun20i-regulator.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright (c) 2021-2022 Samuel Holland <samuel@sholland.org>
  4. //
  5. #include <linux/mfd/syscon.h>
  6. #include <linux/module.h>
  7. #include <linux/of.h>
  8. #include <linux/of_device.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/regmap.h>
  11. #include <linux/regulator/driver.h>
  12. #define SUN20I_SYS_LDO_CTRL_REG 0x150
  13. struct sun20i_regulator_data {
  14. const struct regulator_desc *descs;
  15. unsigned int ndescs;
  16. };
  17. /* regulator_list_voltage_linear() modified for the non-integral uV_step. */
  18. static int sun20i_d1_system_ldo_list_voltage(struct regulator_dev *rdev,
  19. unsigned int selector)
  20. {
  21. const struct regulator_desc *desc = rdev->desc;
  22. unsigned int fraction, uV;
  23. if (selector >= desc->n_voltages)
  24. return -EINVAL;
  25. uV = desc->min_uV + (desc->uV_step * selector);
  26. fraction = selector + (desc->min_uV % 4);
  27. if (uV > 1606667)
  28. uV += 6667;
  29. else
  30. fraction++;
  31. /* Produce correctly-rounded absolute voltages. */
  32. return uV + (fraction / 3);
  33. }
  34. static const struct regulator_ops sun20i_d1_system_ldo_ops = {
  35. .list_voltage = sun20i_d1_system_ldo_list_voltage,
  36. .map_voltage = regulator_map_voltage_ascend,
  37. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  38. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  39. };
  40. static const struct regulator_desc sun20i_d1_system_ldo_descs[] = {
  41. {
  42. .name = "ldoa",
  43. .supply_name = "ldo-in",
  44. .of_match = "ldoa",
  45. .ops = &sun20i_d1_system_ldo_ops,
  46. .type = REGULATOR_VOLTAGE,
  47. .owner = THIS_MODULE,
  48. .n_voltages = 32,
  49. .min_uV = 1593333,
  50. .uV_step = 13333, /* repeating */
  51. .vsel_reg = SUN20I_SYS_LDO_CTRL_REG,
  52. .vsel_mask = GENMASK(7, 0),
  53. },
  54. {
  55. .name = "ldob",
  56. .supply_name = "ldo-in",
  57. .of_match = "ldob",
  58. .ops = &sun20i_d1_system_ldo_ops,
  59. .type = REGULATOR_VOLTAGE,
  60. .owner = THIS_MODULE,
  61. .n_voltages = 64,
  62. .min_uV = 1166666,
  63. .uV_step = 13333, /* repeating */
  64. .vsel_reg = SUN20I_SYS_LDO_CTRL_REG,
  65. .vsel_mask = GENMASK(15, 8),
  66. },
  67. };
  68. static const struct sun20i_regulator_data sun20i_d1_system_ldos = {
  69. .descs = sun20i_d1_system_ldo_descs,
  70. .ndescs = ARRAY_SIZE(sun20i_d1_system_ldo_descs),
  71. };
  72. static struct regmap *sun20i_regulator_get_regmap(struct device *dev)
  73. {
  74. struct regmap *regmap;
  75. /*
  76. * First try the syscon interface. The system control device is not
  77. * compatible with "syscon", so fall back to getting the regmap from
  78. * its platform device. This is ugly, but required for devicetree
  79. * backward compatibility.
  80. */
  81. regmap = syscon_node_to_regmap(dev->parent->of_node);
  82. if (!IS_ERR(regmap))
  83. return regmap;
  84. regmap = dev_get_regmap(dev->parent, NULL);
  85. if (regmap)
  86. return regmap;
  87. return ERR_PTR(-EPROBE_DEFER);
  88. }
  89. static int sun20i_regulator_probe(struct platform_device *pdev)
  90. {
  91. const struct sun20i_regulator_data *data;
  92. struct device *dev = &pdev->dev;
  93. struct regulator_config config;
  94. struct regmap *regmap;
  95. data = of_device_get_match_data(dev);
  96. if (!data)
  97. return -EINVAL;
  98. regmap = sun20i_regulator_get_regmap(dev);
  99. if (IS_ERR(regmap))
  100. return dev_err_probe(dev, PTR_ERR(regmap), "Failed to get regmap\n");
  101. config = (struct regulator_config) {
  102. .dev = dev,
  103. .regmap = regmap,
  104. };
  105. for (unsigned int i = 0; i < data->ndescs; ++i) {
  106. const struct regulator_desc *desc = &data->descs[i];
  107. struct regulator_dev *rdev;
  108. rdev = devm_regulator_register(dev, desc, &config);
  109. if (IS_ERR(rdev))
  110. return PTR_ERR(rdev);
  111. }
  112. return 0;
  113. }
  114. static const struct of_device_id sun20i_regulator_of_match[] = {
  115. {
  116. .compatible = "allwinner,sun20i-d1-system-ldos",
  117. .data = &sun20i_d1_system_ldos,
  118. },
  119. { },
  120. };
  121. MODULE_DEVICE_TABLE(of, sun20i_regulator_of_match);
  122. static struct platform_driver sun20i_regulator_driver = {
  123. .probe = sun20i_regulator_probe,
  124. .driver = {
  125. .name = "sun20i-regulator",
  126. .of_match_table = sun20i_regulator_of_match,
  127. },
  128. };
  129. module_platform_driver(sun20i_regulator_driver);
  130. MODULE_AUTHOR("Samuel Holland <samuel@sholland.org>");
  131. MODULE_DESCRIPTION("Allwinner D1 internal LDO driver");
  132. MODULE_LICENSE("GPL");