refgen.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2017, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/bitops.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/types.h>
  23. #include <linux/regulator/driver.h>
  24. #include <linux/regulator/machine.h>
  25. #include <linux/regulator/of_regulator.h>
  26. #define REFGEN_REG_BIAS_EN 0x08
  27. #define REFGEN_BIAS_EN_MASK GENMASK(2, 0)
  28. #define REFGEN_BIAS_EN_ENABLE 0x7
  29. #define REFGEN_BIAS_EN_DISABLE 0x6
  30. #define REFGEN_REG_BG_CTRL 0x14
  31. #define REFGEN_BG_CTRL_MASK GENMASK(2, 1)
  32. #define REFGEN_BG_CTRL_ENABLE 0x6
  33. #define REFGEN_BG_CTRL_DISABLE 0x4
  34. struct refgen {
  35. struct regulator_desc rdesc;
  36. struct regulator_dev *rdev;
  37. void __iomem *addr;
  38. };
  39. static void masked_writel(u32 val, u32 mask, void __iomem *addr)
  40. {
  41. u32 reg;
  42. reg = readl_relaxed(addr);
  43. reg = (reg & ~mask) | (val & mask);
  44. writel_relaxed(reg, addr);
  45. }
  46. static int refgen_enable(struct regulator_dev *rdev)
  47. {
  48. struct refgen *vreg = rdev_get_drvdata(rdev);
  49. masked_writel(REFGEN_BG_CTRL_ENABLE, REFGEN_BG_CTRL_MASK,
  50. vreg->addr + REFGEN_REG_BG_CTRL);
  51. writel_relaxed(REFGEN_BIAS_EN_ENABLE, vreg->addr + REFGEN_REG_BIAS_EN);
  52. return 0;
  53. }
  54. static int refgen_disable(struct regulator_dev *rdev)
  55. {
  56. struct refgen *vreg = rdev_get_drvdata(rdev);
  57. writel_relaxed(REFGEN_BIAS_EN_DISABLE, vreg->addr + REFGEN_REG_BIAS_EN);
  58. masked_writel(REFGEN_BG_CTRL_DISABLE, REFGEN_BG_CTRL_MASK,
  59. vreg->addr + REFGEN_REG_BG_CTRL);
  60. return 0;
  61. }
  62. static int refgen_is_enabled(struct regulator_dev *rdev)
  63. {
  64. struct refgen *vreg = rdev_get_drvdata(rdev);
  65. u32 val;
  66. val = readl_relaxed(vreg->addr + REFGEN_REG_BG_CTRL);
  67. if ((val & REFGEN_BG_CTRL_MASK) != REFGEN_BG_CTRL_ENABLE)
  68. return 0;
  69. val = readl_relaxed(vreg->addr + REFGEN_REG_BIAS_EN);
  70. if ((val & REFGEN_BIAS_EN_MASK) != REFGEN_BIAS_EN_ENABLE)
  71. return 0;
  72. return 1;
  73. }
  74. static struct regulator_ops refgen_ops = {
  75. .enable = refgen_enable,
  76. .disable = refgen_disable,
  77. .is_enabled = refgen_is_enabled,
  78. };
  79. static int refgen_probe(struct platform_device *pdev)
  80. {
  81. struct regulator_config config = {};
  82. struct regulator_init_data *init_data = NULL;
  83. struct device *dev = &pdev->dev;
  84. struct regulator_desc *rdesc;
  85. struct resource *res;
  86. struct refgen *vreg;
  87. int rc;
  88. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  89. if (!vreg)
  90. return -ENOMEM;
  91. if (!dev->of_node) {
  92. dev_err(dev, "%s: device tree node missing\n", __func__);
  93. return -ENODEV;
  94. }
  95. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  96. if (!res || !res->start) {
  97. dev_err(dev, "reg address is missing\n");
  98. return -EINVAL;
  99. }
  100. vreg->addr = devm_ioremap_resource(&pdev->dev, res);
  101. if (IS_ERR(vreg->addr)) {
  102. rc = PTR_ERR(vreg->addr);
  103. dev_err(dev, "ioremap failed, rc=%d\n", rc);
  104. return rc;
  105. }
  106. init_data = of_get_regulator_init_data(dev, dev->of_node, &vreg->rdesc);
  107. if (!init_data)
  108. return -ENOMEM;
  109. if (init_data->constraints.name == NULL) {
  110. dev_err(dev, "%s: regulator-name not specified\n", __func__);
  111. return -EINVAL;
  112. }
  113. if (of_get_property(dev->of_node, "parent-supply", NULL))
  114. init_data->supply_regulator = "parent";
  115. rdesc = &vreg->rdesc;
  116. rdesc->name = "refgen";
  117. rdesc->ops = &refgen_ops;
  118. rdesc->id = pdev->id;
  119. rdesc->owner = THIS_MODULE;
  120. rdesc->type = REGULATOR_VOLTAGE;
  121. config.dev = dev;
  122. config.init_data = init_data;
  123. config.driver_data = vreg;
  124. config.of_node = dev->of_node;
  125. vreg->rdev = devm_regulator_register(dev, rdesc, &config);
  126. if (IS_ERR(vreg->rdev)) {
  127. rc = PTR_ERR(vreg->rdev);
  128. if (rc != -EPROBE_DEFER)
  129. dev_err(dev, "%s: regulator register failed\n",
  130. __func__);
  131. return rc;
  132. }
  133. return 0;
  134. }
  135. static const struct of_device_id refgen_match_table[] = {
  136. { .compatible = "qcom,refgen-regulator", },
  137. {}
  138. };
  139. static struct platform_driver refgen_driver = {
  140. .probe = refgen_probe,
  141. .driver = {
  142. .name = "qcom,refgen-regulator",
  143. .owner = THIS_MODULE,
  144. .of_match_table = refgen_match_table,
  145. },
  146. };
  147. static int __init refgen_init(void)
  148. {
  149. return platform_driver_register(&refgen_driver);
  150. }
  151. arch_initcall(refgen_init);
  152. static void __exit refgen_exit(void)
  153. {
  154. platform_driver_unregister(&refgen_driver);
  155. }
  156. module_exit(refgen_exit);
  157. MODULE_LICENSE("GPL v2");
  158. MODULE_DESCRIPTION("refgen regulator driver");