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/bitfield.h>
13 #include <linux/clk.h>
14 #include <linux/interrupt.h>
15 #include <linux/irqchip/chained_irq.h>
16 #include <linux/irqdesc.h>
17 #include <linux/irqdomain.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
25 #include <linux/units.h>
27 #include "stm32-adc-core.h"
29 #define STM32_ADC_CORE_SLEEP_DELAY_MS 2000
31 /* SYSCFG registers */
32 #define STM32MP1_SYSCFG_PMCSETR 0x04
33 #define STM32MP1_SYSCFG_PMCCLRR 0x44
35 /* SYSCFG bit fields */
36 #define STM32MP1_SYSCFG_ANASWVDD_MASK BIT(9)
38 /* SYSCFG capability flags */
39 #define HAS_VBOOSTER BIT(0)
40 #define HAS_ANASWVDD BIT(1)
43 * struct stm32_adc_common_regs - stm32 common registers
44 * @csr: common status register offset
45 * @ccr: common control register offset
46 * @eoc_msk: array of eoc (end of conversion flag) masks in csr for adc1..n
47 * @ovr_msk: array of ovr (overrun flag) masks in csr for adc1..n
48 * @ier: interrupt enable register offset for each adc
49 * @eocie_msk: end of conversion interrupt enable mask in @ier
51 struct stm32_adc_common_regs {
54 u32 eoc_msk[STM32_ADC_MAX_ADCS];
55 u32 ovr_msk[STM32_ADC_MAX_ADCS];
60 struct stm32_adc_priv;
63 * struct stm32_adc_priv_cfg - stm32 core compatible configuration data
64 * @regs: common registers for all instances
65 * @clk_sel: clock selection routine
66 * @max_clk_rate_hz: maximum analog clock rate (Hz, from datasheet)
67 * @ipid: adc identification number
68 * @has_syscfg: SYSCFG capability flags
69 * @num_irqs: number of interrupt lines
70 * @num_adcs: maximum number of ADC instances in the common registers
72 struct stm32_adc_priv_cfg {
73 const struct stm32_adc_common_regs *regs;
74 int (*clk_sel)(struct platform_device *, struct stm32_adc_priv *);
77 unsigned int has_syscfg;
78 unsigned int num_irqs;
79 unsigned int num_adcs;
83 * struct stm32_adc_priv - stm32 ADC core private data
84 * @irq: irq(s) for ADC block
85 * @nb_adc_max: actual maximum number of instance per ADC block
86 * @domain: irq domain reference
87 * @aclk: clock reference for the analog circuitry
88 * @bclk: bus clock common for all ADCs, depends on part used
89 * @max_clk_rate: desired maximum clock rate
90 * @booster: booster supply reference
91 * @vdd: vdd supply reference
92 * @vdda: vdda analog supply reference
93 * @vref: regulator reference
94 * @vdd_uv: vdd supply voltage (microvolts)
95 * @vdda_uv: vdda supply voltage (microvolts)
96 * @cfg: compatible configuration data
97 * @common: common data for all ADC instances
98 * @ccr_bak: backup CCR in low power mode
99 * @syscfg: reference to syscon, system control registers
101 struct stm32_adc_priv {
102 int irq[STM32_ADC_MAX_ADCS];
103 unsigned int nb_adc_max;
104 struct irq_domain *domain;
108 struct regulator *booster;
109 struct regulator *vdd;
110 struct regulator *vdda;
111 struct regulator *vref;
114 const struct stm32_adc_priv_cfg *cfg;
115 struct stm32_adc_common common;
117 struct regmap *syscfg;
120 static struct stm32_adc_priv *to_stm32_adc_priv(struct stm32_adc_common *com)
122 return container_of(com, struct stm32_adc_priv, common);
125 /* STM32F4 ADC internal common clock prescaler division ratios */
126 static int stm32f4_pclk_div[] = {2, 4, 6, 8};
129 * stm32f4_adc_clk_sel() - Select stm32f4 ADC common clock prescaler
130 * @pdev: platform device
131 * @priv: stm32 ADC core private data
132 * Select clock prescaler used for analog conversions, before using ADC.
134 static int stm32f4_adc_clk_sel(struct platform_device *pdev,
135 struct stm32_adc_priv *priv)
141 /* stm32f4 has one clk input for analog (mandatory), enforce it here */
143 dev_err(&pdev->dev, "No 'adc' clock found\n");
147 rate = clk_get_rate(priv->aclk);
149 dev_err(&pdev->dev, "Invalid clock rate: 0\n");
153 for (i = 0; i < ARRAY_SIZE(stm32f4_pclk_div); i++) {
154 if ((rate / stm32f4_pclk_div[i]) <= priv->max_clk_rate)
157 if (i >= ARRAY_SIZE(stm32f4_pclk_div)) {
158 dev_err(&pdev->dev, "adc clk selection failed\n");
162 priv->common.rate = rate / stm32f4_pclk_div[i];
163 val = readl_relaxed(priv->common.base + STM32F4_ADC_CCR);
164 val &= ~STM32F4_ADC_ADCPRE_MASK;
165 val |= i << STM32F4_ADC_ADCPRE_SHIFT;
166 writel_relaxed(val, priv->common.base + STM32F4_ADC_CCR);
168 dev_dbg(&pdev->dev, "Using analog clock source at %ld kHz\n",
169 priv->common.rate / 1000);
175 * struct stm32h7_adc_ck_spec - specification for stm32h7 adc clock
176 * @ckmode: ADC clock mode, Async or sync with prescaler.
177 * @presc: prescaler bitfield for async clock mode
178 * @div: prescaler division ratio
180 struct stm32h7_adc_ck_spec {
186 static const struct stm32h7_adc_ck_spec stm32h7_adc_ckmodes_spec[] = {
187 /* 00: CK_ADC[1..3]: Asynchronous clock modes */
200 /* HCLK used: Synchronous clock modes (1, 2 or 4 prescaler) */
206 static int stm32h7_adc_clk_sel(struct platform_device *pdev,
207 struct stm32_adc_priv *priv)
209 u32 ckmode, presc, val;
213 /* stm32h7 bus clock is common for all ADC instances (mandatory) */
215 dev_err(&pdev->dev, "No 'bus' clock found\n");
220 * stm32h7 can use either 'bus' or 'adc' clock for analog circuitry.
221 * So, choice is to have bus clock mandatory and adc clock optional.
222 * If optional 'adc' clock has been found, then try to use it first.
226 * Asynchronous clock modes (e.g. ckmode == 0)
227 * From spec: PLL output musn't exceed max rate
229 rate = clk_get_rate(priv->aclk);
231 dev_err(&pdev->dev, "Invalid adc clock rate: 0\n");
235 /* If duty is an error, kindly use at least /2 divider */
236 duty = clk_get_scaled_duty_cycle(priv->aclk, 100);
238 dev_warn(&pdev->dev, "adc clock duty: %d\n", duty);
240 for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
241 ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
242 presc = stm32h7_adc_ckmodes_spec[i].presc;
243 div = stm32h7_adc_ckmodes_spec[i].div;
249 * For proper operation, clock duty cycle range is 49%
250 * to 51%. Apply at least /2 prescaler otherwise.
252 if (div == 1 && (duty < 49 || duty > 51))
255 if ((rate / div) <= priv->max_clk_rate)
260 /* Synchronous clock modes (e.g. ckmode is 1, 2 or 3) */
261 rate = clk_get_rate(priv->bclk);
263 dev_err(&pdev->dev, "Invalid bus clock rate: 0\n");
267 duty = clk_get_scaled_duty_cycle(priv->bclk, 100);
269 dev_warn(&pdev->dev, "bus clock duty: %d\n", duty);
271 for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
272 ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
273 presc = stm32h7_adc_ckmodes_spec[i].presc;
274 div = stm32h7_adc_ckmodes_spec[i].div;
279 if (div == 1 && (duty < 49 || duty > 51))
282 if ((rate / div) <= priv->max_clk_rate)
286 dev_err(&pdev->dev, "adc clk selection failed\n");
290 /* rate used later by each ADC instance to control BOOST mode */
291 priv->common.rate = rate / div;
293 /* Set common clock mode and prescaler */
294 val = readl_relaxed(priv->common.base + STM32H7_ADC_CCR);
295 val &= ~(STM32H7_CKMODE_MASK | STM32H7_PRESC_MASK);
296 val |= ckmode << STM32H7_CKMODE_SHIFT;
297 val |= presc << STM32H7_PRESC_SHIFT;
298 writel_relaxed(val, priv->common.base + STM32H7_ADC_CCR);
300 dev_dbg(&pdev->dev, "Using %s clock/%d source at %ld kHz\n",
301 ckmode ? "bus" : "adc", div, priv->common.rate / 1000);
306 /* STM32F4 common registers definitions */
307 static const struct stm32_adc_common_regs stm32f4_adc_common_regs = {
308 .csr = STM32F4_ADC_CSR,
309 .ccr = STM32F4_ADC_CCR,
310 .eoc_msk = { STM32F4_EOC1, STM32F4_EOC2, STM32F4_EOC3 },
311 .ovr_msk = { STM32F4_OVR1, STM32F4_OVR2, STM32F4_OVR3 },
312 .ier = STM32F4_ADC_CR1,
313 .eocie_msk = STM32F4_EOCIE,
316 /* STM32H7 common registers definitions */
317 static const struct stm32_adc_common_regs stm32h7_adc_common_regs = {
318 .csr = STM32H7_ADC_CSR,
319 .ccr = STM32H7_ADC_CCR,
320 .eoc_msk = { STM32H7_EOC_MST, STM32H7_EOC_SLV },
321 .ovr_msk = { STM32H7_OVR_MST, STM32H7_OVR_SLV },
322 .ier = STM32H7_ADC_IER,
323 .eocie_msk = STM32H7_EOCIE,
326 /* STM32MP13 common registers definitions */
327 static const struct stm32_adc_common_regs stm32mp13_adc_common_regs = {
328 .csr = STM32H7_ADC_CSR,
329 .ccr = STM32H7_ADC_CCR,
330 .eoc_msk = { STM32H7_EOC_MST },
331 .ovr_msk = { STM32H7_OVR_MST },
332 .ier = STM32H7_ADC_IER,
333 .eocie_msk = STM32H7_EOCIE,
336 static const unsigned int stm32_adc_offset[STM32_ADC_MAX_ADCS] = {
337 0, STM32_ADC_OFFSET, STM32_ADC_OFFSET * 2,
340 static unsigned int stm32_adc_eoc_enabled(struct stm32_adc_priv *priv,
343 u32 ier, offset = stm32_adc_offset[adc];
345 ier = readl_relaxed(priv->common.base + offset + priv->cfg->regs->ier);
347 return ier & priv->cfg->regs->eocie_msk;
350 /* ADC common interrupt for all instances */
351 static void stm32_adc_irq_handler(struct irq_desc *desc)
353 struct stm32_adc_priv *priv = irq_desc_get_handler_data(desc);
354 struct irq_chip *chip = irq_desc_get_chip(desc);
358 chained_irq_enter(chip, desc);
359 status = readl_relaxed(priv->common.base + priv->cfg->regs->csr);
362 * End of conversion may be handled by using IRQ or DMA. There may be a
363 * race here when two conversions complete at the same time on several
364 * ADCs. EOC may be read 'set' for several ADCs, with:
365 * - an ADC configured to use DMA (EOC triggers the DMA request, and
366 * is then automatically cleared by DR read in hardware)
367 * - an ADC configured to use IRQs (EOCIE bit is set. The handler must
368 * be called in this case)
369 * So both EOC status bit in CSR and EOCIE control bit must be checked
370 * before invoking the interrupt handler (e.g. call ISR only for
373 for (i = 0; i < priv->nb_adc_max; i++) {
374 if ((status & priv->cfg->regs->eoc_msk[i] &&
375 stm32_adc_eoc_enabled(priv, i)) ||
376 (status & priv->cfg->regs->ovr_msk[i]))
377 generic_handle_domain_irq(priv->domain, i);
380 chained_irq_exit(chip, desc);
383 static int stm32_adc_domain_map(struct irq_domain *d, unsigned int irq,
384 irq_hw_number_t hwirq)
386 irq_set_chip_data(irq, d->host_data);
387 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_level_irq);
392 static void stm32_adc_domain_unmap(struct irq_domain *d, unsigned int irq)
394 irq_set_chip_and_handler(irq, NULL, NULL);
395 irq_set_chip_data(irq, NULL);
398 static const struct irq_domain_ops stm32_adc_domain_ops = {
399 .map = stm32_adc_domain_map,
400 .unmap = stm32_adc_domain_unmap,
401 .xlate = irq_domain_xlate_onecell,
404 static int stm32_adc_irq_probe(struct platform_device *pdev,
405 struct stm32_adc_priv *priv)
407 struct device_node *np = pdev->dev.of_node;
411 * Interrupt(s) must be provided, depending on the compatible:
412 * - stm32f4/h7 shares a common interrupt line.
413 * - stm32mp1, has one line per ADC
415 for (i = 0; i < priv->cfg->num_irqs; i++) {
416 priv->irq[i] = platform_get_irq(pdev, i);
417 if (priv->irq[i] < 0)
421 priv->domain = irq_domain_add_simple(np, STM32_ADC_MAX_ADCS, 0,
422 &stm32_adc_domain_ops,
425 dev_err(&pdev->dev, "Failed to add irq domain\n");
429 for (i = 0; i < priv->cfg->num_irqs; i++) {
430 irq_set_chained_handler(priv->irq[i], stm32_adc_irq_handler);
431 irq_set_handler_data(priv->irq[i], priv);
437 static void stm32_adc_irq_remove(struct platform_device *pdev,
438 struct stm32_adc_priv *priv)
443 for (hwirq = 0; hwirq < priv->nb_adc_max; hwirq++)
444 irq_dispose_mapping(irq_find_mapping(priv->domain, hwirq));
445 irq_domain_remove(priv->domain);
447 for (i = 0; i < priv->cfg->num_irqs; i++)
448 irq_set_chained_handler(priv->irq[i], NULL);
451 static int stm32_adc_core_switches_supply_en(struct stm32_adc_priv *priv,
457 * On STM32H7 and STM32MP1, the ADC inputs are multiplexed with analog
458 * switches (via PCSEL) which have reduced performances when their
459 * supply is below 2.7V (vdda by default):
460 * - Voltage booster can be used, to get full ADC performances
461 * (increases power consumption).
462 * - Vdd can be used to supply them, if above 2.7V (STM32MP1 only).
464 * Recommended settings for ANASWVDD and EN_BOOSTER:
465 * - vdda < 2.7V but vdd > 2.7V: ANASWVDD = 1, EN_BOOSTER = 0 (stm32mp1)
466 * - vdda < 2.7V and vdd < 2.7V: ANASWVDD = 0, EN_BOOSTER = 1
467 * - vdda >= 2.7V: ANASWVDD = 0, EN_BOOSTER = 0 (default)
469 if (priv->vdda_uv < 2700000) {
470 if (priv->syscfg && priv->vdd_uv > 2700000) {
471 ret = regulator_enable(priv->vdd);
473 dev_err(dev, "vdd enable failed %d\n", ret);
477 ret = regmap_write(priv->syscfg,
478 STM32MP1_SYSCFG_PMCSETR,
479 STM32MP1_SYSCFG_ANASWVDD_MASK);
481 regulator_disable(priv->vdd);
482 dev_err(dev, "vdd select failed, %d\n", ret);
485 dev_dbg(dev, "analog switches supplied by vdd\n");
492 * This is optional, as this is a trade-off between
493 * analog performance and power consumption.
495 ret = regulator_enable(priv->booster);
497 dev_err(dev, "booster enable failed %d\n", ret);
500 dev_dbg(dev, "analog switches supplied by booster\n");
506 /* Fallback using vdda (default), nothing to do */
507 dev_dbg(dev, "analog switches supplied by vdda (%d uV)\n",
513 static void stm32_adc_core_switches_supply_dis(struct stm32_adc_priv *priv)
515 if (priv->vdda_uv < 2700000) {
516 if (priv->syscfg && priv->vdd_uv > 2700000) {
517 regmap_write(priv->syscfg, STM32MP1_SYSCFG_PMCCLRR,
518 STM32MP1_SYSCFG_ANASWVDD_MASK);
519 regulator_disable(priv->vdd);
523 regulator_disable(priv->booster);
527 static int stm32_adc_core_hw_start(struct device *dev)
529 struct stm32_adc_common *common = dev_get_drvdata(dev);
530 struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
533 ret = regulator_enable(priv->vdda);
535 dev_err(dev, "vdda enable failed %d\n", ret);
539 ret = regulator_get_voltage(priv->vdda);
541 dev_err(dev, "vdda get voltage failed, %d\n", ret);
542 goto err_vdda_disable;
546 ret = stm32_adc_core_switches_supply_en(priv, dev);
548 goto err_vdda_disable;
550 ret = regulator_enable(priv->vref);
552 dev_err(dev, "vref enable failed\n");
553 goto err_switches_dis;
556 ret = clk_prepare_enable(priv->bclk);
558 dev_err(dev, "bus clk enable failed\n");
559 goto err_regulator_disable;
562 ret = clk_prepare_enable(priv->aclk);
564 dev_err(dev, "adc clk enable failed\n");
565 goto err_bclk_disable;
568 writel_relaxed(priv->ccr_bak, priv->common.base + priv->cfg->regs->ccr);
573 clk_disable_unprepare(priv->bclk);
574 err_regulator_disable:
575 regulator_disable(priv->vref);
577 stm32_adc_core_switches_supply_dis(priv);
579 regulator_disable(priv->vdda);
584 static void stm32_adc_core_hw_stop(struct device *dev)
586 struct stm32_adc_common *common = dev_get_drvdata(dev);
587 struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
589 /* Backup CCR that may be lost (depends on power state to achieve) */
590 priv->ccr_bak = readl_relaxed(priv->common.base + priv->cfg->regs->ccr);
591 clk_disable_unprepare(priv->aclk);
592 clk_disable_unprepare(priv->bclk);
593 regulator_disable(priv->vref);
594 stm32_adc_core_switches_supply_dis(priv);
595 regulator_disable(priv->vdda);
598 static int stm32_adc_core_switches_probe(struct device *dev,
599 struct stm32_adc_priv *priv)
601 struct device_node *np = dev->of_node;
604 /* Analog switches supply can be controlled by syscfg (optional) */
605 priv->syscfg = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
606 if (IS_ERR(priv->syscfg)) {
607 ret = PTR_ERR(priv->syscfg);
609 return dev_err_probe(dev, ret, "Can't probe syscfg\n");
614 /* Booster can be used to supply analog switches (optional) */
615 if (priv->cfg->has_syscfg & HAS_VBOOSTER &&
616 of_property_read_bool(np, "booster-supply")) {
617 priv->booster = devm_regulator_get_optional(dev, "booster");
618 if (IS_ERR(priv->booster)) {
619 ret = PTR_ERR(priv->booster);
621 return dev_err_probe(dev, ret, "can't get booster\n");
623 priv->booster = NULL;
627 /* Vdd can be used to supply analog switches (optional) */
628 if (priv->cfg->has_syscfg & HAS_ANASWVDD &&
629 of_property_read_bool(np, "vdd-supply")) {
630 priv->vdd = devm_regulator_get_optional(dev, "vdd");
631 if (IS_ERR(priv->vdd)) {
632 ret = PTR_ERR(priv->vdd);
634 return dev_err_probe(dev, ret, "can't get vdd\n");
641 ret = regulator_enable(priv->vdd);
643 dev_err(dev, "vdd enable failed %d\n", ret);
647 ret = regulator_get_voltage(priv->vdd);
649 dev_err(dev, "vdd get voltage failed %d\n", ret);
650 regulator_disable(priv->vdd);
655 regulator_disable(priv->vdd);
661 static int stm32_adc_probe_identification(struct platform_device *pdev,
662 struct stm32_adc_priv *priv)
664 struct device_node *np = pdev->dev.of_node;
665 struct device_node *child;
670 if (!priv->cfg->ipid)
673 id = FIELD_GET(STM32MP1_IPIDR_MASK,
674 readl_relaxed(priv->common.base + STM32MP1_ADC_IPDR));
675 if (id != priv->cfg->ipid) {
676 dev_err(&pdev->dev, "Unexpected IP version: 0x%x", id);
680 for_each_child_of_node(np, child) {
681 ret = of_property_read_string(child, "compatible", &compat);
684 /* Count child nodes with stm32 adc compatible */
685 if (strstr(compat, "st,stm32") && strstr(compat, "adc"))
689 val = readl_relaxed(priv->common.base + STM32MP1_ADC_HWCFGR0);
690 priv->nb_adc_max = FIELD_GET(STM32MP1_ADCNUM_MASK, val);
691 if (count > priv->nb_adc_max) {
692 dev_err(&pdev->dev, "Unexpected child number: %d", count);
696 val = readl_relaxed(priv->common.base + STM32MP1_ADC_VERR);
697 dev_dbg(&pdev->dev, "ADC version: %lu.%lu\n",
698 FIELD_GET(STM32MP1_MAJREV_MASK, val),
699 FIELD_GET(STM32MP1_MINREV_MASK, val));
704 static int stm32_adc_probe(struct platform_device *pdev)
706 struct stm32_adc_priv *priv;
707 struct device *dev = &pdev->dev;
708 struct device_node *np = pdev->dev.of_node;
709 struct resource *res;
713 if (!pdev->dev.of_node)
716 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
719 platform_set_drvdata(pdev, &priv->common);
721 priv->cfg = (const struct stm32_adc_priv_cfg *)
722 of_match_device(dev->driver->of_match_table, dev)->data;
723 priv->nb_adc_max = priv->cfg->num_adcs;
724 spin_lock_init(&priv->common.lock);
726 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
727 priv->common.base = devm_ioremap_resource(&pdev->dev, res);
728 if (IS_ERR(priv->common.base))
729 return PTR_ERR(priv->common.base);
730 priv->common.phys_base = res->start;
732 priv->vdda = devm_regulator_get(&pdev->dev, "vdda");
733 if (IS_ERR(priv->vdda))
734 return dev_err_probe(&pdev->dev, PTR_ERR(priv->vdda),
735 "vdda get failed\n");
737 priv->vref = devm_regulator_get(&pdev->dev, "vref");
738 if (IS_ERR(priv->vref))
739 return dev_err_probe(&pdev->dev, PTR_ERR(priv->vref),
740 "vref get failed\n");
742 priv->aclk = devm_clk_get_optional(&pdev->dev, "adc");
743 if (IS_ERR(priv->aclk))
744 return dev_err_probe(&pdev->dev, PTR_ERR(priv->aclk),
745 "Can't get 'adc' clock\n");
747 priv->bclk = devm_clk_get_optional(&pdev->dev, "bus");
748 if (IS_ERR(priv->bclk))
749 return dev_err_probe(&pdev->dev, PTR_ERR(priv->bclk),
750 "Can't get 'bus' clock\n");
752 ret = stm32_adc_core_switches_probe(dev, priv);
756 pm_runtime_get_noresume(dev);
757 pm_runtime_set_active(dev);
758 pm_runtime_set_autosuspend_delay(dev, STM32_ADC_CORE_SLEEP_DELAY_MS);
759 pm_runtime_use_autosuspend(dev);
760 pm_runtime_enable(dev);
762 ret = stm32_adc_core_hw_start(dev);
766 ret = stm32_adc_probe_identification(pdev, priv);
770 ret = regulator_get_voltage(priv->vref);
772 dev_err(&pdev->dev, "vref get voltage failed, %d\n", ret);
775 priv->common.vref_mv = ret / 1000;
776 dev_dbg(&pdev->dev, "vref+=%dmV\n", priv->common.vref_mv);
778 ret = of_property_read_u32(pdev->dev.of_node, "st,max-clk-rate-hz",
781 priv->max_clk_rate = min(max_rate, priv->cfg->max_clk_rate_hz);
783 priv->max_clk_rate = priv->cfg->max_clk_rate_hz;
785 ret = priv->cfg->clk_sel(pdev, priv);
789 ret = stm32_adc_irq_probe(pdev, priv);
793 ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
795 dev_err(&pdev->dev, "failed to populate DT children\n");
799 pm_runtime_mark_last_busy(dev);
800 pm_runtime_put_autosuspend(dev);
805 stm32_adc_irq_remove(pdev, priv);
807 stm32_adc_core_hw_stop(dev);
809 pm_runtime_disable(dev);
810 pm_runtime_set_suspended(dev);
811 pm_runtime_put_noidle(dev);
816 static int stm32_adc_remove(struct platform_device *pdev)
818 struct stm32_adc_common *common = platform_get_drvdata(pdev);
819 struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
821 pm_runtime_get_sync(&pdev->dev);
822 of_platform_depopulate(&pdev->dev);
823 stm32_adc_irq_remove(pdev, priv);
824 stm32_adc_core_hw_stop(&pdev->dev);
825 pm_runtime_disable(&pdev->dev);
826 pm_runtime_set_suspended(&pdev->dev);
827 pm_runtime_put_noidle(&pdev->dev);
832 static int stm32_adc_core_runtime_suspend(struct device *dev)
834 stm32_adc_core_hw_stop(dev);
839 static int stm32_adc_core_runtime_resume(struct device *dev)
841 return stm32_adc_core_hw_start(dev);
844 static int stm32_adc_core_runtime_idle(struct device *dev)
846 pm_runtime_mark_last_busy(dev);
851 static DEFINE_RUNTIME_DEV_PM_OPS(stm32_adc_core_pm_ops,
852 stm32_adc_core_runtime_suspend,
853 stm32_adc_core_runtime_resume,
854 stm32_adc_core_runtime_idle);
856 static const struct stm32_adc_priv_cfg stm32f4_adc_priv_cfg = {
857 .regs = &stm32f4_adc_common_regs,
858 .clk_sel = stm32f4_adc_clk_sel,
859 .max_clk_rate_hz = 36000000,
864 static const struct stm32_adc_priv_cfg stm32h7_adc_priv_cfg = {
865 .regs = &stm32h7_adc_common_regs,
866 .clk_sel = stm32h7_adc_clk_sel,
867 .max_clk_rate_hz = 36000000,
868 .has_syscfg = HAS_VBOOSTER,
873 static const struct stm32_adc_priv_cfg stm32mp1_adc_priv_cfg = {
874 .regs = &stm32h7_adc_common_regs,
875 .clk_sel = stm32h7_adc_clk_sel,
876 .max_clk_rate_hz = 36000000,
877 .has_syscfg = HAS_VBOOSTER | HAS_ANASWVDD,
878 .ipid = STM32MP15_IPIDR_NUMBER,
882 static const struct stm32_adc_priv_cfg stm32mp13_adc_priv_cfg = {
883 .regs = &stm32mp13_adc_common_regs,
884 .clk_sel = stm32h7_adc_clk_sel,
885 .max_clk_rate_hz = 75 * HZ_PER_MHZ,
886 .ipid = STM32MP13_IPIDR_NUMBER,
890 static const struct of_device_id stm32_adc_of_match[] = {
892 .compatible = "st,stm32f4-adc-core",
893 .data = (void *)&stm32f4_adc_priv_cfg
895 .compatible = "st,stm32h7-adc-core",
896 .data = (void *)&stm32h7_adc_priv_cfg
898 .compatible = "st,stm32mp1-adc-core",
899 .data = (void *)&stm32mp1_adc_priv_cfg
901 .compatible = "st,stm32mp13-adc-core",
902 .data = (void *)&stm32mp13_adc_priv_cfg
906 MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
908 static struct platform_driver stm32_adc_driver = {
909 .probe = stm32_adc_probe,
910 .remove = stm32_adc_remove,
912 .name = "stm32-adc-core",
913 .of_match_table = stm32_adc_of_match,
914 .pm = pm_ptr(&stm32_adc_core_pm_ops),
917 module_platform_driver(stm32_adc_driver);
920 MODULE_DESCRIPTION("STMicroelectronics STM32 ADC core driver");
921 MODULE_LICENSE("GPL v2");
922 MODULE_ALIAS("platform:stm32-adc-core");