blob: 597ab7a5def2c07f908b5da0dd686134fc64a728 [file] [log] [blame]
Fabrice Gasnier1add69882016-11-15 16:30:57 +01001/*
2 * This file is part of STM32 ADC driver
3 *
4 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
5 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
6 *
7 * Inspired from: fsl-imx25-tsadc
8 *
9 * License type: GPLv2
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published by
13 * the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE.
18 * See the GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#include <linux/clk.h>
25#include <linux/interrupt.h>
26#include <linux/irqchip/chained_irq.h>
27#include <linux/irqdesc.h>
28#include <linux/irqdomain.h>
29#include <linux/module.h>
30#include <linux/of_device.h>
31#include <linux/regulator/consumer.h>
32#include <linux/slab.h>
33
34#include "stm32-adc-core.h"
35
36/* STM32F4 - common registers for all ADC instances: 1, 2 & 3 */
37#define STM32F4_ADC_CSR (STM32_ADCX_COMN_OFFSET + 0x00)
38#define STM32F4_ADC_CCR (STM32_ADCX_COMN_OFFSET + 0x04)
39
40/* STM32F4_ADC_CSR - bit fields */
41#define STM32F4_EOC3 BIT(17)
42#define STM32F4_EOC2 BIT(9)
43#define STM32F4_EOC1 BIT(1)
44
45/* STM32F4_ADC_CCR - bit fields */
46#define STM32F4_ADC_ADCPRE_SHIFT 16
47#define STM32F4_ADC_ADCPRE_MASK GENMASK(17, 16)
48
49/* STM32 F4 maximum analog clock rate (from datasheet) */
50#define STM32F4_ADC_MAX_CLK_RATE 36000000
51
52/**
53 * struct stm32_adc_priv - stm32 ADC core private data
54 * @irq: irq for ADC block
55 * @domain: irq domain reference
56 * @aclk: clock reference for the analog circuitry
57 * @vref: regulator reference
58 * @common: common data for all ADC instances
59 */
60struct stm32_adc_priv {
61 int irq;
62 struct irq_domain *domain;
63 struct clk *aclk;
64 struct regulator *vref;
65 struct stm32_adc_common common;
66};
67
68static struct stm32_adc_priv *to_stm32_adc_priv(struct stm32_adc_common *com)
69{
70 return container_of(com, struct stm32_adc_priv, common);
71}
72
73/* STM32F4 ADC internal common clock prescaler division ratios */
74static int stm32f4_pclk_div[] = {2, 4, 6, 8};
75
76/**
77 * stm32f4_adc_clk_sel() - Select stm32f4 ADC common clock prescaler
78 * @priv: stm32 ADC core private data
79 * Select clock prescaler used for analog conversions, before using ADC.
80 */
81static int stm32f4_adc_clk_sel(struct platform_device *pdev,
82 struct stm32_adc_priv *priv)
83{
84 unsigned long rate;
85 u32 val;
86 int i;
87
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +020088 /* stm32f4 has one clk input for analog (mandatory), enforce it here */
89 if (!priv->aclk) {
90 dev_err(&pdev->dev, "No 'adc' clock found\n");
91 return -ENOENT;
92 }
93
Fabrice Gasnier1add69882016-11-15 16:30:57 +010094 rate = clk_get_rate(priv->aclk);
95 for (i = 0; i < ARRAY_SIZE(stm32f4_pclk_div); i++) {
96 if ((rate / stm32f4_pclk_div[i]) <= STM32F4_ADC_MAX_CLK_RATE)
97 break;
98 }
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +020099 if (i >= ARRAY_SIZE(stm32f4_pclk_div)) {
100 dev_err(&pdev->dev, "adc clk selection failed\n");
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100101 return -EINVAL;
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200102 }
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100103
104 val = readl_relaxed(priv->common.base + STM32F4_ADC_CCR);
105 val &= ~STM32F4_ADC_ADCPRE_MASK;
106 val |= i << STM32F4_ADC_ADCPRE_SHIFT;
107 writel_relaxed(val, priv->common.base + STM32F4_ADC_CCR);
108
109 dev_dbg(&pdev->dev, "Using analog clock source at %ld kHz\n",
110 rate / (stm32f4_pclk_div[i] * 1000));
111
112 return 0;
113}
114
115/* ADC common interrupt for all instances */
116static void stm32_adc_irq_handler(struct irq_desc *desc)
117{
118 struct stm32_adc_priv *priv = irq_desc_get_handler_data(desc);
119 struct irq_chip *chip = irq_desc_get_chip(desc);
120 u32 status;
121
122 chained_irq_enter(chip, desc);
123 status = readl_relaxed(priv->common.base + STM32F4_ADC_CSR);
124
125 if (status & STM32F4_EOC1)
126 generic_handle_irq(irq_find_mapping(priv->domain, 0));
127
128 if (status & STM32F4_EOC2)
129 generic_handle_irq(irq_find_mapping(priv->domain, 1));
130
131 if (status & STM32F4_EOC3)
132 generic_handle_irq(irq_find_mapping(priv->domain, 2));
133
134 chained_irq_exit(chip, desc);
135};
136
137static int stm32_adc_domain_map(struct irq_domain *d, unsigned int irq,
138 irq_hw_number_t hwirq)
139{
140 irq_set_chip_data(irq, d->host_data);
141 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_level_irq);
142
143 return 0;
144}
145
146static void stm32_adc_domain_unmap(struct irq_domain *d, unsigned int irq)
147{
148 irq_set_chip_and_handler(irq, NULL, NULL);
149 irq_set_chip_data(irq, NULL);
150}
151
152static const struct irq_domain_ops stm32_adc_domain_ops = {
153 .map = stm32_adc_domain_map,
154 .unmap = stm32_adc_domain_unmap,
155 .xlate = irq_domain_xlate_onecell,
156};
157
158static int stm32_adc_irq_probe(struct platform_device *pdev,
159 struct stm32_adc_priv *priv)
160{
161 struct device_node *np = pdev->dev.of_node;
162
163 priv->irq = platform_get_irq(pdev, 0);
164 if (priv->irq < 0) {
165 dev_err(&pdev->dev, "failed to get irq\n");
166 return priv->irq;
167 }
168
169 priv->domain = irq_domain_add_simple(np, STM32_ADC_MAX_ADCS, 0,
170 &stm32_adc_domain_ops,
171 priv);
172 if (!priv->domain) {
173 dev_err(&pdev->dev, "Failed to add irq domain\n");
174 return -ENOMEM;
175 }
176
177 irq_set_chained_handler(priv->irq, stm32_adc_irq_handler);
178 irq_set_handler_data(priv->irq, priv);
179
180 return 0;
181}
182
183static void stm32_adc_irq_remove(struct platform_device *pdev,
184 struct stm32_adc_priv *priv)
185{
186 int hwirq;
187
188 for (hwirq = 0; hwirq < STM32_ADC_MAX_ADCS; hwirq++)
189 irq_dispose_mapping(irq_find_mapping(priv->domain, hwirq));
190 irq_domain_remove(priv->domain);
191 irq_set_chained_handler(priv->irq, NULL);
192}
193
194static int stm32_adc_probe(struct platform_device *pdev)
195{
196 struct stm32_adc_priv *priv;
197 struct device_node *np = pdev->dev.of_node;
198 struct resource *res;
199 int ret;
200
201 if (!pdev->dev.of_node)
202 return -ENODEV;
203
204 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
205 if (!priv)
206 return -ENOMEM;
207
208 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
209 priv->common.base = devm_ioremap_resource(&pdev->dev, res);
210 if (IS_ERR(priv->common.base))
211 return PTR_ERR(priv->common.base);
Fabrice Gasnier2763ea02017-01-26 15:28:33 +0100212 priv->common.phys_base = res->start;
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100213
214 priv->vref = devm_regulator_get(&pdev->dev, "vref");
215 if (IS_ERR(priv->vref)) {
216 ret = PTR_ERR(priv->vref);
217 dev_err(&pdev->dev, "vref get failed, %d\n", ret);
218 return ret;
219 }
220
221 ret = regulator_enable(priv->vref);
222 if (ret < 0) {
223 dev_err(&pdev->dev, "vref enable failed\n");
224 return ret;
225 }
226
227 ret = regulator_get_voltage(priv->vref);
228 if (ret < 0) {
229 dev_err(&pdev->dev, "vref get voltage failed, %d\n", ret);
230 goto err_regulator_disable;
231 }
232 priv->common.vref_mv = ret / 1000;
233 dev_dbg(&pdev->dev, "vref+=%dmV\n", priv->common.vref_mv);
234
235 priv->aclk = devm_clk_get(&pdev->dev, "adc");
236 if (IS_ERR(priv->aclk)) {
237 ret = PTR_ERR(priv->aclk);
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200238 if (ret == -ENOENT) {
239 priv->aclk = NULL;
240 } else {
241 dev_err(&pdev->dev, "Can't get 'adc' clock\n");
242 goto err_regulator_disable;
243 }
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100244 }
245
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200246 if (priv->aclk) {
247 ret = clk_prepare_enable(priv->aclk);
248 if (ret < 0) {
249 dev_err(&pdev->dev, "adc clk enable failed\n");
250 goto err_regulator_disable;
251 }
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100252 }
253
254 ret = stm32f4_adc_clk_sel(pdev, priv);
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200255 if (ret < 0)
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100256 goto err_clk_disable;
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100257
258 ret = stm32_adc_irq_probe(pdev, priv);
259 if (ret < 0)
260 goto err_clk_disable;
261
262 platform_set_drvdata(pdev, &priv->common);
263
264 ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
265 if (ret < 0) {
266 dev_err(&pdev->dev, "failed to populate DT children\n");
267 goto err_irq_remove;
268 }
269
270 return 0;
271
272err_irq_remove:
273 stm32_adc_irq_remove(pdev, priv);
274
275err_clk_disable:
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200276 if (priv->aclk)
277 clk_disable_unprepare(priv->aclk);
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100278
279err_regulator_disable:
280 regulator_disable(priv->vref);
281
282 return ret;
283}
284
285static int stm32_adc_remove(struct platform_device *pdev)
286{
287 struct stm32_adc_common *common = platform_get_drvdata(pdev);
288 struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
289
290 of_platform_depopulate(&pdev->dev);
291 stm32_adc_irq_remove(pdev, priv);
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200292 if (priv->aclk)
293 clk_disable_unprepare(priv->aclk);
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100294 regulator_disable(priv->vref);
295
296 return 0;
297}
298
299static const struct of_device_id stm32_adc_of_match[] = {
300 { .compatible = "st,stm32f4-adc-core" },
301 {},
302};
303MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
304
305static struct platform_driver stm32_adc_driver = {
306 .probe = stm32_adc_probe,
307 .remove = stm32_adc_remove,
308 .driver = {
309 .name = "stm32-adc-core",
310 .of_match_table = stm32_adc_of_match,
311 },
312};
313module_platform_driver(stm32_adc_driver);
314
315MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
316MODULE_DESCRIPTION("STMicroelectronics STM32 ADC core driver");
317MODULE_LICENSE("GPL v2");
318MODULE_ALIAS("platform:stm32-adc-core");