xref: /linux/drivers/regulator/stm32-vrefbuf.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20cdbf481SFabrice Gasnier /*
30cdbf481SFabrice Gasnier  * Copyright (C) STMicroelectronics 2017
40cdbf481SFabrice Gasnier  *
50cdbf481SFabrice Gasnier  * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
60cdbf481SFabrice Gasnier  */
70cdbf481SFabrice Gasnier 
80cdbf481SFabrice Gasnier #include <linux/bitfield.h>
90cdbf481SFabrice Gasnier #include <linux/clk.h>
100cdbf481SFabrice Gasnier #include <linux/io.h>
110cdbf481SFabrice Gasnier #include <linux/iopoll.h>
120cdbf481SFabrice Gasnier #include <linux/module.h>
13045a44d4SRob Herring #include <linux/of.h>
140cdbf481SFabrice Gasnier #include <linux/platform_device.h>
150cdbf481SFabrice Gasnier #include <linux/regulator/driver.h>
160cdbf481SFabrice Gasnier #include <linux/regulator/of_regulator.h>
17be5295f8SFabrice Gasnier #include <linux/pm_runtime.h>
180cdbf481SFabrice Gasnier 
190cdbf481SFabrice Gasnier /* STM32 VREFBUF registers */
200cdbf481SFabrice Gasnier #define STM32_VREFBUF_CSR		0x00
210cdbf481SFabrice Gasnier 
220cdbf481SFabrice Gasnier /* STM32 VREFBUF CSR bitfields */
230cdbf481SFabrice Gasnier #define STM32_VRS			GENMASK(6, 4)
240cdbf481SFabrice Gasnier #define STM32_VRR			BIT(3)
250cdbf481SFabrice Gasnier #define STM32_HIZ			BIT(1)
260cdbf481SFabrice Gasnier #define STM32_ENVR			BIT(0)
270cdbf481SFabrice Gasnier 
28be5295f8SFabrice Gasnier #define STM32_VREFBUF_AUTO_SUSPEND_DELAY_MS	10
29be5295f8SFabrice Gasnier 
300cdbf481SFabrice Gasnier struct stm32_vrefbuf {
310cdbf481SFabrice Gasnier 	void __iomem *base;
320cdbf481SFabrice Gasnier 	struct clk *clk;
33be5295f8SFabrice Gasnier 	struct device *dev;
340cdbf481SFabrice Gasnier };
350cdbf481SFabrice Gasnier 
360cdbf481SFabrice Gasnier static const unsigned int stm32_vrefbuf_voltages[] = {
370cdbf481SFabrice Gasnier 	/* Matches resp. VRS = 000b, 001b, 010b, 011b */
380cdbf481SFabrice Gasnier 	2500000, 2048000, 1800000, 1500000,
390cdbf481SFabrice Gasnier };
400cdbf481SFabrice Gasnier 
stm32_vrefbuf_enable(struct regulator_dev * rdev)410cdbf481SFabrice Gasnier static int stm32_vrefbuf_enable(struct regulator_dev *rdev)
420cdbf481SFabrice Gasnier {
430cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
44be5295f8SFabrice Gasnier 	u32 val;
450cdbf481SFabrice Gasnier 	int ret;
460cdbf481SFabrice Gasnier 
47bfb5711eSMinghao Chi 	ret = pm_runtime_resume_and_get(priv->dev);
48bfb5711eSMinghao Chi 	if (ret < 0)
49be5295f8SFabrice Gasnier 		return ret;
50be5295f8SFabrice Gasnier 
51be5295f8SFabrice Gasnier 	val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
520cdbf481SFabrice Gasnier 	val = (val & ~STM32_HIZ) | STM32_ENVR;
530cdbf481SFabrice Gasnier 	writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
540cdbf481SFabrice Gasnier 
550cdbf481SFabrice Gasnier 	/*
560cdbf481SFabrice Gasnier 	 * Vrefbuf startup time depends on external capacitor: wait here for
570cdbf481SFabrice Gasnier 	 * VRR to be set. That means output has reached expected value.
580cdbf481SFabrice Gasnier 	 * ~650us sleep should be enough for caps up to 1.5uF. Use 10ms as
590cdbf481SFabrice Gasnier 	 * arbitrary timeout.
600cdbf481SFabrice Gasnier 	 */
610cdbf481SFabrice Gasnier 	ret = readl_poll_timeout(priv->base + STM32_VREFBUF_CSR, val,
62f63248faSFabrice Gasnier 				 val & STM32_VRR, 650, 10000);
630cdbf481SFabrice Gasnier 	if (ret) {
640cdbf481SFabrice Gasnier 		dev_err(&rdev->dev, "stm32 vrefbuf timed out!\n");
650cdbf481SFabrice Gasnier 		val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
660cdbf481SFabrice Gasnier 		val = (val & ~STM32_ENVR) | STM32_HIZ;
670cdbf481SFabrice Gasnier 		writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
680cdbf481SFabrice Gasnier 	}
690cdbf481SFabrice Gasnier 
70be5295f8SFabrice Gasnier 	pm_runtime_mark_last_busy(priv->dev);
71be5295f8SFabrice Gasnier 	pm_runtime_put_autosuspend(priv->dev);
72be5295f8SFabrice Gasnier 
730cdbf481SFabrice Gasnier 	return ret;
740cdbf481SFabrice Gasnier }
750cdbf481SFabrice Gasnier 
stm32_vrefbuf_disable(struct regulator_dev * rdev)760cdbf481SFabrice Gasnier static int stm32_vrefbuf_disable(struct regulator_dev *rdev)
770cdbf481SFabrice Gasnier {
780cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
79be5295f8SFabrice Gasnier 	u32 val;
80be5295f8SFabrice Gasnier 	int ret;
810cdbf481SFabrice Gasnier 
82bfb5711eSMinghao Chi 	ret = pm_runtime_resume_and_get(priv->dev);
83bfb5711eSMinghao Chi 	if (ret < 0)
84be5295f8SFabrice Gasnier 		return ret;
85be5295f8SFabrice Gasnier 
86be5295f8SFabrice Gasnier 	val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
8702fbabd5SFabrice Gasnier 	val &= ~STM32_ENVR;
880cdbf481SFabrice Gasnier 	writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
890cdbf481SFabrice Gasnier 
90be5295f8SFabrice Gasnier 	pm_runtime_mark_last_busy(priv->dev);
91be5295f8SFabrice Gasnier 	pm_runtime_put_autosuspend(priv->dev);
92be5295f8SFabrice Gasnier 
930cdbf481SFabrice Gasnier 	return 0;
940cdbf481SFabrice Gasnier }
950cdbf481SFabrice Gasnier 
stm32_vrefbuf_is_enabled(struct regulator_dev * rdev)960cdbf481SFabrice Gasnier static int stm32_vrefbuf_is_enabled(struct regulator_dev *rdev)
970cdbf481SFabrice Gasnier {
980cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
99be5295f8SFabrice Gasnier 	int ret;
1000cdbf481SFabrice Gasnier 
101bfb5711eSMinghao Chi 	ret = pm_runtime_resume_and_get(priv->dev);
102bfb5711eSMinghao Chi 	if (ret < 0)
103be5295f8SFabrice Gasnier 		return ret;
104be5295f8SFabrice Gasnier 
105be5295f8SFabrice Gasnier 	ret = readl_relaxed(priv->base + STM32_VREFBUF_CSR) & STM32_ENVR;
106be5295f8SFabrice Gasnier 
107be5295f8SFabrice Gasnier 	pm_runtime_mark_last_busy(priv->dev);
108be5295f8SFabrice Gasnier 	pm_runtime_put_autosuspend(priv->dev);
109be5295f8SFabrice Gasnier 
110be5295f8SFabrice Gasnier 	return ret;
1110cdbf481SFabrice Gasnier }
1120cdbf481SFabrice Gasnier 
stm32_vrefbuf_set_voltage_sel(struct regulator_dev * rdev,unsigned sel)1130cdbf481SFabrice Gasnier static int stm32_vrefbuf_set_voltage_sel(struct regulator_dev *rdev,
1140cdbf481SFabrice Gasnier 					 unsigned sel)
1150cdbf481SFabrice Gasnier {
1160cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
117be5295f8SFabrice Gasnier 	u32 val;
118be5295f8SFabrice Gasnier 	int ret;
1190cdbf481SFabrice Gasnier 
120bfb5711eSMinghao Chi 	ret = pm_runtime_resume_and_get(priv->dev);
121bfb5711eSMinghao Chi 	if (ret < 0)
122be5295f8SFabrice Gasnier 		return ret;
123be5295f8SFabrice Gasnier 
124be5295f8SFabrice Gasnier 	val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
1250cdbf481SFabrice Gasnier 	val = (val & ~STM32_VRS) | FIELD_PREP(STM32_VRS, sel);
1260cdbf481SFabrice Gasnier 	writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
1270cdbf481SFabrice Gasnier 
128be5295f8SFabrice Gasnier 	pm_runtime_mark_last_busy(priv->dev);
129be5295f8SFabrice Gasnier 	pm_runtime_put_autosuspend(priv->dev);
130be5295f8SFabrice Gasnier 
1310cdbf481SFabrice Gasnier 	return 0;
1320cdbf481SFabrice Gasnier }
1330cdbf481SFabrice Gasnier 
stm32_vrefbuf_get_voltage_sel(struct regulator_dev * rdev)1340cdbf481SFabrice Gasnier static int stm32_vrefbuf_get_voltage_sel(struct regulator_dev *rdev)
1350cdbf481SFabrice Gasnier {
1360cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
137be5295f8SFabrice Gasnier 	u32 val;
138be5295f8SFabrice Gasnier 	int ret;
1390cdbf481SFabrice Gasnier 
140bfb5711eSMinghao Chi 	ret = pm_runtime_resume_and_get(priv->dev);
141bfb5711eSMinghao Chi 	if (ret < 0)
142be5295f8SFabrice Gasnier 		return ret;
143be5295f8SFabrice Gasnier 
144be5295f8SFabrice Gasnier 	val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
145be5295f8SFabrice Gasnier 	ret = FIELD_GET(STM32_VRS, val);
146be5295f8SFabrice Gasnier 
147be5295f8SFabrice Gasnier 	pm_runtime_mark_last_busy(priv->dev);
148be5295f8SFabrice Gasnier 	pm_runtime_put_autosuspend(priv->dev);
149be5295f8SFabrice Gasnier 
150be5295f8SFabrice Gasnier 	return ret;
1510cdbf481SFabrice Gasnier }
1520cdbf481SFabrice Gasnier 
1530cdbf481SFabrice Gasnier static const struct regulator_ops stm32_vrefbuf_volt_ops = {
1540cdbf481SFabrice Gasnier 	.enable		= stm32_vrefbuf_enable,
1550cdbf481SFabrice Gasnier 	.disable	= stm32_vrefbuf_disable,
1560cdbf481SFabrice Gasnier 	.is_enabled	= stm32_vrefbuf_is_enabled,
1570cdbf481SFabrice Gasnier 	.get_voltage_sel = stm32_vrefbuf_get_voltage_sel,
1580cdbf481SFabrice Gasnier 	.set_voltage_sel = stm32_vrefbuf_set_voltage_sel,
1590cdbf481SFabrice Gasnier 	.list_voltage	= regulator_list_voltage_table,
1600cdbf481SFabrice Gasnier };
1610cdbf481SFabrice Gasnier 
1620cdbf481SFabrice Gasnier static const struct regulator_desc stm32_vrefbuf_regu = {
1630cdbf481SFabrice Gasnier 	.name = "vref",
1640cdbf481SFabrice Gasnier 	.supply_name = "vdda",
1650cdbf481SFabrice Gasnier 	.volt_table = stm32_vrefbuf_voltages,
1660cdbf481SFabrice Gasnier 	.n_voltages = ARRAY_SIZE(stm32_vrefbuf_voltages),
1670cdbf481SFabrice Gasnier 	.ops = &stm32_vrefbuf_volt_ops,
16802fbabd5SFabrice Gasnier 	.off_on_delay = 1000,
1690cdbf481SFabrice Gasnier 	.type = REGULATOR_VOLTAGE,
1700cdbf481SFabrice Gasnier 	.owner = THIS_MODULE,
1710cdbf481SFabrice Gasnier };
1720cdbf481SFabrice Gasnier 
stm32_vrefbuf_probe(struct platform_device * pdev)1730cdbf481SFabrice Gasnier static int stm32_vrefbuf_probe(struct platform_device *pdev)
1740cdbf481SFabrice Gasnier {
1750cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv;
1760cdbf481SFabrice Gasnier 	struct regulator_config config = { };
1770cdbf481SFabrice Gasnier 	struct regulator_dev *rdev;
1780cdbf481SFabrice Gasnier 	int ret;
1790cdbf481SFabrice Gasnier 
1800cdbf481SFabrice Gasnier 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1810cdbf481SFabrice Gasnier 	if (!priv)
1820cdbf481SFabrice Gasnier 		return -ENOMEM;
183be5295f8SFabrice Gasnier 	priv->dev = &pdev->dev;
1840cdbf481SFabrice Gasnier 
18573511a90SYueHaibing 	priv->base = devm_platform_ioremap_resource(pdev, 0);
1860cdbf481SFabrice Gasnier 	if (IS_ERR(priv->base))
1870cdbf481SFabrice Gasnier 		return PTR_ERR(priv->base);
1880cdbf481SFabrice Gasnier 
1890cdbf481SFabrice Gasnier 	priv->clk = devm_clk_get(&pdev->dev, NULL);
1900cdbf481SFabrice Gasnier 	if (IS_ERR(priv->clk))
1910cdbf481SFabrice Gasnier 		return PTR_ERR(priv->clk);
1920cdbf481SFabrice Gasnier 
193be5295f8SFabrice Gasnier 	pm_runtime_get_noresume(&pdev->dev);
194be5295f8SFabrice Gasnier 	pm_runtime_set_active(&pdev->dev);
195be5295f8SFabrice Gasnier 	pm_runtime_set_autosuspend_delay(&pdev->dev,
196be5295f8SFabrice Gasnier 					 STM32_VREFBUF_AUTO_SUSPEND_DELAY_MS);
197be5295f8SFabrice Gasnier 	pm_runtime_use_autosuspend(&pdev->dev);
198be5295f8SFabrice Gasnier 	pm_runtime_enable(&pdev->dev);
199be5295f8SFabrice Gasnier 
2000cdbf481SFabrice Gasnier 	ret = clk_prepare_enable(priv->clk);
2010cdbf481SFabrice Gasnier 	if (ret) {
2020cdbf481SFabrice Gasnier 		dev_err(&pdev->dev, "clk prepare failed with error %d\n", ret);
203be5295f8SFabrice Gasnier 		goto err_pm_stop;
2040cdbf481SFabrice Gasnier 	}
2050cdbf481SFabrice Gasnier 
2060cdbf481SFabrice Gasnier 	config.dev = &pdev->dev;
2070cdbf481SFabrice Gasnier 	config.driver_data = priv;
2080cdbf481SFabrice Gasnier 	config.of_node = pdev->dev.of_node;
2090cdbf481SFabrice Gasnier 	config.init_data = of_get_regulator_init_data(&pdev->dev,
2100cdbf481SFabrice Gasnier 						      pdev->dev.of_node,
2110cdbf481SFabrice Gasnier 						      &stm32_vrefbuf_regu);
2120cdbf481SFabrice Gasnier 
2138f3cbcd6SChiYuan Huang 	rdev = regulator_register(&pdev->dev, &stm32_vrefbuf_regu, &config);
2140cdbf481SFabrice Gasnier 	if (IS_ERR(rdev)) {
2150cdbf481SFabrice Gasnier 		ret = PTR_ERR(rdev);
2160cdbf481SFabrice Gasnier 		dev_err(&pdev->dev, "register failed with error %d\n", ret);
2170cdbf481SFabrice Gasnier 		goto err_clk_dis;
2180cdbf481SFabrice Gasnier 	}
2190cdbf481SFabrice Gasnier 	platform_set_drvdata(pdev, rdev);
2200cdbf481SFabrice Gasnier 
221be5295f8SFabrice Gasnier 	pm_runtime_mark_last_busy(&pdev->dev);
222be5295f8SFabrice Gasnier 	pm_runtime_put_autosuspend(&pdev->dev);
223be5295f8SFabrice Gasnier 
2240cdbf481SFabrice Gasnier 	return 0;
2250cdbf481SFabrice Gasnier 
2260cdbf481SFabrice Gasnier err_clk_dis:
2270cdbf481SFabrice Gasnier 	clk_disable_unprepare(priv->clk);
228be5295f8SFabrice Gasnier err_pm_stop:
229be5295f8SFabrice Gasnier 	pm_runtime_disable(&pdev->dev);
230be5295f8SFabrice Gasnier 	pm_runtime_set_suspended(&pdev->dev);
231be5295f8SFabrice Gasnier 	pm_runtime_put_noidle(&pdev->dev);
2320cdbf481SFabrice Gasnier 
2330cdbf481SFabrice Gasnier 	return ret;
2340cdbf481SFabrice Gasnier }
2350cdbf481SFabrice Gasnier 
stm32_vrefbuf_remove(struct platform_device * pdev)236*6f382a0cSUwe Kleine-König static void stm32_vrefbuf_remove(struct platform_device *pdev)
2370cdbf481SFabrice Gasnier {
2380cdbf481SFabrice Gasnier 	struct regulator_dev *rdev = platform_get_drvdata(pdev);
2390cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
2400cdbf481SFabrice Gasnier 
241be5295f8SFabrice Gasnier 	pm_runtime_get_sync(&pdev->dev);
2420cdbf481SFabrice Gasnier 	regulator_unregister(rdev);
2430cdbf481SFabrice Gasnier 	clk_disable_unprepare(priv->clk);
244be5295f8SFabrice Gasnier 	pm_runtime_disable(&pdev->dev);
245be5295f8SFabrice Gasnier 	pm_runtime_set_suspended(&pdev->dev);
246be5295f8SFabrice Gasnier 	pm_runtime_put_noidle(&pdev->dev);
247be5295f8SFabrice Gasnier };
248be5295f8SFabrice Gasnier 
stm32_vrefbuf_runtime_suspend(struct device * dev)249be5295f8SFabrice Gasnier static int __maybe_unused stm32_vrefbuf_runtime_suspend(struct device *dev)
250be5295f8SFabrice Gasnier {
251be5295f8SFabrice Gasnier 	struct regulator_dev *rdev = dev_get_drvdata(dev);
252be5295f8SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
253be5295f8SFabrice Gasnier 
254be5295f8SFabrice Gasnier 	clk_disable_unprepare(priv->clk);
2550cdbf481SFabrice Gasnier 
2560cdbf481SFabrice Gasnier 	return 0;
257be5295f8SFabrice Gasnier }
258be5295f8SFabrice Gasnier 
stm32_vrefbuf_runtime_resume(struct device * dev)259be5295f8SFabrice Gasnier static int __maybe_unused stm32_vrefbuf_runtime_resume(struct device *dev)
260be5295f8SFabrice Gasnier {
261be5295f8SFabrice Gasnier 	struct regulator_dev *rdev = dev_get_drvdata(dev);
262be5295f8SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
263be5295f8SFabrice Gasnier 
264be5295f8SFabrice Gasnier 	return clk_prepare_enable(priv->clk);
265be5295f8SFabrice Gasnier }
266be5295f8SFabrice Gasnier 
267be5295f8SFabrice Gasnier static const struct dev_pm_ops stm32_vrefbuf_pm_ops = {
268be5295f8SFabrice Gasnier 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
269be5295f8SFabrice Gasnier 				pm_runtime_force_resume)
270be5295f8SFabrice Gasnier 	SET_RUNTIME_PM_OPS(stm32_vrefbuf_runtime_suspend,
271be5295f8SFabrice Gasnier 			   stm32_vrefbuf_runtime_resume,
272be5295f8SFabrice Gasnier 			   NULL)
2730cdbf481SFabrice Gasnier };
2740cdbf481SFabrice Gasnier 
275d5579e7aSJisheng Zhang static const struct of_device_id __maybe_unused stm32_vrefbuf_of_match[] = {
2760cdbf481SFabrice Gasnier 	{ .compatible = "st,stm32-vrefbuf", },
2770cdbf481SFabrice Gasnier 	{},
2780cdbf481SFabrice Gasnier };
2790cdbf481SFabrice Gasnier MODULE_DEVICE_TABLE(of, stm32_vrefbuf_of_match);
2800cdbf481SFabrice Gasnier 
2810cdbf481SFabrice Gasnier static struct platform_driver stm32_vrefbuf_driver = {
2820cdbf481SFabrice Gasnier 	.probe = stm32_vrefbuf_probe,
283*6f382a0cSUwe Kleine-König 	.remove_new = stm32_vrefbuf_remove,
2840cdbf481SFabrice Gasnier 	.driver = {
2850cdbf481SFabrice Gasnier 		.name  = "stm32-vrefbuf",
286259b93b2SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
2870cdbf481SFabrice Gasnier 		.of_match_table = of_match_ptr(stm32_vrefbuf_of_match),
288be5295f8SFabrice Gasnier 		.pm = &stm32_vrefbuf_pm_ops,
2890cdbf481SFabrice Gasnier 	},
2900cdbf481SFabrice Gasnier };
2910cdbf481SFabrice Gasnier module_platform_driver(stm32_vrefbuf_driver);
2920cdbf481SFabrice Gasnier 
2930cdbf481SFabrice Gasnier MODULE_LICENSE("GPL v2");
2940cdbf481SFabrice Gasnier MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
2950cdbf481SFabrice Gasnier MODULE_DESCRIPTION("STMicroelectronics STM32 VREFBUF driver");
2960cdbf481SFabrice Gasnier MODULE_ALIAS("platform:stm32-vrefbuf");
297