1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
6 * Originally based on the Linux kernel v4.16 drivers/regulator/stm32-vrefbuf.c
13 #include <dm/device_compat.h>
14 #include <linux/iopoll.h>
15 #include <linux/kernel.h>
16 #include <power/regulator.h>
18 /* STM32 VREFBUF registers */
19 #define STM32_VREFBUF_CSR 0x00
21 /* STM32 VREFBUF CSR bitfields */
22 #define STM32_VRS GENMASK(6, 4)
23 #define STM32_VRS_SHIFT 4
24 #define STM32_VRR BIT(3)
25 #define STM32_HIZ BIT(1)
26 #define STM32_ENVR BIT(0)
28 struct stm32_vrefbuf {
31 struct udevice *vdda_supply;
34 static const int stm32_vrefbuf_voltages[] = {
35 /* Matches resp. VRS = 000b, 001b, 010b, 011b */
36 2500000, 2048000, 1800000, 1500000,
39 static int stm32_vrefbuf_set_enable(struct udevice *dev, bool enable)
41 struct stm32_vrefbuf *priv = dev_get_priv(dev);
45 clrsetbits_le32(priv->base + STM32_VREFBUF_CSR, STM32_HIZ | STM32_ENVR,
46 enable ? STM32_ENVR : STM32_HIZ);
51 * Vrefbuf startup time depends on external capacitor: wait here for
52 * VRR to be set. That means output has reached expected value.
53 * ~650us sleep should be enough for caps up to 1.5uF. Use 10ms as
56 ret = readl_poll_timeout(priv->base + STM32_VREFBUF_CSR, val,
57 val & STM32_VRR, 10000);
59 dev_err(dev, "stm32 vrefbuf timed out: %d\n", ret);
60 clrsetbits_le32(priv->base + STM32_VREFBUF_CSR, STM32_ENVR,
68 static int stm32_vrefbuf_get_enable(struct udevice *dev)
70 struct stm32_vrefbuf *priv = dev_get_priv(dev);
72 return readl(priv->base + STM32_VREFBUF_CSR) & STM32_ENVR;
75 static int stm32_vrefbuf_set_value(struct udevice *dev, int uV)
77 struct stm32_vrefbuf *priv = dev_get_priv(dev);
80 for (i = 0; i < ARRAY_SIZE(stm32_vrefbuf_voltages); i++) {
81 if (uV == stm32_vrefbuf_voltages[i]) {
82 clrsetbits_le32(priv->base + STM32_VREFBUF_CSR,
83 STM32_VRS, i << STM32_VRS_SHIFT);
91 static int stm32_vrefbuf_get_value(struct udevice *dev)
93 struct stm32_vrefbuf *priv = dev_get_priv(dev);
96 val = readl(priv->base + STM32_VREFBUF_CSR) & STM32_VRS;
97 val >>= STM32_VRS_SHIFT;
99 return stm32_vrefbuf_voltages[val];
102 static const struct dm_regulator_ops stm32_vrefbuf_ops = {
103 .get_value = stm32_vrefbuf_get_value,
104 .set_value = stm32_vrefbuf_set_value,
105 .get_enable = stm32_vrefbuf_get_enable,
106 .set_enable = stm32_vrefbuf_set_enable,
109 static int stm32_vrefbuf_probe(struct udevice *dev)
111 struct stm32_vrefbuf *priv = dev_get_priv(dev);
114 priv->base = dev_read_addr_ptr(dev);
116 ret = clk_get_by_index(dev, 0, &priv->clk);
118 dev_err(dev, "Can't get clock: %d\n", ret);
122 ret = clk_enable(&priv->clk);
124 dev_err(dev, "Can't enable clock: %d\n", ret);
128 ret = device_get_supply_regulator(dev, "vdda-supply",
131 dev_dbg(dev, "No vdda-supply: %d\n", ret);
135 ret = regulator_set_enable(priv->vdda_supply, true);
137 dev_err(dev, "Can't enable vdda-supply: %d\n", ret);
138 clk_disable(&priv->clk);
144 static const struct udevice_id stm32_vrefbuf_ids[] = {
145 { .compatible = "st,stm32-vrefbuf" },
149 U_BOOT_DRIVER(stm32_vrefbuf) = {
150 .name = "stm32-vrefbuf",
151 .id = UCLASS_REGULATOR,
152 .of_match = stm32_vrefbuf_ids,
153 .probe = stm32_vrefbuf_probe,
154 .ops = &stm32_vrefbuf_ops,
155 .priv_auto_alloc_size = sizeof(struct stm32_vrefbuf),