max77541-regulator.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2022 Analog Devices, Inc.
  4. * ADI Regulator driver for the MAX77540 and MAX77541
  5. */
  6. #include <linux/mfd/max77541.h>
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/regmap.h>
  10. #include <linux/regulator/driver.h>
  11. static const struct regulator_ops max77541_buck_ops = {
  12. .enable = regulator_enable_regmap,
  13. .disable = regulator_disable_regmap,
  14. .is_enabled = regulator_is_enabled_regmap,
  15. .list_voltage = regulator_list_voltage_pickable_linear_range,
  16. .get_voltage_sel = regulator_get_voltage_sel_pickable_regmap,
  17. .set_voltage_sel = regulator_set_voltage_sel_pickable_regmap,
  18. };
  19. static const struct linear_range max77540_buck_ranges[] = {
  20. /* Ranges when VOLT_SEL bits are 0x00 */
  21. REGULATOR_LINEAR_RANGE(500000, 0x00, 0x8B, 5000),
  22. REGULATOR_LINEAR_RANGE(1200000, 0x8C, 0xFF, 0),
  23. /* Ranges when VOLT_SEL bits are 0x40 */
  24. REGULATOR_LINEAR_RANGE(1200000, 0x00, 0x8B, 10000),
  25. REGULATOR_LINEAR_RANGE(2400000, 0x8C, 0xFF, 0),
  26. /* Ranges when VOLT_SEL bits are 0x80 */
  27. REGULATOR_LINEAR_RANGE(2000000, 0x00, 0x9F, 20000),
  28. REGULATOR_LINEAR_RANGE(5200000, 0xA0, 0xFF, 0),
  29. };
  30. static const struct linear_range max77541_buck_ranges[] = {
  31. /* Ranges when VOLT_SEL bits are 0x00 */
  32. REGULATOR_LINEAR_RANGE(300000, 0x00, 0xB3, 5000),
  33. REGULATOR_LINEAR_RANGE(1200000, 0xB4, 0xFF, 0),
  34. /* Ranges when VOLT_SEL bits are 0x40 */
  35. REGULATOR_LINEAR_RANGE(1200000, 0x00, 0x8B, 10000),
  36. REGULATOR_LINEAR_RANGE(2400000, 0x8C, 0xFF, 0),
  37. /* Ranges when VOLT_SEL bits are 0x80 */
  38. REGULATOR_LINEAR_RANGE(2000000, 0x00, 0x9F, 20000),
  39. REGULATOR_LINEAR_RANGE(5200000, 0xA0, 0xFF, 0),
  40. };
  41. static const unsigned int max77541_buck_volt_range_sel[] = {
  42. 0x0, 0x0, 0x1, 0x1, 0x2, 0x2,
  43. };
  44. enum max77541_regulators {
  45. MAX77541_BUCK1 = 1,
  46. MAX77541_BUCK2,
  47. };
  48. #define MAX77540_BUCK(_id, _ops) \
  49. { .id = MAX77541_BUCK ## _id, \
  50. .name = "buck"#_id, \
  51. .of_match = "buck"#_id, \
  52. .regulators_node = "regulators", \
  53. .enable_reg = MAX77541_REG_EN_CTRL, \
  54. .enable_mask = MAX77541_BIT_M ## _id ## _EN, \
  55. .ops = &(_ops), \
  56. .type = REGULATOR_VOLTAGE, \
  57. .linear_ranges = max77540_buck_ranges, \
  58. .n_linear_ranges = ARRAY_SIZE(max77540_buck_ranges), \
  59. .vsel_reg = MAX77541_REG_M ## _id ## _VOUT, \
  60. .vsel_mask = MAX77541_BITS_MX_VOUT, \
  61. .vsel_range_reg = MAX77541_REG_M ## _id ## _CFG1, \
  62. .vsel_range_mask = MAX77541_BITS_MX_CFG1_RNG, \
  63. .linear_range_selectors_bitfield = max77541_buck_volt_range_sel, \
  64. .owner = THIS_MODULE, \
  65. }
  66. #define MAX77541_BUCK(_id, _ops) \
  67. { .id = MAX77541_BUCK ## _id, \
  68. .name = "buck"#_id, \
  69. .of_match = "buck"#_id, \
  70. .regulators_node = "regulators", \
  71. .enable_reg = MAX77541_REG_EN_CTRL, \
  72. .enable_mask = MAX77541_BIT_M ## _id ## _EN, \
  73. .ops = &(_ops), \
  74. .type = REGULATOR_VOLTAGE, \
  75. .linear_ranges = max77541_buck_ranges, \
  76. .n_linear_ranges = ARRAY_SIZE(max77541_buck_ranges), \
  77. .vsel_reg = MAX77541_REG_M ## _id ## _VOUT, \
  78. .vsel_mask = MAX77541_BITS_MX_VOUT, \
  79. .vsel_range_reg = MAX77541_REG_M ## _id ## _CFG1, \
  80. .vsel_range_mask = MAX77541_BITS_MX_CFG1_RNG, \
  81. .linear_range_selectors_bitfield = max77541_buck_volt_range_sel, \
  82. .owner = THIS_MODULE, \
  83. }
  84. static const struct regulator_desc max77540_regulators_desc[] = {
  85. MAX77540_BUCK(1, max77541_buck_ops),
  86. MAX77540_BUCK(2, max77541_buck_ops),
  87. };
  88. static const struct regulator_desc max77541_regulators_desc[] = {
  89. MAX77541_BUCK(1, max77541_buck_ops),
  90. MAX77541_BUCK(2, max77541_buck_ops),
  91. };
  92. static int max77541_regulator_probe(struct platform_device *pdev)
  93. {
  94. struct regulator_config config = {};
  95. const struct regulator_desc *desc;
  96. struct device *dev = &pdev->dev;
  97. struct regulator_dev *rdev;
  98. struct max77541 *max77541 = dev_get_drvdata(dev->parent);
  99. unsigned int i;
  100. config.dev = dev->parent;
  101. switch (max77541->id) {
  102. case MAX77540:
  103. desc = max77540_regulators_desc;
  104. break;
  105. case MAX77541:
  106. desc = max77541_regulators_desc;
  107. break;
  108. default:
  109. return -EINVAL;
  110. }
  111. for (i = 0; i < MAX77541_MAX_REGULATORS; i++) {
  112. rdev = devm_regulator_register(dev, &desc[i], &config);
  113. if (IS_ERR(rdev))
  114. return dev_err_probe(dev, PTR_ERR(rdev),
  115. "Failed to register regulator\n");
  116. }
  117. return 0;
  118. }
  119. static const struct platform_device_id max77541_regulator_platform_id[] = {
  120. { "max77540-regulator" },
  121. { "max77541-regulator" },
  122. { }
  123. };
  124. MODULE_DEVICE_TABLE(platform, max77541_regulator_platform_id);
  125. static struct platform_driver max77541_regulator_driver = {
  126. .driver = {
  127. .name = "max77541-regulator",
  128. },
  129. .probe = max77541_regulator_probe,
  130. .id_table = max77541_regulator_platform_id,
  131. };
  132. module_platform_driver(max77541_regulator_driver);
  133. MODULE_AUTHOR("Okan Sahin <Okan.Sahin@analog.com>");
  134. MODULE_DESCRIPTION("MAX77540/MAX77541 regulator driver");
  135. MODULE_LICENSE("GPL");