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