1 // SPDX-License-Identifier: GPL-2.0
3 * This file is part of STM32 ADC driver
5 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
6 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
8 * Inspired from: fsl-imx25-tsadc
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
14 #include <linux/irqchip/chained_irq.h>
15 #include <linux/irqdesc.h>
16 #include <linux/irqdomain.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
22 #include "stm32-adc-core.h"
25 * stm32_adc_common_regs - stm32 common registers, compatible dependent data
26 * @csr: common status register offset
27 * @eoc1: adc1 end of conversion flag in @csr
28 * @eoc2: adc2 end of conversion flag in @csr
29 * @eoc3: adc3 end of conversion flag in @csr
30 * @ier: interrupt enable register offset for each adc
31 * @eocie_msk: end of conversion interrupt enable mask in @ier
33 struct stm32_adc_common_regs {
42 struct stm32_adc_priv;
45 * stm32_adc_priv_cfg - stm32 core compatible configuration data
46 * @regs: common registers for all instances
47 * @clk_sel: clock selection routine
48 * @max_clk_rate_hz: maximum analog clock rate (Hz, from datasheet)
50 struct stm32_adc_priv_cfg {
51 const struct stm32_adc_common_regs *regs;
52 int (*clk_sel)(struct platform_device *, struct stm32_adc_priv *);
57 * struct stm32_adc_priv - stm32 ADC core private data
58 * @irq: irq(s) for ADC block
59 * @domain: irq domain reference
60 * @aclk: clock reference for the analog circuitry
61 * @bclk: bus clock common for all ADCs, depends on part used
62 * @vref: regulator reference
63 * @cfg: compatible configuration data
64 * @common: common data for all ADC instances
66 struct stm32_adc_priv {
67 int irq[STM32_ADC_MAX_ADCS];
68 struct irq_domain *domain;
71 struct regulator *vref;
72 const struct stm32_adc_priv_cfg *cfg;
73 struct stm32_adc_common common;
76 static struct stm32_adc_priv *to_stm32_adc_priv(struct stm32_adc_common *com)
78 return container_of(com, struct stm32_adc_priv, common);
81 /* STM32F4 ADC internal common clock prescaler division ratios */
82 static int stm32f4_pclk_div[] = {2, 4, 6, 8};
85 * stm32f4_adc_clk_sel() - Select stm32f4 ADC common clock prescaler
86 * @priv: stm32 ADC core private data
87 * Select clock prescaler used for analog conversions, before using ADC.
89 static int stm32f4_adc_clk_sel(struct platform_device *pdev,
90 struct stm32_adc_priv *priv)
96 /* stm32f4 has one clk input for analog (mandatory), enforce it here */
98 dev_err(&pdev->dev, "No 'adc' clock found\n");
102 rate = clk_get_rate(priv->aclk);
104 dev_err(&pdev->dev, "Invalid clock rate: 0\n");
108 for (i = 0; i < ARRAY_SIZE(stm32f4_pclk_div); i++) {
109 if ((rate / stm32f4_pclk_div[i]) <= priv->cfg->max_clk_rate_hz)
112 if (i >= ARRAY_SIZE(stm32f4_pclk_div)) {
113 dev_err(&pdev->dev, "adc clk selection failed\n");
117 priv->common.rate = rate / stm32f4_pclk_div[i];
118 val = readl_relaxed(priv->common.base + STM32F4_ADC_CCR);
119 val &= ~STM32F4_ADC_ADCPRE_MASK;
120 val |= i << STM32F4_ADC_ADCPRE_SHIFT;
121 writel_relaxed(val, priv->common.base + STM32F4_ADC_CCR);
123 dev_dbg(&pdev->dev, "Using analog clock source at %ld kHz\n",
124 priv->common.rate / 1000);
130 * struct stm32h7_adc_ck_spec - specification for stm32h7 adc clock
131 * @ckmode: ADC clock mode, Async or sync with prescaler.
132 * @presc: prescaler bitfield for async clock mode
133 * @div: prescaler division ratio
135 struct stm32h7_adc_ck_spec {
141 static const struct stm32h7_adc_ck_spec stm32h7_adc_ckmodes_spec[] = {
142 /* 00: CK_ADC[1..3]: Asynchronous clock modes */
155 /* HCLK used: Synchronous clock modes (1, 2 or 4 prescaler) */
161 static int stm32h7_adc_clk_sel(struct platform_device *pdev,
162 struct stm32_adc_priv *priv)
164 u32 ckmode, presc, val;
168 /* stm32h7 bus clock is common for all ADC instances (mandatory) */
170 dev_err(&pdev->dev, "No 'bus' clock found\n");
175 * stm32h7 can use either 'bus' or 'adc' clock for analog circuitry.
176 * So, choice is to have bus clock mandatory and adc clock optional.
177 * If optional 'adc' clock has been found, then try to use it first.
181 * Asynchronous clock modes (e.g. ckmode == 0)
182 * From spec: PLL output musn't exceed max rate
184 rate = clk_get_rate(priv->aclk);
186 dev_err(&pdev->dev, "Invalid adc clock rate: 0\n");
190 for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
191 ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
192 presc = stm32h7_adc_ckmodes_spec[i].presc;
193 div = stm32h7_adc_ckmodes_spec[i].div;
198 if ((rate / div) <= priv->cfg->max_clk_rate_hz)
203 /* Synchronous clock modes (e.g. ckmode is 1, 2 or 3) */
204 rate = clk_get_rate(priv->bclk);
206 dev_err(&pdev->dev, "Invalid bus clock rate: 0\n");
210 for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
211 ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
212 presc = stm32h7_adc_ckmodes_spec[i].presc;
213 div = stm32h7_adc_ckmodes_spec[i].div;
218 if ((rate / div) <= priv->cfg->max_clk_rate_hz)
222 dev_err(&pdev->dev, "adc clk selection failed\n");
226 /* rate used later by each ADC instance to control BOOST mode */
227 priv->common.rate = rate / div;
229 /* Set common clock mode and prescaler */
230 val = readl_relaxed(priv->common.base + STM32H7_ADC_CCR);
231 val &= ~(STM32H7_CKMODE_MASK | STM32H7_PRESC_MASK);
232 val |= ckmode << STM32H7_CKMODE_SHIFT;
233 val |= presc << STM32H7_PRESC_SHIFT;
234 writel_relaxed(val, priv->common.base + STM32H7_ADC_CCR);
236 dev_dbg(&pdev->dev, "Using %s clock/%d source at %ld kHz\n",
237 ckmode ? "bus" : "adc", div, priv->common.rate / 1000);
242 /* STM32F4 common registers definitions */
243 static const struct stm32_adc_common_regs stm32f4_adc_common_regs = {
244 .csr = STM32F4_ADC_CSR,
245 .eoc1_msk = STM32F4_EOC1,
246 .eoc2_msk = STM32F4_EOC2,
247 .eoc3_msk = STM32F4_EOC3,
248 .ier = STM32F4_ADC_CR1,
249 .eocie_msk = STM32F4_EOCIE,
252 /* STM32H7 common registers definitions */
253 static const struct stm32_adc_common_regs stm32h7_adc_common_regs = {
254 .csr = STM32H7_ADC_CSR,
255 .eoc1_msk = STM32H7_EOC_MST,
256 .eoc2_msk = STM32H7_EOC_SLV,
257 .ier = STM32H7_ADC_IER,
258 .eocie_msk = STM32H7_EOCIE,
261 static const unsigned int stm32_adc_offset[STM32_ADC_MAX_ADCS] = {
262 0, STM32_ADC_OFFSET, STM32_ADC_OFFSET * 2,
265 static unsigned int stm32_adc_eoc_enabled(struct stm32_adc_priv *priv,
268 u32 ier, offset = stm32_adc_offset[adc];
270 ier = readl_relaxed(priv->common.base + offset + priv->cfg->regs->ier);
272 return ier & priv->cfg->regs->eocie_msk;
275 /* ADC common interrupt for all instances */
276 static void stm32_adc_irq_handler(struct irq_desc *desc)
278 struct stm32_adc_priv *priv = irq_desc_get_handler_data(desc);
279 struct irq_chip *chip = irq_desc_get_chip(desc);
282 chained_irq_enter(chip, desc);
283 status = readl_relaxed(priv->common.base + priv->cfg->regs->csr);
286 * End of conversion may be handled by using IRQ or DMA. There may be a
287 * race here when two conversions complete at the same time on several
288 * ADCs. EOC may be read 'set' for several ADCs, with:
289 * - an ADC configured to use DMA (EOC triggers the DMA request, and
290 * is then automatically cleared by DR read in hardware)
291 * - an ADC configured to use IRQs (EOCIE bit is set. The handler must
292 * be called in this case)
293 * So both EOC status bit in CSR and EOCIE control bit must be checked
294 * before invoking the interrupt handler (e.g. call ISR only for
297 if (status & priv->cfg->regs->eoc1_msk &&
298 stm32_adc_eoc_enabled(priv, 0))
299 generic_handle_irq(irq_find_mapping(priv->domain, 0));
301 if (status & priv->cfg->regs->eoc2_msk &&
302 stm32_adc_eoc_enabled(priv, 1))
303 generic_handle_irq(irq_find_mapping(priv->domain, 1));
305 if (status & priv->cfg->regs->eoc3_msk &&
306 stm32_adc_eoc_enabled(priv, 2))
307 generic_handle_irq(irq_find_mapping(priv->domain, 2));
309 chained_irq_exit(chip, desc);
312 static int stm32_adc_domain_map(struct irq_domain *d, unsigned int irq,
313 irq_hw_number_t hwirq)
315 irq_set_chip_data(irq, d->host_data);
316 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_level_irq);
321 static void stm32_adc_domain_unmap(struct irq_domain *d, unsigned int irq)
323 irq_set_chip_and_handler(irq, NULL, NULL);
324 irq_set_chip_data(irq, NULL);
327 static const struct irq_domain_ops stm32_adc_domain_ops = {
328 .map = stm32_adc_domain_map,
329 .unmap = stm32_adc_domain_unmap,
330 .xlate = irq_domain_xlate_onecell,
333 static int stm32_adc_irq_probe(struct platform_device *pdev,
334 struct stm32_adc_priv *priv)
336 struct device_node *np = pdev->dev.of_node;
339 for (i = 0; i < STM32_ADC_MAX_ADCS; i++) {
340 priv->irq[i] = platform_get_irq(pdev, i);
341 if (priv->irq[i] < 0) {
343 * At least one interrupt must be provided, make others
345 * - stm32f4/h7 shares a common interrupt.
346 * - stm32mp1, has one line per ADC (either for ADC1,
349 if (i && priv->irq[i] == -ENXIO)
351 dev_err(&pdev->dev, "failed to get irq\n");
357 priv->domain = irq_domain_add_simple(np, STM32_ADC_MAX_ADCS, 0,
358 &stm32_adc_domain_ops,
361 dev_err(&pdev->dev, "Failed to add irq domain\n");
365 for (i = 0; i < STM32_ADC_MAX_ADCS; i++) {
366 if (priv->irq[i] < 0)
368 irq_set_chained_handler(priv->irq[i], stm32_adc_irq_handler);
369 irq_set_handler_data(priv->irq[i], priv);
375 static void stm32_adc_irq_remove(struct platform_device *pdev,
376 struct stm32_adc_priv *priv)
381 for (hwirq = 0; hwirq < STM32_ADC_MAX_ADCS; hwirq++)
382 irq_dispose_mapping(irq_find_mapping(priv->domain, hwirq));
383 irq_domain_remove(priv->domain);
385 for (i = 0; i < STM32_ADC_MAX_ADCS; i++) {
386 if (priv->irq[i] < 0)
388 irq_set_chained_handler(priv->irq[i], NULL);
392 static int stm32_adc_probe(struct platform_device *pdev)
394 struct stm32_adc_priv *priv;
395 struct device *dev = &pdev->dev;
396 struct device_node *np = pdev->dev.of_node;
397 struct resource *res;
400 if (!pdev->dev.of_node)
403 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
407 priv->cfg = (const struct stm32_adc_priv_cfg *)
408 of_match_device(dev->driver->of_match_table, dev)->data;
410 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
411 priv->common.base = devm_ioremap_resource(&pdev->dev, res);
412 if (IS_ERR(priv->common.base))
413 return PTR_ERR(priv->common.base);
414 priv->common.phys_base = res->start;
416 priv->vref = devm_regulator_get(&pdev->dev, "vref");
417 if (IS_ERR(priv->vref)) {
418 ret = PTR_ERR(priv->vref);
419 dev_err(&pdev->dev, "vref get failed, %d\n", ret);
423 ret = regulator_enable(priv->vref);
425 dev_err(&pdev->dev, "vref enable failed\n");
429 ret = regulator_get_voltage(priv->vref);
431 dev_err(&pdev->dev, "vref get voltage failed, %d\n", ret);
432 goto err_regulator_disable;
434 priv->common.vref_mv = ret / 1000;
435 dev_dbg(&pdev->dev, "vref+=%dmV\n", priv->common.vref_mv);
437 priv->aclk = devm_clk_get(&pdev->dev, "adc");
438 if (IS_ERR(priv->aclk)) {
439 ret = PTR_ERR(priv->aclk);
440 if (ret == -ENOENT) {
443 dev_err(&pdev->dev, "Can't get 'adc' clock\n");
444 goto err_regulator_disable;
449 ret = clk_prepare_enable(priv->aclk);
451 dev_err(&pdev->dev, "adc clk enable failed\n");
452 goto err_regulator_disable;
456 priv->bclk = devm_clk_get(&pdev->dev, "bus");
457 if (IS_ERR(priv->bclk)) {
458 ret = PTR_ERR(priv->bclk);
459 if (ret == -ENOENT) {
462 dev_err(&pdev->dev, "Can't get 'bus' clock\n");
463 goto err_aclk_disable;
468 ret = clk_prepare_enable(priv->bclk);
470 dev_err(&pdev->dev, "adc clk enable failed\n");
471 goto err_aclk_disable;
475 ret = priv->cfg->clk_sel(pdev, priv);
477 goto err_bclk_disable;
479 ret = stm32_adc_irq_probe(pdev, priv);
481 goto err_bclk_disable;
483 platform_set_drvdata(pdev, &priv->common);
485 ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
487 dev_err(&pdev->dev, "failed to populate DT children\n");
494 stm32_adc_irq_remove(pdev, priv);
498 clk_disable_unprepare(priv->bclk);
502 clk_disable_unprepare(priv->aclk);
504 err_regulator_disable:
505 regulator_disable(priv->vref);
510 static int stm32_adc_remove(struct platform_device *pdev)
512 struct stm32_adc_common *common = platform_get_drvdata(pdev);
513 struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
515 of_platform_depopulate(&pdev->dev);
516 stm32_adc_irq_remove(pdev, priv);
518 clk_disable_unprepare(priv->bclk);
520 clk_disable_unprepare(priv->aclk);
521 regulator_disable(priv->vref);
526 static const struct stm32_adc_priv_cfg stm32f4_adc_priv_cfg = {
527 .regs = &stm32f4_adc_common_regs,
528 .clk_sel = stm32f4_adc_clk_sel,
529 .max_clk_rate_hz = 36000000,
532 static const struct stm32_adc_priv_cfg stm32h7_adc_priv_cfg = {
533 .regs = &stm32h7_adc_common_regs,
534 .clk_sel = stm32h7_adc_clk_sel,
535 .max_clk_rate_hz = 36000000,
538 static const struct stm32_adc_priv_cfg stm32mp1_adc_priv_cfg = {
539 .regs = &stm32h7_adc_common_regs,
540 .clk_sel = stm32h7_adc_clk_sel,
541 .max_clk_rate_hz = 40000000,
544 static const struct of_device_id stm32_adc_of_match[] = {
546 .compatible = "st,stm32f4-adc-core",
547 .data = (void *)&stm32f4_adc_priv_cfg
549 .compatible = "st,stm32h7-adc-core",
550 .data = (void *)&stm32h7_adc_priv_cfg
552 .compatible = "st,stm32mp1-adc-core",
553 .data = (void *)&stm32mp1_adc_priv_cfg
557 MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
559 static struct platform_driver stm32_adc_driver = {
560 .probe = stm32_adc_probe,
561 .remove = stm32_adc_remove,
563 .name = "stm32-adc-core",
564 .of_match_table = stm32_adc_of_match,
567 module_platform_driver(stm32_adc_driver);
569 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
570 MODULE_DESCRIPTION("STMicroelectronics STM32 ADC core driver");
571 MODULE_LICENSE("GPL v2");
572 MODULE_ALIAS("platform:stm32-adc-core");