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 <linux/iopoll.h>
13 #include "stm32-adc-core.h"
15 /* STM32H7 - Registers for each ADC instance */
16 #define STM32H7_ADC_ISR 0x00
17 #define STM32H7_ADC_CR 0x08
18 #define STM32H7_ADC_CFGR 0x0C
19 #define STM32H7_ADC_SMPR1 0x14
20 #define STM32H7_ADC_SMPR2 0x18
21 #define STM32H7_ADC_PCSEL 0x1C
22 #define STM32H7_ADC_SQR1 0x30
23 #define STM32H7_ADC_DR 0x40
24 #define STM32H7_ADC_DIFSEL 0xC0
26 /* STM32H7_ADC_ISR - bit fields */
27 #define STM32MP1_VREGREADY BIT(12)
28 #define STM32H7_EOC BIT(2)
29 #define STM32H7_ADRDY BIT(0)
31 /* STM32H7_ADC_CR - bit fields */
32 #define STM32H7_DEEPPWD BIT(29)
33 #define STM32H7_ADVREGEN BIT(28)
34 #define STM32H7_BOOST BIT(8)
35 #define STM32H7_ADSTART BIT(2)
36 #define STM32H7_ADDIS BIT(1)
37 #define STM32H7_ADEN BIT(0)
39 /* STM32H7_ADC_CFGR bit fields */
40 #define STM32H7_EXTEN GENMASK(11, 10)
41 #define STM32H7_DMNGT GENMASK(1, 0)
43 /* STM32H7_ADC_SQR1 - bit fields */
44 #define STM32H7_SQ1_SHIFT 6
46 /* BOOST bit must be set on STM32H7 when ADC clock is above 20MHz */
47 #define STM32H7_BOOST_CLKRATE 20000000UL
49 #define STM32_ADC_CH_MAX 20 /* max number of channels */
50 #define STM32_ADC_TIMEOUT_US 100000
52 struct stm32_adc_cfg {
53 unsigned int max_channels;
54 unsigned int num_bits;
61 const struct stm32_adc_cfg *cfg;
64 static int stm32_adc_stop(struct udevice *dev)
66 struct stm32_adc *adc = dev_get_priv(dev);
68 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADDIS);
69 clrbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_BOOST);
70 /* Setting DEEPPWD disables ADC vreg and clears ADVREGEN */
71 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_DEEPPWD);
72 adc->active_channel = -1;
77 static int stm32_adc_start_channel(struct udevice *dev, int channel)
79 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
80 struct stm32_adc_common *common = dev_get_priv(dev_get_parent(dev));
81 struct stm32_adc *adc = dev_get_priv(dev);
85 /* Exit deep power down, then enable ADC voltage regulator */
86 clrbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_DEEPPWD);
87 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADVREGEN);
88 if (common->rate > STM32H7_BOOST_CLKRATE)
89 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_BOOST);
91 /* Wait for startup time */
92 if (!adc->cfg->has_vregready) {
95 ret = readl_poll_timeout(adc->regs + STM32H7_ADC_ISR, val,
96 val & STM32MP1_VREGREADY,
97 STM32_ADC_TIMEOUT_US);
100 dev_err(dev, "Failed to enable vreg: %d\n", ret);
105 /* Only use single ended channels */
106 writel(0, adc->regs + STM32H7_ADC_DIFSEL);
108 /* Enable ADC, Poll for ADRDY to be set (after adc startup time) */
109 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADEN);
110 ret = readl_poll_timeout(adc->regs + STM32H7_ADC_ISR, val,
111 val & STM32H7_ADRDY, STM32_ADC_TIMEOUT_US);
114 dev_err(dev, "Failed to enable ADC: %d\n", ret);
118 /* Preselect channels */
119 writel(uc_pdata->channel_mask, adc->regs + STM32H7_ADC_PCSEL);
121 /* Set sampling time to max value by default */
122 writel(0xffffffff, adc->regs + STM32H7_ADC_SMPR1);
123 writel(0xffffffff, adc->regs + STM32H7_ADC_SMPR2);
125 /* Program regular sequence: chan in SQ1 & len = 0 for one channel */
126 writel(channel << STM32H7_SQ1_SHIFT, adc->regs + STM32H7_ADC_SQR1);
128 /* Trigger detection disabled (conversion can be launched in SW) */
129 clrbits_le32(adc->regs + STM32H7_ADC_CFGR, STM32H7_EXTEN |
131 adc->active_channel = channel;
136 static int stm32_adc_channel_data(struct udevice *dev, int channel,
139 struct stm32_adc *adc = dev_get_priv(dev);
143 if (channel != adc->active_channel) {
144 dev_err(dev, "Requested channel is not active!\n");
148 setbits_le32(adc->regs + STM32H7_ADC_CR, STM32H7_ADSTART);
149 ret = readl_poll_timeout(adc->regs + STM32H7_ADC_ISR, val,
150 val & STM32H7_EOC, STM32_ADC_TIMEOUT_US);
152 dev_err(dev, "conversion timed out: %d\n", ret);
156 *data = readl(adc->regs + STM32H7_ADC_DR);
161 static int stm32_adc_chan_of_init(struct udevice *dev)
163 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
164 struct stm32_adc *adc = dev_get_priv(dev);
165 u32 chans[STM32_ADC_CH_MAX];
166 unsigned int i, num_channels;
169 /* Retrieve single ended channels listed in device tree */
170 ret = dev_read_size(dev, "st,adc-channels");
172 dev_err(dev, "can't get st,adc-channels: %d\n", ret);
175 num_channels = ret / sizeof(u32);
177 if (num_channels > adc->cfg->max_channels) {
178 dev_err(dev, "too many st,adc-channels: %d\n", num_channels);
182 ret = dev_read_u32_array(dev, "st,adc-channels", chans, num_channels);
184 dev_err(dev, "can't read st,adc-channels: %d\n", ret);
188 for (i = 0; i < num_channels; i++) {
189 if (chans[i] >= adc->cfg->max_channels) {
190 dev_err(dev, "bad channel %u\n", chans[i]);
193 uc_pdata->channel_mask |= 1 << chans[i];
196 uc_pdata->data_mask = (1 << adc->cfg->num_bits) - 1;
197 uc_pdata->data_format = ADC_DATA_FORMAT_BIN;
198 uc_pdata->data_timeout_us = 100000;
203 static int stm32_adc_probe(struct udevice *dev)
205 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
206 struct stm32_adc_common *common = dev_get_priv(dev_get_parent(dev));
207 struct stm32_adc *adc = dev_get_priv(dev);
210 offset = dev_read_u32_default(dev, "reg", -ENODATA);
212 dev_err(dev, "Can't read reg property\n");
215 adc->regs = common->base + offset;
216 adc->cfg = (const struct stm32_adc_cfg *)dev_get_driver_data(dev);
218 /* VDD supplied by common vref pin */
219 uc_pdata->vdd_supply = common->vref;
220 uc_pdata->vdd_microvolts = common->vref_uv;
221 uc_pdata->vss_microvolts = 0;
223 return stm32_adc_chan_of_init(dev);
226 static const struct adc_ops stm32_adc_ops = {
227 .start_channel = stm32_adc_start_channel,
228 .channel_data = stm32_adc_channel_data,
229 .stop = stm32_adc_stop,
232 static const struct stm32_adc_cfg stm32h7_adc_cfg = {
234 .max_channels = STM32_ADC_CH_MAX,
237 static const struct stm32_adc_cfg stm32mp1_adc_cfg = {
239 .max_channels = STM32_ADC_CH_MAX,
240 .has_vregready = true,
243 static const struct udevice_id stm32_adc_ids[] = {
244 { .compatible = "st,stm32h7-adc",
245 .data = (ulong)&stm32h7_adc_cfg },
246 { .compatible = "st,stm32mp1-adc",
247 .data = (ulong)&stm32mp1_adc_cfg },
251 U_BOOT_DRIVER(stm32_adc) = {
254 .of_match = stm32_adc_ids,
255 .probe = stm32_adc_probe,
256 .ops = &stm32_adc_ops,
257 .priv_auto_alloc_size = sizeof(struct stm32_adc),