blob: c5d292ccc7634c2be342482a49a57d81d0d6e4ff [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/**
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +020053 * stm32_adc_common_regs - stm32 common registers, compatible dependent data
54 * @csr: common status register offset
55 * @eoc1: adc1 end of conversion flag in @csr
56 * @eoc2: adc2 end of conversion flag in @csr
57 * @eoc3: adc3 end of conversion flag in @csr
58 */
59struct stm32_adc_common_regs {
60 u32 csr;
61 u32 eoc1_msk;
62 u32 eoc2_msk;
63 u32 eoc3_msk;
64};
65
66struct stm32_adc_priv;
67
68/**
69 * stm32_adc_priv_cfg - stm32 core compatible configuration data
70 * @regs: common registers for all instances
71 * @clk_sel: clock selection routine
72 */
73struct stm32_adc_priv_cfg {
74 const struct stm32_adc_common_regs *regs;
75 int (*clk_sel)(struct platform_device *, struct stm32_adc_priv *);
76};
77
78/**
Fabrice Gasnier1add69882016-11-15 16:30:57 +010079 * struct stm32_adc_priv - stm32 ADC core private data
80 * @irq: irq for ADC block
81 * @domain: irq domain reference
82 * @aclk: clock reference for the analog circuitry
83 * @vref: regulator reference
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +020084 * @cfg: compatible configuration data
Fabrice Gasnier1add69882016-11-15 16:30:57 +010085 * @common: common data for all ADC instances
86 */
87struct stm32_adc_priv {
88 int irq;
89 struct irq_domain *domain;
90 struct clk *aclk;
91 struct regulator *vref;
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +020092 const struct stm32_adc_priv_cfg *cfg;
Fabrice Gasnier1add69882016-11-15 16:30:57 +010093 struct stm32_adc_common common;
94};
95
96static struct stm32_adc_priv *to_stm32_adc_priv(struct stm32_adc_common *com)
97{
98 return container_of(com, struct stm32_adc_priv, common);
99}
100
101/* STM32F4 ADC internal common clock prescaler division ratios */
102static int stm32f4_pclk_div[] = {2, 4, 6, 8};
103
104/**
105 * stm32f4_adc_clk_sel() - Select stm32f4 ADC common clock prescaler
106 * @priv: stm32 ADC core private data
107 * Select clock prescaler used for analog conversions, before using ADC.
108 */
109static int stm32f4_adc_clk_sel(struct platform_device *pdev,
110 struct stm32_adc_priv *priv)
111{
112 unsigned long rate;
113 u32 val;
114 int i;
115
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200116 /* stm32f4 has one clk input for analog (mandatory), enforce it here */
117 if (!priv->aclk) {
118 dev_err(&pdev->dev, "No 'adc' clock found\n");
119 return -ENOENT;
120 }
121
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100122 rate = clk_get_rate(priv->aclk);
123 for (i = 0; i < ARRAY_SIZE(stm32f4_pclk_div); i++) {
124 if ((rate / stm32f4_pclk_div[i]) <= STM32F4_ADC_MAX_CLK_RATE)
125 break;
126 }
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200127 if (i >= ARRAY_SIZE(stm32f4_pclk_div)) {
128 dev_err(&pdev->dev, "adc clk selection failed\n");
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100129 return -EINVAL;
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200130 }
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100131
132 val = readl_relaxed(priv->common.base + STM32F4_ADC_CCR);
133 val &= ~STM32F4_ADC_ADCPRE_MASK;
134 val |= i << STM32F4_ADC_ADCPRE_SHIFT;
135 writel_relaxed(val, priv->common.base + STM32F4_ADC_CCR);
136
137 dev_dbg(&pdev->dev, "Using analog clock source at %ld kHz\n",
138 rate / (stm32f4_pclk_div[i] * 1000));
139
140 return 0;
141}
142
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +0200143/* STM32F4 common registers definitions */
144static const struct stm32_adc_common_regs stm32f4_adc_common_regs = {
145 .csr = STM32F4_ADC_CSR,
146 .eoc1_msk = STM32F4_EOC1,
147 .eoc2_msk = STM32F4_EOC2,
148 .eoc3_msk = STM32F4_EOC3,
149};
150
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100151/* ADC common interrupt for all instances */
152static void stm32_adc_irq_handler(struct irq_desc *desc)
153{
154 struct stm32_adc_priv *priv = irq_desc_get_handler_data(desc);
155 struct irq_chip *chip = irq_desc_get_chip(desc);
156 u32 status;
157
158 chained_irq_enter(chip, desc);
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +0200159 status = readl_relaxed(priv->common.base + priv->cfg->regs->csr);
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100160
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +0200161 if (status & priv->cfg->regs->eoc1_msk)
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100162 generic_handle_irq(irq_find_mapping(priv->domain, 0));
163
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +0200164 if (status & priv->cfg->regs->eoc2_msk)
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100165 generic_handle_irq(irq_find_mapping(priv->domain, 1));
166
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +0200167 if (status & priv->cfg->regs->eoc3_msk)
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100168 generic_handle_irq(irq_find_mapping(priv->domain, 2));
169
170 chained_irq_exit(chip, desc);
171};
172
173static int stm32_adc_domain_map(struct irq_domain *d, unsigned int irq,
174 irq_hw_number_t hwirq)
175{
176 irq_set_chip_data(irq, d->host_data);
177 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_level_irq);
178
179 return 0;
180}
181
182static void stm32_adc_domain_unmap(struct irq_domain *d, unsigned int irq)
183{
184 irq_set_chip_and_handler(irq, NULL, NULL);
185 irq_set_chip_data(irq, NULL);
186}
187
188static const struct irq_domain_ops stm32_adc_domain_ops = {
189 .map = stm32_adc_domain_map,
190 .unmap = stm32_adc_domain_unmap,
191 .xlate = irq_domain_xlate_onecell,
192};
193
194static int stm32_adc_irq_probe(struct platform_device *pdev,
195 struct stm32_adc_priv *priv)
196{
197 struct device_node *np = pdev->dev.of_node;
198
199 priv->irq = platform_get_irq(pdev, 0);
200 if (priv->irq < 0) {
201 dev_err(&pdev->dev, "failed to get irq\n");
202 return priv->irq;
203 }
204
205 priv->domain = irq_domain_add_simple(np, STM32_ADC_MAX_ADCS, 0,
206 &stm32_adc_domain_ops,
207 priv);
208 if (!priv->domain) {
209 dev_err(&pdev->dev, "Failed to add irq domain\n");
210 return -ENOMEM;
211 }
212
213 irq_set_chained_handler(priv->irq, stm32_adc_irq_handler);
214 irq_set_handler_data(priv->irq, priv);
215
216 return 0;
217}
218
219static void stm32_adc_irq_remove(struct platform_device *pdev,
220 struct stm32_adc_priv *priv)
221{
222 int hwirq;
223
224 for (hwirq = 0; hwirq < STM32_ADC_MAX_ADCS; hwirq++)
225 irq_dispose_mapping(irq_find_mapping(priv->domain, hwirq));
226 irq_domain_remove(priv->domain);
227 irq_set_chained_handler(priv->irq, NULL);
228}
229
230static int stm32_adc_probe(struct platform_device *pdev)
231{
232 struct stm32_adc_priv *priv;
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +0200233 struct device *dev = &pdev->dev;
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100234 struct device_node *np = pdev->dev.of_node;
235 struct resource *res;
236 int ret;
237
238 if (!pdev->dev.of_node)
239 return -ENODEV;
240
241 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
242 if (!priv)
243 return -ENOMEM;
244
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +0200245 priv->cfg = (const struct stm32_adc_priv_cfg *)
246 of_match_device(dev->driver->of_match_table, dev)->data;
247
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100248 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
249 priv->common.base = devm_ioremap_resource(&pdev->dev, res);
250 if (IS_ERR(priv->common.base))
251 return PTR_ERR(priv->common.base);
Fabrice Gasnier2763ea02017-01-26 15:28:33 +0100252 priv->common.phys_base = res->start;
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100253
254 priv->vref = devm_regulator_get(&pdev->dev, "vref");
255 if (IS_ERR(priv->vref)) {
256 ret = PTR_ERR(priv->vref);
257 dev_err(&pdev->dev, "vref get failed, %d\n", ret);
258 return ret;
259 }
260
261 ret = regulator_enable(priv->vref);
262 if (ret < 0) {
263 dev_err(&pdev->dev, "vref enable failed\n");
264 return ret;
265 }
266
267 ret = regulator_get_voltage(priv->vref);
268 if (ret < 0) {
269 dev_err(&pdev->dev, "vref get voltage failed, %d\n", ret);
270 goto err_regulator_disable;
271 }
272 priv->common.vref_mv = ret / 1000;
273 dev_dbg(&pdev->dev, "vref+=%dmV\n", priv->common.vref_mv);
274
275 priv->aclk = devm_clk_get(&pdev->dev, "adc");
276 if (IS_ERR(priv->aclk)) {
277 ret = PTR_ERR(priv->aclk);
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200278 if (ret == -ENOENT) {
279 priv->aclk = NULL;
280 } else {
281 dev_err(&pdev->dev, "Can't get 'adc' clock\n");
282 goto err_regulator_disable;
283 }
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100284 }
285
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200286 if (priv->aclk) {
287 ret = clk_prepare_enable(priv->aclk);
288 if (ret < 0) {
289 dev_err(&pdev->dev, "adc clk enable failed\n");
290 goto err_regulator_disable;
291 }
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100292 }
293
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +0200294 ret = priv->cfg->clk_sel(pdev, priv);
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200295 if (ret < 0)
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100296 goto err_clk_disable;
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100297
298 ret = stm32_adc_irq_probe(pdev, priv);
299 if (ret < 0)
300 goto err_clk_disable;
301
302 platform_set_drvdata(pdev, &priv->common);
303
304 ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
305 if (ret < 0) {
306 dev_err(&pdev->dev, "failed to populate DT children\n");
307 goto err_irq_remove;
308 }
309
310 return 0;
311
312err_irq_remove:
313 stm32_adc_irq_remove(pdev, priv);
314
315err_clk_disable:
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200316 if (priv->aclk)
317 clk_disable_unprepare(priv->aclk);
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100318
319err_regulator_disable:
320 regulator_disable(priv->vref);
321
322 return ret;
323}
324
325static int stm32_adc_remove(struct platform_device *pdev)
326{
327 struct stm32_adc_common *common = platform_get_drvdata(pdev);
328 struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
329
330 of_platform_depopulate(&pdev->dev);
331 stm32_adc_irq_remove(pdev, priv);
Fabrice Gasnier9fd243c2017-05-29 11:28:17 +0200332 if (priv->aclk)
333 clk_disable_unprepare(priv->aclk);
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100334 regulator_disable(priv->vref);
335
336 return 0;
337}
338
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +0200339static const struct stm32_adc_priv_cfg stm32f4_adc_priv_cfg = {
340 .regs = &stm32f4_adc_common_regs,
341 .clk_sel = stm32f4_adc_clk_sel,
342};
343
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100344static const struct of_device_id stm32_adc_of_match[] = {
Fabrice Gasnier64ad7f62017-05-29 11:28:18 +0200345 {
346 .compatible = "st,stm32f4-adc-core",
347 .data = (void *)&stm32f4_adc_priv_cfg
348 }, {
349 },
Fabrice Gasnier1add69882016-11-15 16:30:57 +0100350};
351MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
352
353static struct platform_driver stm32_adc_driver = {
354 .probe = stm32_adc_probe,
355 .remove = stm32_adc_remove,
356 .driver = {
357 .name = "stm32-adc-core",
358 .of_match_table = stm32_adc_of_match,
359 },
360};
361module_platform_driver(stm32_adc_driver);
362
363MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
364MODULE_DESCRIPTION("STMicroelectronics STM32 ADC core driver");
365MODULE_LICENSE("GPL v2");
366MODULE_ALIAS("platform:stm32-adc-core");