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 <dm/device_compat.h>
12 #include <linux/bitops.h>
13 #include <power/regulator.h>
14 #include "stm32-adc-core.h"
16 /* STM32H7 - common registers for all ADC instances */
17 #define STM32H7_ADC_CCR (STM32_ADCX_COMN_OFFSET + 0x08)
19 /* STM32H7_ADC_CCR - bit fields */
20 #define STM32H7_PRESC_SHIFT 18
21 #define STM32H7_PRESC_MASK GENMASK(21, 18)
22 #define STM32H7_CKMODE_SHIFT 16
23 #define STM32H7_CKMODE_MASK GENMASK(17, 16)
25 /* STM32 H7 maximum analog clock rate (from datasheet) */
26 #define STM32H7_ADC_MAX_CLK_RATE 36000000
29 * struct stm32h7_adc_ck_spec - specification for stm32h7 adc clock
30 * @ckmode: ADC clock mode, Async or sync with prescaler.
31 * @presc: prescaler bitfield for async clock mode
32 * @div: prescaler division ratio
34 struct stm32h7_adc_ck_spec {
40 static const struct stm32h7_adc_ck_spec stm32h7_adc_ckmodes_spec[] = {
41 /* 00: CK_ADC[1..3]: Asynchronous clock modes */
54 /* HCLK used: Synchronous clock modes (1, 2 or 4 prescaler) */
60 static int stm32h7_adc_clk_sel(struct udevice *dev,
61 struct stm32_adc_common *common)
68 /* stm32h7 bus clock is common for all ADC instances (mandatory) */
69 if (!clk_valid(&common->bclk)) {
70 dev_err(dev, "No bclk clock found\n");
75 * stm32h7 can use either 'bus' or 'adc' clock for analog circuitry.
76 * So, choice is to have bus clock mandatory and adc clock optional.
77 * If optional 'adc' clock has been found, then try to use it first.
79 if (clk_valid(&common->aclk)) {
81 * Asynchronous clock modes (e.g. ckmode == 0)
82 * From spec: PLL output musn't exceed max rate
84 rate = clk_get_rate(&common->aclk);
86 dev_err(dev, "Invalid aclk rate: 0\n");
90 for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
91 ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
92 presc = stm32h7_adc_ckmodes_spec[i].presc;
93 div = stm32h7_adc_ckmodes_spec[i].div;
98 if ((rate / div) <= STM32H7_ADC_MAX_CLK_RATE)
103 /* Synchronous clock modes (e.g. ckmode is 1, 2 or 3) */
104 rate = clk_get_rate(&common->bclk);
106 dev_err(dev, "Invalid bus clock rate: 0\n");
110 for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
111 ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
112 presc = stm32h7_adc_ckmodes_spec[i].presc;
113 div = stm32h7_adc_ckmodes_spec[i].div;
118 if ((rate / div) <= STM32H7_ADC_MAX_CLK_RATE)
122 dev_err(dev, "clk selection failed\n");
126 /* rate used later by each ADC instance to control BOOST mode */
127 common->rate = rate / div;
129 /* Set common clock mode and prescaler */
130 clrsetbits_le32(common->base + STM32H7_ADC_CCR,
131 STM32H7_CKMODE_MASK | STM32H7_PRESC_MASK,
132 ckmode << STM32H7_CKMODE_SHIFT |
133 presc << STM32H7_PRESC_SHIFT);
135 dev_dbg(dev, "Using %s clock/%d source at %ld kHz\n",
136 ckmode ? "bus" : "adc", div, common->rate / 1000);
141 static int stm32_adc_core_probe(struct udevice *dev)
143 struct stm32_adc_common *common = dev_get_priv(dev);
146 common->base = dev_read_addr_ptr(dev);
148 dev_err(dev, "can't get address\n");
152 ret = device_get_supply_regulator(dev, "vref-supply", &common->vref);
154 dev_err(dev, "can't get vref-supply: %d\n", ret);
158 ret = regulator_get_value(common->vref);
160 dev_err(dev, "can't get vref-supply value: %d\n", ret);
163 common->vref_uv = ret;
165 ret = clk_get_by_name(dev, "adc", &common->aclk);
167 ret = clk_enable(&common->aclk);
169 dev_err(dev, "Can't enable aclk: %d\n", ret);
174 ret = clk_get_by_name(dev, "bus", &common->bclk);
176 ret = clk_enable(&common->bclk);
178 dev_err(dev, "Can't enable bclk: %d\n", ret);
179 goto err_aclk_disable;
183 ret = stm32h7_adc_clk_sel(dev, common);
185 goto err_bclk_disable;
190 if (clk_valid(&common->bclk))
191 clk_disable(&common->bclk);
194 if (clk_valid(&common->aclk))
195 clk_disable(&common->aclk);
200 static const struct udevice_id stm32_adc_core_ids[] = {
201 { .compatible = "st,stm32h7-adc-core" },
202 { .compatible = "st,stm32mp1-adc-core" },
206 U_BOOT_DRIVER(stm32_adc_core) = {
207 .name = "stm32-adc-core",
208 .id = UCLASS_SIMPLE_BUS,
209 .of_match = stm32_adc_core_ids,
210 .probe = stm32_adc_core_probe,
211 .priv_auto_alloc_size = sizeof(struct stm32_adc_common),