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.c.
12 #include <dm/device_compat.h>
13 #include <linux/bitops.h>
14 #include <linux/delay.h>
15 #include <linux/iopoll.h>
16 #include "stm32-adc-core.h"
18 /* STM32H7 - Registers for each ADC instance */
19 #define STM32H7_ADC_ISR 0x00
20 #define STM32H7_ADC_CR 0x08
21 #define STM32H7_ADC_CFGR 0x0C
22 #define STM32H7_ADC_SMPR1 0x14
23 #define STM32H7_ADC_SMPR2 0x18
24 #define STM32H7_ADC_PCSEL 0x1C
25 #define STM32H7_ADC_SQR1 0x30
26 #define STM32H7_ADC_DR 0x40
27 #define STM32H7_ADC_DIFSEL 0xC0
29 /* STM32H7_ADC_ISR - bit fields */
30 #define STM32MP1_VREGREADY BIT(12)
31 #define STM32H7_EOC BIT(2)
32 #define STM32H7_ADRDY BIT(0)
34 /* STM32H7_ADC_CR - bit fields */
35 #define STM32H7_ADCAL BIT(31)
36 #define STM32H7_ADCALDIF BIT(30)
37 #define STM32H7_DEEPPWD BIT(29)
38 #define STM32H7_ADVREGEN BIT(28)
39 #define STM32H7_ADCALLIN BIT(16)
40 #define STM32H7_BOOST BIT(8)
41 #define STM32H7_ADSTART BIT(2)
42 #define STM32H7_ADDIS BIT(1)
43 #define STM32H7_ADEN BIT(0)
45 /* STM32H7_ADC_CFGR bit fields */
46 #define STM32H7_EXTEN GENMASK(11, 10)
47 #define STM32H7_DMNGT GENMASK(1, 0)
49 /* STM32H7_ADC_SQR1 - bit fields */
50 #define STM32H7_SQ1_SHIFT 6
52 /* BOOST bit must be set on STM32H7 when ADC clock is above 20MHz */
53 #define STM32H7_BOOST_CLKRATE 20000000UL
55 #define STM32_ADC_CH_MAX 20 /* max number of channels */
56 #define STM32_ADC_TIMEOUT_US 100000
58 struct stm32_adc_cfg {
59 unsigned int max_channels;
60 unsigned int num_bits;
67 const struct stm32_adc_cfg *cfg;
70 static void stm32_adc_enter_pwr_down(struct udevice *dev)
72 struct stm32_adc *adc = dev_get_priv(dev);
74 clrbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_BOOST);
75 /* Setting DEEPPWD disables ADC vreg and clears ADVREGEN */
76 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_DEEPPWD);
79 static int stm32_adc_exit_pwr_down(struct udevice *dev)
81 struct stm32_adc_common *common = dev_get_priv(dev_get_parent(dev));
82 struct stm32_adc *adc = dev_get_priv(dev);
86 /* return immediately if ADC is not in deep power down mode */
87 if (!(readl(adc->regs + STM32H7_ADC_CR) & STM32H7_DEEPPWD))
90 /* Exit deep power down, then enable ADC voltage regulator */
91 clrbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_DEEPPWD);
92 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADVREGEN);
94 if (common->rate > STM32H7_BOOST_CLKRATE)
95 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_BOOST);
97 /* Wait for startup time */
98 if (!adc->cfg->has_vregready) {
103 ret = readl_poll_timeout(adc->regs + STM32H7_ADC_ISR, val,
104 val & STM32MP1_VREGREADY,
105 STM32_ADC_TIMEOUT_US);
107 stm32_adc_enter_pwr_down(dev);
108 dev_err(dev, "Failed to enable vreg: %d\n", ret);
114 static int stm32_adc_stop(struct udevice *dev)
116 struct stm32_adc *adc = dev_get_priv(dev);
118 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADDIS);
119 stm32_adc_enter_pwr_down(dev);
120 adc->active_channel = -1;
125 static int stm32_adc_start_channel(struct udevice *dev, int channel)
127 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
128 struct stm32_adc *adc = dev_get_priv(dev);
132 ret = stm32_adc_exit_pwr_down(dev);
136 /* Only use single ended channels */
137 writel(0, adc->regs + STM32H7_ADC_DIFSEL);
139 /* Enable ADC, Poll for ADRDY to be set (after adc startup time) */
140 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADEN);
141 ret = readl_poll_timeout(adc->regs + STM32H7_ADC_ISR, val,
142 val & STM32H7_ADRDY, STM32_ADC_TIMEOUT_US);
145 dev_err(dev, "Failed to enable ADC: %d\n", ret);
149 /* Preselect channels */
150 writel(uc_pdata->channel_mask, adc->regs + STM32H7_ADC_PCSEL);
152 /* Set sampling time to max value by default */
153 writel(0xffffffff, adc->regs + STM32H7_ADC_SMPR1);
154 writel(0xffffffff, adc->regs + STM32H7_ADC_SMPR2);
156 /* Program regular sequence: chan in SQ1 & len = 0 for one channel */
157 writel(channel << STM32H7_SQ1_SHIFT, adc->regs + STM32H7_ADC_SQR1);
159 /* Trigger detection disabled (conversion can be launched in SW) */
160 clrbits_le32(adc->regs + STM32H7_ADC_CFGR, STM32H7_EXTEN |
162 adc->active_channel = channel;
167 static int stm32_adc_channel_data(struct udevice *dev, int channel,
170 struct stm32_adc *adc = dev_get_priv(dev);
174 if (channel != adc->active_channel) {
175 dev_err(dev, "Requested channel is not active!\n");
179 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADSTART);
180 ret = readl_poll_timeout(adc->regs + STM32H7_ADC_ISR, val,
181 val & STM32H7_EOC, STM32_ADC_TIMEOUT_US);
183 dev_err(dev, "conversion timed out: %d\n", ret);
187 *data = readl(adc->regs + STM32H7_ADC_DR);
193 * Fixed timeout value for ADC calibration.
195 * - low clock frequency (0.12 MHz min)
196 * - maximum prescalers
197 * Calibration requires:
198 * - 16384 ADC clock cycle for the linear calibration
199 * - 20 ADC clock cycle for the offset calibration
201 * Set to 100ms for now
203 #define STM32H7_ADC_CALIB_TIMEOUT_US 100000
205 static int stm32_adc_selfcalib(struct udevice *dev)
207 struct stm32_adc *adc = dev_get_priv(dev);
212 * Select calibration mode:
213 * - Offset calibration for single ended inputs
214 * - No linearity calibration. Done in next step.
216 clrbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADCALDIF | STM32H7_ADCALLIN);
218 /* Start calibration, then wait for completion */
219 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADCAL);
220 ret = readl_poll_sleep_timeout(adc->regs + STM32H7_ADC_CR, val,
221 !(val & STM32H7_ADCAL), 100,
222 STM32H7_ADC_CALIB_TIMEOUT_US);
224 dev_err(dev, "calibration failed\n");
229 * Select calibration mode, then start calibration:
230 * - Offset calibration for differential input
231 * - Linearity calibration (needs to be done only once for single/diff)
232 * will run simultaneously with offset calibration.
234 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADCALDIF | STM32H7_ADCALLIN);
236 /* Start calibration, then wait for completion */
237 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADCAL);
238 ret = readl_poll_sleep_timeout(adc->regs + STM32H7_ADC_CR, val,
239 !(val & STM32H7_ADCAL), 100,
240 STM32H7_ADC_CALIB_TIMEOUT_US);
242 dev_err(dev, "calibration failed\n");
245 clrbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADCALDIF | STM32H7_ADCALLIN);
250 static int stm32_adc_get_legacy_chan_count(struct udevice *dev)
254 /* Retrieve single ended channels listed in device tree */
255 ret = dev_read_size(dev, "st,adc-channels");
257 dev_err(dev, "can't get st,adc-channels: %d\n", ret);
261 return (ret / sizeof(u32));
264 static int stm32_adc_legacy_chan_init(struct udevice *dev, unsigned int num_channels)
266 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
267 struct stm32_adc *adc = dev_get_priv(dev);
268 u32 chans[STM32_ADC_CH_MAX];
271 ret = dev_read_u32_array(dev, "st,adc-channels", chans, num_channels);
273 dev_err(dev, "can't read st,adc-channels: %d\n", ret);
277 for (i = 0; i < num_channels; i++) {
278 if (chans[i] >= adc->cfg->max_channels) {
279 dev_err(dev, "bad channel %u\n", chans[i]);
282 uc_pdata->channel_mask |= 1 << chans[i];
288 static int stm32_adc_generic_chan_init(struct udevice *dev, unsigned int num_channels)
290 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
291 struct stm32_adc *adc = dev_get_priv(dev);
295 ofnode_for_each_subnode(child, dev_ofnode(dev)) {
296 ret = ofnode_read_u32(child, "reg", &val);
298 dev_err(dev, "Missing channel index %d\n", ret);
302 if (val >= adc->cfg->max_channels) {
303 dev_err(dev, "Invalid channel %d\n", val);
307 uc_pdata->channel_mask |= 1 << val;
313 static int stm32_adc_chan_of_init(struct udevice *dev)
315 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
316 struct stm32_adc *adc = dev_get_priv(dev);
317 unsigned int num_channels;
321 num_channels = dev_get_child_count(dev);
322 /* If no channels have been found, fallback to channels legacy properties. */
326 ret = stm32_adc_get_legacy_chan_count(dev);
328 dev_err(dev, "No channel found\n");
330 } else if (ret < 0) {
336 if (num_channels > adc->cfg->max_channels) {
337 dev_err(dev, "too many st,adc-channels: %d\n", num_channels);
342 ret = stm32_adc_legacy_chan_init(dev, num_channels);
344 ret = stm32_adc_generic_chan_init(dev, num_channels);
348 uc_pdata->data_mask = (1 << adc->cfg->num_bits) - 1;
349 uc_pdata->data_format = ADC_DATA_FORMAT_BIN;
350 uc_pdata->data_timeout_us = 100000;
355 static int stm32_adc_probe(struct udevice *dev)
357 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
358 struct stm32_adc_common *common = dev_get_priv(dev_get_parent(dev));
359 struct stm32_adc *adc = dev_get_priv(dev);
362 offset = dev_read_u32_default(dev, "reg", -ENODATA);
364 dev_err(dev, "Can't read reg property\n");
367 adc->regs = common->base + offset;
368 adc->cfg = (const struct stm32_adc_cfg *)dev_get_driver_data(dev);
370 /* VDD supplied by common vref pin */
371 uc_pdata->vdd_supply = common->vref;
372 uc_pdata->vdd_microvolts = common->vref_uv;
373 uc_pdata->vss_microvolts = 0;
375 ret = stm32_adc_chan_of_init(dev);
379 ret = stm32_adc_exit_pwr_down(dev);
383 ret = stm32_adc_selfcalib(dev);
385 stm32_adc_enter_pwr_down(dev);
390 static const struct adc_ops stm32_adc_ops = {
391 .start_channel = stm32_adc_start_channel,
392 .channel_data = stm32_adc_channel_data,
393 .stop = stm32_adc_stop,
396 static const struct stm32_adc_cfg stm32h7_adc_cfg = {
398 .max_channels = STM32_ADC_CH_MAX,
401 static const struct stm32_adc_cfg stm32mp1_adc_cfg = {
403 .max_channels = STM32_ADC_CH_MAX,
404 .has_vregready = true,
407 static const struct udevice_id stm32_adc_ids[] = {
408 { .compatible = "st,stm32h7-adc",
409 .data = (ulong)&stm32h7_adc_cfg },
410 { .compatible = "st,stm32mp1-adc",
411 .data = (ulong)&stm32mp1_adc_cfg },
415 U_BOOT_DRIVER(stm32_adc) = {
418 .of_match = stm32_adc_ids,
419 .probe = stm32_adc_probe,
420 .ops = &stm32_adc_ops,
421 .priv_auto = sizeof(struct stm32_adc),