1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
6 * Originally based on the Linux kernel v4.18 drivers/iio/adc/stm32-adc-core.c.
11 #include <power/regulator.h>
12 #include "stm32-adc-core.h"
14 /* STM32H7 - common registers for all ADC instances */
15 #define STM32H7_ADC_CCR (STM32_ADCX_COMN_OFFSET + 0x08)
17 /* STM32H7_ADC_CCR - bit fields */
18 #define STM32H7_PRESC_SHIFT 18
19 #define STM32H7_PRESC_MASK GENMASK(21, 18)
20 #define STM32H7_CKMODE_SHIFT 16
21 #define STM32H7_CKMODE_MASK GENMASK(17, 16)
23 /* STM32 H7 maximum analog clock rate (from datasheet) */
24 #define STM32H7_ADC_MAX_CLK_RATE 36000000
27 * struct stm32h7_adc_ck_spec - specification for stm32h7 adc clock
28 * @ckmode: ADC clock mode, Async or sync with prescaler.
29 * @presc: prescaler bitfield for async clock mode
30 * @div: prescaler division ratio
32 struct stm32h7_adc_ck_spec {
38 static const struct stm32h7_adc_ck_spec stm32h7_adc_ckmodes_spec[] = {
39 /* 00: CK_ADC[1..3]: Asynchronous clock modes */
52 /* HCLK used: Synchronous clock modes (1, 2 or 4 prescaler) */
58 static int stm32h7_adc_clk_sel(struct udevice *dev,
59 struct stm32_adc_common *common)
65 /* stm32h7 bus clock is common for all ADC instances (mandatory) */
66 if (!clk_valid(&common->bclk)) {
67 dev_err(dev, "No bclk clock found\n");
72 * stm32h7 can use either 'bus' or 'adc' clock for analog circuitry.
73 * So, choice is to have bus clock mandatory and adc clock optional.
74 * If optional 'adc' clock has been found, then try to use it first.
76 if (clk_valid(&common->aclk)) {
78 * Asynchronous clock modes (e.g. ckmode == 0)
79 * From spec: PLL output musn't exceed max rate
81 rate = clk_get_rate(&common->aclk);
83 dev_err(dev, "Invalid aclk rate: 0\n");
87 for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
88 ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
89 presc = stm32h7_adc_ckmodes_spec[i].presc;
90 div = stm32h7_adc_ckmodes_spec[i].div;
95 if ((rate / div) <= STM32H7_ADC_MAX_CLK_RATE)
100 /* Synchronous clock modes (e.g. ckmode is 1, 2 or 3) */
101 rate = clk_get_rate(&common->bclk);
103 dev_err(dev, "Invalid bus clock rate: 0\n");
107 for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
108 ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
109 presc = stm32h7_adc_ckmodes_spec[i].presc;
110 div = stm32h7_adc_ckmodes_spec[i].div;
115 if ((rate / div) <= STM32H7_ADC_MAX_CLK_RATE)
119 dev_err(dev, "clk selection failed\n");
123 /* rate used later by each ADC instance to control BOOST mode */
124 common->rate = rate / div;
126 /* Set common clock mode and prescaler */
127 clrsetbits_le32(common->base + STM32H7_ADC_CCR,
128 STM32H7_CKMODE_MASK | STM32H7_PRESC_MASK,
129 ckmode << STM32H7_CKMODE_SHIFT |
130 presc << STM32H7_PRESC_SHIFT);
132 dev_dbg(dev, "Using %s clock/%d source at %ld kHz\n",
133 ckmode ? "bus" : "adc", div, common->rate / 1000);
138 static int stm32_adc_core_probe(struct udevice *dev)
140 struct stm32_adc_common *common = dev_get_priv(dev);
143 common->base = dev_read_addr_ptr(dev);
145 dev_err(dev, "can't get address\n");
149 ret = device_get_supply_regulator(dev, "vref-supply", &common->vref);
151 dev_err(dev, "can't get vref-supply: %d\n", ret);
155 ret = regulator_get_value(common->vref);
157 dev_err(dev, "can't get vref-supply value: %d\n", ret);
160 common->vref_uv = ret;
162 ret = clk_get_by_name(dev, "adc", &common->aclk);
164 ret = clk_enable(&common->aclk);
166 dev_err(dev, "Can't enable aclk: %d\n", ret);
171 ret = clk_get_by_name(dev, "bus", &common->bclk);
173 ret = clk_enable(&common->bclk);
175 dev_err(dev, "Can't enable bclk: %d\n", ret);
176 goto err_aclk_disable;
180 ret = stm32h7_adc_clk_sel(dev, common);
182 goto err_bclk_disable;
187 if (clk_valid(&common->bclk))
188 clk_disable(&common->bclk);
191 if (clk_valid(&common->aclk))
192 clk_disable(&common->aclk);
197 static const struct udevice_id stm32_adc_core_ids[] = {
198 { .compatible = "st,stm32h7-adc-core" },
199 { .compatible = "st,stm32mp1-adc-core" },
203 U_BOOT_DRIVER(stm32_adc_core) = {
204 .name = "stm32-adc-core",
205 .id = UCLASS_SIMPLE_BUS,
206 .of_match = stm32_adc_core_ids,
207 .probe = stm32_adc_core_probe,
208 .priv_auto_alloc_size = sizeof(struct stm32_adc_common),