stm32-vrefbuf.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) STMicroelectronics 2017
  4. *
  5. * Author: Fabrice Gasnier <[email protected]>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/clk.h>
  9. #include <linux/io.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/module.h>
  12. #include <linux/of_device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regulator/driver.h>
  15. #include <linux/regulator/of_regulator.h>
  16. #include <linux/pm_runtime.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. #define STM32_VREFBUF_AUTO_SUSPEND_DELAY_MS 10
  25. struct stm32_vrefbuf {
  26. void __iomem *base;
  27. struct clk *clk;
  28. struct device *dev;
  29. };
  30. static const unsigned int stm32_vrefbuf_voltages[] = {
  31. /* Matches resp. VRS = 000b, 001b, 010b, 011b */
  32. 2500000, 2048000, 1800000, 1500000,
  33. };
  34. static int stm32_vrefbuf_enable(struct regulator_dev *rdev)
  35. {
  36. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  37. u32 val;
  38. int ret;
  39. ret = pm_runtime_resume_and_get(priv->dev);
  40. if (ret < 0)
  41. return ret;
  42. val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
  43. val = (val & ~STM32_HIZ) | STM32_ENVR;
  44. writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
  45. /*
  46. * Vrefbuf startup time depends on external capacitor: wait here for
  47. * VRR to be set. That means output has reached expected value.
  48. * ~650us sleep should be enough for caps up to 1.5uF. Use 10ms as
  49. * arbitrary timeout.
  50. */
  51. ret = readl_poll_timeout(priv->base + STM32_VREFBUF_CSR, val,
  52. val & STM32_VRR, 650, 10000);
  53. if (ret) {
  54. dev_err(&rdev->dev, "stm32 vrefbuf timed out!\n");
  55. val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
  56. val = (val & ~STM32_ENVR) | STM32_HIZ;
  57. writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
  58. }
  59. pm_runtime_mark_last_busy(priv->dev);
  60. pm_runtime_put_autosuspend(priv->dev);
  61. return ret;
  62. }
  63. static int stm32_vrefbuf_disable(struct regulator_dev *rdev)
  64. {
  65. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  66. u32 val;
  67. int ret;
  68. ret = pm_runtime_resume_and_get(priv->dev);
  69. if (ret < 0)
  70. return ret;
  71. val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
  72. val &= ~STM32_ENVR;
  73. writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
  74. pm_runtime_mark_last_busy(priv->dev);
  75. pm_runtime_put_autosuspend(priv->dev);
  76. return 0;
  77. }
  78. static int stm32_vrefbuf_is_enabled(struct regulator_dev *rdev)
  79. {
  80. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  81. int ret;
  82. ret = pm_runtime_resume_and_get(priv->dev);
  83. if (ret < 0)
  84. return ret;
  85. ret = readl_relaxed(priv->base + STM32_VREFBUF_CSR) & STM32_ENVR;
  86. pm_runtime_mark_last_busy(priv->dev);
  87. pm_runtime_put_autosuspend(priv->dev);
  88. return ret;
  89. }
  90. static int stm32_vrefbuf_set_voltage_sel(struct regulator_dev *rdev,
  91. unsigned sel)
  92. {
  93. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  94. u32 val;
  95. int ret;
  96. ret = pm_runtime_resume_and_get(priv->dev);
  97. if (ret < 0)
  98. return ret;
  99. val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
  100. val = (val & ~STM32_VRS) | FIELD_PREP(STM32_VRS, sel);
  101. writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
  102. pm_runtime_mark_last_busy(priv->dev);
  103. pm_runtime_put_autosuspend(priv->dev);
  104. return 0;
  105. }
  106. static int stm32_vrefbuf_get_voltage_sel(struct regulator_dev *rdev)
  107. {
  108. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  109. u32 val;
  110. int ret;
  111. ret = pm_runtime_resume_and_get(priv->dev);
  112. if (ret < 0)
  113. return ret;
  114. val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
  115. ret = FIELD_GET(STM32_VRS, val);
  116. pm_runtime_mark_last_busy(priv->dev);
  117. pm_runtime_put_autosuspend(priv->dev);
  118. return ret;
  119. }
  120. static const struct regulator_ops stm32_vrefbuf_volt_ops = {
  121. .enable = stm32_vrefbuf_enable,
  122. .disable = stm32_vrefbuf_disable,
  123. .is_enabled = stm32_vrefbuf_is_enabled,
  124. .get_voltage_sel = stm32_vrefbuf_get_voltage_sel,
  125. .set_voltage_sel = stm32_vrefbuf_set_voltage_sel,
  126. .list_voltage = regulator_list_voltage_table,
  127. };
  128. static const struct regulator_desc stm32_vrefbuf_regu = {
  129. .name = "vref",
  130. .supply_name = "vdda",
  131. .volt_table = stm32_vrefbuf_voltages,
  132. .n_voltages = ARRAY_SIZE(stm32_vrefbuf_voltages),
  133. .ops = &stm32_vrefbuf_volt_ops,
  134. .off_on_delay = 1000,
  135. .type = REGULATOR_VOLTAGE,
  136. .owner = THIS_MODULE,
  137. };
  138. static int stm32_vrefbuf_probe(struct platform_device *pdev)
  139. {
  140. struct stm32_vrefbuf *priv;
  141. struct regulator_config config = { };
  142. struct regulator_dev *rdev;
  143. int ret;
  144. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  145. if (!priv)
  146. return -ENOMEM;
  147. priv->dev = &pdev->dev;
  148. priv->base = devm_platform_ioremap_resource(pdev, 0);
  149. if (IS_ERR(priv->base))
  150. return PTR_ERR(priv->base);
  151. priv->clk = devm_clk_get(&pdev->dev, NULL);
  152. if (IS_ERR(priv->clk))
  153. return PTR_ERR(priv->clk);
  154. pm_runtime_get_noresume(&pdev->dev);
  155. pm_runtime_set_active(&pdev->dev);
  156. pm_runtime_set_autosuspend_delay(&pdev->dev,
  157. STM32_VREFBUF_AUTO_SUSPEND_DELAY_MS);
  158. pm_runtime_use_autosuspend(&pdev->dev);
  159. pm_runtime_enable(&pdev->dev);
  160. ret = clk_prepare_enable(priv->clk);
  161. if (ret) {
  162. dev_err(&pdev->dev, "clk prepare failed with error %d\n", ret);
  163. goto err_pm_stop;
  164. }
  165. config.dev = &pdev->dev;
  166. config.driver_data = priv;
  167. config.of_node = pdev->dev.of_node;
  168. config.init_data = of_get_regulator_init_data(&pdev->dev,
  169. pdev->dev.of_node,
  170. &stm32_vrefbuf_regu);
  171. rdev = regulator_register(&pdev->dev, &stm32_vrefbuf_regu, &config);
  172. if (IS_ERR(rdev)) {
  173. ret = PTR_ERR(rdev);
  174. dev_err(&pdev->dev, "register failed with error %d\n", ret);
  175. goto err_clk_dis;
  176. }
  177. platform_set_drvdata(pdev, rdev);
  178. pm_runtime_mark_last_busy(&pdev->dev);
  179. pm_runtime_put_autosuspend(&pdev->dev);
  180. return 0;
  181. err_clk_dis:
  182. clk_disable_unprepare(priv->clk);
  183. err_pm_stop:
  184. pm_runtime_disable(&pdev->dev);
  185. pm_runtime_set_suspended(&pdev->dev);
  186. pm_runtime_put_noidle(&pdev->dev);
  187. return ret;
  188. }
  189. static int stm32_vrefbuf_remove(struct platform_device *pdev)
  190. {
  191. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  192. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  193. pm_runtime_get_sync(&pdev->dev);
  194. regulator_unregister(rdev);
  195. clk_disable_unprepare(priv->clk);
  196. pm_runtime_disable(&pdev->dev);
  197. pm_runtime_set_suspended(&pdev->dev);
  198. pm_runtime_put_noidle(&pdev->dev);
  199. return 0;
  200. };
  201. static int __maybe_unused stm32_vrefbuf_runtime_suspend(struct device *dev)
  202. {
  203. struct regulator_dev *rdev = dev_get_drvdata(dev);
  204. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  205. clk_disable_unprepare(priv->clk);
  206. return 0;
  207. }
  208. static int __maybe_unused stm32_vrefbuf_runtime_resume(struct device *dev)
  209. {
  210. struct regulator_dev *rdev = dev_get_drvdata(dev);
  211. struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
  212. return clk_prepare_enable(priv->clk);
  213. }
  214. static const struct dev_pm_ops stm32_vrefbuf_pm_ops = {
  215. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  216. pm_runtime_force_resume)
  217. SET_RUNTIME_PM_OPS(stm32_vrefbuf_runtime_suspend,
  218. stm32_vrefbuf_runtime_resume,
  219. NULL)
  220. };
  221. static const struct of_device_id __maybe_unused stm32_vrefbuf_of_match[] = {
  222. { .compatible = "st,stm32-vrefbuf", },
  223. {},
  224. };
  225. MODULE_DEVICE_TABLE(of, stm32_vrefbuf_of_match);
  226. static struct platform_driver stm32_vrefbuf_driver = {
  227. .probe = stm32_vrefbuf_probe,
  228. .remove = stm32_vrefbuf_remove,
  229. .driver = {
  230. .name = "stm32-vrefbuf",
  231. .of_match_table = of_match_ptr(stm32_vrefbuf_of_match),
  232. .pm = &stm32_vrefbuf_pm_ops,
  233. },
  234. };
  235. module_platform_driver(stm32_vrefbuf_driver);
  236. MODULE_LICENSE("GPL v2");
  237. MODULE_AUTHOR("Fabrice Gasnier <[email protected]>");
  238. MODULE_DESCRIPTION("STMicroelectronics STM32 VREFBUF driver");
  239. MODULE_ALIAS("platform:stm32-vrefbuf");