1 // SPDX-License-Identifier: GPL-2.0
3 * This file is part of STM32 ADC driver
5 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
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"
24 /* STM32F4 - common registers for all ADC instances: 1, 2 & 3 */
25 #define STM32F4_ADC_CSR (STM32_ADCX_COMN_OFFSET + 0x00)
26 #define STM32F4_ADC_CCR (STM32_ADCX_COMN_OFFSET + 0x04)
28 /* STM32F4_ADC_CSR - bit fields */
29 #define STM32F4_EOC3 BIT(17)
30 #define STM32F4_EOC2 BIT(9)
31 #define STM32F4_EOC1 BIT(1)
33 /* STM32F4_ADC_CCR - bit fields */
34 #define STM32F4_ADC_ADCPRE_SHIFT 16
35 #define STM32F4_ADC_ADCPRE_MASK GENMASK(17, 16)
37 /* STM32H7 - common registers for all ADC instances */
38 #define STM32H7_ADC_CSR (STM32_ADCX_COMN_OFFSET + 0x00)
39 #define STM32H7_ADC_CCR (STM32_ADCX_COMN_OFFSET + 0x08)
41 /* STM32H7_ADC_CSR - bit fields */
42 #define STM32H7_EOC_SLV BIT(18)
43 #define STM32H7_EOC_MST BIT(2)
45 /* STM32H7_ADC_CCR - bit fields */
46 #define STM32H7_PRESC_SHIFT 18
47 #define STM32H7_PRESC_MASK GENMASK(21, 18)
48 #define STM32H7_CKMODE_SHIFT 16
49 #define STM32H7_CKMODE_MASK GENMASK(17, 16)
52 * stm32_adc_common_regs - stm32 common registers, compatible dependent data
53 * @csr: common status register offset
54 * @eoc1: adc1 end of conversion flag in @csr
55 * @eoc2: adc2 end of conversion flag in @csr
56 * @eoc3: adc3 end of conversion flag in @csr
58 struct stm32_adc_common_regs {
65 struct stm32_adc_priv;
68 * stm32_adc_priv_cfg - stm32 core compatible configuration data
69 * @regs: common registers for all instances
70 * @clk_sel: clock selection routine
71 * @max_clk_rate_hz: maximum analog clock rate (Hz, from datasheet)
73 struct stm32_adc_priv_cfg {
74 const struct stm32_adc_common_regs *regs;
75 int (*clk_sel)(struct platform_device *, struct stm32_adc_priv *);
80 * struct stm32_adc_priv - stm32 ADC core private data
81 * @irq: irq(s) for ADC block
82 * @domain: irq domain reference
83 * @aclk: clock reference for the analog circuitry
84 * @bclk: bus clock common for all ADCs, depends on part used
85 * @vref: regulator reference
86 * @cfg: compatible configuration data
87 * @common: common data for all ADC instances
89 struct stm32_adc_priv {
90 int irq[STM32_ADC_MAX_ADCS];
91 struct irq_domain *domain;
94 struct regulator *vref;
95 const struct stm32_adc_priv_cfg *cfg;
96 struct stm32_adc_common common;
99 static struct stm32_adc_priv *to_stm32_adc_priv(struct stm32_adc_common *com)
101 return container_of(com, struct stm32_adc_priv, common);
104 /* STM32F4 ADC internal common clock prescaler division ratios */
105 static int stm32f4_pclk_div[] = {2, 4, 6, 8};
108 * stm32f4_adc_clk_sel() - Select stm32f4 ADC common clock prescaler
109 * @priv: stm32 ADC core private data
110 * Select clock prescaler used for analog conversions, before using ADC.
112 static int stm32f4_adc_clk_sel(struct platform_device *pdev,
113 struct stm32_adc_priv *priv)
119 /* stm32f4 has one clk input for analog (mandatory), enforce it here */
121 dev_err(&pdev->dev, "No 'adc' clock found\n");
125 rate = clk_get_rate(priv->aclk);
127 dev_err(&pdev->dev, "Invalid clock rate: 0\n");
131 for (i = 0; i < ARRAY_SIZE(stm32f4_pclk_div); i++) {
132 if ((rate / stm32f4_pclk_div[i]) <= priv->cfg->max_clk_rate_hz)
135 if (i >= ARRAY_SIZE(stm32f4_pclk_div)) {
136 dev_err(&pdev->dev, "adc clk selection failed\n");
140 priv->common.rate = rate / stm32f4_pclk_div[i];
141 val = readl_relaxed(priv->common.base + STM32F4_ADC_CCR);
142 val &= ~STM32F4_ADC_ADCPRE_MASK;
143 val |= i << STM32F4_ADC_ADCPRE_SHIFT;
144 writel_relaxed(val, priv->common.base + STM32F4_ADC_CCR);
146 dev_dbg(&pdev->dev, "Using analog clock source at %ld kHz\n",
147 priv->common.rate / 1000);
153 * struct stm32h7_adc_ck_spec - specification for stm32h7 adc clock
154 * @ckmode: ADC clock mode, Async or sync with prescaler.
155 * @presc: prescaler bitfield for async clock mode
156 * @div: prescaler division ratio
158 struct stm32h7_adc_ck_spec {
164 static const struct stm32h7_adc_ck_spec stm32h7_adc_ckmodes_spec[] = {
165 /* 00: CK_ADC[1..3]: Asynchronous clock modes */
178 /* HCLK used: Synchronous clock modes (1, 2 or 4 prescaler) */
184 static int stm32h7_adc_clk_sel(struct platform_device *pdev,
185 struct stm32_adc_priv *priv)
187 u32 ckmode, presc, val;
191 /* stm32h7 bus clock is common for all ADC instances (mandatory) */
193 dev_err(&pdev->dev, "No 'bus' clock found\n");
198 * stm32h7 can use either 'bus' or 'adc' clock for analog circuitry.
199 * So, choice is to have bus clock mandatory and adc clock optional.
200 * If optional 'adc' clock has been found, then try to use it first.
204 * Asynchronous clock modes (e.g. ckmode == 0)
205 * From spec: PLL output musn't exceed max rate
207 rate = clk_get_rate(priv->aclk);
209 dev_err(&pdev->dev, "Invalid adc clock rate: 0\n");
213 for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
214 ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
215 presc = stm32h7_adc_ckmodes_spec[i].presc;
216 div = stm32h7_adc_ckmodes_spec[i].div;
221 if ((rate / div) <= priv->cfg->max_clk_rate_hz)
226 /* Synchronous clock modes (e.g. ckmode is 1, 2 or 3) */
227 rate = clk_get_rate(priv->bclk);
229 dev_err(&pdev->dev, "Invalid bus clock rate: 0\n");
233 for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
234 ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
235 presc = stm32h7_adc_ckmodes_spec[i].presc;
236 div = stm32h7_adc_ckmodes_spec[i].div;
241 if ((rate / div) <= priv->cfg->max_clk_rate_hz)
245 dev_err(&pdev->dev, "adc clk selection failed\n");
249 /* rate used later by each ADC instance to control BOOST mode */
250 priv->common.rate = rate / div;
252 /* Set common clock mode and prescaler */
253 val = readl_relaxed(priv->common.base + STM32H7_ADC_CCR);
254 val &= ~(STM32H7_CKMODE_MASK | STM32H7_PRESC_MASK);
255 val |= ckmode << STM32H7_CKMODE_SHIFT;
256 val |= presc << STM32H7_PRESC_SHIFT;
257 writel_relaxed(val, priv->common.base + STM32H7_ADC_CCR);
259 dev_dbg(&pdev->dev, "Using %s clock/%d source at %ld kHz\n",
260 ckmode ? "bus" : "adc", div, priv->common.rate / 1000);
265 /* STM32F4 common registers definitions */
266 static const struct stm32_adc_common_regs stm32f4_adc_common_regs = {
267 .csr = STM32F4_ADC_CSR,
268 .eoc1_msk = STM32F4_EOC1,
269 .eoc2_msk = STM32F4_EOC2,
270 .eoc3_msk = STM32F4_EOC3,
273 /* STM32H7 common registers definitions */
274 static const struct stm32_adc_common_regs stm32h7_adc_common_regs = {
275 .csr = STM32H7_ADC_CSR,
276 .eoc1_msk = STM32H7_EOC_MST,
277 .eoc2_msk = STM32H7_EOC_SLV,
280 /* ADC common interrupt for all instances */
281 static void stm32_adc_irq_handler(struct irq_desc *desc)
283 struct stm32_adc_priv *priv = irq_desc_get_handler_data(desc);
284 struct irq_chip *chip = irq_desc_get_chip(desc);
287 chained_irq_enter(chip, desc);
288 status = readl_relaxed(priv->common.base + priv->cfg->regs->csr);
290 if (status & priv->cfg->regs->eoc1_msk)
291 generic_handle_irq(irq_find_mapping(priv->domain, 0));
293 if (status & priv->cfg->regs->eoc2_msk)
294 generic_handle_irq(irq_find_mapping(priv->domain, 1));
296 if (status & priv->cfg->regs->eoc3_msk)
297 generic_handle_irq(irq_find_mapping(priv->domain, 2));
299 chained_irq_exit(chip, desc);
302 static int stm32_adc_domain_map(struct irq_domain *d, unsigned int irq,
303 irq_hw_number_t hwirq)
305 irq_set_chip_data(irq, d->host_data);
306 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_level_irq);
311 static void stm32_adc_domain_unmap(struct irq_domain *d, unsigned int irq)
313 irq_set_chip_and_handler(irq, NULL, NULL);
314 irq_set_chip_data(irq, NULL);
317 static const struct irq_domain_ops stm32_adc_domain_ops = {
318 .map = stm32_adc_domain_map,
319 .unmap = stm32_adc_domain_unmap,
320 .xlate = irq_domain_xlate_onecell,
323 static int stm32_adc_irq_probe(struct platform_device *pdev,
324 struct stm32_adc_priv *priv)
326 struct device_node *np = pdev->dev.of_node;
329 for (i = 0; i < STM32_ADC_MAX_ADCS; i++) {
330 priv->irq[i] = platform_get_irq(pdev, i);
331 if (priv->irq[i] < 0) {
333 * At least one interrupt must be provided, make others
335 * - stm32f4/h7 shares a common interrupt.
336 * - stm32mp1, has one line per ADC (either for ADC1,
339 if (i && priv->irq[i] == -ENXIO)
341 dev_err(&pdev->dev, "failed to get irq\n");
347 priv->domain = irq_domain_add_simple(np, STM32_ADC_MAX_ADCS, 0,
348 &stm32_adc_domain_ops,
351 dev_err(&pdev->dev, "Failed to add irq domain\n");
355 for (i = 0; i < STM32_ADC_MAX_ADCS; i++) {
356 if (priv->irq[i] < 0)
358 irq_set_chained_handler(priv->irq[i], stm32_adc_irq_handler);
359 irq_set_handler_data(priv->irq[i], priv);
365 static void stm32_adc_irq_remove(struct platform_device *pdev,
366 struct stm32_adc_priv *priv)
371 for (hwirq = 0; hwirq < STM32_ADC_MAX_ADCS; hwirq++)
372 irq_dispose_mapping(irq_find_mapping(priv->domain, hwirq));
373 irq_domain_remove(priv->domain);
375 for (i = 0; i < STM32_ADC_MAX_ADCS; i++) {
376 if (priv->irq[i] < 0)
378 irq_set_chained_handler(priv->irq[i], NULL);
382 static int stm32_adc_probe(struct platform_device *pdev)
384 struct stm32_adc_priv *priv;
385 struct device *dev = &pdev->dev;
386 struct device_node *np = pdev->dev.of_node;
387 struct resource *res;
390 if (!pdev->dev.of_node)
393 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
397 priv->cfg = (const struct stm32_adc_priv_cfg *)
398 of_match_device(dev->driver->of_match_table, dev)->data;
400 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
401 priv->common.base = devm_ioremap_resource(&pdev->dev, res);
402 if (IS_ERR(priv->common.base))
403 return PTR_ERR(priv->common.base);
404 priv->common.phys_base = res->start;
406 priv->vref = devm_regulator_get(&pdev->dev, "vref");
407 if (IS_ERR(priv->vref)) {
408 ret = PTR_ERR(priv->vref);
409 dev_err(&pdev->dev, "vref get failed, %d\n", ret);
413 ret = regulator_enable(priv->vref);
415 dev_err(&pdev->dev, "vref enable failed\n");
419 ret = regulator_get_voltage(priv->vref);
421 dev_err(&pdev->dev, "vref get voltage failed, %d\n", ret);
422 goto err_regulator_disable;
424 priv->common.vref_mv = ret / 1000;
425 dev_dbg(&pdev->dev, "vref+=%dmV\n", priv->common.vref_mv);
427 priv->aclk = devm_clk_get(&pdev->dev, "adc");
428 if (IS_ERR(priv->aclk)) {
429 ret = PTR_ERR(priv->aclk);
430 if (ret == -ENOENT) {
433 dev_err(&pdev->dev, "Can't get 'adc' clock\n");
434 goto err_regulator_disable;
439 ret = clk_prepare_enable(priv->aclk);
441 dev_err(&pdev->dev, "adc clk enable failed\n");
442 goto err_regulator_disable;
446 priv->bclk = devm_clk_get(&pdev->dev, "bus");
447 if (IS_ERR(priv->bclk)) {
448 ret = PTR_ERR(priv->bclk);
449 if (ret == -ENOENT) {
452 dev_err(&pdev->dev, "Can't get 'bus' clock\n");
453 goto err_aclk_disable;
458 ret = clk_prepare_enable(priv->bclk);
460 dev_err(&pdev->dev, "adc clk enable failed\n");
461 goto err_aclk_disable;
465 ret = priv->cfg->clk_sel(pdev, priv);
467 goto err_bclk_disable;
469 ret = stm32_adc_irq_probe(pdev, priv);
471 goto err_bclk_disable;
473 platform_set_drvdata(pdev, &priv->common);
475 ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
477 dev_err(&pdev->dev, "failed to populate DT children\n");
484 stm32_adc_irq_remove(pdev, priv);
488 clk_disable_unprepare(priv->bclk);
492 clk_disable_unprepare(priv->aclk);
494 err_regulator_disable:
495 regulator_disable(priv->vref);
500 static int stm32_adc_remove(struct platform_device *pdev)
502 struct stm32_adc_common *common = platform_get_drvdata(pdev);
503 struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
505 of_platform_depopulate(&pdev->dev);
506 stm32_adc_irq_remove(pdev, priv);
508 clk_disable_unprepare(priv->bclk);
510 clk_disable_unprepare(priv->aclk);
511 regulator_disable(priv->vref);
516 static const struct stm32_adc_priv_cfg stm32f4_adc_priv_cfg = {
517 .regs = &stm32f4_adc_common_regs,
518 .clk_sel = stm32f4_adc_clk_sel,
519 .max_clk_rate_hz = 36000000,
522 static const struct stm32_adc_priv_cfg stm32h7_adc_priv_cfg = {
523 .regs = &stm32h7_adc_common_regs,
524 .clk_sel = stm32h7_adc_clk_sel,
525 .max_clk_rate_hz = 36000000,
528 static const struct stm32_adc_priv_cfg stm32mp1_adc_priv_cfg = {
529 .regs = &stm32h7_adc_common_regs,
530 .clk_sel = stm32h7_adc_clk_sel,
531 .max_clk_rate_hz = 40000000,
534 static const struct of_device_id stm32_adc_of_match[] = {
536 .compatible = "st,stm32f4-adc-core",
537 .data = (void *)&stm32f4_adc_priv_cfg
539 .compatible = "st,stm32h7-adc-core",
540 .data = (void *)&stm32h7_adc_priv_cfg
542 .compatible = "st,stm32mp1-adc-core",
543 .data = (void *)&stm32mp1_adc_priv_cfg
547 MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
549 static struct platform_driver stm32_adc_driver = {
550 .probe = stm32_adc_probe,
551 .remove = stm32_adc_remove,
553 .name = "stm32-adc-core",
554 .of_match_table = stm32_adc_of_match,
557 module_platform_driver(stm32_adc_driver);
560 MODULE_DESCRIPTION("STMicroelectronics STM32 ADC core driver");
561 MODULE_LICENSE("GPL v2");
562 MODULE_ALIAS("platform:stm32-adc-core");