blob: 250773514f5825184560a78cb187ff06f18c73a8 [file] [log] [blame]
Fabrice Gasnier95c868e2018-04-26 17:00:46 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
5 *
6 * Originally based on the Linux kernel v4.16 drivers/regulator/stm32-vrefbuf.c
7 */
8
9#include <common.h>
10#include <clk.h>
11#include <dm.h>
12#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060014#include <linux/bitops.h>
Fabrice Gasnier95c868e2018-04-26 17:00:46 +020015#include <linux/iopoll.h>
16#include <linux/kernel.h>
17#include <power/regulator.h>
18
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_VRS_SHIFT 4
25#define STM32_VRR BIT(3)
26#define STM32_HIZ BIT(1)
27#define STM32_ENVR BIT(0)
28
29struct stm32_vrefbuf {
30 void __iomem *base;
31 struct clk clk;
32 struct udevice *vdda_supply;
33};
34
Patrick Delaunay5a10c752019-06-21 15:26:49 +020035static const int stm32_vrefbuf_voltages[] = {
Fabrice Gasnier95c868e2018-04-26 17:00:46 +020036 /* Matches resp. VRS = 000b, 001b, 010b, 011b */
37 2500000, 2048000, 1800000, 1500000,
38};
39
40static int stm32_vrefbuf_set_enable(struct udevice *dev, bool enable)
41{
42 struct stm32_vrefbuf *priv = dev_get_priv(dev);
43 u32 val;
44 int ret;
45
46 clrsetbits_le32(priv->base + STM32_VREFBUF_CSR, STM32_HIZ | STM32_ENVR,
47 enable ? STM32_ENVR : STM32_HIZ);
48 if (!enable)
49 return 0;
50
51 /*
52 * Vrefbuf startup time depends on external capacitor: wait here for
53 * VRR to be set. That means output has reached expected value.
54 * ~650us sleep should be enough for caps up to 1.5uF. Use 10ms as
55 * arbitrary timeout.
56 */
57 ret = readl_poll_timeout(priv->base + STM32_VREFBUF_CSR, val,
58 val & STM32_VRR, 10000);
59 if (ret < 0) {
60 dev_err(dev, "stm32 vrefbuf timed out: %d\n", ret);
61 clrsetbits_le32(priv->base + STM32_VREFBUF_CSR, STM32_ENVR,
62 STM32_HIZ);
63 return ret;
64 }
65
66 return 0;
67}
68
69static int stm32_vrefbuf_get_enable(struct udevice *dev)
70{
71 struct stm32_vrefbuf *priv = dev_get_priv(dev);
72
73 return readl(priv->base + STM32_VREFBUF_CSR) & STM32_ENVR;
74}
75
76static int stm32_vrefbuf_set_value(struct udevice *dev, int uV)
77{
78 struct stm32_vrefbuf *priv = dev_get_priv(dev);
79 unsigned int i;
80
81 for (i = 0; i < ARRAY_SIZE(stm32_vrefbuf_voltages); i++) {
82 if (uV == stm32_vrefbuf_voltages[i]) {
83 clrsetbits_le32(priv->base + STM32_VREFBUF_CSR,
84 STM32_VRS, i << STM32_VRS_SHIFT);
85 return 0;
86 }
87 }
88
89 return -EINVAL;
90}
91
92static int stm32_vrefbuf_get_value(struct udevice *dev)
93{
94 struct stm32_vrefbuf *priv = dev_get_priv(dev);
95 u32 val;
96
97 val = readl(priv->base + STM32_VREFBUF_CSR) & STM32_VRS;
98 val >>= STM32_VRS_SHIFT;
99
100 return stm32_vrefbuf_voltages[val];
101}
102
103static const struct dm_regulator_ops stm32_vrefbuf_ops = {
104 .get_value = stm32_vrefbuf_get_value,
105 .set_value = stm32_vrefbuf_set_value,
106 .get_enable = stm32_vrefbuf_get_enable,
107 .set_enable = stm32_vrefbuf_set_enable,
108};
109
110static int stm32_vrefbuf_probe(struct udevice *dev)
111{
112 struct stm32_vrefbuf *priv = dev_get_priv(dev);
113 int ret;
114
115 priv->base = dev_read_addr_ptr(dev);
116
117 ret = clk_get_by_index(dev, 0, &priv->clk);
118 if (ret) {
119 dev_err(dev, "Can't get clock: %d\n", ret);
120 return ret;
121 }
122
123 ret = clk_enable(&priv->clk);
124 if (ret) {
125 dev_err(dev, "Can't enable clock: %d\n", ret);
126 return ret;
127 }
128
129 ret = device_get_supply_regulator(dev, "vdda-supply",
130 &priv->vdda_supply);
131 if (ret) {
132 dev_dbg(dev, "No vdda-supply: %d\n", ret);
133 return 0;
134 }
135
136 ret = regulator_set_enable(priv->vdda_supply, true);
137 if (ret) {
138 dev_err(dev, "Can't enable vdda-supply: %d\n", ret);
139 clk_disable(&priv->clk);
140 }
141
142 return ret;
143}
144
145static const struct udevice_id stm32_vrefbuf_ids[] = {
146 { .compatible = "st,stm32-vrefbuf" },
147 { }
148};
149
150U_BOOT_DRIVER(stm32_vrefbuf) = {
151 .name = "stm32-vrefbuf",
152 .id = UCLASS_REGULATOR,
153 .of_match = stm32_vrefbuf_ids,
154 .probe = stm32_vrefbuf_probe,
155 .ops = &stm32_vrefbuf_ops,
156 .priv_auto_alloc_size = sizeof(struct stm32_vrefbuf),
157};