Linux-libre 4.14.14-gnu
[librecmc/linux-libre.git] / drivers / iio / dac / stm32-dac-core.c
1 /*
2  * This file is part of STM32 DAC driver
3  *
4  * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
5  * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
6  *
7  * License type: GPLv2
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License version 2 as published by
11  * the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  * or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program. If not, see <http://www.gnu.org/licenses/>.
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  * struct stm32_dac_priv - stm32 DAC core private data
33  * @pclk:               peripheral clock common for all DACs
34  * @rst:                peripheral reset control
35  * @vref:               regulator reference
36  * @common:             Common data for all DAC instances
37  */
38 struct 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  * struct stm32_dac_cfg - DAC configuration
47  * @has_hfsel: DAC has high frequency control
48  */
49 struct stm32_dac_cfg {
50         bool has_hfsel;
51 };
52
53 static 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
58 static 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
65 static 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                 /* When clock speed is higher than 80MHz, set HFSEL */
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
156 err_pclk:
157         clk_disable_unprepare(priv->pclk);
158 err_vref:
159         regulator_disable(priv->vref);
160
161         return ret;
162 }
163
164 static 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
176 static const struct stm32_dac_cfg stm32h7_dac_cfg = {
177         .has_hfsel = true,
178 };
179
180 static 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 };
189 MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
190
191 static 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 };
199 module_platform_driver(stm32_dac_driver);
200
201 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
202 MODULE_DESCRIPTION("STMicroelectronics STM32 DAC core driver");
203 MODULE_LICENSE("GPL v2");
204 MODULE_ALIAS("platform:stm32-dac-core");