blob: 7a454b7b6eab9958ad28ef2ba09739f3b893ce2c [file] [log] [blame]
Thomas Gleixneraf873fc2019-05-28 09:57:21 -07001// SPDX-License-Identifier: GPL-2.0-only
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +02002/*
3 * Copyright (C) STMicroelectronics 2017
4 *
5 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +02006 */
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>
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +010017#include <linux/pm_runtime.h>
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +020018
19/* STM32 VREFBUF registers */
20#define STM32_VREFBUF_CSR 0x00
21
22/* STM32 VREFBUF CSR bitfields */
23#define STM32_VRS GENMASK(6, 4)
24#define STM32_VRR BIT(3)
25#define STM32_HIZ BIT(1)
26#define STM32_ENVR BIT(0)
27
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +010028#define STM32_VREFBUF_AUTO_SUSPEND_DELAY_MS 10
29
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +020030struct stm32_vrefbuf {
31 void __iomem *base;
32 struct clk *clk;
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +010033 struct device *dev;
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +020034};
35
36static const unsigned int stm32_vrefbuf_voltages[] = {
37 /* Matches resp. VRS = 000b, 001b, 010b, 011b */
38 2500000, 2048000, 1800000, 1500000,
39};
40
41static int stm32_vrefbuf_enable(struct regulator_dev *rdev)
42{
43 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +010044 u32 val;
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +020045 int ret;
46
Minghao Chibfb57112022-04-12 07:10:30 +000047 ret = pm_runtime_resume_and_get(priv->dev);
48 if (ret < 0)
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +010049 return ret;
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +010050
51 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +020052 val = (val & ~STM32_HIZ) | STM32_ENVR;
53 writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
54
55 /*
56 * Vrefbuf startup time depends on external capacitor: wait here for
57 * VRR to be set. That means output has reached expected value.
58 * ~650us sleep should be enough for caps up to 1.5uF. Use 10ms as
59 * arbitrary timeout.
60 */
61 ret = readl_poll_timeout(priv->base + STM32_VREFBUF_CSR, val,
Fabrice Gasnierf63248f2018-02-08 14:43:05 +010062 val & STM32_VRR, 650, 10000);
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +020063 if (ret) {
64 dev_err(&rdev->dev, "stm32 vrefbuf timed out!\n");
65 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
66 val = (val & ~STM32_ENVR) | STM32_HIZ;
67 writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
68 }
69
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +010070 pm_runtime_mark_last_busy(priv->dev);
71 pm_runtime_put_autosuspend(priv->dev);
72
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +020073 return ret;
74}
75
76static int stm32_vrefbuf_disable(struct regulator_dev *rdev)
77{
78 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +010079 u32 val;
80 int ret;
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +020081
Minghao Chibfb57112022-04-12 07:10:30 +000082 ret = pm_runtime_resume_and_get(priv->dev);
83 if (ret < 0)
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +010084 return ret;
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +010085
86 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
Fabrice Gasnier02fbabd2020-03-04 09:55:32 +010087 val &= ~STM32_ENVR;
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +020088 writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
89
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +010090 pm_runtime_mark_last_busy(priv->dev);
91 pm_runtime_put_autosuspend(priv->dev);
92
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +020093 return 0;
94}
95
96static int stm32_vrefbuf_is_enabled(struct regulator_dev *rdev)
97{
98 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +010099 int ret;
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200100
Minghao Chibfb57112022-04-12 07:10:30 +0000101 ret = pm_runtime_resume_and_get(priv->dev);
102 if (ret < 0)
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100103 return ret;
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100104
105 ret = readl_relaxed(priv->base + STM32_VREFBUF_CSR) & STM32_ENVR;
106
107 pm_runtime_mark_last_busy(priv->dev);
108 pm_runtime_put_autosuspend(priv->dev);
109
110 return ret;
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200111}
112
113static int stm32_vrefbuf_set_voltage_sel(struct regulator_dev *rdev,
114 unsigned sel)
115{
116 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100117 u32 val;
118 int ret;
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200119
Minghao Chibfb57112022-04-12 07:10:30 +0000120 ret = pm_runtime_resume_and_get(priv->dev);
121 if (ret < 0)
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100122 return ret;
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100123
124 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200125 val = (val & ~STM32_VRS) | FIELD_PREP(STM32_VRS, sel);
126 writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
127
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100128 pm_runtime_mark_last_busy(priv->dev);
129 pm_runtime_put_autosuspend(priv->dev);
130
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200131 return 0;
132}
133
134static int stm32_vrefbuf_get_voltage_sel(struct regulator_dev *rdev)
135{
136 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100137 u32 val;
138 int ret;
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200139
Minghao Chibfb57112022-04-12 07:10:30 +0000140 ret = pm_runtime_resume_and_get(priv->dev);
141 if (ret < 0)
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100142 return ret;
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100143
144 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
145 ret = FIELD_GET(STM32_VRS, val);
146
147 pm_runtime_mark_last_busy(priv->dev);
148 pm_runtime_put_autosuspend(priv->dev);
149
150 return ret;
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200151}
152
153static const struct regulator_ops stm32_vrefbuf_volt_ops = {
154 .enable = stm32_vrefbuf_enable,
155 .disable = stm32_vrefbuf_disable,
156 .is_enabled = stm32_vrefbuf_is_enabled,
157 .get_voltage_sel = stm32_vrefbuf_get_voltage_sel,
158 .set_voltage_sel = stm32_vrefbuf_set_voltage_sel,
159 .list_voltage = regulator_list_voltage_table,
160};
161
162static const struct regulator_desc stm32_vrefbuf_regu = {
163 .name = "vref",
164 .supply_name = "vdda",
165 .volt_table = stm32_vrefbuf_voltages,
166 .n_voltages = ARRAY_SIZE(stm32_vrefbuf_voltages),
167 .ops = &stm32_vrefbuf_volt_ops,
Fabrice Gasnier02fbabd2020-03-04 09:55:32 +0100168 .off_on_delay = 1000,
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200169 .type = REGULATOR_VOLTAGE,
170 .owner = THIS_MODULE,
171};
172
173static int stm32_vrefbuf_probe(struct platform_device *pdev)
174{
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200175 struct stm32_vrefbuf *priv;
176 struct regulator_config config = { };
177 struct regulator_dev *rdev;
178 int ret;
179
180 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
181 if (!priv)
182 return -ENOMEM;
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100183 priv->dev = &pdev->dev;
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200184
YueHaibing73511a92019-10-09 23:01:38 +0800185 priv->base = devm_platform_ioremap_resource(pdev, 0);
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200186 if (IS_ERR(priv->base))
187 return PTR_ERR(priv->base);
188
189 priv->clk = devm_clk_get(&pdev->dev, NULL);
190 if (IS_ERR(priv->clk))
191 return PTR_ERR(priv->clk);
192
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100193 pm_runtime_get_noresume(&pdev->dev);
194 pm_runtime_set_active(&pdev->dev);
195 pm_runtime_set_autosuspend_delay(&pdev->dev,
196 STM32_VREFBUF_AUTO_SUSPEND_DELAY_MS);
197 pm_runtime_use_autosuspend(&pdev->dev);
198 pm_runtime_enable(&pdev->dev);
199
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200200 ret = clk_prepare_enable(priv->clk);
201 if (ret) {
202 dev_err(&pdev->dev, "clk prepare failed with error %d\n", ret);
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100203 goto err_pm_stop;
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200204 }
205
206 config.dev = &pdev->dev;
207 config.driver_data = priv;
208 config.of_node = pdev->dev.of_node;
209 config.init_data = of_get_regulator_init_data(&pdev->dev,
210 pdev->dev.of_node,
211 &stm32_vrefbuf_regu);
212
ChiYuan Huang8f3cbcd2022-12-06 15:22:21 +0800213 rdev = regulator_register(&pdev->dev, &stm32_vrefbuf_regu, &config);
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200214 if (IS_ERR(rdev)) {
215 ret = PTR_ERR(rdev);
216 dev_err(&pdev->dev, "register failed with error %d\n", ret);
217 goto err_clk_dis;
218 }
219 platform_set_drvdata(pdev, rdev);
220
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100221 pm_runtime_mark_last_busy(&pdev->dev);
222 pm_runtime_put_autosuspend(&pdev->dev);
223
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200224 return 0;
225
226err_clk_dis:
227 clk_disable_unprepare(priv->clk);
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100228err_pm_stop:
229 pm_runtime_disable(&pdev->dev);
230 pm_runtime_set_suspended(&pdev->dev);
231 pm_runtime_put_noidle(&pdev->dev);
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200232
233 return ret;
234}
235
236static int stm32_vrefbuf_remove(struct platform_device *pdev)
237{
238 struct regulator_dev *rdev = platform_get_drvdata(pdev);
239 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
240
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100241 pm_runtime_get_sync(&pdev->dev);
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200242 regulator_unregister(rdev);
243 clk_disable_unprepare(priv->clk);
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100244 pm_runtime_disable(&pdev->dev);
245 pm_runtime_set_suspended(&pdev->dev);
246 pm_runtime_put_noidle(&pdev->dev);
247
248 return 0;
249};
250
251static int __maybe_unused stm32_vrefbuf_runtime_suspend(struct device *dev)
252{
253 struct regulator_dev *rdev = dev_get_drvdata(dev);
254 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
255
256 clk_disable_unprepare(priv->clk);
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200257
258 return 0;
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100259}
260
261static int __maybe_unused stm32_vrefbuf_runtime_resume(struct device *dev)
262{
263 struct regulator_dev *rdev = dev_get_drvdata(dev);
264 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
265
266 return clk_prepare_enable(priv->clk);
267}
268
269static const struct dev_pm_ops stm32_vrefbuf_pm_ops = {
270 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
271 pm_runtime_force_resume)
272 SET_RUNTIME_PM_OPS(stm32_vrefbuf_runtime_suspend,
273 stm32_vrefbuf_runtime_resume,
274 NULL)
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200275};
276
Jisheng Zhangd5579e72020-08-21 11:17:55 +0800277static const struct of_device_id __maybe_unused stm32_vrefbuf_of_match[] = {
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200278 { .compatible = "st,stm32-vrefbuf", },
279 {},
280};
281MODULE_DEVICE_TABLE(of, stm32_vrefbuf_of_match);
282
283static struct platform_driver stm32_vrefbuf_driver = {
284 .probe = stm32_vrefbuf_probe,
285 .remove = stm32_vrefbuf_remove,
286 .driver = {
287 .name = "stm32-vrefbuf",
288 .of_match_table = of_match_ptr(stm32_vrefbuf_of_match),
Fabrice Gasnierbe5295f2019-02-28 11:45:18 +0100289 .pm = &stm32_vrefbuf_pm_ops,
Fabrice Gasnier0cdbf482017-08-30 17:55:28 +0200290 },
291};
292module_platform_driver(stm32_vrefbuf_driver);
293
294MODULE_LICENSE("GPL v2");
295MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
296MODULE_DESCRIPTION("STMicroelectronics STM32 VREFBUF driver");
297MODULE_ALIAS("platform:stm32-vrefbuf");