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_DEEPPWD BIT(29)
36 #define STM32H7_ADVREGEN BIT(28)
37 #define STM32H7_BOOST BIT(8)
38 #define STM32H7_ADSTART BIT(2)
39 #define STM32H7_ADDIS BIT(1)
40 #define STM32H7_ADEN BIT(0)
42 /* STM32H7_ADC_CFGR bit fields */
43 #define STM32H7_EXTEN GENMASK(11, 10)
44 #define STM32H7_DMNGT GENMASK(1, 0)
46 /* STM32H7_ADC_SQR1 - bit fields */
47 #define STM32H7_SQ1_SHIFT 6
49 /* BOOST bit must be set on STM32H7 when ADC clock is above 20MHz */
50 #define STM32H7_BOOST_CLKRATE 20000000UL
52 #define STM32_ADC_CH_MAX 20 /* max number of channels */
53 #define STM32_ADC_TIMEOUT_US 100000
55 struct stm32_adc_cfg {
56 unsigned int max_channels;
57 unsigned int num_bits;
64 const struct stm32_adc_cfg *cfg;
67 static int stm32_adc_stop(struct udevice *dev)
69 struct stm32_adc *adc = dev_get_priv(dev);
71 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADDIS);
72 clrbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_BOOST);
73 /* Setting DEEPPWD disables ADC vreg and clears ADVREGEN */
74 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_DEEPPWD);
75 adc->active_channel = -1;
80 static int stm32_adc_start_channel(struct udevice *dev, int channel)
82 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
83 struct stm32_adc_common *common = dev_get_priv(dev_get_parent(dev));
84 struct stm32_adc *adc = dev_get_priv(dev);
88 /* Exit deep power down, then enable ADC voltage regulator */
89 clrbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_DEEPPWD);
90 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADVREGEN);
91 if (common->rate > STM32H7_BOOST_CLKRATE)
92 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_BOOST);
94 /* Wait for startup time */
95 if (!adc->cfg->has_vregready) {
98 ret = readl_poll_timeout(adc->regs + STM32H7_ADC_ISR, val,
99 val & STM32MP1_VREGREADY,
100 STM32_ADC_TIMEOUT_US);
103 dev_err(dev, "Failed to enable vreg: %d\n", ret);
108 /* Only use single ended channels */
109 writel(0, adc->regs + STM32H7_ADC_DIFSEL);
111 /* Enable ADC, Poll for ADRDY to be set (after adc startup time) */
112 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADEN);
113 ret = readl_poll_timeout(adc->regs + STM32H7_ADC_ISR, val,
114 val & STM32H7_ADRDY, STM32_ADC_TIMEOUT_US);
117 dev_err(dev, "Failed to enable ADC: %d\n", ret);
121 /* Preselect channels */
122 writel(uc_pdata->channel_mask, adc->regs + STM32H7_ADC_PCSEL);
124 /* Set sampling time to max value by default */
125 writel(0xffffffff, adc->regs + STM32H7_ADC_SMPR1);
126 writel(0xffffffff, adc->regs + STM32H7_ADC_SMPR2);
128 /* Program regular sequence: chan in SQ1 & len = 0 for one channel */
129 writel(channel << STM32H7_SQ1_SHIFT, adc->regs + STM32H7_ADC_SQR1);
131 /* Trigger detection disabled (conversion can be launched in SW) */
132 clrbits_le32(adc->regs + STM32H7_ADC_CFGR, STM32H7_EXTEN |
134 adc->active_channel = channel;
139 static int stm32_adc_channel_data(struct udevice *dev, int channel,
142 struct stm32_adc *adc = dev_get_priv(dev);
146 if (channel != adc->active_channel) {
147 dev_err(dev, "Requested channel is not active!\n");
151 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADSTART);
152 ret = readl_poll_timeout(adc->regs + STM32H7_ADC_ISR, val,
153 val & STM32H7_EOC, STM32_ADC_TIMEOUT_US);
155 dev_err(dev, "conversion timed out: %d\n", ret);
159 *data = readl(adc->regs + STM32H7_ADC_DR);
164 static int stm32_adc_chan_of_init(struct udevice *dev)
166 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
167 struct stm32_adc *adc = dev_get_priv(dev);
168 u32 chans[STM32_ADC_CH_MAX];
169 unsigned int i, num_channels;
172 /* Retrieve single ended channels listed in device tree */
173 ret = dev_read_size(dev, "st,adc-channels");
175 dev_err(dev, "can't get st,adc-channels: %d\n", ret);
178 num_channels = ret / sizeof(u32);
180 if (num_channels > adc->cfg->max_channels) {
181 dev_err(dev, "too many st,adc-channels: %d\n", num_channels);
185 ret = dev_read_u32_array(dev, "st,adc-channels", chans, num_channels);
187 dev_err(dev, "can't read st,adc-channels: %d\n", ret);
191 for (i = 0; i < num_channels; i++) {
192 if (chans[i] >= adc->cfg->max_channels) {
193 dev_err(dev, "bad channel %u\n", chans[i]);
196 uc_pdata->channel_mask |= 1 << chans[i];
199 uc_pdata->data_mask = (1 << adc->cfg->num_bits) - 1;
200 uc_pdata->data_format = ADC_DATA_FORMAT_BIN;
201 uc_pdata->data_timeout_us = 100000;
206 static int stm32_adc_probe(struct udevice *dev)
208 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
209 struct stm32_adc_common *common = dev_get_priv(dev_get_parent(dev));
210 struct stm32_adc *adc = dev_get_priv(dev);
213 offset = dev_read_u32_default(dev, "reg", -ENODATA);
215 dev_err(dev, "Can't read reg property\n");
218 adc->regs = common->base + offset;
219 adc->cfg = (const struct stm32_adc_cfg *)dev_get_driver_data(dev);
221 /* VDD supplied by common vref pin */
222 uc_pdata->vdd_supply = common->vref;
223 uc_pdata->vdd_microvolts = common->vref_uv;
224 uc_pdata->vss_microvolts = 0;
226 return stm32_adc_chan_of_init(dev);
229 static const struct adc_ops stm32_adc_ops = {
230 .start_channel = stm32_adc_start_channel,
231 .channel_data = stm32_adc_channel_data,
232 .stop = stm32_adc_stop,
235 static const struct stm32_adc_cfg stm32h7_adc_cfg = {
237 .max_channels = STM32_ADC_CH_MAX,
240 static const struct stm32_adc_cfg stm32mp1_adc_cfg = {
242 .max_channels = STM32_ADC_CH_MAX,
243 .has_vregready = true,
246 static const struct udevice_id stm32_adc_ids[] = {
247 { .compatible = "st,stm32h7-adc",
248 .data = (ulong)&stm32h7_adc_cfg },
249 { .compatible = "st,stm32mp1-adc",
250 .data = (ulong)&stm32mp1_adc_cfg },
254 U_BOOT_DRIVER(stm32_adc) = {
257 .of_match = stm32_adc_ids,
258 .probe = stm32_adc_probe,
259 .ops = &stm32_adc_ops,
260 .priv_auto_alloc_size = sizeof(struct stm32_adc),