rn5t618-regulator.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Regulator driver for Ricoh RN5T618 PMIC
  3. *
  4. * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * You should have received a copy of the GNU General Public License
  11. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include <linux/mfd/rn5t618.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/regulator/driver.h>
  19. #include <linux/regulator/of_regulator.h>
  20. static struct regulator_ops rn5t618_reg_ops = {
  21. .enable = regulator_enable_regmap,
  22. .disable = regulator_disable_regmap,
  23. .is_enabled = regulator_is_enabled_regmap,
  24. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  25. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  26. .list_voltage = regulator_list_voltage_linear,
  27. };
  28. #define REG(rid, ereg, emask, vreg, vmask, min, max, step) \
  29. [RN5T618_##rid] = { \
  30. .name = #rid, \
  31. .id = RN5T618_##rid, \
  32. .type = REGULATOR_VOLTAGE, \
  33. .owner = THIS_MODULE, \
  34. .ops = &rn5t618_reg_ops, \
  35. .n_voltages = ((max) - (min)) / (step) + 1, \
  36. .min_uV = (min), \
  37. .uV_step = (step), \
  38. .enable_reg = RN5T618_##ereg, \
  39. .enable_mask = (emask), \
  40. .vsel_reg = RN5T618_##vreg, \
  41. .vsel_mask = (vmask), \
  42. }
  43. static struct regulator_desc rn5t618_regulators[] = {
  44. /* DCDC */
  45. REG(DCDC1, DC1CTL, BIT(0), DC1DAC, 0xff, 600000, 3500000, 12500),
  46. REG(DCDC2, DC2CTL, BIT(0), DC2DAC, 0xff, 600000, 3500000, 12500),
  47. REG(DCDC3, DC3CTL, BIT(0), DC3DAC, 0xff, 600000, 3500000, 12500),
  48. /* LDO */
  49. REG(LDO1, LDOEN1, BIT(0), LDO1DAC, 0x7f, 900000, 3500000, 25000),
  50. REG(LDO2, LDOEN1, BIT(1), LDO2DAC, 0x7f, 900000, 3500000, 25000),
  51. REG(LDO3, LDOEN1, BIT(2), LDO3DAC, 0x7f, 600000, 3500000, 25000),
  52. REG(LDO4, LDOEN1, BIT(3), LDO4DAC, 0x7f, 900000, 3500000, 25000),
  53. REG(LDO5, LDOEN1, BIT(4), LDO5DAC, 0x7f, 900000, 3500000, 25000),
  54. /* LDO RTC */
  55. REG(LDORTC1, LDOEN2, BIT(4), LDORTCDAC, 0x7f, 1700000, 3500000, 25000),
  56. REG(LDORTC2, LDOEN2, BIT(5), LDORTC2DAC, 0x7f, 900000, 3500000, 25000),
  57. };
  58. static struct of_regulator_match rn5t618_matches[] = {
  59. [RN5T618_DCDC1] = { .name = "DCDC1" },
  60. [RN5T618_DCDC2] = { .name = "DCDC2" },
  61. [RN5T618_DCDC3] = { .name = "DCDC3" },
  62. [RN5T618_LDO1] = { .name = "LDO1" },
  63. [RN5T618_LDO2] = { .name = "LDO2" },
  64. [RN5T618_LDO3] = { .name = "LDO3" },
  65. [RN5T618_LDO4] = { .name = "LDO4" },
  66. [RN5T618_LDO5] = { .name = "LDO5" },
  67. [RN5T618_LDORTC1] = { .name = "LDORTC1" },
  68. [RN5T618_LDORTC2] = { .name = "LDORTC2" },
  69. };
  70. static int rn5t618_regulator_parse_dt(struct platform_device *pdev)
  71. {
  72. struct device_node *np, *regulators;
  73. int ret;
  74. np = of_node_get(pdev->dev.parent->of_node);
  75. if (!np)
  76. return 0;
  77. regulators = of_get_child_by_name(np, "regulators");
  78. if (!regulators) {
  79. dev_err(&pdev->dev, "regulators node not found\n");
  80. return -EINVAL;
  81. }
  82. ret = of_regulator_match(&pdev->dev, regulators, rn5t618_matches,
  83. ARRAY_SIZE(rn5t618_matches));
  84. of_node_put(regulators);
  85. if (ret < 0) {
  86. dev_err(&pdev->dev, "error parsing regulator init data: %d\n",
  87. ret);
  88. }
  89. return 0;
  90. }
  91. static int rn5t618_regulator_probe(struct platform_device *pdev)
  92. {
  93. struct rn5t618 *rn5t618 = dev_get_drvdata(pdev->dev.parent);
  94. struct regulator_config config = { };
  95. struct regulator_dev *rdev;
  96. int ret, i;
  97. ret = rn5t618_regulator_parse_dt(pdev);
  98. if (ret)
  99. return ret;
  100. for (i = 0; i < RN5T618_REG_NUM; i++) {
  101. config.dev = &pdev->dev;
  102. config.init_data = rn5t618_matches[i].init_data;
  103. config.of_node = rn5t618_matches[i].of_node;
  104. config.regmap = rn5t618->regmap;
  105. rdev = devm_regulator_register(&pdev->dev,
  106. &rn5t618_regulators[i],
  107. &config);
  108. if (IS_ERR(rdev)) {
  109. dev_err(&pdev->dev, "failed to register %s regulator\n",
  110. rn5t618_regulators[i].name);
  111. return PTR_ERR(rdev);
  112. }
  113. }
  114. return 0;
  115. }
  116. static struct platform_driver rn5t618_regulator_driver = {
  117. .probe = rn5t618_regulator_probe,
  118. .driver = {
  119. .name = "rn5t618-regulator",
  120. },
  121. };
  122. module_platform_driver(rn5t618_regulator_driver);
  123. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  124. MODULE_DESCRIPTION("RN5T618 regulator driver");
  125. MODULE_LICENSE("GPL v2");