stm32-vrefbuf.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (C) STMicroelectronics 2017
  3. *
  4. * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
  5. *
  6. * License terms: GNU General Public License (GPL), version 2
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/clk.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/module.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/of_regulator.h>
  17. /* STM32 VREFBUF registers */
  18. #define STM32_VREFBUF_CSR 0x00
  19. /* STM32 VREFBUF CSR bitfields */
  20. #define STM32_VRS GENMASK(6, 4)
  21. #define STM32_VRR BIT(3)
  22. #define STM32_HIZ BIT(1)
  23. #define STM32_ENVR BIT(0)
  24. struct stm32_vrefbuf {
  25. void __iomem *base;
  26. struct clk *clk;
  27. };
  28. static const unsigned int stm32_vrefbuf_voltages[] = {
  29. /* Matches resp. VRS = 000b, 001b, 010b, 011b */
  30. 2500000, 2048000, 1800000, 1500000,
  31. };
  32. static int stm32_vrefbuf_enable(struct regulator_dev *rdev)
  33. {
  34. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  35. u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
  36. int ret;
  37. val = (val & ~STM32_HIZ) | STM32_ENVR;
  38. writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
  39. /*
  40. * Vrefbuf startup time depends on external capacitor: wait here for
  41. * VRR to be set. That means output has reached expected value.
  42. * ~650us sleep should be enough for caps up to 1.5uF. Use 10ms as
  43. * arbitrary timeout.
  44. */
  45. ret = readl_poll_timeout(priv->base + STM32_VREFBUF_CSR, val,
  46. val & STM32_VRR, 650, 10000);
  47. if (ret) {
  48. dev_err(&rdev->dev, "stm32 vrefbuf timed out!\n");
  49. val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
  50. val = (val & ~STM32_ENVR) | STM32_HIZ;
  51. writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
  52. }
  53. return ret;
  54. }
  55. static int stm32_vrefbuf_disable(struct regulator_dev *rdev)
  56. {
  57. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  58. u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
  59. val = (val & ~STM32_ENVR) | STM32_HIZ;
  60. writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
  61. return 0;
  62. }
  63. static int stm32_vrefbuf_is_enabled(struct regulator_dev *rdev)
  64. {
  65. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  66. return readl_relaxed(priv->base + STM32_VREFBUF_CSR) & STM32_ENVR;
  67. }
  68. static int stm32_vrefbuf_set_voltage_sel(struct regulator_dev *rdev,
  69. unsigned sel)
  70. {
  71. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  72. u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
  73. val = (val & ~STM32_VRS) | FIELD_PREP(STM32_VRS, sel);
  74. writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
  75. return 0;
  76. }
  77. static int stm32_vrefbuf_get_voltage_sel(struct regulator_dev *rdev)
  78. {
  79. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  80. u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
  81. return FIELD_GET(STM32_VRS, val);
  82. }
  83. static const struct regulator_ops stm32_vrefbuf_volt_ops = {
  84. .enable = stm32_vrefbuf_enable,
  85. .disable = stm32_vrefbuf_disable,
  86. .is_enabled = stm32_vrefbuf_is_enabled,
  87. .get_voltage_sel = stm32_vrefbuf_get_voltage_sel,
  88. .set_voltage_sel = stm32_vrefbuf_set_voltage_sel,
  89. .list_voltage = regulator_list_voltage_table,
  90. };
  91. static const struct regulator_desc stm32_vrefbuf_regu = {
  92. .name = "vref",
  93. .supply_name = "vdda",
  94. .volt_table = stm32_vrefbuf_voltages,
  95. .n_voltages = ARRAY_SIZE(stm32_vrefbuf_voltages),
  96. .ops = &stm32_vrefbuf_volt_ops,
  97. .type = REGULATOR_VOLTAGE,
  98. .owner = THIS_MODULE,
  99. };
  100. static int stm32_vrefbuf_probe(struct platform_device *pdev)
  101. {
  102. struct resource *res;
  103. struct stm32_vrefbuf *priv;
  104. struct regulator_config config = { };
  105. struct regulator_dev *rdev;
  106. int ret;
  107. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  108. if (!priv)
  109. return -ENOMEM;
  110. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  111. priv->base = devm_ioremap_resource(&pdev->dev, res);
  112. if (IS_ERR(priv->base))
  113. return PTR_ERR(priv->base);
  114. priv->clk = devm_clk_get(&pdev->dev, NULL);
  115. if (IS_ERR(priv->clk))
  116. return PTR_ERR(priv->clk);
  117. ret = clk_prepare_enable(priv->clk);
  118. if (ret) {
  119. dev_err(&pdev->dev, "clk prepare failed with error %d\n", ret);
  120. return ret;
  121. }
  122. config.dev = &pdev->dev;
  123. config.driver_data = priv;
  124. config.of_node = pdev->dev.of_node;
  125. config.init_data = of_get_regulator_init_data(&pdev->dev,
  126. pdev->dev.of_node,
  127. &stm32_vrefbuf_regu);
  128. rdev = regulator_register(&stm32_vrefbuf_regu, &config);
  129. if (IS_ERR(rdev)) {
  130. ret = PTR_ERR(rdev);
  131. dev_err(&pdev->dev, "register failed with error %d\n", ret);
  132. goto err_clk_dis;
  133. }
  134. platform_set_drvdata(pdev, rdev);
  135. return 0;
  136. err_clk_dis:
  137. clk_disable_unprepare(priv->clk);
  138. return ret;
  139. }
  140. static int stm32_vrefbuf_remove(struct platform_device *pdev)
  141. {
  142. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  143. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  144. regulator_unregister(rdev);
  145. clk_disable_unprepare(priv->clk);
  146. return 0;
  147. };
  148. static const struct of_device_id stm32_vrefbuf_of_match[] = {
  149. { .compatible = "st,stm32-vrefbuf", },
  150. {},
  151. };
  152. MODULE_DEVICE_TABLE(of, stm32_vrefbuf_of_match);
  153. static struct platform_driver stm32_vrefbuf_driver = {
  154. .probe = stm32_vrefbuf_probe,
  155. .remove = stm32_vrefbuf_remove,
  156. .driver = {
  157. .name = "stm32-vrefbuf",
  158. .of_match_table = of_match_ptr(stm32_vrefbuf_of_match),
  159. },
  160. };
  161. module_platform_driver(stm32_vrefbuf_driver);
  162. MODULE_LICENSE("GPL v2");
  163. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  164. MODULE_DESCRIPTION("STMicroelectronics STM32 VREFBUF driver");
  165. MODULE_ALIAS("platform:stm32-vrefbuf");