xref: /linux/drivers/regulator/stm32-vrefbuf.c (revision f63248fac563125fd5a2f0bc780ce7a299872cab)
10cdbf481SFabrice Gasnier /*
20cdbf481SFabrice Gasnier  * Copyright (C) STMicroelectronics 2017
30cdbf481SFabrice Gasnier  *
40cdbf481SFabrice Gasnier  * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
50cdbf481SFabrice Gasnier  *
60cdbf481SFabrice Gasnier  * License terms:  GNU General Public License (GPL), version 2
70cdbf481SFabrice Gasnier  */
80cdbf481SFabrice Gasnier 
90cdbf481SFabrice Gasnier #include <linux/bitfield.h>
100cdbf481SFabrice Gasnier #include <linux/clk.h>
110cdbf481SFabrice Gasnier #include <linux/io.h>
120cdbf481SFabrice Gasnier #include <linux/iopoll.h>
130cdbf481SFabrice Gasnier #include <linux/module.h>
140cdbf481SFabrice Gasnier #include <linux/of_device.h>
150cdbf481SFabrice Gasnier #include <linux/platform_device.h>
160cdbf481SFabrice Gasnier #include <linux/regulator/driver.h>
170cdbf481SFabrice Gasnier #include <linux/regulator/of_regulator.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 
280cdbf481SFabrice Gasnier struct stm32_vrefbuf {
290cdbf481SFabrice Gasnier 	void __iomem *base;
300cdbf481SFabrice Gasnier 	struct clk *clk;
310cdbf481SFabrice Gasnier };
320cdbf481SFabrice Gasnier 
330cdbf481SFabrice Gasnier static const unsigned int stm32_vrefbuf_voltages[] = {
340cdbf481SFabrice Gasnier 	/* Matches resp. VRS = 000b, 001b, 010b, 011b */
350cdbf481SFabrice Gasnier 	2500000, 2048000, 1800000, 1500000,
360cdbf481SFabrice Gasnier };
370cdbf481SFabrice Gasnier 
380cdbf481SFabrice Gasnier static int stm32_vrefbuf_enable(struct regulator_dev *rdev)
390cdbf481SFabrice Gasnier {
400cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
410cdbf481SFabrice Gasnier 	u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
420cdbf481SFabrice Gasnier 	int ret;
430cdbf481SFabrice Gasnier 
440cdbf481SFabrice Gasnier 	val = (val & ~STM32_HIZ) | STM32_ENVR;
450cdbf481SFabrice Gasnier 	writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
460cdbf481SFabrice Gasnier 
470cdbf481SFabrice Gasnier 	/*
480cdbf481SFabrice Gasnier 	 * Vrefbuf startup time depends on external capacitor: wait here for
490cdbf481SFabrice Gasnier 	 * VRR to be set. That means output has reached expected value.
500cdbf481SFabrice Gasnier 	 * ~650us sleep should be enough for caps up to 1.5uF. Use 10ms as
510cdbf481SFabrice Gasnier 	 * arbitrary timeout.
520cdbf481SFabrice Gasnier 	 */
530cdbf481SFabrice Gasnier 	ret = readl_poll_timeout(priv->base + STM32_VREFBUF_CSR, val,
54*f63248faSFabrice Gasnier 				 val & STM32_VRR, 650, 10000);
550cdbf481SFabrice Gasnier 	if (ret) {
560cdbf481SFabrice Gasnier 		dev_err(&rdev->dev, "stm32 vrefbuf timed out!\n");
570cdbf481SFabrice Gasnier 		val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
580cdbf481SFabrice Gasnier 		val = (val & ~STM32_ENVR) | STM32_HIZ;
590cdbf481SFabrice Gasnier 		writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
600cdbf481SFabrice Gasnier 	}
610cdbf481SFabrice Gasnier 
620cdbf481SFabrice Gasnier 	return ret;
630cdbf481SFabrice Gasnier }
640cdbf481SFabrice Gasnier 
650cdbf481SFabrice Gasnier static int stm32_vrefbuf_disable(struct regulator_dev *rdev)
660cdbf481SFabrice Gasnier {
670cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
680cdbf481SFabrice Gasnier 	u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
690cdbf481SFabrice Gasnier 
700cdbf481SFabrice Gasnier 	val = (val & ~STM32_ENVR) | STM32_HIZ;
710cdbf481SFabrice Gasnier 	writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
720cdbf481SFabrice Gasnier 
730cdbf481SFabrice Gasnier 	return 0;
740cdbf481SFabrice Gasnier }
750cdbf481SFabrice Gasnier 
760cdbf481SFabrice Gasnier static int stm32_vrefbuf_is_enabled(struct regulator_dev *rdev)
770cdbf481SFabrice Gasnier {
780cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
790cdbf481SFabrice Gasnier 
800cdbf481SFabrice Gasnier 	return readl_relaxed(priv->base + STM32_VREFBUF_CSR) & STM32_ENVR;
810cdbf481SFabrice Gasnier }
820cdbf481SFabrice Gasnier 
830cdbf481SFabrice Gasnier static int stm32_vrefbuf_set_voltage_sel(struct regulator_dev *rdev,
840cdbf481SFabrice Gasnier 					 unsigned sel)
850cdbf481SFabrice Gasnier {
860cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
870cdbf481SFabrice Gasnier 	u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
880cdbf481SFabrice Gasnier 
890cdbf481SFabrice Gasnier 	val = (val & ~STM32_VRS) | FIELD_PREP(STM32_VRS, sel);
900cdbf481SFabrice Gasnier 	writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
910cdbf481SFabrice Gasnier 
920cdbf481SFabrice Gasnier 	return 0;
930cdbf481SFabrice Gasnier }
940cdbf481SFabrice Gasnier 
950cdbf481SFabrice Gasnier static int stm32_vrefbuf_get_voltage_sel(struct regulator_dev *rdev)
960cdbf481SFabrice Gasnier {
970cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
980cdbf481SFabrice Gasnier 	u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
990cdbf481SFabrice Gasnier 
1000cdbf481SFabrice Gasnier 	return FIELD_GET(STM32_VRS, val);
1010cdbf481SFabrice Gasnier }
1020cdbf481SFabrice Gasnier 
1030cdbf481SFabrice Gasnier static const struct regulator_ops stm32_vrefbuf_volt_ops = {
1040cdbf481SFabrice Gasnier 	.enable		= stm32_vrefbuf_enable,
1050cdbf481SFabrice Gasnier 	.disable	= stm32_vrefbuf_disable,
1060cdbf481SFabrice Gasnier 	.is_enabled	= stm32_vrefbuf_is_enabled,
1070cdbf481SFabrice Gasnier 	.get_voltage_sel = stm32_vrefbuf_get_voltage_sel,
1080cdbf481SFabrice Gasnier 	.set_voltage_sel = stm32_vrefbuf_set_voltage_sel,
1090cdbf481SFabrice Gasnier 	.list_voltage	= regulator_list_voltage_table,
1100cdbf481SFabrice Gasnier };
1110cdbf481SFabrice Gasnier 
1120cdbf481SFabrice Gasnier static const struct regulator_desc stm32_vrefbuf_regu = {
1130cdbf481SFabrice Gasnier 	.name = "vref",
1140cdbf481SFabrice Gasnier 	.supply_name = "vdda",
1150cdbf481SFabrice Gasnier 	.volt_table = stm32_vrefbuf_voltages,
1160cdbf481SFabrice Gasnier 	.n_voltages = ARRAY_SIZE(stm32_vrefbuf_voltages),
1170cdbf481SFabrice Gasnier 	.ops = &stm32_vrefbuf_volt_ops,
1180cdbf481SFabrice Gasnier 	.type = REGULATOR_VOLTAGE,
1190cdbf481SFabrice Gasnier 	.owner = THIS_MODULE,
1200cdbf481SFabrice Gasnier };
1210cdbf481SFabrice Gasnier 
1220cdbf481SFabrice Gasnier static int stm32_vrefbuf_probe(struct platform_device *pdev)
1230cdbf481SFabrice Gasnier {
1240cdbf481SFabrice Gasnier 	struct resource *res;
1250cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv;
1260cdbf481SFabrice Gasnier 	struct regulator_config config = { };
1270cdbf481SFabrice Gasnier 	struct regulator_dev *rdev;
1280cdbf481SFabrice Gasnier 	int ret;
1290cdbf481SFabrice Gasnier 
1300cdbf481SFabrice Gasnier 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1310cdbf481SFabrice Gasnier 	if (!priv)
1320cdbf481SFabrice Gasnier 		return -ENOMEM;
1330cdbf481SFabrice Gasnier 
1340cdbf481SFabrice Gasnier 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1350cdbf481SFabrice Gasnier 	priv->base = devm_ioremap_resource(&pdev->dev, res);
1360cdbf481SFabrice Gasnier 	if (IS_ERR(priv->base))
1370cdbf481SFabrice Gasnier 		return PTR_ERR(priv->base);
1380cdbf481SFabrice Gasnier 
1390cdbf481SFabrice Gasnier 	priv->clk = devm_clk_get(&pdev->dev, NULL);
1400cdbf481SFabrice Gasnier 	if (IS_ERR(priv->clk))
1410cdbf481SFabrice Gasnier 		return PTR_ERR(priv->clk);
1420cdbf481SFabrice Gasnier 
1430cdbf481SFabrice Gasnier 	ret = clk_prepare_enable(priv->clk);
1440cdbf481SFabrice Gasnier 	if (ret) {
1450cdbf481SFabrice Gasnier 		dev_err(&pdev->dev, "clk prepare failed with error %d\n", ret);
1460cdbf481SFabrice Gasnier 		return ret;
1470cdbf481SFabrice Gasnier 	}
1480cdbf481SFabrice Gasnier 
1490cdbf481SFabrice Gasnier 	config.dev = &pdev->dev;
1500cdbf481SFabrice Gasnier 	config.driver_data = priv;
1510cdbf481SFabrice Gasnier 	config.of_node = pdev->dev.of_node;
1520cdbf481SFabrice Gasnier 	config.init_data = of_get_regulator_init_data(&pdev->dev,
1530cdbf481SFabrice Gasnier 						      pdev->dev.of_node,
1540cdbf481SFabrice Gasnier 						      &stm32_vrefbuf_regu);
1550cdbf481SFabrice Gasnier 
1560cdbf481SFabrice Gasnier 	rdev = regulator_register(&stm32_vrefbuf_regu, &config);
1570cdbf481SFabrice Gasnier 	if (IS_ERR(rdev)) {
1580cdbf481SFabrice Gasnier 		ret = PTR_ERR(rdev);
1590cdbf481SFabrice Gasnier 		dev_err(&pdev->dev, "register failed with error %d\n", ret);
1600cdbf481SFabrice Gasnier 		goto err_clk_dis;
1610cdbf481SFabrice Gasnier 	}
1620cdbf481SFabrice Gasnier 	platform_set_drvdata(pdev, rdev);
1630cdbf481SFabrice Gasnier 
1640cdbf481SFabrice Gasnier 	return 0;
1650cdbf481SFabrice Gasnier 
1660cdbf481SFabrice Gasnier err_clk_dis:
1670cdbf481SFabrice Gasnier 	clk_disable_unprepare(priv->clk);
1680cdbf481SFabrice Gasnier 
1690cdbf481SFabrice Gasnier 	return ret;
1700cdbf481SFabrice Gasnier }
1710cdbf481SFabrice Gasnier 
1720cdbf481SFabrice Gasnier static int stm32_vrefbuf_remove(struct platform_device *pdev)
1730cdbf481SFabrice Gasnier {
1740cdbf481SFabrice Gasnier 	struct regulator_dev *rdev = platform_get_drvdata(pdev);
1750cdbf481SFabrice Gasnier 	struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
1760cdbf481SFabrice Gasnier 
1770cdbf481SFabrice Gasnier 	regulator_unregister(rdev);
1780cdbf481SFabrice Gasnier 	clk_disable_unprepare(priv->clk);
1790cdbf481SFabrice Gasnier 
1800cdbf481SFabrice Gasnier 	return 0;
1810cdbf481SFabrice Gasnier };
1820cdbf481SFabrice Gasnier 
1830cdbf481SFabrice Gasnier static const struct of_device_id stm32_vrefbuf_of_match[] = {
1840cdbf481SFabrice Gasnier 	{ .compatible = "st,stm32-vrefbuf", },
1850cdbf481SFabrice Gasnier 	{},
1860cdbf481SFabrice Gasnier };
1870cdbf481SFabrice Gasnier MODULE_DEVICE_TABLE(of, stm32_vrefbuf_of_match);
1880cdbf481SFabrice Gasnier 
1890cdbf481SFabrice Gasnier static struct platform_driver stm32_vrefbuf_driver = {
1900cdbf481SFabrice Gasnier 	.probe = stm32_vrefbuf_probe,
1910cdbf481SFabrice Gasnier 	.remove = stm32_vrefbuf_remove,
1920cdbf481SFabrice Gasnier 	.driver = {
1930cdbf481SFabrice Gasnier 		.name  = "stm32-vrefbuf",
1940cdbf481SFabrice Gasnier 		.of_match_table = of_match_ptr(stm32_vrefbuf_of_match),
1950cdbf481SFabrice Gasnier 	},
1960cdbf481SFabrice Gasnier };
1970cdbf481SFabrice Gasnier module_platform_driver(stm32_vrefbuf_driver);
1980cdbf481SFabrice Gasnier 
1990cdbf481SFabrice Gasnier MODULE_LICENSE("GPL v2");
2000cdbf481SFabrice Gasnier MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
2010cdbf481SFabrice Gasnier MODULE_DESCRIPTION("STMicroelectronics STM32 VREFBUF driver");
2020cdbf481SFabrice Gasnier MODULE_ALIAS("platform:stm32-vrefbuf");
203