1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/clk.h>
23#include <linux/delay.h>
24#include <linux/module.h>
25#include <linux/of_platform.h>
26#include <linux/regulator/consumer.h>
27#include <linux/reset.h>
28
29#include "stm32-dac-core.h"
30
31
32
33
34
35
36
37
38struct stm32_dac_priv {
39 struct clk *pclk;
40 struct reset_control *rst;
41 struct regulator *vref;
42 struct stm32_dac_common common;
43};
44
45
46
47
48
49struct stm32_dac_cfg {
50 bool has_hfsel;
51};
52
53static struct stm32_dac_priv *to_stm32_dac_priv(struct stm32_dac_common *com)
54{
55 return container_of(com, struct stm32_dac_priv, common);
56}
57
58static const struct regmap_config stm32_dac_regmap_cfg = {
59 .reg_bits = 32,
60 .val_bits = 32,
61 .reg_stride = sizeof(u32),
62 .max_register = 0x3fc,
63};
64
65static int stm32_dac_probe(struct platform_device *pdev)
66{
67 struct device *dev = &pdev->dev;
68 const struct stm32_dac_cfg *cfg;
69 struct stm32_dac_priv *priv;
70 struct regmap *regmap;
71 struct resource *res;
72 void __iomem *mmio;
73 int ret;
74
75 if (!dev->of_node)
76 return -ENODEV;
77
78 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
79 if (!priv)
80 return -ENOMEM;
81 cfg = (const struct stm32_dac_cfg *)
82 of_match_device(dev->driver->of_match_table, dev)->data;
83
84 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
85 mmio = devm_ioremap_resource(dev, res);
86 if (IS_ERR(mmio))
87 return PTR_ERR(mmio);
88
89 regmap = devm_regmap_init_mmio(dev, mmio, &stm32_dac_regmap_cfg);
90 if (IS_ERR(regmap))
91 return PTR_ERR(regmap);
92 priv->common.regmap = regmap;
93
94 priv->vref = devm_regulator_get(dev, "vref");
95 if (IS_ERR(priv->vref)) {
96 ret = PTR_ERR(priv->vref);
97 dev_err(dev, "vref get failed, %d\n", ret);
98 return ret;
99 }
100
101 ret = regulator_enable(priv->vref);
102 if (ret < 0) {
103 dev_err(dev, "vref enable failed\n");
104 return ret;
105 }
106
107 ret = regulator_get_voltage(priv->vref);
108 if (ret < 0) {
109 dev_err(dev, "vref get voltage failed, %d\n", ret);
110 goto err_vref;
111 }
112 priv->common.vref_mv = ret / 1000;
113 dev_dbg(dev, "vref+=%dmV\n", priv->common.vref_mv);
114
115 priv->pclk = devm_clk_get(dev, "pclk");
116 if (IS_ERR(priv->pclk)) {
117 ret = PTR_ERR(priv->pclk);
118 dev_err(dev, "pclk get failed\n");
119 goto err_vref;
120 }
121
122 ret = clk_prepare_enable(priv->pclk);
123 if (ret < 0) {
124 dev_err(dev, "pclk enable failed\n");
125 goto err_vref;
126 }
127
128 priv->rst = devm_reset_control_get_exclusive(dev, NULL);
129 if (!IS_ERR(priv->rst)) {
130 reset_control_assert(priv->rst);
131 udelay(2);
132 reset_control_deassert(priv->rst);
133 }
134
135 if (cfg && cfg->has_hfsel) {
136
137 priv->common.hfsel = (clk_get_rate(priv->pclk) > 80000000UL);
138 ret = regmap_update_bits(regmap, STM32_DAC_CR,
139 STM32H7_DAC_CR_HFSEL,
140 priv->common.hfsel ?
141 STM32H7_DAC_CR_HFSEL : 0);
142 if (ret)
143 goto err_pclk;
144 }
145
146 platform_set_drvdata(pdev, &priv->common);
147
148 ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, dev);
149 if (ret < 0) {
150 dev_err(dev, "failed to populate DT children\n");
151 goto err_pclk;
152 }
153
154 return 0;
155
156err_pclk:
157 clk_disable_unprepare(priv->pclk);
158err_vref:
159 regulator_disable(priv->vref);
160
161 return ret;
162}
163
164static int stm32_dac_remove(struct platform_device *pdev)
165{
166 struct stm32_dac_common *common = platform_get_drvdata(pdev);
167 struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
168
169 of_platform_depopulate(&pdev->dev);
170 clk_disable_unprepare(priv->pclk);
171 regulator_disable(priv->vref);
172
173 return 0;
174}
175
176static const struct stm32_dac_cfg stm32h7_dac_cfg = {
177 .has_hfsel = true,
178};
179
180static const struct of_device_id stm32_dac_of_match[] = {
181 {
182 .compatible = "st,stm32f4-dac-core",
183 }, {
184 .compatible = "st,stm32h7-dac-core",
185 .data = (void *)&stm32h7_dac_cfg,
186 },
187 {},
188};
189MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
190
191static struct platform_driver stm32_dac_driver = {
192 .probe = stm32_dac_probe,
193 .remove = stm32_dac_remove,
194 .driver = {
195 .name = "stm32-dac-core",
196 .of_match_table = stm32_dac_of_match,
197 },
198};
199module_platform_driver(stm32_dac_driver);
200
201MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
202MODULE_DESCRIPTION("STMicroelectronics STM32 DAC core driver");
203MODULE_LICENSE("GPL v2");
204MODULE_ALIAS("platform:stm32-dac-core");
205