xref: /linux/drivers/iio/adc/stm32-adc.c (revision d7705f35448ada5a04f15326404e40d4254c538d)
16e93e261SBenjamin Gaignard // SPDX-License-Identifier: GPL-2.0
20f883b22SFabrice Gasnier /*
30f883b22SFabrice Gasnier  * This file is part of STM32 ADC driver
40f883b22SFabrice Gasnier  *
50f883b22SFabrice Gasnier  * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
60f883b22SFabrice Gasnier  * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
70f883b22SFabrice Gasnier  */
80f883b22SFabrice Gasnier 
90f883b22SFabrice Gasnier #include <linux/clk.h>
100f883b22SFabrice Gasnier #include <linux/delay.h>
112763ea05SFabrice Gasnier #include <linux/dma-mapping.h>
122763ea05SFabrice Gasnier #include <linux/dmaengine.h>
130f883b22SFabrice Gasnier #include <linux/iio/iio.h>
14da9b9485SFabrice Gasnier #include <linux/iio/buffer.h>
15f0b638a7SFabrice Gasnier #include <linux/iio/timer/stm32-lptim-trigger.h>
16f24a33b3SFabrice Gasnier #include <linux/iio/timer/stm32-timer-trigger.h>
17da9b9485SFabrice Gasnier #include <linux/iio/trigger.h>
18da9b9485SFabrice Gasnier #include <linux/iio/trigger_consumer.h>
19da9b9485SFabrice Gasnier #include <linux/iio/triggered_buffer.h>
200f883b22SFabrice Gasnier #include <linux/interrupt.h>
210f883b22SFabrice Gasnier #include <linux/io.h>
2295e339b6SFabrice Gasnier #include <linux/iopoll.h>
230f883b22SFabrice Gasnier #include <linux/module.h>
24*d7705f35SNuno Sá #include <linux/mod_devicetable.h>
250e346b2cSOlivier Moysan #include <linux/nvmem-consumer.h>
260f883b22SFabrice Gasnier #include <linux/platform_device.h>
279bdbb113SFabrice Gasnier #include <linux/pm_runtime.h>
28*d7705f35SNuno Sá #include <linux/property.h>
290f883b22SFabrice Gasnier 
300f883b22SFabrice Gasnier #include "stm32-adc-core.h"
310f883b22SFabrice Gasnier 
3295e339b6SFabrice Gasnier /* Number of linear calibration shadow registers / LINCALRDYW control bits */
3395e339b6SFabrice Gasnier #define STM32H7_LINCALFACT_NUM		6
3495e339b6SFabrice Gasnier 
3595e339b6SFabrice Gasnier /* BOOST bit must be set on STM32H7 when ADC clock is above 20MHz */
3695e339b6SFabrice Gasnier #define STM32H7_BOOST_CLKRATE		20000000UL
3795e339b6SFabrice Gasnier 
380bae72aaSFabrice Gasnier #define STM32_ADC_CH_MAX		20	/* max number of channels */
3995bc8184SOlivier Moysan #define STM32_ADC_CH_SZ			16	/* max channel name size */
40da9b9485SFabrice Gasnier #define STM32_ADC_MAX_SQ		16	/* SQ1..SQ16 */
41ee2ac1cdSFabrice Gasnier #define STM32_ADC_MAX_SMP		7	/* SMPx range is [0..7] */
420f883b22SFabrice Gasnier #define STM32_ADC_TIMEOUT_US		100000
430f883b22SFabrice Gasnier #define STM32_ADC_TIMEOUT	(msecs_to_jiffies(STM32_ADC_TIMEOUT_US / 1000))
449bdbb113SFabrice Gasnier #define STM32_ADC_HW_STOP_DELAY_MS	100
450e346b2cSOlivier Moysan #define STM32_ADC_VREFINT_VOLTAGE	3300
460f883b22SFabrice Gasnier 
472763ea05SFabrice Gasnier #define STM32_DMA_BUFFER_SIZE		PAGE_SIZE
482763ea05SFabrice Gasnier 
49da9b9485SFabrice Gasnier /* External trigger enable */
50da9b9485SFabrice Gasnier enum stm32_adc_exten {
51da9b9485SFabrice Gasnier 	STM32_EXTEN_SWTRIG,
52da9b9485SFabrice Gasnier 	STM32_EXTEN_HWTRIG_RISING_EDGE,
53da9b9485SFabrice Gasnier 	STM32_EXTEN_HWTRIG_FALLING_EDGE,
54da9b9485SFabrice Gasnier 	STM32_EXTEN_HWTRIG_BOTH_EDGES,
55da9b9485SFabrice Gasnier };
56da9b9485SFabrice Gasnier 
57f24a33b3SFabrice Gasnier /* extsel - trigger mux selection value */
58f24a33b3SFabrice Gasnier enum stm32_adc_extsel {
59f24a33b3SFabrice Gasnier 	STM32_EXT0,
60f24a33b3SFabrice Gasnier 	STM32_EXT1,
61f24a33b3SFabrice Gasnier 	STM32_EXT2,
62f24a33b3SFabrice Gasnier 	STM32_EXT3,
63f24a33b3SFabrice Gasnier 	STM32_EXT4,
64f24a33b3SFabrice Gasnier 	STM32_EXT5,
65f24a33b3SFabrice Gasnier 	STM32_EXT6,
66f24a33b3SFabrice Gasnier 	STM32_EXT7,
67f24a33b3SFabrice Gasnier 	STM32_EXT8,
68f24a33b3SFabrice Gasnier 	STM32_EXT9,
69f24a33b3SFabrice Gasnier 	STM32_EXT10,
70f24a33b3SFabrice Gasnier 	STM32_EXT11,
71f24a33b3SFabrice Gasnier 	STM32_EXT12,
72f24a33b3SFabrice Gasnier 	STM32_EXT13,
73f24a33b3SFabrice Gasnier 	STM32_EXT14,
74f24a33b3SFabrice Gasnier 	STM32_EXT15,
75f0b638a7SFabrice Gasnier 	STM32_EXT16,
76f0b638a7SFabrice Gasnier 	STM32_EXT17,
77f0b638a7SFabrice Gasnier 	STM32_EXT18,
78f0b638a7SFabrice Gasnier 	STM32_EXT19,
79f0b638a7SFabrice Gasnier 	STM32_EXT20,
80f24a33b3SFabrice Gasnier };
81f24a33b3SFabrice Gasnier 
82aec6e0d8SOlivier Moysan enum stm32_adc_int_ch {
83aec6e0d8SOlivier Moysan 	STM32_ADC_INT_CH_NONE = -1,
84aec6e0d8SOlivier Moysan 	STM32_ADC_INT_CH_VDDCORE,
85aec6e0d8SOlivier Moysan 	STM32_ADC_INT_CH_VREFINT,
86aec6e0d8SOlivier Moysan 	STM32_ADC_INT_CH_VBAT,
87aec6e0d8SOlivier Moysan 	STM32_ADC_INT_CH_NB,
88aec6e0d8SOlivier Moysan };
89aec6e0d8SOlivier Moysan 
90aec6e0d8SOlivier Moysan /**
91aec6e0d8SOlivier Moysan  * struct stm32_adc_ic - ADC internal channels
92aec6e0d8SOlivier Moysan  * @name:	name of the internal channel
93aec6e0d8SOlivier Moysan  * @idx:	internal channel enum index
94aec6e0d8SOlivier Moysan  */
95aec6e0d8SOlivier Moysan struct stm32_adc_ic {
96aec6e0d8SOlivier Moysan 	const char *name;
97aec6e0d8SOlivier Moysan 	u32 idx;
98aec6e0d8SOlivier Moysan };
99aec6e0d8SOlivier Moysan 
100aec6e0d8SOlivier Moysan static const struct stm32_adc_ic stm32_adc_ic[STM32_ADC_INT_CH_NB] = {
101aec6e0d8SOlivier Moysan 	{ "vddcore", STM32_ADC_INT_CH_VDDCORE },
102aec6e0d8SOlivier Moysan 	{ "vrefint", STM32_ADC_INT_CH_VREFINT },
103aec6e0d8SOlivier Moysan 	{ "vbat", STM32_ADC_INT_CH_VBAT },
104aec6e0d8SOlivier Moysan };
105aec6e0d8SOlivier Moysan 
106f24a33b3SFabrice Gasnier /**
107f24a33b3SFabrice Gasnier  * struct stm32_adc_trig_info - ADC trigger info
108f24a33b3SFabrice Gasnier  * @name:		name of the trigger, corresponding to its source
109f24a33b3SFabrice Gasnier  * @extsel:		trigger selection
110f24a33b3SFabrice Gasnier  */
111f24a33b3SFabrice Gasnier struct stm32_adc_trig_info {
112f24a33b3SFabrice Gasnier 	const char *name;
113f24a33b3SFabrice Gasnier 	enum stm32_adc_extsel extsel;
114f24a33b3SFabrice Gasnier };
115f24a33b3SFabrice Gasnier 
116da9b9485SFabrice Gasnier /**
11795e339b6SFabrice Gasnier  * struct stm32_adc_calib - optional adc calibration data
11895e339b6SFabrice Gasnier  * @calfact_s: Calibration offset for single ended channels
11995e339b6SFabrice Gasnier  * @calfact_d: Calibration offset in differential
12095e339b6SFabrice Gasnier  * @lincalfact: Linearity calibration factor
1210da98c7bSFabrice Gasnier  * @calibrated: Indicates calibration status
12295e339b6SFabrice Gasnier  */
12395e339b6SFabrice Gasnier struct stm32_adc_calib {
12495e339b6SFabrice Gasnier 	u32			calfact_s;
12595e339b6SFabrice Gasnier 	u32			calfact_d;
12695e339b6SFabrice Gasnier 	u32			lincalfact[STM32H7_LINCALFACT_NUM];
1270da98c7bSFabrice Gasnier 	bool			calibrated;
12895e339b6SFabrice Gasnier };
12995e339b6SFabrice Gasnier 
13095e339b6SFabrice Gasnier /**
1311cd92d42SFabrice Gasnier  * struct stm32_adc_regs - stm32 ADC misc registers & bitfield desc
132da9b9485SFabrice Gasnier  * @reg:		register offset
133da9b9485SFabrice Gasnier  * @mask:		bitfield mask
134da9b9485SFabrice Gasnier  * @shift:		left shift
135da9b9485SFabrice Gasnier  */
136da9b9485SFabrice Gasnier struct stm32_adc_regs {
137da9b9485SFabrice Gasnier 	int reg;
138da9b9485SFabrice Gasnier 	int mask;
139da9b9485SFabrice Gasnier 	int shift;
140da9b9485SFabrice Gasnier };
141da9b9485SFabrice Gasnier 
1420f883b22SFabrice Gasnier /**
1430e346b2cSOlivier Moysan  * struct stm32_adc_vrefint - stm32 ADC internal reference voltage data
1440e346b2cSOlivier Moysan  * @vrefint_cal:	vrefint calibration value from nvmem
1450e346b2cSOlivier Moysan  * @vrefint_data:	vrefint actual value
1460e346b2cSOlivier Moysan  */
1470e346b2cSOlivier Moysan struct stm32_adc_vrefint {
1480e346b2cSOlivier Moysan 	u32 vrefint_cal;
1490e346b2cSOlivier Moysan 	u32 vrefint_data;
1500e346b2cSOlivier Moysan };
1510e346b2cSOlivier Moysan 
1520e346b2cSOlivier Moysan /**
1531cd92d42SFabrice Gasnier  * struct stm32_adc_regspec - stm32 registers definition
15464ad7f64SFabrice Gasnier  * @dr:			data register offset
15564ad7f64SFabrice Gasnier  * @ier_eoc:		interrupt enable register & eocie bitfield
156cc06e67dSFabrice Gasnier  * @ier_ovr:		interrupt enable register & overrun bitfield
15764ad7f64SFabrice Gasnier  * @isr_eoc:		interrupt status register & eoc bitfield
158cc06e67dSFabrice Gasnier  * @isr_ovr:		interrupt status register & overrun bitfield
15964ad7f64SFabrice Gasnier  * @sqr:		reference to sequence registers array
16064ad7f64SFabrice Gasnier  * @exten:		trigger control register & bitfield
16164ad7f64SFabrice Gasnier  * @extsel:		trigger selection register & bitfield
16264ad7f64SFabrice Gasnier  * @res:		resolution selection register & bitfield
163ee2ac1cdSFabrice Gasnier  * @smpr:		smpr1 & smpr2 registers offset array
164ee2ac1cdSFabrice Gasnier  * @smp_bits:		smpr1 & smpr2 index and bitfields
165aec6e0d8SOlivier Moysan  * @or_vdd:		option register & vddcore bitfield
166aec6e0d8SOlivier Moysan  * @ccr_vbat:		common register & vbat bitfield
167aec6e0d8SOlivier Moysan  * @ccr_vref:		common register & vrefint bitfield
16864ad7f64SFabrice Gasnier  */
16964ad7f64SFabrice Gasnier struct stm32_adc_regspec {
17064ad7f64SFabrice Gasnier 	const u32 dr;
17164ad7f64SFabrice Gasnier 	const struct stm32_adc_regs ier_eoc;
172cc06e67dSFabrice Gasnier 	const struct stm32_adc_regs ier_ovr;
17364ad7f64SFabrice Gasnier 	const struct stm32_adc_regs isr_eoc;
174cc06e67dSFabrice Gasnier 	const struct stm32_adc_regs isr_ovr;
17564ad7f64SFabrice Gasnier 	const struct stm32_adc_regs *sqr;
17664ad7f64SFabrice Gasnier 	const struct stm32_adc_regs exten;
17764ad7f64SFabrice Gasnier 	const struct stm32_adc_regs extsel;
17864ad7f64SFabrice Gasnier 	const struct stm32_adc_regs res;
179ee2ac1cdSFabrice Gasnier 	const u32 smpr[2];
180ee2ac1cdSFabrice Gasnier 	const struct stm32_adc_regs *smp_bits;
181aec6e0d8SOlivier Moysan 	const struct stm32_adc_regs or_vdd;
182aec6e0d8SOlivier Moysan 	const struct stm32_adc_regs ccr_vbat;
183aec6e0d8SOlivier Moysan 	const struct stm32_adc_regs ccr_vref;
18464ad7f64SFabrice Gasnier };
18564ad7f64SFabrice Gasnier 
18664ad7f64SFabrice Gasnier struct stm32_adc;
18764ad7f64SFabrice Gasnier 
18864ad7f64SFabrice Gasnier /**
1891cd92d42SFabrice Gasnier  * struct stm32_adc_cfg - stm32 compatible configuration data
19064ad7f64SFabrice Gasnier  * @regs:		registers descriptions
19164ad7f64SFabrice Gasnier  * @adc_info:		per instance input channels definitions
19264ad7f64SFabrice Gasnier  * @trigs:		external trigger sources
193204a6a25SFabrice Gasnier  * @clk_required:	clock is required
194d58c67d1SFabrice Gasnier  * @has_vregready:	vregready status flag presence
19595e339b6SFabrice Gasnier  * @prepare:		optional prepare routine (power-up, enable)
19664ad7f64SFabrice Gasnier  * @start_conv:		routine to start conversions
19764ad7f64SFabrice Gasnier  * @stop_conv:		routine to stop conversions
19895e339b6SFabrice Gasnier  * @unprepare:		optional unprepare routine (disable, power-down)
199695e2f5cSOlivier Moysan  * @irq_clear:		routine to clear irqs
200ee2ac1cdSFabrice Gasnier  * @smp_cycles:		programmable sampling time (ADC clock cycles)
2010e346b2cSOlivier Moysan  * @ts_vrefint_ns:	vrefint minimum sampling time in ns
20264ad7f64SFabrice Gasnier  */
20364ad7f64SFabrice Gasnier struct stm32_adc_cfg {
20464ad7f64SFabrice Gasnier 	const struct stm32_adc_regspec	*regs;
20564ad7f64SFabrice Gasnier 	const struct stm32_adc_info	*adc_info;
20664ad7f64SFabrice Gasnier 	struct stm32_adc_trig_info	*trigs;
207204a6a25SFabrice Gasnier 	bool clk_required;
208d58c67d1SFabrice Gasnier 	bool has_vregready;
209cd64d357SAlexandru Ardelean 	int (*prepare)(struct iio_dev *);
210cd64d357SAlexandru Ardelean 	void (*start_conv)(struct iio_dev *, bool dma);
211cd64d357SAlexandru Ardelean 	void (*stop_conv)(struct iio_dev *);
212cd64d357SAlexandru Ardelean 	void (*unprepare)(struct iio_dev *);
213695e2f5cSOlivier Moysan 	void (*irq_clear)(struct iio_dev *indio_dev, u32 msk);
214ee2ac1cdSFabrice Gasnier 	const unsigned int *smp_cycles;
2150e346b2cSOlivier Moysan 	const unsigned int ts_vrefint_ns;
21664ad7f64SFabrice Gasnier };
21764ad7f64SFabrice Gasnier 
21864ad7f64SFabrice Gasnier /**
2190f883b22SFabrice Gasnier  * struct stm32_adc - private data of each ADC IIO instance
2200f883b22SFabrice Gasnier  * @common:		reference to ADC block common data
2210f883b22SFabrice Gasnier  * @offset:		ADC instance register offset in ADC block
22264ad7f64SFabrice Gasnier  * @cfg:		compatible configuration data
2230f883b22SFabrice Gasnier  * @completion:		end of single conversion completion
2248a09054fSAhmad Fatoum  * @buffer:		data buffer + 8 bytes for timestamp if enabled
2250f883b22SFabrice Gasnier  * @clk:		clock for this adc instance
2260f883b22SFabrice Gasnier  * @irq:		interrupt for this adc instance
2270f883b22SFabrice Gasnier  * @lock:		spinlock
228da9b9485SFabrice Gasnier  * @bufi:		data buffer index
229da9b9485SFabrice Gasnier  * @num_conv:		expected number of scan conversions
23025a85bedSFabrice Gasnier  * @res:		data resolution (e.g. RES bitfield value)
231732f2dc4SFabrice Gasnier  * @trigger_polarity:	external trigger polarity (e.g. exten)
2322763ea05SFabrice Gasnier  * @dma_chan:		dma channel
2332763ea05SFabrice Gasnier  * @rx_buf:		dma rx buffer cpu address
2342763ea05SFabrice Gasnier  * @rx_dma_buf:		dma rx buffer bus address
2352763ea05SFabrice Gasnier  * @rx_buf_sz:		dma rx buffer size
2361cd92d42SFabrice Gasnier  * @difsel:		bitmask to set single-ended/differential channel
2371cd92d42SFabrice Gasnier  * @pcsel:		bitmask to preselect channels on some devices
238ee2ac1cdSFabrice Gasnier  * @smpr_val:		sampling time settings (e.g. smpr1 / smpr2)
23995e339b6SFabrice Gasnier  * @cal:		optional calibration data on some devices
2400e346b2cSOlivier Moysan  * @vrefint:		internal reference voltage data
2410bae72aaSFabrice Gasnier  * @chan_name:		channel name array
24245571a36SOlivier Moysan  * @num_diff:		number of differential channels
243aec6e0d8SOlivier Moysan  * @int_ch:		internal channel indexes array
244*d7705f35SNuno Sá  * @nsmps:		number of channels with optional sample time
2450f883b22SFabrice Gasnier  */
2460f883b22SFabrice Gasnier struct stm32_adc {
2470f883b22SFabrice Gasnier 	struct stm32_adc_common	*common;
2480f883b22SFabrice Gasnier 	u32			offset;
24964ad7f64SFabrice Gasnier 	const struct stm32_adc_cfg	*cfg;
2500f883b22SFabrice Gasnier 	struct completion	completion;
2518a09054fSAhmad Fatoum 	u16			buffer[STM32_ADC_MAX_SQ + 4] __aligned(8);
2520f883b22SFabrice Gasnier 	struct clk		*clk;
2530f883b22SFabrice Gasnier 	int			irq;
2540f883b22SFabrice Gasnier 	spinlock_t		lock;		/* interrupt lock */
255da9b9485SFabrice Gasnier 	unsigned int		bufi;
256da9b9485SFabrice Gasnier 	unsigned int		num_conv;
25725a85bedSFabrice Gasnier 	u32			res;
258732f2dc4SFabrice Gasnier 	u32			trigger_polarity;
2592763ea05SFabrice Gasnier 	struct dma_chan		*dma_chan;
2602763ea05SFabrice Gasnier 	u8			*rx_buf;
2612763ea05SFabrice Gasnier 	dma_addr_t		rx_dma_buf;
2622763ea05SFabrice Gasnier 	unsigned int		rx_buf_sz;
2633fb2e24eSFabrice Gasnier 	u32			difsel;
26495e339b6SFabrice Gasnier 	u32			pcsel;
265ee2ac1cdSFabrice Gasnier 	u32			smpr_val[2];
26695e339b6SFabrice Gasnier 	struct stm32_adc_calib	cal;
2670e346b2cSOlivier Moysan 	struct stm32_adc_vrefint vrefint;
2680bae72aaSFabrice Gasnier 	char			chan_name[STM32_ADC_CH_MAX][STM32_ADC_CH_SZ];
26945571a36SOlivier Moysan 	u32			num_diff;
270aec6e0d8SOlivier Moysan 	int			int_ch[STM32_ADC_INT_CH_NB];
271*d7705f35SNuno Sá 	int			nsmps;
2720f883b22SFabrice Gasnier };
2730f883b22SFabrice Gasnier 
2743fb2e24eSFabrice Gasnier struct stm32_adc_diff_channel {
2753fb2e24eSFabrice Gasnier 	u32 vinp;
2763fb2e24eSFabrice Gasnier 	u32 vinn;
2773fb2e24eSFabrice Gasnier };
2783fb2e24eSFabrice Gasnier 
27964ad7f64SFabrice Gasnier /**
28064ad7f64SFabrice Gasnier  * struct stm32_adc_info - stm32 ADC, per instance config data
28164ad7f64SFabrice Gasnier  * @max_channels:	Number of channels
28264ad7f64SFabrice Gasnier  * @resolutions:	available resolutions
28364ad7f64SFabrice Gasnier  * @num_res:		number of available resolutions
28464ad7f64SFabrice Gasnier  */
28564ad7f64SFabrice Gasnier struct stm32_adc_info {
28664ad7f64SFabrice Gasnier 	int max_channels;
28764ad7f64SFabrice Gasnier 	const unsigned int *resolutions;
28864ad7f64SFabrice Gasnier 	const unsigned int num_res;
28964ad7f64SFabrice Gasnier };
29064ad7f64SFabrice Gasnier 
29125a85bedSFabrice Gasnier static const unsigned int stm32f4_adc_resolutions[] = {
29225a85bedSFabrice Gasnier 	/* sorted values so the index matches RES[1:0] in STM32F4_ADC_CR1 */
29325a85bedSFabrice Gasnier 	12, 10, 8, 6,
29425a85bedSFabrice Gasnier };
29525a85bedSFabrice Gasnier 
2960bae72aaSFabrice Gasnier /* stm32f4 can have up to 16 channels */
29764ad7f64SFabrice Gasnier static const struct stm32_adc_info stm32f4_adc_info = {
29864ad7f64SFabrice Gasnier 	.max_channels = 16,
29964ad7f64SFabrice Gasnier 	.resolutions = stm32f4_adc_resolutions,
30064ad7f64SFabrice Gasnier 	.num_res = ARRAY_SIZE(stm32f4_adc_resolutions),
30164ad7f64SFabrice Gasnier };
30264ad7f64SFabrice Gasnier 
30395e339b6SFabrice Gasnier static const unsigned int stm32h7_adc_resolutions[] = {
30495e339b6SFabrice Gasnier 	/* sorted values so the index matches RES[2:0] in STM32H7_ADC_CFGR */
30595e339b6SFabrice Gasnier 	16, 14, 12, 10, 8,
30695e339b6SFabrice Gasnier };
30795e339b6SFabrice Gasnier 
3080bae72aaSFabrice Gasnier /* stm32h7 can have up to 20 channels */
30995e339b6SFabrice Gasnier static const struct stm32_adc_info stm32h7_adc_info = {
3100bae72aaSFabrice Gasnier 	.max_channels = STM32_ADC_CH_MAX,
31195e339b6SFabrice Gasnier 	.resolutions = stm32h7_adc_resolutions,
31295e339b6SFabrice Gasnier 	.num_res = ARRAY_SIZE(stm32h7_adc_resolutions),
31395e339b6SFabrice Gasnier };
31495e339b6SFabrice Gasnier 
3151cd92d42SFabrice Gasnier /*
316da9b9485SFabrice Gasnier  * stm32f4_sq - describe regular sequence registers
317da9b9485SFabrice Gasnier  * - L: sequence len (register & bit field)
318da9b9485SFabrice Gasnier  * - SQ1..SQ16: sequence entries (register & bit field)
319da9b9485SFabrice Gasnier  */
320da9b9485SFabrice Gasnier static const struct stm32_adc_regs stm32f4_sq[STM32_ADC_MAX_SQ + 1] = {
321da9b9485SFabrice Gasnier 	/* L: len bit field description to be kept as first element */
322da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(23, 20), 20 },
323da9b9485SFabrice Gasnier 	/* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
324da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(4, 0), 0 },
325da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(9, 5), 5 },
326da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(14, 10), 10 },
327da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(19, 15), 15 },
328da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(24, 20), 20 },
329da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(29, 25), 25 },
330da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(4, 0), 0 },
331da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(9, 5), 5 },
332da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(14, 10), 10 },
333da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(19, 15), 15 },
334da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(24, 20), 20 },
335da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(29, 25), 25 },
336da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(4, 0), 0 },
337da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(9, 5), 5 },
338da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(14, 10), 10 },
339da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(19, 15), 15 },
340da9b9485SFabrice Gasnier };
341da9b9485SFabrice Gasnier 
342f24a33b3SFabrice Gasnier /* STM32F4 external trigger sources for all instances */
343f24a33b3SFabrice Gasnier static struct stm32_adc_trig_info stm32f4_adc_trigs[] = {
344f24a33b3SFabrice Gasnier 	{ TIM1_CH1, STM32_EXT0 },
345f24a33b3SFabrice Gasnier 	{ TIM1_CH2, STM32_EXT1 },
346f24a33b3SFabrice Gasnier 	{ TIM1_CH3, STM32_EXT2 },
347f24a33b3SFabrice Gasnier 	{ TIM2_CH2, STM32_EXT3 },
348f24a33b3SFabrice Gasnier 	{ TIM2_CH3, STM32_EXT4 },
349f24a33b3SFabrice Gasnier 	{ TIM2_CH4, STM32_EXT5 },
350f24a33b3SFabrice Gasnier 	{ TIM2_TRGO, STM32_EXT6 },
351f24a33b3SFabrice Gasnier 	{ TIM3_CH1, STM32_EXT7 },
352f24a33b3SFabrice Gasnier 	{ TIM3_TRGO, STM32_EXT8 },
353f24a33b3SFabrice Gasnier 	{ TIM4_CH4, STM32_EXT9 },
354f24a33b3SFabrice Gasnier 	{ TIM5_CH1, STM32_EXT10 },
355f24a33b3SFabrice Gasnier 	{ TIM5_CH2, STM32_EXT11 },
356f24a33b3SFabrice Gasnier 	{ TIM5_CH3, STM32_EXT12 },
357f24a33b3SFabrice Gasnier 	{ TIM8_CH1, STM32_EXT13 },
358f24a33b3SFabrice Gasnier 	{ TIM8_TRGO, STM32_EXT14 },
359f24a33b3SFabrice Gasnier 	{}, /* sentinel */
360f24a33b3SFabrice Gasnier };
361f24a33b3SFabrice Gasnier 
3621cd92d42SFabrice Gasnier /*
363ee2ac1cdSFabrice Gasnier  * stm32f4_smp_bits[] - describe sampling time register index & bit fields
364ee2ac1cdSFabrice Gasnier  * Sorted so it can be indexed by channel number.
365ee2ac1cdSFabrice Gasnier  */
366ee2ac1cdSFabrice Gasnier static const struct stm32_adc_regs stm32f4_smp_bits[] = {
367ee2ac1cdSFabrice Gasnier 	/* STM32F4_ADC_SMPR2: smpr[] index, mask, shift for SMP0 to SMP9 */
368ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(2, 0), 0 },
369ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(5, 3), 3 },
370ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(8, 6), 6 },
371ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(11, 9), 9 },
372ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(14, 12), 12 },
373ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(17, 15), 15 },
374ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(20, 18), 18 },
375ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(23, 21), 21 },
376ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(26, 24), 24 },
377ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(29, 27), 27 },
378ee2ac1cdSFabrice Gasnier 	/* STM32F4_ADC_SMPR1, smpr[] index, mask, shift for SMP10 to SMP18 */
379ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(2, 0), 0 },
380ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(5, 3), 3 },
381ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(8, 6), 6 },
382ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(11, 9), 9 },
383ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(14, 12), 12 },
384ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(17, 15), 15 },
385ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(20, 18), 18 },
386ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(23, 21), 21 },
387ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(26, 24), 24 },
388ee2ac1cdSFabrice Gasnier };
389ee2ac1cdSFabrice Gasnier 
390ee2ac1cdSFabrice Gasnier /* STM32F4 programmable sampling time (ADC clock cycles) */
391ee2ac1cdSFabrice Gasnier static const unsigned int stm32f4_adc_smp_cycles[STM32_ADC_MAX_SMP + 1] = {
392ee2ac1cdSFabrice Gasnier 	3, 15, 28, 56, 84, 112, 144, 480,
393ee2ac1cdSFabrice Gasnier };
394ee2ac1cdSFabrice Gasnier 
39564ad7f64SFabrice Gasnier static const struct stm32_adc_regspec stm32f4_adc_regspec = {
39664ad7f64SFabrice Gasnier 	.dr = STM32F4_ADC_DR,
39764ad7f64SFabrice Gasnier 	.ier_eoc = { STM32F4_ADC_CR1, STM32F4_EOCIE },
398cc06e67dSFabrice Gasnier 	.ier_ovr = { STM32F4_ADC_CR1, STM32F4_OVRIE },
39964ad7f64SFabrice Gasnier 	.isr_eoc = { STM32F4_ADC_SR, STM32F4_EOC },
400cc06e67dSFabrice Gasnier 	.isr_ovr = { STM32F4_ADC_SR, STM32F4_OVR },
40164ad7f64SFabrice Gasnier 	.sqr = stm32f4_sq,
40264ad7f64SFabrice Gasnier 	.exten = { STM32F4_ADC_CR2, STM32F4_EXTEN_MASK, STM32F4_EXTEN_SHIFT },
40364ad7f64SFabrice Gasnier 	.extsel = { STM32F4_ADC_CR2, STM32F4_EXTSEL_MASK,
40464ad7f64SFabrice Gasnier 		    STM32F4_EXTSEL_SHIFT },
40564ad7f64SFabrice Gasnier 	.res = { STM32F4_ADC_CR1, STM32F4_RES_MASK, STM32F4_RES_SHIFT },
406ee2ac1cdSFabrice Gasnier 	.smpr = { STM32F4_ADC_SMPR1, STM32F4_ADC_SMPR2 },
407ee2ac1cdSFabrice Gasnier 	.smp_bits = stm32f4_smp_bits,
40864ad7f64SFabrice Gasnier };
40964ad7f64SFabrice Gasnier 
41095e339b6SFabrice Gasnier static const struct stm32_adc_regs stm32h7_sq[STM32_ADC_MAX_SQ + 1] = {
41195e339b6SFabrice Gasnier 	/* L: len bit field description to be kept as first element */
41295e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR1, GENMASK(3, 0), 0 },
41395e339b6SFabrice Gasnier 	/* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
41495e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR1, GENMASK(10, 6), 6 },
41595e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR1, GENMASK(16, 12), 12 },
41695e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR1, GENMASK(22, 18), 18 },
41795e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR1, GENMASK(28, 24), 24 },
41895e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR2, GENMASK(4, 0), 0 },
41995e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR2, GENMASK(10, 6), 6 },
42095e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR2, GENMASK(16, 12), 12 },
42195e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR2, GENMASK(22, 18), 18 },
42295e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR2, GENMASK(28, 24), 24 },
42395e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR3, GENMASK(4, 0), 0 },
42495e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR3, GENMASK(10, 6), 6 },
42595e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR3, GENMASK(16, 12), 12 },
42695e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR3, GENMASK(22, 18), 18 },
42795e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR3, GENMASK(28, 24), 24 },
42895e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR4, GENMASK(4, 0), 0 },
42995e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR4, GENMASK(10, 6), 6 },
43095e339b6SFabrice Gasnier };
43195e339b6SFabrice Gasnier 
43295e339b6SFabrice Gasnier /* STM32H7 external trigger sources for all instances */
43395e339b6SFabrice Gasnier static struct stm32_adc_trig_info stm32h7_adc_trigs[] = {
43495e339b6SFabrice Gasnier 	{ TIM1_CH1, STM32_EXT0 },
43595e339b6SFabrice Gasnier 	{ TIM1_CH2, STM32_EXT1 },
43695e339b6SFabrice Gasnier 	{ TIM1_CH3, STM32_EXT2 },
43795e339b6SFabrice Gasnier 	{ TIM2_CH2, STM32_EXT3 },
43895e339b6SFabrice Gasnier 	{ TIM3_TRGO, STM32_EXT4 },
43995e339b6SFabrice Gasnier 	{ TIM4_CH4, STM32_EXT5 },
44095e339b6SFabrice Gasnier 	{ TIM8_TRGO, STM32_EXT7 },
44195e339b6SFabrice Gasnier 	{ TIM8_TRGO2, STM32_EXT8 },
44295e339b6SFabrice Gasnier 	{ TIM1_TRGO, STM32_EXT9 },
44395e339b6SFabrice Gasnier 	{ TIM1_TRGO2, STM32_EXT10 },
44495e339b6SFabrice Gasnier 	{ TIM2_TRGO, STM32_EXT11 },
44595e339b6SFabrice Gasnier 	{ TIM4_TRGO, STM32_EXT12 },
44695e339b6SFabrice Gasnier 	{ TIM6_TRGO, STM32_EXT13 },
4473a069904SFabrice Gasnier 	{ TIM15_TRGO, STM32_EXT14 },
44895e339b6SFabrice Gasnier 	{ TIM3_CH4, STM32_EXT15 },
449f0b638a7SFabrice Gasnier 	{ LPTIM1_OUT, STM32_EXT18 },
450f0b638a7SFabrice Gasnier 	{ LPTIM2_OUT, STM32_EXT19 },
451f0b638a7SFabrice Gasnier 	{ LPTIM3_OUT, STM32_EXT20 },
45295e339b6SFabrice Gasnier 	{},
45395e339b6SFabrice Gasnier };
45495e339b6SFabrice Gasnier 
4551cd92d42SFabrice Gasnier /*
456ee2ac1cdSFabrice Gasnier  * stm32h7_smp_bits - describe sampling time register index & bit fields
457ee2ac1cdSFabrice Gasnier  * Sorted so it can be indexed by channel number.
458ee2ac1cdSFabrice Gasnier  */
459ee2ac1cdSFabrice Gasnier static const struct stm32_adc_regs stm32h7_smp_bits[] = {
460ee2ac1cdSFabrice Gasnier 	/* STM32H7_ADC_SMPR1, smpr[] index, mask, shift for SMP0 to SMP9 */
461ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(2, 0), 0 },
462ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(5, 3), 3 },
463ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(8, 6), 6 },
464ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(11, 9), 9 },
465ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(14, 12), 12 },
466ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(17, 15), 15 },
467ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(20, 18), 18 },
468ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(23, 21), 21 },
469ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(26, 24), 24 },
470ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(29, 27), 27 },
471ee2ac1cdSFabrice Gasnier 	/* STM32H7_ADC_SMPR2, smpr[] index, mask, shift for SMP10 to SMP19 */
472ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(2, 0), 0 },
473ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(5, 3), 3 },
474ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(8, 6), 6 },
475ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(11, 9), 9 },
476ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(14, 12), 12 },
477ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(17, 15), 15 },
478ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(20, 18), 18 },
479ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(23, 21), 21 },
480ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(26, 24), 24 },
481ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(29, 27), 27 },
482ee2ac1cdSFabrice Gasnier };
483ee2ac1cdSFabrice Gasnier 
484ee2ac1cdSFabrice Gasnier /* STM32H7 programmable sampling time (ADC clock cycles, rounded down) */
485ee2ac1cdSFabrice Gasnier static const unsigned int stm32h7_adc_smp_cycles[STM32_ADC_MAX_SMP + 1] = {
486ee2ac1cdSFabrice Gasnier 	1, 2, 8, 16, 32, 64, 387, 810,
487ee2ac1cdSFabrice Gasnier };
488ee2ac1cdSFabrice Gasnier 
48995e339b6SFabrice Gasnier static const struct stm32_adc_regspec stm32h7_adc_regspec = {
49095e339b6SFabrice Gasnier 	.dr = STM32H7_ADC_DR,
49195e339b6SFabrice Gasnier 	.ier_eoc = { STM32H7_ADC_IER, STM32H7_EOCIE },
492cc06e67dSFabrice Gasnier 	.ier_ovr = { STM32H7_ADC_IER, STM32H7_OVRIE },
49395e339b6SFabrice Gasnier 	.isr_eoc = { STM32H7_ADC_ISR, STM32H7_EOC },
494cc06e67dSFabrice Gasnier 	.isr_ovr = { STM32H7_ADC_ISR, STM32H7_OVR },
49595e339b6SFabrice Gasnier 	.sqr = stm32h7_sq,
49695e339b6SFabrice Gasnier 	.exten = { STM32H7_ADC_CFGR, STM32H7_EXTEN_MASK, STM32H7_EXTEN_SHIFT },
49795e339b6SFabrice Gasnier 	.extsel = { STM32H7_ADC_CFGR, STM32H7_EXTSEL_MASK,
49895e339b6SFabrice Gasnier 		    STM32H7_EXTSEL_SHIFT },
49995e339b6SFabrice Gasnier 	.res = { STM32H7_ADC_CFGR, STM32H7_RES_MASK, STM32H7_RES_SHIFT },
500ee2ac1cdSFabrice Gasnier 	.smpr = { STM32H7_ADC_SMPR1, STM32H7_ADC_SMPR2 },
501ee2ac1cdSFabrice Gasnier 	.smp_bits = stm32h7_smp_bits,
50295e339b6SFabrice Gasnier };
50395e339b6SFabrice Gasnier 
504aec6e0d8SOlivier Moysan static const struct stm32_adc_regspec stm32mp1_adc_regspec = {
505aec6e0d8SOlivier Moysan 	.dr = STM32H7_ADC_DR,
506aec6e0d8SOlivier Moysan 	.ier_eoc = { STM32H7_ADC_IER, STM32H7_EOCIE },
507aec6e0d8SOlivier Moysan 	.ier_ovr = { STM32H7_ADC_IER, STM32H7_OVRIE },
508aec6e0d8SOlivier Moysan 	.isr_eoc = { STM32H7_ADC_ISR, STM32H7_EOC },
509aec6e0d8SOlivier Moysan 	.isr_ovr = { STM32H7_ADC_ISR, STM32H7_OVR },
510aec6e0d8SOlivier Moysan 	.sqr = stm32h7_sq,
511aec6e0d8SOlivier Moysan 	.exten = { STM32H7_ADC_CFGR, STM32H7_EXTEN_MASK, STM32H7_EXTEN_SHIFT },
512aec6e0d8SOlivier Moysan 	.extsel = { STM32H7_ADC_CFGR, STM32H7_EXTSEL_MASK,
513aec6e0d8SOlivier Moysan 		    STM32H7_EXTSEL_SHIFT },
514aec6e0d8SOlivier Moysan 	.res = { STM32H7_ADC_CFGR, STM32H7_RES_MASK, STM32H7_RES_SHIFT },
515aec6e0d8SOlivier Moysan 	.smpr = { STM32H7_ADC_SMPR1, STM32H7_ADC_SMPR2 },
516aec6e0d8SOlivier Moysan 	.smp_bits = stm32h7_smp_bits,
517aec6e0d8SOlivier Moysan 	.or_vdd = { STM32MP1_ADC2_OR, STM32MP1_VDDCOREEN },
518aec6e0d8SOlivier Moysan 	.ccr_vbat = { STM32H7_ADC_CCR, STM32H7_VBATEN },
519aec6e0d8SOlivier Moysan 	.ccr_vref = { STM32H7_ADC_CCR, STM32H7_VREFEN },
520aec6e0d8SOlivier Moysan };
521aec6e0d8SOlivier Moysan 
522fb6da706SJonathan Cameron /*
5230f883b22SFabrice Gasnier  * STM32 ADC registers access routines
5240f883b22SFabrice Gasnier  * @adc: stm32 adc instance
5250f883b22SFabrice Gasnier  * @reg: reg offset in adc instance
5260f883b22SFabrice Gasnier  *
5270f883b22SFabrice Gasnier  * Note: All instances share same base, with 0x0, 0x100 or 0x200 offset resp.
5280f883b22SFabrice Gasnier  * for adc1, adc2 and adc3.
5290f883b22SFabrice Gasnier  */
5300f883b22SFabrice Gasnier static u32 stm32_adc_readl(struct stm32_adc *adc, u32 reg)
5310f883b22SFabrice Gasnier {
5320f883b22SFabrice Gasnier 	return readl_relaxed(adc->common->base + adc->offset + reg);
5330f883b22SFabrice Gasnier }
5340f883b22SFabrice Gasnier 
53595e339b6SFabrice Gasnier #define stm32_adc_readl_addr(addr)	stm32_adc_readl(adc, addr)
53695e339b6SFabrice Gasnier 
53795e339b6SFabrice Gasnier #define stm32_adc_readl_poll_timeout(reg, val, cond, sleep_us, timeout_us) \
53895e339b6SFabrice Gasnier 	readx_poll_timeout(stm32_adc_readl_addr, reg, val, \
53995e339b6SFabrice Gasnier 			   cond, sleep_us, timeout_us)
54095e339b6SFabrice Gasnier 
5410f883b22SFabrice Gasnier static u16 stm32_adc_readw(struct stm32_adc *adc, u32 reg)
5420f883b22SFabrice Gasnier {
5430f883b22SFabrice Gasnier 	return readw_relaxed(adc->common->base + adc->offset + reg);
5440f883b22SFabrice Gasnier }
5450f883b22SFabrice Gasnier 
5460f883b22SFabrice Gasnier static void stm32_adc_writel(struct stm32_adc *adc, u32 reg, u32 val)
5470f883b22SFabrice Gasnier {
5480f883b22SFabrice Gasnier 	writel_relaxed(val, adc->common->base + adc->offset + reg);
5490f883b22SFabrice Gasnier }
5500f883b22SFabrice Gasnier 
5510f883b22SFabrice Gasnier static void stm32_adc_set_bits(struct stm32_adc *adc, u32 reg, u32 bits)
5520f883b22SFabrice Gasnier {
5530f883b22SFabrice Gasnier 	unsigned long flags;
5540f883b22SFabrice Gasnier 
5550f883b22SFabrice Gasnier 	spin_lock_irqsave(&adc->lock, flags);
5560f883b22SFabrice Gasnier 	stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) | bits);
5570f883b22SFabrice Gasnier 	spin_unlock_irqrestore(&adc->lock, flags);
5580f883b22SFabrice Gasnier }
5590f883b22SFabrice Gasnier 
560aec6e0d8SOlivier Moysan static void stm32_adc_set_bits_common(struct stm32_adc *adc, u32 reg, u32 bits)
561aec6e0d8SOlivier Moysan {
562aec6e0d8SOlivier Moysan 	spin_lock(&adc->common->lock);
563aec6e0d8SOlivier Moysan 	writel_relaxed(readl_relaxed(adc->common->base + reg) | bits,
564aec6e0d8SOlivier Moysan 		       adc->common->base + reg);
565aec6e0d8SOlivier Moysan 	spin_unlock(&adc->common->lock);
566aec6e0d8SOlivier Moysan }
567aec6e0d8SOlivier Moysan 
5680f883b22SFabrice Gasnier static void stm32_adc_clr_bits(struct stm32_adc *adc, u32 reg, u32 bits)
5690f883b22SFabrice Gasnier {
5700f883b22SFabrice Gasnier 	unsigned long flags;
5710f883b22SFabrice Gasnier 
5720f883b22SFabrice Gasnier 	spin_lock_irqsave(&adc->lock, flags);
5730f883b22SFabrice Gasnier 	stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) & ~bits);
5740f883b22SFabrice Gasnier 	spin_unlock_irqrestore(&adc->lock, flags);
5750f883b22SFabrice Gasnier }
5760f883b22SFabrice Gasnier 
577aec6e0d8SOlivier Moysan static void stm32_adc_clr_bits_common(struct stm32_adc *adc, u32 reg, u32 bits)
578aec6e0d8SOlivier Moysan {
579aec6e0d8SOlivier Moysan 	spin_lock(&adc->common->lock);
580aec6e0d8SOlivier Moysan 	writel_relaxed(readl_relaxed(adc->common->base + reg) & ~bits,
581aec6e0d8SOlivier Moysan 		       adc->common->base + reg);
582aec6e0d8SOlivier Moysan 	spin_unlock(&adc->common->lock);
583aec6e0d8SOlivier Moysan }
584aec6e0d8SOlivier Moysan 
5850f883b22SFabrice Gasnier /**
5860f883b22SFabrice Gasnier  * stm32_adc_conv_irq_enable() - Enable end of conversion interrupt
5870f883b22SFabrice Gasnier  * @adc: stm32 adc instance
5880f883b22SFabrice Gasnier  */
5890f883b22SFabrice Gasnier static void stm32_adc_conv_irq_enable(struct stm32_adc *adc)
5900f883b22SFabrice Gasnier {
59164ad7f64SFabrice Gasnier 	stm32_adc_set_bits(adc, adc->cfg->regs->ier_eoc.reg,
59264ad7f64SFabrice Gasnier 			   adc->cfg->regs->ier_eoc.mask);
5930f883b22SFabrice Gasnier };
5940f883b22SFabrice Gasnier 
5950f883b22SFabrice Gasnier /**
5960f883b22SFabrice Gasnier  * stm32_adc_conv_irq_disable() - Disable end of conversion interrupt
5970f883b22SFabrice Gasnier  * @adc: stm32 adc instance
5980f883b22SFabrice Gasnier  */
5990f883b22SFabrice Gasnier static void stm32_adc_conv_irq_disable(struct stm32_adc *adc)
6000f883b22SFabrice Gasnier {
60164ad7f64SFabrice Gasnier 	stm32_adc_clr_bits(adc, adc->cfg->regs->ier_eoc.reg,
60264ad7f64SFabrice Gasnier 			   adc->cfg->regs->ier_eoc.mask);
6030f883b22SFabrice Gasnier }
6040f883b22SFabrice Gasnier 
605cc06e67dSFabrice Gasnier static void stm32_adc_ovr_irq_enable(struct stm32_adc *adc)
606cc06e67dSFabrice Gasnier {
607cc06e67dSFabrice Gasnier 	stm32_adc_set_bits(adc, adc->cfg->regs->ier_ovr.reg,
608cc06e67dSFabrice Gasnier 			   adc->cfg->regs->ier_ovr.mask);
609cc06e67dSFabrice Gasnier }
610cc06e67dSFabrice Gasnier 
611cc06e67dSFabrice Gasnier static void stm32_adc_ovr_irq_disable(struct stm32_adc *adc)
612cc06e67dSFabrice Gasnier {
613cc06e67dSFabrice Gasnier 	stm32_adc_clr_bits(adc, adc->cfg->regs->ier_ovr.reg,
614cc06e67dSFabrice Gasnier 			   adc->cfg->regs->ier_ovr.mask);
615cc06e67dSFabrice Gasnier }
616cc06e67dSFabrice Gasnier 
61725a85bedSFabrice Gasnier static void stm32_adc_set_res(struct stm32_adc *adc)
61825a85bedSFabrice Gasnier {
61964ad7f64SFabrice Gasnier 	const struct stm32_adc_regs *res = &adc->cfg->regs->res;
62064ad7f64SFabrice Gasnier 	u32 val;
62125a85bedSFabrice Gasnier 
62264ad7f64SFabrice Gasnier 	val = stm32_adc_readl(adc, res->reg);
62364ad7f64SFabrice Gasnier 	val = (val & ~res->mask) | (adc->res << res->shift);
62464ad7f64SFabrice Gasnier 	stm32_adc_writel(adc, res->reg, val);
62525a85bedSFabrice Gasnier }
62625a85bedSFabrice Gasnier 
6279bdbb113SFabrice Gasnier static int stm32_adc_hw_stop(struct device *dev)
6289bdbb113SFabrice Gasnier {
629cd64d357SAlexandru Ardelean 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
630cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
6319bdbb113SFabrice Gasnier 
6329bdbb113SFabrice Gasnier 	if (adc->cfg->unprepare)
633cd64d357SAlexandru Ardelean 		adc->cfg->unprepare(indio_dev);
6349bdbb113SFabrice Gasnier 
6359bdbb113SFabrice Gasnier 	clk_disable_unprepare(adc->clk);
6369bdbb113SFabrice Gasnier 
6379bdbb113SFabrice Gasnier 	return 0;
6389bdbb113SFabrice Gasnier }
6399bdbb113SFabrice Gasnier 
6409bdbb113SFabrice Gasnier static int stm32_adc_hw_start(struct device *dev)
6419bdbb113SFabrice Gasnier {
642cd64d357SAlexandru Ardelean 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
643cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
6449bdbb113SFabrice Gasnier 	int ret;
6459bdbb113SFabrice Gasnier 
6469bdbb113SFabrice Gasnier 	ret = clk_prepare_enable(adc->clk);
6479bdbb113SFabrice Gasnier 	if (ret)
6489bdbb113SFabrice Gasnier 		return ret;
6499bdbb113SFabrice Gasnier 
6509bdbb113SFabrice Gasnier 	stm32_adc_set_res(adc);
6519bdbb113SFabrice Gasnier 
6529bdbb113SFabrice Gasnier 	if (adc->cfg->prepare) {
653cd64d357SAlexandru Ardelean 		ret = adc->cfg->prepare(indio_dev);
6549bdbb113SFabrice Gasnier 		if (ret)
6559bdbb113SFabrice Gasnier 			goto err_clk_dis;
6569bdbb113SFabrice Gasnier 	}
6579bdbb113SFabrice Gasnier 
6589bdbb113SFabrice Gasnier 	return 0;
6599bdbb113SFabrice Gasnier 
6609bdbb113SFabrice Gasnier err_clk_dis:
6619bdbb113SFabrice Gasnier 	clk_disable_unprepare(adc->clk);
6629bdbb113SFabrice Gasnier 
6639bdbb113SFabrice Gasnier 	return ret;
6649bdbb113SFabrice Gasnier }
6659bdbb113SFabrice Gasnier 
666aec6e0d8SOlivier Moysan static void stm32_adc_int_ch_enable(struct iio_dev *indio_dev)
667aec6e0d8SOlivier Moysan {
668aec6e0d8SOlivier Moysan 	struct stm32_adc *adc = iio_priv(indio_dev);
669aec6e0d8SOlivier Moysan 	u32 i;
670aec6e0d8SOlivier Moysan 
671aec6e0d8SOlivier Moysan 	for (i = 0; i < STM32_ADC_INT_CH_NB; i++) {
672aec6e0d8SOlivier Moysan 		if (adc->int_ch[i] == STM32_ADC_INT_CH_NONE)
673aec6e0d8SOlivier Moysan 			continue;
674aec6e0d8SOlivier Moysan 
675aec6e0d8SOlivier Moysan 		switch (i) {
676aec6e0d8SOlivier Moysan 		case STM32_ADC_INT_CH_VDDCORE:
677aec6e0d8SOlivier Moysan 			dev_dbg(&indio_dev->dev, "Enable VDDCore\n");
678aec6e0d8SOlivier Moysan 			stm32_adc_set_bits(adc, adc->cfg->regs->or_vdd.reg,
679aec6e0d8SOlivier Moysan 					   adc->cfg->regs->or_vdd.mask);
680aec6e0d8SOlivier Moysan 			break;
681aec6e0d8SOlivier Moysan 		case STM32_ADC_INT_CH_VREFINT:
682aec6e0d8SOlivier Moysan 			dev_dbg(&indio_dev->dev, "Enable VREFInt\n");
683aec6e0d8SOlivier Moysan 			stm32_adc_set_bits_common(adc, adc->cfg->regs->ccr_vref.reg,
684aec6e0d8SOlivier Moysan 						  adc->cfg->regs->ccr_vref.mask);
685aec6e0d8SOlivier Moysan 			break;
686aec6e0d8SOlivier Moysan 		case STM32_ADC_INT_CH_VBAT:
687aec6e0d8SOlivier Moysan 			dev_dbg(&indio_dev->dev, "Enable VBAT\n");
688aec6e0d8SOlivier Moysan 			stm32_adc_set_bits_common(adc, adc->cfg->regs->ccr_vbat.reg,
689aec6e0d8SOlivier Moysan 						  adc->cfg->regs->ccr_vbat.mask);
690aec6e0d8SOlivier Moysan 			break;
691aec6e0d8SOlivier Moysan 		}
692aec6e0d8SOlivier Moysan 	}
693aec6e0d8SOlivier Moysan }
694aec6e0d8SOlivier Moysan 
695aec6e0d8SOlivier Moysan static void stm32_adc_int_ch_disable(struct stm32_adc *adc)
696aec6e0d8SOlivier Moysan {
697aec6e0d8SOlivier Moysan 	u32 i;
698aec6e0d8SOlivier Moysan 
699aec6e0d8SOlivier Moysan 	for (i = 0; i < STM32_ADC_INT_CH_NB; i++) {
700aec6e0d8SOlivier Moysan 		if (adc->int_ch[i] == STM32_ADC_INT_CH_NONE)
701aec6e0d8SOlivier Moysan 			continue;
702aec6e0d8SOlivier Moysan 
703aec6e0d8SOlivier Moysan 		switch (i) {
704aec6e0d8SOlivier Moysan 		case STM32_ADC_INT_CH_VDDCORE:
705aec6e0d8SOlivier Moysan 			stm32_adc_clr_bits(adc, adc->cfg->regs->or_vdd.reg,
706aec6e0d8SOlivier Moysan 					   adc->cfg->regs->or_vdd.mask);
707aec6e0d8SOlivier Moysan 			break;
708aec6e0d8SOlivier Moysan 		case STM32_ADC_INT_CH_VREFINT:
709aec6e0d8SOlivier Moysan 			stm32_adc_clr_bits_common(adc, adc->cfg->regs->ccr_vref.reg,
710aec6e0d8SOlivier Moysan 						  adc->cfg->regs->ccr_vref.mask);
711aec6e0d8SOlivier Moysan 			break;
712aec6e0d8SOlivier Moysan 		case STM32_ADC_INT_CH_VBAT:
713aec6e0d8SOlivier Moysan 			stm32_adc_clr_bits_common(adc, adc->cfg->regs->ccr_vbat.reg,
714aec6e0d8SOlivier Moysan 						  adc->cfg->regs->ccr_vbat.mask);
715aec6e0d8SOlivier Moysan 			break;
716aec6e0d8SOlivier Moysan 		}
717aec6e0d8SOlivier Moysan 	}
718aec6e0d8SOlivier Moysan }
719aec6e0d8SOlivier Moysan 
7200f883b22SFabrice Gasnier /**
72164ad7f64SFabrice Gasnier  * stm32f4_adc_start_conv() - Start conversions for regular channels.
722cd64d357SAlexandru Ardelean  * @indio_dev: IIO device instance
7232763ea05SFabrice Gasnier  * @dma: use dma to transfer conversion result
7242763ea05SFabrice Gasnier  *
7252763ea05SFabrice Gasnier  * Start conversions for regular channels.
7262763ea05SFabrice Gasnier  * Also take care of normal or DMA mode. Circular DMA may be used for regular
7272763ea05SFabrice Gasnier  * conversions, in IIO buffer modes. Otherwise, use ADC interrupt with direct
7282763ea05SFabrice Gasnier  * DR read instead (e.g. read_raw, or triggered buffer mode without DMA).
7290f883b22SFabrice Gasnier  */
730cd64d357SAlexandru Ardelean static void stm32f4_adc_start_conv(struct iio_dev *indio_dev, bool dma)
7310f883b22SFabrice Gasnier {
732cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
733cd64d357SAlexandru Ardelean 
7340f883b22SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN);
7352763ea05SFabrice Gasnier 
7362763ea05SFabrice Gasnier 	if (dma)
7372763ea05SFabrice Gasnier 		stm32_adc_set_bits(adc, STM32F4_ADC_CR2,
7382763ea05SFabrice Gasnier 				   STM32F4_DMA | STM32F4_DDS);
7392763ea05SFabrice Gasnier 
7400f883b22SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_EOCS | STM32F4_ADON);
7410f883b22SFabrice Gasnier 
7420f883b22SFabrice Gasnier 	/* Wait for Power-up time (tSTAB from datasheet) */
7430f883b22SFabrice Gasnier 	usleep_range(2, 3);
7440f883b22SFabrice Gasnier 
7450f883b22SFabrice Gasnier 	/* Software start ? (e.g. trigger detection disabled ?) */
7460f883b22SFabrice Gasnier 	if (!(stm32_adc_readl(adc, STM32F4_ADC_CR2) & STM32F4_EXTEN_MASK))
7470f883b22SFabrice Gasnier 		stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_SWSTART);
7480f883b22SFabrice Gasnier }
7490f883b22SFabrice Gasnier 
750cd64d357SAlexandru Ardelean static void stm32f4_adc_stop_conv(struct iio_dev *indio_dev)
7510f883b22SFabrice Gasnier {
752cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
753cd64d357SAlexandru Ardelean 
7540f883b22SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK);
7550f883b22SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_SR, STM32F4_STRT);
7560f883b22SFabrice Gasnier 
7570f883b22SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN);
7582763ea05SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_CR2,
7592763ea05SFabrice Gasnier 			   STM32F4_ADON | STM32F4_DMA | STM32F4_DDS);
7600f883b22SFabrice Gasnier }
7610f883b22SFabrice Gasnier 
762695e2f5cSOlivier Moysan static void stm32f4_adc_irq_clear(struct iio_dev *indio_dev, u32 msk)
763695e2f5cSOlivier Moysan {
764695e2f5cSOlivier Moysan 	struct stm32_adc *adc = iio_priv(indio_dev);
765695e2f5cSOlivier Moysan 
766695e2f5cSOlivier Moysan 	stm32_adc_clr_bits(adc, adc->cfg->regs->isr_eoc.reg, msk);
767695e2f5cSOlivier Moysan }
768695e2f5cSOlivier Moysan 
769cd64d357SAlexandru Ardelean static void stm32h7_adc_start_conv(struct iio_dev *indio_dev, bool dma)
77095e339b6SFabrice Gasnier {
771cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
77295e339b6SFabrice Gasnier 	enum stm32h7_adc_dmngt dmngt;
77395e339b6SFabrice Gasnier 	unsigned long flags;
77495e339b6SFabrice Gasnier 	u32 val;
77595e339b6SFabrice Gasnier 
77695e339b6SFabrice Gasnier 	if (dma)
77795e339b6SFabrice Gasnier 		dmngt = STM32H7_DMNGT_DMA_CIRC;
77895e339b6SFabrice Gasnier 	else
77995e339b6SFabrice Gasnier 		dmngt = STM32H7_DMNGT_DR_ONLY;
78095e339b6SFabrice Gasnier 
78195e339b6SFabrice Gasnier 	spin_lock_irqsave(&adc->lock, flags);
78295e339b6SFabrice Gasnier 	val = stm32_adc_readl(adc, STM32H7_ADC_CFGR);
78395e339b6SFabrice Gasnier 	val = (val & ~STM32H7_DMNGT_MASK) | (dmngt << STM32H7_DMNGT_SHIFT);
78495e339b6SFabrice Gasnier 	stm32_adc_writel(adc, STM32H7_ADC_CFGR, val);
78595e339b6SFabrice Gasnier 	spin_unlock_irqrestore(&adc->lock, flags);
78695e339b6SFabrice Gasnier 
78795e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTART);
78895e339b6SFabrice Gasnier }
78995e339b6SFabrice Gasnier 
790cd64d357SAlexandru Ardelean static void stm32h7_adc_stop_conv(struct iio_dev *indio_dev)
79195e339b6SFabrice Gasnier {
792cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
79395e339b6SFabrice Gasnier 	int ret;
79495e339b6SFabrice Gasnier 	u32 val;
79595e339b6SFabrice Gasnier 
79695e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTP);
79795e339b6SFabrice Gasnier 
79895e339b6SFabrice Gasnier 	ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
79995e339b6SFabrice Gasnier 					   !(val & (STM32H7_ADSTART)),
80095e339b6SFabrice Gasnier 					   100, STM32_ADC_TIMEOUT_US);
80195e339b6SFabrice Gasnier 	if (ret)
80295e339b6SFabrice Gasnier 		dev_warn(&indio_dev->dev, "stop failed\n");
80395e339b6SFabrice Gasnier 
80495e339b6SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32H7_ADC_CFGR, STM32H7_DMNGT_MASK);
80595e339b6SFabrice Gasnier }
80695e339b6SFabrice Gasnier 
807695e2f5cSOlivier Moysan static void stm32h7_adc_irq_clear(struct iio_dev *indio_dev, u32 msk)
808695e2f5cSOlivier Moysan {
809695e2f5cSOlivier Moysan 	struct stm32_adc *adc = iio_priv(indio_dev);
810695e2f5cSOlivier Moysan 	/* On STM32H7 IRQs are cleared by writing 1 into ISR register */
811695e2f5cSOlivier Moysan 	stm32_adc_set_bits(adc, adc->cfg->regs->isr_eoc.reg, msk);
812695e2f5cSOlivier Moysan }
813695e2f5cSOlivier Moysan 
814cd64d357SAlexandru Ardelean static int stm32h7_adc_exit_pwr_down(struct iio_dev *indio_dev)
81595e339b6SFabrice Gasnier {
816cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
817d58c67d1SFabrice Gasnier 	int ret;
818d58c67d1SFabrice Gasnier 	u32 val;
819d58c67d1SFabrice Gasnier 
82095e339b6SFabrice Gasnier 	/* Exit deep power down, then enable ADC voltage regulator */
82195e339b6SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD);
82295e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADVREGEN);
82395e339b6SFabrice Gasnier 
82495e339b6SFabrice Gasnier 	if (adc->common->rate > STM32H7_BOOST_CLKRATE)
82595e339b6SFabrice Gasnier 		stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_BOOST);
82695e339b6SFabrice Gasnier 
82795e339b6SFabrice Gasnier 	/* Wait for startup time */
828d58c67d1SFabrice Gasnier 	if (!adc->cfg->has_vregready) {
82995e339b6SFabrice Gasnier 		usleep_range(10, 20);
830d58c67d1SFabrice Gasnier 		return 0;
831d58c67d1SFabrice Gasnier 	}
832d58c67d1SFabrice Gasnier 
833d58c67d1SFabrice Gasnier 	ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR, val,
834d58c67d1SFabrice Gasnier 					   val & STM32MP1_VREGREADY, 100,
835d58c67d1SFabrice Gasnier 					   STM32_ADC_TIMEOUT_US);
836d58c67d1SFabrice Gasnier 	if (ret) {
837d58c67d1SFabrice Gasnier 		stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD);
838d58c67d1SFabrice Gasnier 		dev_err(&indio_dev->dev, "Failed to exit power down\n");
839d58c67d1SFabrice Gasnier 	}
840d58c67d1SFabrice Gasnier 
841d58c67d1SFabrice Gasnier 	return ret;
84295e339b6SFabrice Gasnier }
84395e339b6SFabrice Gasnier 
84495e339b6SFabrice Gasnier static void stm32h7_adc_enter_pwr_down(struct stm32_adc *adc)
84595e339b6SFabrice Gasnier {
84695e339b6SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_BOOST);
84795e339b6SFabrice Gasnier 
84895e339b6SFabrice Gasnier 	/* Setting DEEPPWD disables ADC vreg and clears ADVREGEN */
84995e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD);
85095e339b6SFabrice Gasnier }
85195e339b6SFabrice Gasnier 
852cd64d357SAlexandru Ardelean static int stm32h7_adc_enable(struct iio_dev *indio_dev)
85395e339b6SFabrice Gasnier {
854cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
85595e339b6SFabrice Gasnier 	int ret;
85695e339b6SFabrice Gasnier 	u32 val;
85795e339b6SFabrice Gasnier 
85895e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADEN);
85995e339b6SFabrice Gasnier 
86095e339b6SFabrice Gasnier 	/* Poll for ADRDY to be set (after adc startup time) */
86195e339b6SFabrice Gasnier 	ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR, val,
86295e339b6SFabrice Gasnier 					   val & STM32H7_ADRDY,
86395e339b6SFabrice Gasnier 					   100, STM32_ADC_TIMEOUT_US);
86495e339b6SFabrice Gasnier 	if (ret) {
865a3b5655eSFabrice Gasnier 		stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADDIS);
86695e339b6SFabrice Gasnier 		dev_err(&indio_dev->dev, "Failed to enable ADC\n");
867a3b5655eSFabrice Gasnier 	} else {
868a3b5655eSFabrice Gasnier 		/* Clear ADRDY by writing one */
869a3b5655eSFabrice Gasnier 		stm32_adc_set_bits(adc, STM32H7_ADC_ISR, STM32H7_ADRDY);
87095e339b6SFabrice Gasnier 	}
87195e339b6SFabrice Gasnier 
87295e339b6SFabrice Gasnier 	return ret;
87395e339b6SFabrice Gasnier }
87495e339b6SFabrice Gasnier 
875cd64d357SAlexandru Ardelean static void stm32h7_adc_disable(struct iio_dev *indio_dev)
87695e339b6SFabrice Gasnier {
877cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
87895e339b6SFabrice Gasnier 	int ret;
87995e339b6SFabrice Gasnier 	u32 val;
88095e339b6SFabrice Gasnier 
881cf59ad96SOlivier Moysan 	if (!(stm32_adc_readl(adc, STM32H7_ADC_CR) & STM32H7_ADEN))
882cf59ad96SOlivier Moysan 		return;
883cf59ad96SOlivier Moysan 
88495e339b6SFabrice Gasnier 	/* Disable ADC and wait until it's effectively disabled */
88595e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADDIS);
88695e339b6SFabrice Gasnier 	ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
88795e339b6SFabrice Gasnier 					   !(val & STM32H7_ADEN), 100,
88895e339b6SFabrice Gasnier 					   STM32_ADC_TIMEOUT_US);
88995e339b6SFabrice Gasnier 	if (ret)
89095e339b6SFabrice Gasnier 		dev_warn(&indio_dev->dev, "Failed to disable\n");
89195e339b6SFabrice Gasnier }
89295e339b6SFabrice Gasnier 
89395e339b6SFabrice Gasnier /**
89495e339b6SFabrice Gasnier  * stm32h7_adc_read_selfcalib() - read calibration shadow regs, save result
895cd64d357SAlexandru Ardelean  * @indio_dev: IIO device instance
8960da98c7bSFabrice Gasnier  * Note: Must be called once ADC is enabled, so LINCALRDYW[1..6] are writable
89795e339b6SFabrice Gasnier  */
898cd64d357SAlexandru Ardelean static int stm32h7_adc_read_selfcalib(struct iio_dev *indio_dev)
89995e339b6SFabrice Gasnier {
900cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
90195e339b6SFabrice Gasnier 	int i, ret;
90295e339b6SFabrice Gasnier 	u32 lincalrdyw_mask, val;
90395e339b6SFabrice Gasnier 
90495e339b6SFabrice Gasnier 	/* Read linearity calibration */
90595e339b6SFabrice Gasnier 	lincalrdyw_mask = STM32H7_LINCALRDYW6;
90695e339b6SFabrice Gasnier 	for (i = STM32H7_LINCALFACT_NUM - 1; i >= 0; i--) {
90795e339b6SFabrice Gasnier 		/* Clear STM32H7_LINCALRDYW[6..1]: transfer calib to CALFACT2 */
90895e339b6SFabrice Gasnier 		stm32_adc_clr_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask);
90995e339b6SFabrice Gasnier 
91095e339b6SFabrice Gasnier 		/* Poll: wait calib data to be ready in CALFACT2 register */
91195e339b6SFabrice Gasnier 		ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
91295e339b6SFabrice Gasnier 						   !(val & lincalrdyw_mask),
91395e339b6SFabrice Gasnier 						   100, STM32_ADC_TIMEOUT_US);
91495e339b6SFabrice Gasnier 		if (ret) {
91595e339b6SFabrice Gasnier 			dev_err(&indio_dev->dev, "Failed to read calfact\n");
9160da98c7bSFabrice Gasnier 			return ret;
91795e339b6SFabrice Gasnier 		}
91895e339b6SFabrice Gasnier 
91995e339b6SFabrice Gasnier 		val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2);
92095e339b6SFabrice Gasnier 		adc->cal.lincalfact[i] = (val & STM32H7_LINCALFACT_MASK);
92195e339b6SFabrice Gasnier 		adc->cal.lincalfact[i] >>= STM32H7_LINCALFACT_SHIFT;
92295e339b6SFabrice Gasnier 
92395e339b6SFabrice Gasnier 		lincalrdyw_mask >>= 1;
92495e339b6SFabrice Gasnier 	}
92595e339b6SFabrice Gasnier 
92695e339b6SFabrice Gasnier 	/* Read offset calibration */
92795e339b6SFabrice Gasnier 	val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT);
92895e339b6SFabrice Gasnier 	adc->cal.calfact_s = (val & STM32H7_CALFACT_S_MASK);
92995e339b6SFabrice Gasnier 	adc->cal.calfact_s >>= STM32H7_CALFACT_S_SHIFT;
93095e339b6SFabrice Gasnier 	adc->cal.calfact_d = (val & STM32H7_CALFACT_D_MASK);
93195e339b6SFabrice Gasnier 	adc->cal.calfact_d >>= STM32H7_CALFACT_D_SHIFT;
9320da98c7bSFabrice Gasnier 	adc->cal.calibrated = true;
93395e339b6SFabrice Gasnier 
9340da98c7bSFabrice Gasnier 	return 0;
93595e339b6SFabrice Gasnier }
93695e339b6SFabrice Gasnier 
93795e339b6SFabrice Gasnier /**
93895e339b6SFabrice Gasnier  * stm32h7_adc_restore_selfcalib() - Restore saved self-calibration result
939cd64d357SAlexandru Ardelean  * @indio_dev: IIO device instance
94095e339b6SFabrice Gasnier  * Note: ADC must be enabled, with no on-going conversions.
94195e339b6SFabrice Gasnier  */
942cd64d357SAlexandru Ardelean static int stm32h7_adc_restore_selfcalib(struct iio_dev *indio_dev)
94395e339b6SFabrice Gasnier {
944cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
94595e339b6SFabrice Gasnier 	int i, ret;
94695e339b6SFabrice Gasnier 	u32 lincalrdyw_mask, val;
94795e339b6SFabrice Gasnier 
94895e339b6SFabrice Gasnier 	val = (adc->cal.calfact_s << STM32H7_CALFACT_S_SHIFT) |
94995e339b6SFabrice Gasnier 		(adc->cal.calfact_d << STM32H7_CALFACT_D_SHIFT);
95095e339b6SFabrice Gasnier 	stm32_adc_writel(adc, STM32H7_ADC_CALFACT, val);
95195e339b6SFabrice Gasnier 
95295e339b6SFabrice Gasnier 	lincalrdyw_mask = STM32H7_LINCALRDYW6;
95395e339b6SFabrice Gasnier 	for (i = STM32H7_LINCALFACT_NUM - 1; i >= 0; i--) {
95495e339b6SFabrice Gasnier 		/*
95595e339b6SFabrice Gasnier 		 * Write saved calibration data to shadow registers:
95695e339b6SFabrice Gasnier 		 * Write CALFACT2, and set LINCALRDYW[6..1] bit to trigger
95795e339b6SFabrice Gasnier 		 * data write. Then poll to wait for complete transfer.
95895e339b6SFabrice Gasnier 		 */
95995e339b6SFabrice Gasnier 		val = adc->cal.lincalfact[i] << STM32H7_LINCALFACT_SHIFT;
96095e339b6SFabrice Gasnier 		stm32_adc_writel(adc, STM32H7_ADC_CALFACT2, val);
96195e339b6SFabrice Gasnier 		stm32_adc_set_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask);
96295e339b6SFabrice Gasnier 		ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
96395e339b6SFabrice Gasnier 						   val & lincalrdyw_mask,
96495e339b6SFabrice Gasnier 						   100, STM32_ADC_TIMEOUT_US);
96595e339b6SFabrice Gasnier 		if (ret) {
96695e339b6SFabrice Gasnier 			dev_err(&indio_dev->dev, "Failed to write calfact\n");
96795e339b6SFabrice Gasnier 			return ret;
96895e339b6SFabrice Gasnier 		}
96995e339b6SFabrice Gasnier 
97095e339b6SFabrice Gasnier 		/*
97195e339b6SFabrice Gasnier 		 * Read back calibration data, has two effects:
97295e339b6SFabrice Gasnier 		 * - It ensures bits LINCALRDYW[6..1] are kept cleared
97395e339b6SFabrice Gasnier 		 *   for next time calibration needs to be restored.
97495e339b6SFabrice Gasnier 		 * - BTW, bit clear triggers a read, then check data has been
97595e339b6SFabrice Gasnier 		 *   correctly written.
97695e339b6SFabrice Gasnier 		 */
97795e339b6SFabrice Gasnier 		stm32_adc_clr_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask);
97895e339b6SFabrice Gasnier 		ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
97995e339b6SFabrice Gasnier 						   !(val & lincalrdyw_mask),
98095e339b6SFabrice Gasnier 						   100, STM32_ADC_TIMEOUT_US);
98195e339b6SFabrice Gasnier 		if (ret) {
98295e339b6SFabrice Gasnier 			dev_err(&indio_dev->dev, "Failed to read calfact\n");
98395e339b6SFabrice Gasnier 			return ret;
98495e339b6SFabrice Gasnier 		}
98595e339b6SFabrice Gasnier 		val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2);
98695e339b6SFabrice Gasnier 		if (val != adc->cal.lincalfact[i] << STM32H7_LINCALFACT_SHIFT) {
98795e339b6SFabrice Gasnier 			dev_err(&indio_dev->dev, "calfact not consistent\n");
98895e339b6SFabrice Gasnier 			return -EIO;
98995e339b6SFabrice Gasnier 		}
99095e339b6SFabrice Gasnier 
99195e339b6SFabrice Gasnier 		lincalrdyw_mask >>= 1;
99295e339b6SFabrice Gasnier 	}
99395e339b6SFabrice Gasnier 
99495e339b6SFabrice Gasnier 	return 0;
99595e339b6SFabrice Gasnier }
99695e339b6SFabrice Gasnier 
997fb6da706SJonathan Cameron /*
99895e339b6SFabrice Gasnier  * Fixed timeout value for ADC calibration.
99995e339b6SFabrice Gasnier  * worst cases:
100095e339b6SFabrice Gasnier  * - low clock frequency
100195e339b6SFabrice Gasnier  * - maximum prescalers
100295e339b6SFabrice Gasnier  * Calibration requires:
100395e339b6SFabrice Gasnier  * - 131,072 ADC clock cycle for the linear calibration
100495e339b6SFabrice Gasnier  * - 20 ADC clock cycle for the offset calibration
100595e339b6SFabrice Gasnier  *
100695e339b6SFabrice Gasnier  * Set to 100ms for now
100795e339b6SFabrice Gasnier  */
100895e339b6SFabrice Gasnier #define STM32H7_ADC_CALIB_TIMEOUT_US		100000
100995e339b6SFabrice Gasnier 
101095e339b6SFabrice Gasnier /**
10110da98c7bSFabrice Gasnier  * stm32h7_adc_selfcalib() - Procedure to calibrate ADC
1012cd64d357SAlexandru Ardelean  * @indio_dev: IIO device instance
10130da98c7bSFabrice Gasnier  * Note: Must be called once ADC is out of power down.
101495e339b6SFabrice Gasnier  */
1015cd64d357SAlexandru Ardelean static int stm32h7_adc_selfcalib(struct iio_dev *indio_dev)
101695e339b6SFabrice Gasnier {
1017cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
101895e339b6SFabrice Gasnier 	int ret;
101995e339b6SFabrice Gasnier 	u32 val;
102095e339b6SFabrice Gasnier 
10210da98c7bSFabrice Gasnier 	if (adc->cal.calibrated)
10220da98c7bSFabrice Gasnier 		return true;
102395e339b6SFabrice Gasnier 
1024883f50eaSOlivier Moysan 	/* ADC must be disabled for calibration */
1025883f50eaSOlivier Moysan 	stm32h7_adc_disable(indio_dev);
1026883f50eaSOlivier Moysan 
102795e339b6SFabrice Gasnier 	/*
102895e339b6SFabrice Gasnier 	 * Select calibration mode:
102995e339b6SFabrice Gasnier 	 * - Offset calibration for single ended inputs
103095e339b6SFabrice Gasnier 	 * - No linearity calibration (do it later, before reading it)
103195e339b6SFabrice Gasnier 	 */
103295e339b6SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADCALDIF);
103395e339b6SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADCALLIN);
103495e339b6SFabrice Gasnier 
103595e339b6SFabrice Gasnier 	/* Start calibration, then wait for completion */
103695e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADCAL);
103795e339b6SFabrice Gasnier 	ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
103895e339b6SFabrice Gasnier 					   !(val & STM32H7_ADCAL), 100,
103995e339b6SFabrice Gasnier 					   STM32H7_ADC_CALIB_TIMEOUT_US);
104095e339b6SFabrice Gasnier 	if (ret) {
104195e339b6SFabrice Gasnier 		dev_err(&indio_dev->dev, "calibration failed\n");
10420da98c7bSFabrice Gasnier 		goto out;
104395e339b6SFabrice Gasnier 	}
104495e339b6SFabrice Gasnier 
104595e339b6SFabrice Gasnier 	/*
104695e339b6SFabrice Gasnier 	 * Select calibration mode, then start calibration:
104795e339b6SFabrice Gasnier 	 * - Offset calibration for differential input
104895e339b6SFabrice Gasnier 	 * - Linearity calibration (needs to be done only once for single/diff)
104995e339b6SFabrice Gasnier 	 *   will run simultaneously with offset calibration.
105095e339b6SFabrice Gasnier 	 */
105195e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR,
105295e339b6SFabrice Gasnier 			   STM32H7_ADCALDIF | STM32H7_ADCALLIN);
105395e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADCAL);
105495e339b6SFabrice Gasnier 	ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
105595e339b6SFabrice Gasnier 					   !(val & STM32H7_ADCAL), 100,
105695e339b6SFabrice Gasnier 					   STM32H7_ADC_CALIB_TIMEOUT_US);
105795e339b6SFabrice Gasnier 	if (ret) {
105895e339b6SFabrice Gasnier 		dev_err(&indio_dev->dev, "calibration failed\n");
10590da98c7bSFabrice Gasnier 		goto out;
106095e339b6SFabrice Gasnier 	}
106195e339b6SFabrice Gasnier 
10620da98c7bSFabrice Gasnier out:
106395e339b6SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32H7_ADC_CR,
106495e339b6SFabrice Gasnier 			   STM32H7_ADCALDIF | STM32H7_ADCALLIN);
106595e339b6SFabrice Gasnier 
106695e339b6SFabrice Gasnier 	return ret;
106795e339b6SFabrice Gasnier }
106895e339b6SFabrice Gasnier 
106995e339b6SFabrice Gasnier /**
107095e339b6SFabrice Gasnier  * stm32h7_adc_prepare() - Leave power down mode to enable ADC.
1071cd64d357SAlexandru Ardelean  * @indio_dev: IIO device instance
107295e339b6SFabrice Gasnier  * Leave power down mode.
10733fb2e24eSFabrice Gasnier  * Configure channels as single ended or differential before enabling ADC.
107495e339b6SFabrice Gasnier  * Enable ADC.
107595e339b6SFabrice Gasnier  * Restore calibration data.
10763fb2e24eSFabrice Gasnier  * Pre-select channels that may be used in PCSEL (required by input MUX / IO):
10773fb2e24eSFabrice Gasnier  * - Only one input is selected for single ended (e.g. 'vinp')
10783fb2e24eSFabrice Gasnier  * - Two inputs are selected for differential channels (e.g. 'vinp' & 'vinn')
107995e339b6SFabrice Gasnier  */
1080cd64d357SAlexandru Ardelean static int stm32h7_adc_prepare(struct iio_dev *indio_dev)
108195e339b6SFabrice Gasnier {
1082cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
10830da98c7bSFabrice Gasnier 	int calib, ret;
108495e339b6SFabrice Gasnier 
1085cd64d357SAlexandru Ardelean 	ret = stm32h7_adc_exit_pwr_down(indio_dev);
1086d58c67d1SFabrice Gasnier 	if (ret)
1087d58c67d1SFabrice Gasnier 		return ret;
1088d58c67d1SFabrice Gasnier 
1089cd64d357SAlexandru Ardelean 	ret = stm32h7_adc_selfcalib(indio_dev);
10900da98c7bSFabrice Gasnier 	if (ret < 0)
10910da98c7bSFabrice Gasnier 		goto pwr_dwn;
10920da98c7bSFabrice Gasnier 	calib = ret;
10930da98c7bSFabrice Gasnier 
1094aec6e0d8SOlivier Moysan 	stm32_adc_int_ch_enable(indio_dev);
1095aec6e0d8SOlivier Moysan 
10963fb2e24eSFabrice Gasnier 	stm32_adc_writel(adc, STM32H7_ADC_DIFSEL, adc->difsel);
109795e339b6SFabrice Gasnier 
1098cd64d357SAlexandru Ardelean 	ret = stm32h7_adc_enable(indio_dev);
109995e339b6SFabrice Gasnier 	if (ret)
1100aec6e0d8SOlivier Moysan 		goto ch_disable;
110195e339b6SFabrice Gasnier 
11020da98c7bSFabrice Gasnier 	/* Either restore or read calibration result for future reference */
11030da98c7bSFabrice Gasnier 	if (calib)
1104cd64d357SAlexandru Ardelean 		ret = stm32h7_adc_restore_selfcalib(indio_dev);
11050da98c7bSFabrice Gasnier 	else
1106cd64d357SAlexandru Ardelean 		ret = stm32h7_adc_read_selfcalib(indio_dev);
110795e339b6SFabrice Gasnier 	if (ret)
110895e339b6SFabrice Gasnier 		goto disable;
110995e339b6SFabrice Gasnier 
111095e339b6SFabrice Gasnier 	stm32_adc_writel(adc, STM32H7_ADC_PCSEL, adc->pcsel);
111195e339b6SFabrice Gasnier 
111295e339b6SFabrice Gasnier 	return 0;
111395e339b6SFabrice Gasnier 
111495e339b6SFabrice Gasnier disable:
1115cd64d357SAlexandru Ardelean 	stm32h7_adc_disable(indio_dev);
1116aec6e0d8SOlivier Moysan ch_disable:
1117aec6e0d8SOlivier Moysan 	stm32_adc_int_ch_disable(adc);
111895e339b6SFabrice Gasnier pwr_dwn:
111995e339b6SFabrice Gasnier 	stm32h7_adc_enter_pwr_down(adc);
112095e339b6SFabrice Gasnier 
112195e339b6SFabrice Gasnier 	return ret;
112295e339b6SFabrice Gasnier }
112395e339b6SFabrice Gasnier 
1124cd64d357SAlexandru Ardelean static void stm32h7_adc_unprepare(struct iio_dev *indio_dev)
112595e339b6SFabrice Gasnier {
1126cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
1127cd64d357SAlexandru Ardelean 
1128f711f28eSFabrice Gasnier 	stm32_adc_writel(adc, STM32H7_ADC_PCSEL, 0);
1129cd64d357SAlexandru Ardelean 	stm32h7_adc_disable(indio_dev);
1130aec6e0d8SOlivier Moysan 	stm32_adc_int_ch_disable(adc);
113195e339b6SFabrice Gasnier 	stm32h7_adc_enter_pwr_down(adc);
113295e339b6SFabrice Gasnier }
113395e339b6SFabrice Gasnier 
11340f883b22SFabrice Gasnier /**
1135da9b9485SFabrice Gasnier  * stm32_adc_conf_scan_seq() - Build regular channels scan sequence
1136da9b9485SFabrice Gasnier  * @indio_dev: IIO device
1137da9b9485SFabrice Gasnier  * @scan_mask: channels to be converted
1138da9b9485SFabrice Gasnier  *
1139da9b9485SFabrice Gasnier  * Conversion sequence :
1140ee2ac1cdSFabrice Gasnier  * Apply sampling time settings for all channels.
1141da9b9485SFabrice Gasnier  * Configure ADC scan sequence based on selected channels in scan_mask.
1142da9b9485SFabrice Gasnier  * Add channels to SQR registers, from scan_mask LSB to MSB, then
1143da9b9485SFabrice Gasnier  * program sequence len.
1144da9b9485SFabrice Gasnier  */
1145da9b9485SFabrice Gasnier static int stm32_adc_conf_scan_seq(struct iio_dev *indio_dev,
1146da9b9485SFabrice Gasnier 				   const unsigned long *scan_mask)
1147da9b9485SFabrice Gasnier {
1148da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
114964ad7f64SFabrice Gasnier 	const struct stm32_adc_regs *sqr = adc->cfg->regs->sqr;
1150da9b9485SFabrice Gasnier 	const struct iio_chan_spec *chan;
1151da9b9485SFabrice Gasnier 	u32 val, bit;
1152da9b9485SFabrice Gasnier 	int i = 0;
1153da9b9485SFabrice Gasnier 
1154ee2ac1cdSFabrice Gasnier 	/* Apply sampling time settings */
1155ee2ac1cdSFabrice Gasnier 	stm32_adc_writel(adc, adc->cfg->regs->smpr[0], adc->smpr_val[0]);
1156ee2ac1cdSFabrice Gasnier 	stm32_adc_writel(adc, adc->cfg->regs->smpr[1], adc->smpr_val[1]);
1157ee2ac1cdSFabrice Gasnier 
1158da9b9485SFabrice Gasnier 	for_each_set_bit(bit, scan_mask, indio_dev->masklength) {
1159da9b9485SFabrice Gasnier 		chan = indio_dev->channels + bit;
1160da9b9485SFabrice Gasnier 		/*
1161da9b9485SFabrice Gasnier 		 * Assign one channel per SQ entry in regular
1162da9b9485SFabrice Gasnier 		 * sequence, starting with SQ1.
1163da9b9485SFabrice Gasnier 		 */
1164da9b9485SFabrice Gasnier 		i++;
1165da9b9485SFabrice Gasnier 		if (i > STM32_ADC_MAX_SQ)
1166da9b9485SFabrice Gasnier 			return -EINVAL;
1167da9b9485SFabrice Gasnier 
1168da9b9485SFabrice Gasnier 		dev_dbg(&indio_dev->dev, "%s chan %d to SQ%d\n",
1169da9b9485SFabrice Gasnier 			__func__, chan->channel, i);
1170da9b9485SFabrice Gasnier 
117164ad7f64SFabrice Gasnier 		val = stm32_adc_readl(adc, sqr[i].reg);
117264ad7f64SFabrice Gasnier 		val &= ~sqr[i].mask;
117364ad7f64SFabrice Gasnier 		val |= chan->channel << sqr[i].shift;
117464ad7f64SFabrice Gasnier 		stm32_adc_writel(adc, sqr[i].reg, val);
1175da9b9485SFabrice Gasnier 	}
1176da9b9485SFabrice Gasnier 
1177da9b9485SFabrice Gasnier 	if (!i)
1178da9b9485SFabrice Gasnier 		return -EINVAL;
1179da9b9485SFabrice Gasnier 
1180da9b9485SFabrice Gasnier 	/* Sequence len */
118164ad7f64SFabrice Gasnier 	val = stm32_adc_readl(adc, sqr[0].reg);
118264ad7f64SFabrice Gasnier 	val &= ~sqr[0].mask;
118364ad7f64SFabrice Gasnier 	val |= ((i - 1) << sqr[0].shift);
118464ad7f64SFabrice Gasnier 	stm32_adc_writel(adc, sqr[0].reg, val);
1185da9b9485SFabrice Gasnier 
1186da9b9485SFabrice Gasnier 	return 0;
1187da9b9485SFabrice Gasnier }
1188da9b9485SFabrice Gasnier 
1189da9b9485SFabrice Gasnier /**
1190da9b9485SFabrice Gasnier  * stm32_adc_get_trig_extsel() - Get external trigger selection
11911cd92d42SFabrice Gasnier  * @indio_dev: IIO device structure
1192da9b9485SFabrice Gasnier  * @trig: trigger
1193da9b9485SFabrice Gasnier  *
1194da9b9485SFabrice Gasnier  * Returns trigger extsel value, if trig matches, -EINVAL otherwise.
1195da9b9485SFabrice Gasnier  */
119664ad7f64SFabrice Gasnier static int stm32_adc_get_trig_extsel(struct iio_dev *indio_dev,
119764ad7f64SFabrice Gasnier 				     struct iio_trigger *trig)
1198da9b9485SFabrice Gasnier {
119964ad7f64SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
1200f24a33b3SFabrice Gasnier 	int i;
1201f24a33b3SFabrice Gasnier 
1202f24a33b3SFabrice Gasnier 	/* lookup triggers registered by stm32 timer trigger driver */
120364ad7f64SFabrice Gasnier 	for (i = 0; adc->cfg->trigs[i].name; i++) {
1204f24a33b3SFabrice Gasnier 		/**
1205f24a33b3SFabrice Gasnier 		 * Checking both stm32 timer trigger type and trig name
1206f24a33b3SFabrice Gasnier 		 * should be safe against arbitrary trigger names.
1207f24a33b3SFabrice Gasnier 		 */
1208f0b638a7SFabrice Gasnier 		if ((is_stm32_timer_trigger(trig) ||
1209f0b638a7SFabrice Gasnier 		     is_stm32_lptim_trigger(trig)) &&
121064ad7f64SFabrice Gasnier 		    !strcmp(adc->cfg->trigs[i].name, trig->name)) {
121164ad7f64SFabrice Gasnier 			return adc->cfg->trigs[i].extsel;
1212f24a33b3SFabrice Gasnier 		}
1213f24a33b3SFabrice Gasnier 	}
1214f24a33b3SFabrice Gasnier 
1215da9b9485SFabrice Gasnier 	return -EINVAL;
1216da9b9485SFabrice Gasnier }
1217da9b9485SFabrice Gasnier 
1218da9b9485SFabrice Gasnier /**
1219da9b9485SFabrice Gasnier  * stm32_adc_set_trig() - Set a regular trigger
1220da9b9485SFabrice Gasnier  * @indio_dev: IIO device
1221da9b9485SFabrice Gasnier  * @trig: IIO trigger
1222da9b9485SFabrice Gasnier  *
1223da9b9485SFabrice Gasnier  * Set trigger source/polarity (e.g. SW, or HW with polarity) :
1224da9b9485SFabrice Gasnier  * - if HW trigger disabled (e.g. trig == NULL, conversion launched by sw)
1225da9b9485SFabrice Gasnier  * - if HW trigger enabled, set source & polarity
1226da9b9485SFabrice Gasnier  */
1227da9b9485SFabrice Gasnier static int stm32_adc_set_trig(struct iio_dev *indio_dev,
1228da9b9485SFabrice Gasnier 			      struct iio_trigger *trig)
1229da9b9485SFabrice Gasnier {
1230da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
1231da9b9485SFabrice Gasnier 	u32 val, extsel = 0, exten = STM32_EXTEN_SWTRIG;
1232da9b9485SFabrice Gasnier 	unsigned long flags;
1233da9b9485SFabrice Gasnier 	int ret;
1234da9b9485SFabrice Gasnier 
1235da9b9485SFabrice Gasnier 	if (trig) {
123664ad7f64SFabrice Gasnier 		ret = stm32_adc_get_trig_extsel(indio_dev, trig);
1237da9b9485SFabrice Gasnier 		if (ret < 0)
1238da9b9485SFabrice Gasnier 			return ret;
1239da9b9485SFabrice Gasnier 
1240da9b9485SFabrice Gasnier 		/* set trigger source and polarity (default to rising edge) */
1241da9b9485SFabrice Gasnier 		extsel = ret;
1242732f2dc4SFabrice Gasnier 		exten = adc->trigger_polarity + STM32_EXTEN_HWTRIG_RISING_EDGE;
1243da9b9485SFabrice Gasnier 	}
1244da9b9485SFabrice Gasnier 
1245da9b9485SFabrice Gasnier 	spin_lock_irqsave(&adc->lock, flags);
124664ad7f64SFabrice Gasnier 	val = stm32_adc_readl(adc, adc->cfg->regs->exten.reg);
124764ad7f64SFabrice Gasnier 	val &= ~(adc->cfg->regs->exten.mask | adc->cfg->regs->extsel.mask);
124864ad7f64SFabrice Gasnier 	val |= exten << adc->cfg->regs->exten.shift;
124964ad7f64SFabrice Gasnier 	val |= extsel << adc->cfg->regs->extsel.shift;
125064ad7f64SFabrice Gasnier 	stm32_adc_writel(adc,  adc->cfg->regs->exten.reg, val);
1251da9b9485SFabrice Gasnier 	spin_unlock_irqrestore(&adc->lock, flags);
1252da9b9485SFabrice Gasnier 
1253da9b9485SFabrice Gasnier 	return 0;
1254da9b9485SFabrice Gasnier }
1255da9b9485SFabrice Gasnier 
1256732f2dc4SFabrice Gasnier static int stm32_adc_set_trig_pol(struct iio_dev *indio_dev,
1257732f2dc4SFabrice Gasnier 				  const struct iio_chan_spec *chan,
1258732f2dc4SFabrice Gasnier 				  unsigned int type)
1259732f2dc4SFabrice Gasnier {
1260732f2dc4SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
1261732f2dc4SFabrice Gasnier 
1262732f2dc4SFabrice Gasnier 	adc->trigger_polarity = type;
1263732f2dc4SFabrice Gasnier 
1264732f2dc4SFabrice Gasnier 	return 0;
1265732f2dc4SFabrice Gasnier }
1266732f2dc4SFabrice Gasnier 
1267732f2dc4SFabrice Gasnier static int stm32_adc_get_trig_pol(struct iio_dev *indio_dev,
1268732f2dc4SFabrice Gasnier 				  const struct iio_chan_spec *chan)
1269732f2dc4SFabrice Gasnier {
1270732f2dc4SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
1271732f2dc4SFabrice Gasnier 
1272732f2dc4SFabrice Gasnier 	return adc->trigger_polarity;
1273732f2dc4SFabrice Gasnier }
1274732f2dc4SFabrice Gasnier 
1275732f2dc4SFabrice Gasnier static const char * const stm32_trig_pol_items[] = {
1276732f2dc4SFabrice Gasnier 	"rising-edge", "falling-edge", "both-edges",
1277732f2dc4SFabrice Gasnier };
1278732f2dc4SFabrice Gasnier 
12792763ea05SFabrice Gasnier static const struct iio_enum stm32_adc_trig_pol = {
1280732f2dc4SFabrice Gasnier 	.items = stm32_trig_pol_items,
1281732f2dc4SFabrice Gasnier 	.num_items = ARRAY_SIZE(stm32_trig_pol_items),
1282732f2dc4SFabrice Gasnier 	.get = stm32_adc_get_trig_pol,
1283732f2dc4SFabrice Gasnier 	.set = stm32_adc_set_trig_pol,
1284732f2dc4SFabrice Gasnier };
1285732f2dc4SFabrice Gasnier 
1286da9b9485SFabrice Gasnier /**
12870f883b22SFabrice Gasnier  * stm32_adc_single_conv() - Performs a single conversion
12880f883b22SFabrice Gasnier  * @indio_dev: IIO device
12890f883b22SFabrice Gasnier  * @chan: IIO channel
12900f883b22SFabrice Gasnier  * @res: conversion result
12910f883b22SFabrice Gasnier  *
12920f883b22SFabrice Gasnier  * The function performs a single conversion on a given channel:
1293ee2ac1cdSFabrice Gasnier  * - Apply sampling time settings
12940f883b22SFabrice Gasnier  * - Program sequencer with one channel (e.g. in SQ1 with len = 1)
12950f883b22SFabrice Gasnier  * - Use SW trigger
12960f883b22SFabrice Gasnier  * - Start conversion, then wait for interrupt completion.
12970f883b22SFabrice Gasnier  */
12980f883b22SFabrice Gasnier static int stm32_adc_single_conv(struct iio_dev *indio_dev,
12990f883b22SFabrice Gasnier 				 const struct iio_chan_spec *chan,
13000f883b22SFabrice Gasnier 				 int *res)
13010f883b22SFabrice Gasnier {
13020f883b22SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
13039bdbb113SFabrice Gasnier 	struct device *dev = indio_dev->dev.parent;
130464ad7f64SFabrice Gasnier 	const struct stm32_adc_regspec *regs = adc->cfg->regs;
13050f883b22SFabrice Gasnier 	long timeout;
13060f883b22SFabrice Gasnier 	u32 val;
13070f883b22SFabrice Gasnier 	int ret;
13080f883b22SFabrice Gasnier 
13090f883b22SFabrice Gasnier 	reinit_completion(&adc->completion);
13100f883b22SFabrice Gasnier 
1311da9b9485SFabrice Gasnier 	adc->bufi = 0;
13120f883b22SFabrice Gasnier 
1313265028b8SJonathan Cameron 	ret = pm_runtime_resume_and_get(dev);
1314265028b8SJonathan Cameron 	if (ret < 0)
131595e339b6SFabrice Gasnier 		return ret;
131695e339b6SFabrice Gasnier 
1317ee2ac1cdSFabrice Gasnier 	/* Apply sampling time settings */
1318ee2ac1cdSFabrice Gasnier 	stm32_adc_writel(adc, regs->smpr[0], adc->smpr_val[0]);
1319ee2ac1cdSFabrice Gasnier 	stm32_adc_writel(adc, regs->smpr[1], adc->smpr_val[1]);
1320ee2ac1cdSFabrice Gasnier 
1321da9b9485SFabrice Gasnier 	/* Program chan number in regular sequence (SQ1) */
132264ad7f64SFabrice Gasnier 	val = stm32_adc_readl(adc, regs->sqr[1].reg);
132364ad7f64SFabrice Gasnier 	val &= ~regs->sqr[1].mask;
132464ad7f64SFabrice Gasnier 	val |= chan->channel << regs->sqr[1].shift;
132564ad7f64SFabrice Gasnier 	stm32_adc_writel(adc, regs->sqr[1].reg, val);
13260f883b22SFabrice Gasnier 
13270f883b22SFabrice Gasnier 	/* Set regular sequence len (0 for 1 conversion) */
132864ad7f64SFabrice Gasnier 	stm32_adc_clr_bits(adc, regs->sqr[0].reg, regs->sqr[0].mask);
13290f883b22SFabrice Gasnier 
13300f883b22SFabrice Gasnier 	/* Trigger detection disabled (conversion can be launched in SW) */
133164ad7f64SFabrice Gasnier 	stm32_adc_clr_bits(adc, regs->exten.reg, regs->exten.mask);
13320f883b22SFabrice Gasnier 
13330f883b22SFabrice Gasnier 	stm32_adc_conv_irq_enable(adc);
13340f883b22SFabrice Gasnier 
1335cd64d357SAlexandru Ardelean 	adc->cfg->start_conv(indio_dev, false);
13360f883b22SFabrice Gasnier 
13370f883b22SFabrice Gasnier 	timeout = wait_for_completion_interruptible_timeout(
13380f883b22SFabrice Gasnier 					&adc->completion, STM32_ADC_TIMEOUT);
13390f883b22SFabrice Gasnier 	if (timeout == 0) {
13400f883b22SFabrice Gasnier 		ret = -ETIMEDOUT;
13410f883b22SFabrice Gasnier 	} else if (timeout < 0) {
13420f883b22SFabrice Gasnier 		ret = timeout;
13430f883b22SFabrice Gasnier 	} else {
1344da9b9485SFabrice Gasnier 		*res = adc->buffer[0];
13450f883b22SFabrice Gasnier 		ret = IIO_VAL_INT;
13460f883b22SFabrice Gasnier 	}
13470f883b22SFabrice Gasnier 
1348cd64d357SAlexandru Ardelean 	adc->cfg->stop_conv(indio_dev);
13490f883b22SFabrice Gasnier 
13500f883b22SFabrice Gasnier 	stm32_adc_conv_irq_disable(adc);
13510f883b22SFabrice Gasnier 
13529bdbb113SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
13539bdbb113SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
135495e339b6SFabrice Gasnier 
13550f883b22SFabrice Gasnier 	return ret;
13560f883b22SFabrice Gasnier }
13570f883b22SFabrice Gasnier 
13580f883b22SFabrice Gasnier static int stm32_adc_read_raw(struct iio_dev *indio_dev,
13590f883b22SFabrice Gasnier 			      struct iio_chan_spec const *chan,
13600f883b22SFabrice Gasnier 			      int *val, int *val2, long mask)
13610f883b22SFabrice Gasnier {
13620f883b22SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
13630f883b22SFabrice Gasnier 	int ret;
13640f883b22SFabrice Gasnier 
13650f883b22SFabrice Gasnier 	switch (mask) {
13660f883b22SFabrice Gasnier 	case IIO_CHAN_INFO_RAW:
13670e346b2cSOlivier Moysan 	case IIO_CHAN_INFO_PROCESSED:
13680f883b22SFabrice Gasnier 		ret = iio_device_claim_direct_mode(indio_dev);
13690f883b22SFabrice Gasnier 		if (ret)
13700f883b22SFabrice Gasnier 			return ret;
13710f883b22SFabrice Gasnier 		if (chan->type == IIO_VOLTAGE)
13720f883b22SFabrice Gasnier 			ret = stm32_adc_single_conv(indio_dev, chan, val);
13730f883b22SFabrice Gasnier 		else
13740f883b22SFabrice Gasnier 			ret = -EINVAL;
13750e346b2cSOlivier Moysan 
1376bc05f30fSOlivier Moysan 		if (mask == IIO_CHAN_INFO_PROCESSED)
13770e346b2cSOlivier Moysan 			*val = STM32_ADC_VREFINT_VOLTAGE * adc->vrefint.vrefint_cal / *val;
13780e346b2cSOlivier Moysan 
13790f883b22SFabrice Gasnier 		iio_device_release_direct_mode(indio_dev);
13800f883b22SFabrice Gasnier 		return ret;
13810f883b22SFabrice Gasnier 
13820f883b22SFabrice Gasnier 	case IIO_CHAN_INFO_SCALE:
13833fb2e24eSFabrice Gasnier 		if (chan->differential) {
13843fb2e24eSFabrice Gasnier 			*val = adc->common->vref_mv * 2;
13853fb2e24eSFabrice Gasnier 			*val2 = chan->scan_type.realbits;
13863fb2e24eSFabrice Gasnier 		} else {
13870f883b22SFabrice Gasnier 			*val = adc->common->vref_mv;
13880f883b22SFabrice Gasnier 			*val2 = chan->scan_type.realbits;
13893fb2e24eSFabrice Gasnier 		}
13900f883b22SFabrice Gasnier 		return IIO_VAL_FRACTIONAL_LOG2;
13910f883b22SFabrice Gasnier 
13923fb2e24eSFabrice Gasnier 	case IIO_CHAN_INFO_OFFSET:
13933fb2e24eSFabrice Gasnier 		if (chan->differential)
13943fb2e24eSFabrice Gasnier 			/* ADC_full_scale / 2 */
13953fb2e24eSFabrice Gasnier 			*val = -((1 << chan->scan_type.realbits) / 2);
13963fb2e24eSFabrice Gasnier 		else
13973fb2e24eSFabrice Gasnier 			*val = 0;
13983fb2e24eSFabrice Gasnier 		return IIO_VAL_INT;
13993fb2e24eSFabrice Gasnier 
14000f883b22SFabrice Gasnier 	default:
14010f883b22SFabrice Gasnier 		return -EINVAL;
14020f883b22SFabrice Gasnier 	}
14030f883b22SFabrice Gasnier }
14040f883b22SFabrice Gasnier 
1405695e2f5cSOlivier Moysan static void stm32_adc_irq_clear(struct iio_dev *indio_dev, u32 msk)
1406695e2f5cSOlivier Moysan {
1407695e2f5cSOlivier Moysan 	struct stm32_adc *adc = iio_priv(indio_dev);
1408695e2f5cSOlivier Moysan 
1409695e2f5cSOlivier Moysan 	adc->cfg->irq_clear(indio_dev, msk);
1410695e2f5cSOlivier Moysan }
1411695e2f5cSOlivier Moysan 
1412cc06e67dSFabrice Gasnier static irqreturn_t stm32_adc_threaded_isr(int irq, void *data)
1413cc06e67dSFabrice Gasnier {
1414cd64d357SAlexandru Ardelean 	struct iio_dev *indio_dev = data;
1415cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
1416cc06e67dSFabrice Gasnier 	const struct stm32_adc_regspec *regs = adc->cfg->regs;
1417cc06e67dSFabrice Gasnier 	u32 status = stm32_adc_readl(adc, regs->isr_eoc.reg);
1418cc06e67dSFabrice Gasnier 
1419695e2f5cSOlivier Moysan 	/* Check ovr status right now, as ovr mask should be already disabled */
1420695e2f5cSOlivier Moysan 	if (status & regs->isr_ovr.mask) {
1421695e2f5cSOlivier Moysan 		/*
1422695e2f5cSOlivier Moysan 		 * Clear ovr bit to avoid subsequent calls to IRQ handler.
1423695e2f5cSOlivier Moysan 		 * This requires to stop ADC first. OVR bit state in ISR,
1424695e2f5cSOlivier Moysan 		 * is propaged to CSR register by hardware.
1425695e2f5cSOlivier Moysan 		 */
1426695e2f5cSOlivier Moysan 		adc->cfg->stop_conv(indio_dev);
1427695e2f5cSOlivier Moysan 		stm32_adc_irq_clear(indio_dev, regs->isr_ovr.mask);
1428cc06e67dSFabrice Gasnier 		dev_err(&indio_dev->dev, "Overrun, stopping: restart needed\n");
1429cc06e67dSFabrice Gasnier 		return IRQ_HANDLED;
1430cc06e67dSFabrice Gasnier 	}
1431cc06e67dSFabrice Gasnier 
1432695e2f5cSOlivier Moysan 	return IRQ_NONE;
1433695e2f5cSOlivier Moysan }
1434695e2f5cSOlivier Moysan 
14350f883b22SFabrice Gasnier static irqreturn_t stm32_adc_isr(int irq, void *data)
14360f883b22SFabrice Gasnier {
1437cd64d357SAlexandru Ardelean 	struct iio_dev *indio_dev = data;
1438cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
143964ad7f64SFabrice Gasnier 	const struct stm32_adc_regspec *regs = adc->cfg->regs;
144064ad7f64SFabrice Gasnier 	u32 status = stm32_adc_readl(adc, regs->isr_eoc.reg);
14410f883b22SFabrice Gasnier 
1442cc06e67dSFabrice Gasnier 	if (status & regs->isr_ovr.mask) {
1443cc06e67dSFabrice Gasnier 		/*
1444cc06e67dSFabrice Gasnier 		 * Overrun occurred on regular conversions: data for wrong
1445cc06e67dSFabrice Gasnier 		 * channel may be read. Unconditionally disable interrupts
1446cc06e67dSFabrice Gasnier 		 * to stop processing data and print error message.
1447cc06e67dSFabrice Gasnier 		 * Restarting the capture can be done by disabling, then
1448cc06e67dSFabrice Gasnier 		 * re-enabling it (e.g. write 0, then 1 to buffer/enable).
1449cc06e67dSFabrice Gasnier 		 */
1450cc06e67dSFabrice Gasnier 		stm32_adc_ovr_irq_disable(adc);
1451cc06e67dSFabrice Gasnier 		stm32_adc_conv_irq_disable(adc);
1452cc06e67dSFabrice Gasnier 		return IRQ_WAKE_THREAD;
1453cc06e67dSFabrice Gasnier 	}
1454cc06e67dSFabrice Gasnier 
145564ad7f64SFabrice Gasnier 	if (status & regs->isr_eoc.mask) {
1456da9b9485SFabrice Gasnier 		/* Reading DR also clears EOC status flag */
145764ad7f64SFabrice Gasnier 		adc->buffer[adc->bufi] = stm32_adc_readw(adc, regs->dr);
1458da9b9485SFabrice Gasnier 		if (iio_buffer_enabled(indio_dev)) {
1459da9b9485SFabrice Gasnier 			adc->bufi++;
1460da9b9485SFabrice Gasnier 			if (adc->bufi >= adc->num_conv) {
1461da9b9485SFabrice Gasnier 				stm32_adc_conv_irq_disable(adc);
1462da9b9485SFabrice Gasnier 				iio_trigger_poll(indio_dev->trig);
1463da9b9485SFabrice Gasnier 			}
1464da9b9485SFabrice Gasnier 		} else {
14650f883b22SFabrice Gasnier 			complete(&adc->completion);
1466da9b9485SFabrice Gasnier 		}
14670f883b22SFabrice Gasnier 		return IRQ_HANDLED;
14680f883b22SFabrice Gasnier 	}
14690f883b22SFabrice Gasnier 
14700f883b22SFabrice Gasnier 	return IRQ_NONE;
14710f883b22SFabrice Gasnier }
14720f883b22SFabrice Gasnier 
1473da9b9485SFabrice Gasnier /**
1474da9b9485SFabrice Gasnier  * stm32_adc_validate_trigger() - validate trigger for stm32 adc
1475da9b9485SFabrice Gasnier  * @indio_dev: IIO device
1476da9b9485SFabrice Gasnier  * @trig: new trigger
1477da9b9485SFabrice Gasnier  *
1478da9b9485SFabrice Gasnier  * Returns: 0 if trig matches one of the triggers registered by stm32 adc
1479da9b9485SFabrice Gasnier  * driver, -EINVAL otherwise.
1480da9b9485SFabrice Gasnier  */
1481da9b9485SFabrice Gasnier static int stm32_adc_validate_trigger(struct iio_dev *indio_dev,
1482da9b9485SFabrice Gasnier 				      struct iio_trigger *trig)
1483da9b9485SFabrice Gasnier {
148464ad7f64SFabrice Gasnier 	return stm32_adc_get_trig_extsel(indio_dev, trig) < 0 ? -EINVAL : 0;
1485da9b9485SFabrice Gasnier }
1486da9b9485SFabrice Gasnier 
14872763ea05SFabrice Gasnier static int stm32_adc_set_watermark(struct iio_dev *indio_dev, unsigned int val)
14882763ea05SFabrice Gasnier {
14892763ea05SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
14902763ea05SFabrice Gasnier 	unsigned int watermark = STM32_DMA_BUFFER_SIZE / 2;
149104e491caSFabrice Gasnier 	unsigned int rx_buf_sz = STM32_DMA_BUFFER_SIZE;
14922763ea05SFabrice Gasnier 
14932763ea05SFabrice Gasnier 	/*
14942763ea05SFabrice Gasnier 	 * dma cyclic transfers are used, buffer is split into two periods.
14952763ea05SFabrice Gasnier 	 * There should be :
14962763ea05SFabrice Gasnier 	 * - always one buffer (period) dma is working on
1497499da8bdSOlivier Moysan 	 * - one buffer (period) driver can push data.
14982763ea05SFabrice Gasnier 	 */
14992763ea05SFabrice Gasnier 	watermark = min(watermark, val * (unsigned)(sizeof(u16)));
150004e491caSFabrice Gasnier 	adc->rx_buf_sz = min(rx_buf_sz, watermark * 2 * adc->num_conv);
15012763ea05SFabrice Gasnier 
15022763ea05SFabrice Gasnier 	return 0;
15032763ea05SFabrice Gasnier }
15042763ea05SFabrice Gasnier 
1505da9b9485SFabrice Gasnier static int stm32_adc_update_scan_mode(struct iio_dev *indio_dev,
1506da9b9485SFabrice Gasnier 				      const unsigned long *scan_mask)
1507da9b9485SFabrice Gasnier {
1508da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
15099bdbb113SFabrice Gasnier 	struct device *dev = indio_dev->dev.parent;
1510da9b9485SFabrice Gasnier 	int ret;
1511da9b9485SFabrice Gasnier 
1512265028b8SJonathan Cameron 	ret = pm_runtime_resume_and_get(dev);
1513265028b8SJonathan Cameron 	if (ret < 0)
15149bdbb113SFabrice Gasnier 		return ret;
15159bdbb113SFabrice Gasnier 
1516da9b9485SFabrice Gasnier 	adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength);
1517da9b9485SFabrice Gasnier 
1518da9b9485SFabrice Gasnier 	ret = stm32_adc_conf_scan_seq(indio_dev, scan_mask);
15199bdbb113SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
15209bdbb113SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
1521da9b9485SFabrice Gasnier 
15229bdbb113SFabrice Gasnier 	return ret;
1523da9b9485SFabrice Gasnier }
1524da9b9485SFabrice Gasnier 
1525*d7705f35SNuno Sá static int stm32_adc_fwnode_xlate(struct iio_dev *indio_dev,
1526*d7705f35SNuno Sá 				  const struct fwnode_reference_args *iiospec)
15270f883b22SFabrice Gasnier {
15280f883b22SFabrice Gasnier 	int i;
15290f883b22SFabrice Gasnier 
15300f883b22SFabrice Gasnier 	for (i = 0; i < indio_dev->num_channels; i++)
15310f883b22SFabrice Gasnier 		if (indio_dev->channels[i].channel == iiospec->args[0])
15320f883b22SFabrice Gasnier 			return i;
15330f883b22SFabrice Gasnier 
15340f883b22SFabrice Gasnier 	return -EINVAL;
15350f883b22SFabrice Gasnier }
15360f883b22SFabrice Gasnier 
15370f883b22SFabrice Gasnier /**
15380f883b22SFabrice Gasnier  * stm32_adc_debugfs_reg_access - read or write register value
15391cd92d42SFabrice Gasnier  * @indio_dev: IIO device structure
15401cd92d42SFabrice Gasnier  * @reg: register offset
15411cd92d42SFabrice Gasnier  * @writeval: value to write
15421cd92d42SFabrice Gasnier  * @readval: value to read
15430f883b22SFabrice Gasnier  *
15440f883b22SFabrice Gasnier  * To read a value from an ADC register:
15450f883b22SFabrice Gasnier  *   echo [ADC reg offset] > direct_reg_access
15460f883b22SFabrice Gasnier  *   cat direct_reg_access
15470f883b22SFabrice Gasnier  *
15480f883b22SFabrice Gasnier  * To write a value in a ADC register:
15490f883b22SFabrice Gasnier  *   echo [ADC_reg_offset] [value] > direct_reg_access
15500f883b22SFabrice Gasnier  */
15510f883b22SFabrice Gasnier static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev,
15520f883b22SFabrice Gasnier 					unsigned reg, unsigned writeval,
15530f883b22SFabrice Gasnier 					unsigned *readval)
15540f883b22SFabrice Gasnier {
15550f883b22SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
15569bdbb113SFabrice Gasnier 	struct device *dev = indio_dev->dev.parent;
15579bdbb113SFabrice Gasnier 	int ret;
15589bdbb113SFabrice Gasnier 
1559265028b8SJonathan Cameron 	ret = pm_runtime_resume_and_get(dev);
1560265028b8SJonathan Cameron 	if (ret < 0)
15619bdbb113SFabrice Gasnier 		return ret;
15620f883b22SFabrice Gasnier 
15630f883b22SFabrice Gasnier 	if (!readval)
15640f883b22SFabrice Gasnier 		stm32_adc_writel(adc, reg, writeval);
15650f883b22SFabrice Gasnier 	else
15660f883b22SFabrice Gasnier 		*readval = stm32_adc_readl(adc, reg);
15670f883b22SFabrice Gasnier 
15689bdbb113SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
15699bdbb113SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
15709bdbb113SFabrice Gasnier 
15710f883b22SFabrice Gasnier 	return 0;
15720f883b22SFabrice Gasnier }
15730f883b22SFabrice Gasnier 
15740f883b22SFabrice Gasnier static const struct iio_info stm32_adc_iio_info = {
15750f883b22SFabrice Gasnier 	.read_raw = stm32_adc_read_raw,
1576da9b9485SFabrice Gasnier 	.validate_trigger = stm32_adc_validate_trigger,
15772763ea05SFabrice Gasnier 	.hwfifo_set_watermark = stm32_adc_set_watermark,
1578da9b9485SFabrice Gasnier 	.update_scan_mode = stm32_adc_update_scan_mode,
15790f883b22SFabrice Gasnier 	.debugfs_reg_access = stm32_adc_debugfs_reg_access,
1580*d7705f35SNuno Sá 	.fwnode_xlate = stm32_adc_fwnode_xlate,
15810f883b22SFabrice Gasnier };
15820f883b22SFabrice Gasnier 
15832763ea05SFabrice Gasnier static unsigned int stm32_adc_dma_residue(struct stm32_adc *adc)
15842763ea05SFabrice Gasnier {
15852763ea05SFabrice Gasnier 	struct dma_tx_state state;
15862763ea05SFabrice Gasnier 	enum dma_status status;
15872763ea05SFabrice Gasnier 
15882763ea05SFabrice Gasnier 	status = dmaengine_tx_status(adc->dma_chan,
15892763ea05SFabrice Gasnier 				     adc->dma_chan->cookie,
15902763ea05SFabrice Gasnier 				     &state);
15912763ea05SFabrice Gasnier 	if (status == DMA_IN_PROGRESS) {
15922763ea05SFabrice Gasnier 		/* Residue is size in bytes from end of buffer */
15932763ea05SFabrice Gasnier 		unsigned int i = adc->rx_buf_sz - state.residue;
15942763ea05SFabrice Gasnier 		unsigned int size;
15952763ea05SFabrice Gasnier 
15962763ea05SFabrice Gasnier 		/* Return available bytes */
15972763ea05SFabrice Gasnier 		if (i >= adc->bufi)
15982763ea05SFabrice Gasnier 			size = i - adc->bufi;
15992763ea05SFabrice Gasnier 		else
16002763ea05SFabrice Gasnier 			size = adc->rx_buf_sz + i - adc->bufi;
16012763ea05SFabrice Gasnier 
16022763ea05SFabrice Gasnier 		return size;
16032763ea05SFabrice Gasnier 	}
16042763ea05SFabrice Gasnier 
16052763ea05SFabrice Gasnier 	return 0;
16062763ea05SFabrice Gasnier }
16072763ea05SFabrice Gasnier 
16082763ea05SFabrice Gasnier static void stm32_adc_dma_buffer_done(void *data)
16092763ea05SFabrice Gasnier {
16102763ea05SFabrice Gasnier 	struct iio_dev *indio_dev = data;
1611e2042d29SOlivier Moysan 	struct stm32_adc *adc = iio_priv(indio_dev);
1612e2042d29SOlivier Moysan 	int residue = stm32_adc_dma_residue(adc);
16132763ea05SFabrice Gasnier 
1614e2042d29SOlivier Moysan 	/*
1615e2042d29SOlivier Moysan 	 * In DMA mode the trigger services of IIO are not used
1616e2042d29SOlivier Moysan 	 * (e.g. no call to iio_trigger_poll).
1617e2042d29SOlivier Moysan 	 * Calling irq handler associated to the hardware trigger is not
1618e2042d29SOlivier Moysan 	 * relevant as the conversions have already been done. Data
1619e2042d29SOlivier Moysan 	 * transfers are performed directly in DMA callback instead.
1620e2042d29SOlivier Moysan 	 * This implementation avoids to call trigger irq handler that
1621e2042d29SOlivier Moysan 	 * may sleep, in an atomic context (DMA irq handler context).
1622e2042d29SOlivier Moysan 	 */
1623e2042d29SOlivier Moysan 	dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi);
1624e2042d29SOlivier Moysan 
1625e2042d29SOlivier Moysan 	while (residue >= indio_dev->scan_bytes) {
1626e2042d29SOlivier Moysan 		u16 *buffer = (u16 *)&adc->rx_buf[adc->bufi];
1627e2042d29SOlivier Moysan 
1628e2042d29SOlivier Moysan 		iio_push_to_buffers(indio_dev, buffer);
1629e2042d29SOlivier Moysan 
1630e2042d29SOlivier Moysan 		residue -= indio_dev->scan_bytes;
1631e2042d29SOlivier Moysan 		adc->bufi += indio_dev->scan_bytes;
1632e2042d29SOlivier Moysan 		if (adc->bufi >= adc->rx_buf_sz)
1633e2042d29SOlivier Moysan 			adc->bufi = 0;
1634e2042d29SOlivier Moysan 	}
16352763ea05SFabrice Gasnier }
16362763ea05SFabrice Gasnier 
16372763ea05SFabrice Gasnier static int stm32_adc_dma_start(struct iio_dev *indio_dev)
16382763ea05SFabrice Gasnier {
16392763ea05SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
16402763ea05SFabrice Gasnier 	struct dma_async_tx_descriptor *desc;
16412763ea05SFabrice Gasnier 	dma_cookie_t cookie;
16422763ea05SFabrice Gasnier 	int ret;
16432763ea05SFabrice Gasnier 
16442763ea05SFabrice Gasnier 	if (!adc->dma_chan)
16452763ea05SFabrice Gasnier 		return 0;
16462763ea05SFabrice Gasnier 
16472763ea05SFabrice Gasnier 	dev_dbg(&indio_dev->dev, "%s size=%d watermark=%d\n", __func__,
16482763ea05SFabrice Gasnier 		adc->rx_buf_sz, adc->rx_buf_sz / 2);
16492763ea05SFabrice Gasnier 
16502763ea05SFabrice Gasnier 	/* Prepare a DMA cyclic transaction */
16512763ea05SFabrice Gasnier 	desc = dmaengine_prep_dma_cyclic(adc->dma_chan,
16522763ea05SFabrice Gasnier 					 adc->rx_dma_buf,
16532763ea05SFabrice Gasnier 					 adc->rx_buf_sz, adc->rx_buf_sz / 2,
16542763ea05SFabrice Gasnier 					 DMA_DEV_TO_MEM,
16552763ea05SFabrice Gasnier 					 DMA_PREP_INTERRUPT);
16562763ea05SFabrice Gasnier 	if (!desc)
16572763ea05SFabrice Gasnier 		return -EBUSY;
16582763ea05SFabrice Gasnier 
16592763ea05SFabrice Gasnier 	desc->callback = stm32_adc_dma_buffer_done;
16602763ea05SFabrice Gasnier 	desc->callback_param = indio_dev;
16612763ea05SFabrice Gasnier 
16622763ea05SFabrice Gasnier 	cookie = dmaengine_submit(desc);
16632763ea05SFabrice Gasnier 	ret = dma_submit_error(cookie);
16642763ea05SFabrice Gasnier 	if (ret) {
1665e6afcf6cSFabrice Gasnier 		dmaengine_terminate_sync(adc->dma_chan);
16662763ea05SFabrice Gasnier 		return ret;
16672763ea05SFabrice Gasnier 	}
16682763ea05SFabrice Gasnier 
16692763ea05SFabrice Gasnier 	/* Issue pending DMA requests */
16702763ea05SFabrice Gasnier 	dma_async_issue_pending(adc->dma_chan);
16712763ea05SFabrice Gasnier 
16722763ea05SFabrice Gasnier 	return 0;
16732763ea05SFabrice Gasnier }
16742763ea05SFabrice Gasnier 
1675f11d59d8SLars-Peter Clausen static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev)
1676da9b9485SFabrice Gasnier {
1677da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
16789bdbb113SFabrice Gasnier 	struct device *dev = indio_dev->dev.parent;
1679da9b9485SFabrice Gasnier 	int ret;
1680da9b9485SFabrice Gasnier 
1681265028b8SJonathan Cameron 	ret = pm_runtime_resume_and_get(dev);
1682265028b8SJonathan Cameron 	if (ret < 0)
168395e339b6SFabrice Gasnier 		return ret;
168495e339b6SFabrice Gasnier 
1685da9b9485SFabrice Gasnier 	ret = stm32_adc_set_trig(indio_dev, indio_dev->trig);
1686da9b9485SFabrice Gasnier 	if (ret) {
1687da9b9485SFabrice Gasnier 		dev_err(&indio_dev->dev, "Can't set trigger\n");
16889bdbb113SFabrice Gasnier 		goto err_pm_put;
1689da9b9485SFabrice Gasnier 	}
1690da9b9485SFabrice Gasnier 
16912763ea05SFabrice Gasnier 	ret = stm32_adc_dma_start(indio_dev);
16922763ea05SFabrice Gasnier 	if (ret) {
16932763ea05SFabrice Gasnier 		dev_err(&indio_dev->dev, "Can't start dma\n");
16942763ea05SFabrice Gasnier 		goto err_clr_trig;
16952763ea05SFabrice Gasnier 	}
16962763ea05SFabrice Gasnier 
1697da9b9485SFabrice Gasnier 	/* Reset adc buffer index */
1698da9b9485SFabrice Gasnier 	adc->bufi = 0;
1699da9b9485SFabrice Gasnier 
1700cc06e67dSFabrice Gasnier 	stm32_adc_ovr_irq_enable(adc);
1701cc06e67dSFabrice Gasnier 
17022763ea05SFabrice Gasnier 	if (!adc->dma_chan)
1703da9b9485SFabrice Gasnier 		stm32_adc_conv_irq_enable(adc);
17042763ea05SFabrice Gasnier 
1705cd64d357SAlexandru Ardelean 	adc->cfg->start_conv(indio_dev, !!adc->dma_chan);
1706da9b9485SFabrice Gasnier 
1707da9b9485SFabrice Gasnier 	return 0;
1708da9b9485SFabrice Gasnier 
1709da9b9485SFabrice Gasnier err_clr_trig:
1710da9b9485SFabrice Gasnier 	stm32_adc_set_trig(indio_dev, NULL);
17119bdbb113SFabrice Gasnier err_pm_put:
17129bdbb113SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
17139bdbb113SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
1714da9b9485SFabrice Gasnier 
1715da9b9485SFabrice Gasnier 	return ret;
1716da9b9485SFabrice Gasnier }
1717da9b9485SFabrice Gasnier 
1718f11d59d8SLars-Peter Clausen static int stm32_adc_buffer_predisable(struct iio_dev *indio_dev)
1719da9b9485SFabrice Gasnier {
1720da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
17219bdbb113SFabrice Gasnier 	struct device *dev = indio_dev->dev.parent;
1722da9b9485SFabrice Gasnier 
1723cd64d357SAlexandru Ardelean 	adc->cfg->stop_conv(indio_dev);
17242763ea05SFabrice Gasnier 	if (!adc->dma_chan)
1725da9b9485SFabrice Gasnier 		stm32_adc_conv_irq_disable(adc);
1726da9b9485SFabrice Gasnier 
1727cc06e67dSFabrice Gasnier 	stm32_adc_ovr_irq_disable(adc);
1728cc06e67dSFabrice Gasnier 
17292763ea05SFabrice Gasnier 	if (adc->dma_chan)
1730e6afcf6cSFabrice Gasnier 		dmaengine_terminate_sync(adc->dma_chan);
17312763ea05SFabrice Gasnier 
1732da9b9485SFabrice Gasnier 	if (stm32_adc_set_trig(indio_dev, NULL))
1733da9b9485SFabrice Gasnier 		dev_err(&indio_dev->dev, "Can't clear trigger\n");
1734da9b9485SFabrice Gasnier 
17359bdbb113SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
17369bdbb113SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
173749ad8d28SFabrice Gasnier 
1738f11d59d8SLars-Peter Clausen 	return 0;
1739da9b9485SFabrice Gasnier }
1740da9b9485SFabrice Gasnier 
1741da9b9485SFabrice Gasnier static const struct iio_buffer_setup_ops stm32_adc_buffer_setup_ops = {
1742da9b9485SFabrice Gasnier 	.postenable = &stm32_adc_buffer_postenable,
1743da9b9485SFabrice Gasnier 	.predisable = &stm32_adc_buffer_predisable,
1744da9b9485SFabrice Gasnier };
1745da9b9485SFabrice Gasnier 
1746da9b9485SFabrice Gasnier static irqreturn_t stm32_adc_trigger_handler(int irq, void *p)
1747da9b9485SFabrice Gasnier {
1748da9b9485SFabrice Gasnier 	struct iio_poll_func *pf = p;
1749da9b9485SFabrice Gasnier 	struct iio_dev *indio_dev = pf->indio_dev;
1750da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
1751da9b9485SFabrice Gasnier 
1752da9b9485SFabrice Gasnier 	dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi);
1753da9b9485SFabrice Gasnier 
1754da9b9485SFabrice Gasnier 	/* reset buffer index */
1755da9b9485SFabrice Gasnier 	adc->bufi = 0;
1756da9b9485SFabrice Gasnier 	iio_push_to_buffers_with_timestamp(indio_dev, adc->buffer,
1757da9b9485SFabrice Gasnier 					   pf->timestamp);
1758da9b9485SFabrice Gasnier 	iio_trigger_notify_done(indio_dev->trig);
1759da9b9485SFabrice Gasnier 
1760da9b9485SFabrice Gasnier 	/* re-enable eoc irq */
1761da9b9485SFabrice Gasnier 	stm32_adc_conv_irq_enable(adc);
1762da9b9485SFabrice Gasnier 
1763da9b9485SFabrice Gasnier 	return IRQ_HANDLED;
1764da9b9485SFabrice Gasnier }
1765da9b9485SFabrice Gasnier 
1766732f2dc4SFabrice Gasnier static const struct iio_chan_spec_ext_info stm32_adc_ext_info[] = {
1767732f2dc4SFabrice Gasnier 	IIO_ENUM("trigger_polarity", IIO_SHARED_BY_ALL, &stm32_adc_trig_pol),
1768732f2dc4SFabrice Gasnier 	{
1769732f2dc4SFabrice Gasnier 		.name = "trigger_polarity_available",
1770732f2dc4SFabrice Gasnier 		.shared = IIO_SHARED_BY_ALL,
1771732f2dc4SFabrice Gasnier 		.read = iio_enum_available_read,
1772732f2dc4SFabrice Gasnier 		.private = (uintptr_t)&stm32_adc_trig_pol,
1773732f2dc4SFabrice Gasnier 	},
1774732f2dc4SFabrice Gasnier 	{},
1775732f2dc4SFabrice Gasnier };
1776732f2dc4SFabrice Gasnier 
1777*d7705f35SNuno Sá static int stm32_adc_fw_get_resolution(struct iio_dev *indio_dev)
177825a85bedSFabrice Gasnier {
1779*d7705f35SNuno Sá 	struct device *dev = &indio_dev->dev;
178025a85bedSFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
178125a85bedSFabrice Gasnier 	unsigned int i;
178225a85bedSFabrice Gasnier 	u32 res;
178325a85bedSFabrice Gasnier 
1784*d7705f35SNuno Sá 	if (device_property_read_u32(dev, "assigned-resolution-bits", &res))
178564ad7f64SFabrice Gasnier 		res = adc->cfg->adc_info->resolutions[0];
178625a85bedSFabrice Gasnier 
178764ad7f64SFabrice Gasnier 	for (i = 0; i < adc->cfg->adc_info->num_res; i++)
178864ad7f64SFabrice Gasnier 		if (res == adc->cfg->adc_info->resolutions[i])
178925a85bedSFabrice Gasnier 			break;
179064ad7f64SFabrice Gasnier 	if (i >= adc->cfg->adc_info->num_res) {
179125a85bedSFabrice Gasnier 		dev_err(&indio_dev->dev, "Bad resolution: %u bits\n", res);
179225a85bedSFabrice Gasnier 		return -EINVAL;
179325a85bedSFabrice Gasnier 	}
179425a85bedSFabrice Gasnier 
179525a85bedSFabrice Gasnier 	dev_dbg(&indio_dev->dev, "Using %u bits resolution\n", res);
179625a85bedSFabrice Gasnier 	adc->res = i;
179725a85bedSFabrice Gasnier 
179825a85bedSFabrice Gasnier 	return 0;
179925a85bedSFabrice Gasnier }
180025a85bedSFabrice Gasnier 
1801ee2ac1cdSFabrice Gasnier static void stm32_adc_smpr_init(struct stm32_adc *adc, int channel, u32 smp_ns)
1802ee2ac1cdSFabrice Gasnier {
1803ee2ac1cdSFabrice Gasnier 	const struct stm32_adc_regs *smpr = &adc->cfg->regs->smp_bits[channel];
1804ee2ac1cdSFabrice Gasnier 	u32 period_ns, shift = smpr->shift, mask = smpr->mask;
1805ee2ac1cdSFabrice Gasnier 	unsigned int smp, r = smpr->reg;
1806ee2ac1cdSFabrice Gasnier 
1807796e5d0bSOlivier Moysan 	/*
1808796e5d0bSOlivier Moysan 	 * For vrefint channel, ensure that the sampling time cannot
1809796e5d0bSOlivier Moysan 	 * be lower than the one specified in the datasheet
1810796e5d0bSOlivier Moysan 	 */
1811796e5d0bSOlivier Moysan 	if (channel == adc->int_ch[STM32_ADC_INT_CH_VREFINT])
1812796e5d0bSOlivier Moysan 		smp_ns = max(smp_ns, adc->cfg->ts_vrefint_ns);
1813796e5d0bSOlivier Moysan 
1814ee2ac1cdSFabrice Gasnier 	/* Determine sampling time (ADC clock cycles) */
1815ee2ac1cdSFabrice Gasnier 	period_ns = NSEC_PER_SEC / adc->common->rate;
1816ee2ac1cdSFabrice Gasnier 	for (smp = 0; smp <= STM32_ADC_MAX_SMP; smp++)
1817ee2ac1cdSFabrice Gasnier 		if ((period_ns * adc->cfg->smp_cycles[smp]) >= smp_ns)
1818ee2ac1cdSFabrice Gasnier 			break;
1819ee2ac1cdSFabrice Gasnier 	if (smp > STM32_ADC_MAX_SMP)
1820ee2ac1cdSFabrice Gasnier 		smp = STM32_ADC_MAX_SMP;
1821ee2ac1cdSFabrice Gasnier 
1822ee2ac1cdSFabrice Gasnier 	/* pre-build sampling time registers (e.g. smpr1, smpr2) */
1823ee2ac1cdSFabrice Gasnier 	adc->smpr_val[r] = (adc->smpr_val[r] & ~mask) | (smp << shift);
1824ee2ac1cdSFabrice Gasnier }
1825ee2ac1cdSFabrice Gasnier 
18260f883b22SFabrice Gasnier static void stm32_adc_chan_init_one(struct iio_dev *indio_dev,
18270bae72aaSFabrice Gasnier 				    struct iio_chan_spec *chan, u32 vinp,
18283fb2e24eSFabrice Gasnier 				    u32 vinn, int scan_index, bool differential)
18290f883b22SFabrice Gasnier {
183025a85bedSFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
18310bae72aaSFabrice Gasnier 	char *name = adc->chan_name[vinp];
183225a85bedSFabrice Gasnier 
18330bae72aaSFabrice Gasnier 	chan->type = IIO_VOLTAGE;
18340bae72aaSFabrice Gasnier 	chan->channel = vinp;
18353fb2e24eSFabrice Gasnier 	if (differential) {
18363fb2e24eSFabrice Gasnier 		chan->differential = 1;
18373fb2e24eSFabrice Gasnier 		chan->channel2 = vinn;
18383fb2e24eSFabrice Gasnier 		snprintf(name, STM32_ADC_CH_SZ, "in%d-in%d", vinp, vinn);
18393fb2e24eSFabrice Gasnier 	} else {
18400bae72aaSFabrice Gasnier 		snprintf(name, STM32_ADC_CH_SZ, "in%d", vinp);
18413fb2e24eSFabrice Gasnier 	}
18420bae72aaSFabrice Gasnier 	chan->datasheet_name = name;
18430f883b22SFabrice Gasnier 	chan->scan_index = scan_index;
18440f883b22SFabrice Gasnier 	chan->indexed = 1;
18450e346b2cSOlivier Moysan 	if (chan->channel == adc->int_ch[STM32_ADC_INT_CH_VREFINT])
18460e346b2cSOlivier Moysan 		chan->info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED);
18470e346b2cSOlivier Moysan 	else
18480f883b22SFabrice Gasnier 		chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
18493fb2e24eSFabrice Gasnier 	chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
18503fb2e24eSFabrice Gasnier 					 BIT(IIO_CHAN_INFO_OFFSET);
18510f883b22SFabrice Gasnier 	chan->scan_type.sign = 'u';
185264ad7f64SFabrice Gasnier 	chan->scan_type.realbits = adc->cfg->adc_info->resolutions[adc->res];
18530f883b22SFabrice Gasnier 	chan->scan_type.storagebits = 16;
1854732f2dc4SFabrice Gasnier 	chan->ext_info = stm32_adc_ext_info;
185595e339b6SFabrice Gasnier 
185695e339b6SFabrice Gasnier 	/* pre-build selected channels mask */
185795e339b6SFabrice Gasnier 	adc->pcsel |= BIT(chan->channel);
18583fb2e24eSFabrice Gasnier 	if (differential) {
18593fb2e24eSFabrice Gasnier 		/* pre-build diff channels mask */
18603fb2e24eSFabrice Gasnier 		adc->difsel |= BIT(chan->channel);
18613fb2e24eSFabrice Gasnier 		/* Also add negative input to pre-selected channels */
18623fb2e24eSFabrice Gasnier 		adc->pcsel |= BIT(chan->channel2);
18633fb2e24eSFabrice Gasnier 	}
18640f883b22SFabrice Gasnier }
18650f883b22SFabrice Gasnier 
186645571a36SOlivier Moysan static int stm32_adc_get_legacy_chan_count(struct iio_dev *indio_dev, struct stm32_adc *adc)
18670f883b22SFabrice Gasnier {
1868*d7705f35SNuno Sá 	struct device *dev = &indio_dev->dev;
186964ad7f64SFabrice Gasnier 	const struct stm32_adc_info *adc_info = adc->cfg->adc_info;
187045571a36SOlivier Moysan 	int num_channels = 0, ret;
18710f883b22SFabrice Gasnier 
1872*d7705f35SNuno Sá 	ret = device_property_count_u32(dev, "st,adc-channels");
18733fb2e24eSFabrice Gasnier 	if (ret > adc_info->max_channels) {
18740f883b22SFabrice Gasnier 		dev_err(&indio_dev->dev, "Bad st,adc-channels?\n");
18753fb2e24eSFabrice Gasnier 		return -EINVAL;
18763fb2e24eSFabrice Gasnier 	} else if (ret > 0) {
18773fb2e24eSFabrice Gasnier 		num_channels += ret;
18783fb2e24eSFabrice Gasnier 	}
18793fb2e24eSFabrice Gasnier 
1880*d7705f35SNuno Sá 	/*
1881*d7705f35SNuno Sá 	 * each st,adc-diff-channels is a group of 2 u32 so we divide @ret
1882*d7705f35SNuno Sá 	 * to get the *real* number of channels.
1883*d7705f35SNuno Sá 	 */
1884*d7705f35SNuno Sá 	ret = device_property_count_u32(dev, "st,adc-diff-channels");
1885*d7705f35SNuno Sá 	if (ret < 0)
1886*d7705f35SNuno Sá 		return ret;
1887*d7705f35SNuno Sá 
1888*d7705f35SNuno Sá 	ret /= (int)(sizeof(struct stm32_adc_diff_channel) / sizeof(u32));
18893fb2e24eSFabrice Gasnier 	if (ret > adc_info->max_channels) {
18903fb2e24eSFabrice Gasnier 		dev_err(&indio_dev->dev, "Bad st,adc-diff-channels?\n");
18913fb2e24eSFabrice Gasnier 		return -EINVAL;
18923fb2e24eSFabrice Gasnier 	} else if (ret > 0) {
189345571a36SOlivier Moysan 		adc->num_diff = ret;
18943fb2e24eSFabrice Gasnier 		num_channels += ret;
189545571a36SOlivier Moysan 	}
189645571a36SOlivier Moysan 
1897796e5d0bSOlivier Moysan 	/* Optional sample time is provided either for each, or all channels */
1898*d7705f35SNuno Sá 	adc->nsmps = device_property_count_u32(dev, "st,min-sample-time-nsecs");
1899*d7705f35SNuno Sá 	if (adc->nsmps > 1 && adc->nsmps != num_channels) {
1900796e5d0bSOlivier Moysan 		dev_err(&indio_dev->dev, "Invalid st,min-sample-time-nsecs\n");
1901796e5d0bSOlivier Moysan 		return -EINVAL;
1902796e5d0bSOlivier Moysan 	}
1903796e5d0bSOlivier Moysan 
190445571a36SOlivier Moysan 	return num_channels;
190545571a36SOlivier Moysan }
190645571a36SOlivier Moysan 
190745571a36SOlivier Moysan static int stm32_adc_legacy_chan_init(struct iio_dev *indio_dev,
190845571a36SOlivier Moysan 				      struct stm32_adc *adc,
1909*d7705f35SNuno Sá 				      struct iio_chan_spec *channels,
1910*d7705f35SNuno Sá 				      int nchans)
191145571a36SOlivier Moysan {
191245571a36SOlivier Moysan 	const struct stm32_adc_info *adc_info = adc->cfg->adc_info;
191345571a36SOlivier Moysan 	struct stm32_adc_diff_channel diff[STM32_ADC_CH_MAX];
1914*d7705f35SNuno Sá 	struct device *dev = &indio_dev->dev;
191545571a36SOlivier Moysan 	u32 num_diff = adc->num_diff;
191645571a36SOlivier Moysan 	int size = num_diff * sizeof(*diff) / sizeof(u32);
1917*d7705f35SNuno Sá 	int scan_index = 0, ret, i, c;
1918*d7705f35SNuno Sá 	u32 smp = 0, smps[STM32_ADC_CH_MAX], chans[STM32_ADC_CH_MAX];
191945571a36SOlivier Moysan 
192045571a36SOlivier Moysan 	if (num_diff) {
1921*d7705f35SNuno Sá 		ret = device_property_read_u32_array(dev, "st,adc-diff-channels",
19223fb2e24eSFabrice Gasnier 						     (u32 *)diff, size);
192345571a36SOlivier Moysan 		if (ret) {
192445571a36SOlivier Moysan 			dev_err(&indio_dev->dev, "Failed to get diff channels %d\n", ret);
19253fb2e24eSFabrice Gasnier 			return ret;
19263fb2e24eSFabrice Gasnier 		}
19273fb2e24eSFabrice Gasnier 
192845571a36SOlivier Moysan 		for (i = 0; i < num_diff; i++) {
192945571a36SOlivier Moysan 			if (diff[i].vinp >= adc_info->max_channels ||
193045571a36SOlivier Moysan 			    diff[i].vinn >= adc_info->max_channels) {
193145571a36SOlivier Moysan 				dev_err(&indio_dev->dev, "Invalid channel in%d-in%d\n",
193245571a36SOlivier Moysan 					diff[i].vinp, diff[i].vinn);
193345571a36SOlivier Moysan 				return -EINVAL;
193445571a36SOlivier Moysan 			}
193545571a36SOlivier Moysan 
193645571a36SOlivier Moysan 			stm32_adc_chan_init_one(indio_dev, &channels[scan_index],
193745571a36SOlivier Moysan 						diff[i].vinp, diff[i].vinn,
193845571a36SOlivier Moysan 						scan_index, true);
193945571a36SOlivier Moysan 			scan_index++;
194045571a36SOlivier Moysan 		}
194145571a36SOlivier Moysan 	}
194245571a36SOlivier Moysan 
1943*d7705f35SNuno Sá 	ret = device_property_read_u32_array(dev, "st,adc-channels", chans,
1944*d7705f35SNuno Sá 					     nchans);
1945*d7705f35SNuno Sá 	if (ret)
1946*d7705f35SNuno Sá 		return ret;
1947*d7705f35SNuno Sá 
1948*d7705f35SNuno Sá 	for (c = 0; c < nchans; c++) {
1949*d7705f35SNuno Sá 		if (chans[c] >= adc_info->max_channels) {
1950*d7705f35SNuno Sá 			dev_err(&indio_dev->dev, "Invalid channel %d\n",
1951*d7705f35SNuno Sá 				chans[c]);
195245571a36SOlivier Moysan 			return -EINVAL;
195345571a36SOlivier Moysan 		}
195445571a36SOlivier Moysan 
195545571a36SOlivier Moysan 		/* Channel can't be configured both as single-ended & diff */
195645571a36SOlivier Moysan 		for (i = 0; i < num_diff; i++) {
1957*d7705f35SNuno Sá 			if (chans[c] == diff[i].vinp) {
1958*d7705f35SNuno Sá 				dev_err(&indio_dev->dev, "channel %d misconfigured\n",	chans[c]);
195945571a36SOlivier Moysan 				return -EINVAL;
196045571a36SOlivier Moysan 			}
196145571a36SOlivier Moysan 		}
1962*d7705f35SNuno Sá 		stm32_adc_chan_init_one(indio_dev, &channels[scan_index],
1963*d7705f35SNuno Sá 					chans[c], 0, scan_index, false);
196445571a36SOlivier Moysan 		scan_index++;
196545571a36SOlivier Moysan 	}
196645571a36SOlivier Moysan 
1967*d7705f35SNuno Sá 	if (adc->nsmps > 0) {
1968*d7705f35SNuno Sá 		ret = device_property_read_u32_array(dev, "st,min-sample-time-nsecs",
1969*d7705f35SNuno Sá 						     smps, adc->nsmps);
1970*d7705f35SNuno Sá 		if (ret)
1971*d7705f35SNuno Sá 			return ret;
1972*d7705f35SNuno Sá 	}
1973*d7705f35SNuno Sá 
1974796e5d0bSOlivier Moysan 	for (i = 0; i < scan_index; i++) {
1975796e5d0bSOlivier Moysan 		/*
1976*d7705f35SNuno Sá 		 * This check is used with the above logic so that smp value
1977*d7705f35SNuno Sá 		 * will only be modified if valid u32 value can be decoded. This
1978*d7705f35SNuno Sá 		 * allows to get either no value, 1 shared value for all indexes,
1979*d7705f35SNuno Sá 		 * or one value per channel. The point is to have the same
1980*d7705f35SNuno Sá 		 * behavior as 'of_property_read_u32_index()'.
1981796e5d0bSOlivier Moysan 		 */
1982*d7705f35SNuno Sá 		if (i < adc->nsmps)
1983*d7705f35SNuno Sá 			smp = smps[i];
1984796e5d0bSOlivier Moysan 
1985796e5d0bSOlivier Moysan 		/* Prepare sampling time settings */
1986796e5d0bSOlivier Moysan 		stm32_adc_smpr_init(adc, channels[i].channel, smp);
1987796e5d0bSOlivier Moysan 	}
1988796e5d0bSOlivier Moysan 
198945571a36SOlivier Moysan 	return scan_index;
199045571a36SOlivier Moysan }
199145571a36SOlivier Moysan 
19920e346b2cSOlivier Moysan static int stm32_adc_populate_int_ch(struct iio_dev *indio_dev, const char *ch_name,
19930e346b2cSOlivier Moysan 				     int chan)
19940e346b2cSOlivier Moysan {
19950e346b2cSOlivier Moysan 	struct stm32_adc *adc = iio_priv(indio_dev);
19960e346b2cSOlivier Moysan 	u16 vrefint;
19970e346b2cSOlivier Moysan 	int i, ret;
19980e346b2cSOlivier Moysan 
19990e346b2cSOlivier Moysan 	for (i = 0; i < STM32_ADC_INT_CH_NB; i++) {
20000e346b2cSOlivier Moysan 		if (!strncmp(stm32_adc_ic[i].name, ch_name, STM32_ADC_CH_SZ)) {
2001bc05f30fSOlivier Moysan 			if (stm32_adc_ic[i].idx != STM32_ADC_INT_CH_VREFINT) {
20020e346b2cSOlivier Moysan 				adc->int_ch[i] = chan;
2003bc05f30fSOlivier Moysan 				break;
2004bc05f30fSOlivier Moysan 			}
20050e346b2cSOlivier Moysan 
20060e346b2cSOlivier Moysan 			/* Get calibration data for vrefint channel */
20070e346b2cSOlivier Moysan 			ret = nvmem_cell_read_u16(&indio_dev->dev, "vrefint", &vrefint);
20080e346b2cSOlivier Moysan 			if (ret && ret != -ENOENT) {
2009784b4707SOlivier Moysan 				return dev_err_probe(indio_dev->dev.parent, ret,
20100e346b2cSOlivier Moysan 						     "nvmem access error\n");
20110e346b2cSOlivier Moysan 			}
2012bc05f30fSOlivier Moysan 			if (ret == -ENOENT) {
2013bc05f30fSOlivier Moysan 				dev_dbg(&indio_dev->dev, "vrefint calibration not found. Skip vrefint channel\n");
2014bc05f30fSOlivier Moysan 				return ret;
2015bc05f30fSOlivier Moysan 			} else if (!vrefint) {
2016bc05f30fSOlivier Moysan 				dev_dbg(&indio_dev->dev, "Null vrefint calibration value. Skip vrefint channel\n");
2017bc05f30fSOlivier Moysan 				return -ENOENT;
2018bc05f30fSOlivier Moysan 			}
2019bc05f30fSOlivier Moysan 			adc->int_ch[i] = chan;
20200e346b2cSOlivier Moysan 			adc->vrefint.vrefint_cal = vrefint;
20210e346b2cSOlivier Moysan 		}
20220e346b2cSOlivier Moysan 	}
20230e346b2cSOlivier Moysan 
20240e346b2cSOlivier Moysan 	return 0;
20250e346b2cSOlivier Moysan }
20260e346b2cSOlivier Moysan 
202795bc8184SOlivier Moysan static int stm32_adc_generic_chan_init(struct iio_dev *indio_dev,
202895bc8184SOlivier Moysan 				       struct stm32_adc *adc,
202995bc8184SOlivier Moysan 				       struct iio_chan_spec *channels)
203095bc8184SOlivier Moysan {
203195bc8184SOlivier Moysan 	const struct stm32_adc_info *adc_info = adc->cfg->adc_info;
2032*d7705f35SNuno Sá 	struct fwnode_handle *child;
203395bc8184SOlivier Moysan 	const char *name;
20340e346b2cSOlivier Moysan 	int val, scan_index = 0, ret;
203595bc8184SOlivier Moysan 	bool differential;
203695bc8184SOlivier Moysan 	u32 vin[2];
203795bc8184SOlivier Moysan 
2038*d7705f35SNuno Sá 	device_for_each_child_node(&indio_dev->dev, child) {
2039*d7705f35SNuno Sá 		ret = fwnode_property_read_u32(child, "reg", &val);
204095bc8184SOlivier Moysan 		if (ret) {
204195bc8184SOlivier Moysan 			dev_err(&indio_dev->dev, "Missing channel index %d\n", ret);
204295bc8184SOlivier Moysan 			goto err;
204395bc8184SOlivier Moysan 		}
204495bc8184SOlivier Moysan 
2045*d7705f35SNuno Sá 		ret = fwnode_property_read_string(child, "label", &name);
204695bc8184SOlivier Moysan 		/* label is optional */
204795bc8184SOlivier Moysan 		if (!ret) {
204895bc8184SOlivier Moysan 			if (strlen(name) >= STM32_ADC_CH_SZ) {
204995bc8184SOlivier Moysan 				dev_err(&indio_dev->dev, "Label %s exceeds %d characters\n",
205095bc8184SOlivier Moysan 					name, STM32_ADC_CH_SZ);
20518cf524beSWan Jiabing 				ret = -EINVAL;
20528cf524beSWan Jiabing 				goto err;
205395bc8184SOlivier Moysan 			}
205495bc8184SOlivier Moysan 			strncpy(adc->chan_name[val], name, STM32_ADC_CH_SZ);
20550e346b2cSOlivier Moysan 			ret = stm32_adc_populate_int_ch(indio_dev, name, val);
2056bc05f30fSOlivier Moysan 			if (ret == -ENOENT)
2057bc05f30fSOlivier Moysan 				continue;
2058bc05f30fSOlivier Moysan 			else if (ret)
20590e346b2cSOlivier Moysan 				goto err;
206095bc8184SOlivier Moysan 		} else if (ret != -EINVAL) {
206195bc8184SOlivier Moysan 			dev_err(&indio_dev->dev, "Invalid label %d\n", ret);
206295bc8184SOlivier Moysan 			goto err;
206395bc8184SOlivier Moysan 		}
206495bc8184SOlivier Moysan 
206595bc8184SOlivier Moysan 		if (val >= adc_info->max_channels) {
206695bc8184SOlivier Moysan 			dev_err(&indio_dev->dev, "Invalid channel %d\n", val);
206795bc8184SOlivier Moysan 			ret = -EINVAL;
206895bc8184SOlivier Moysan 			goto err;
206995bc8184SOlivier Moysan 		}
207095bc8184SOlivier Moysan 
207195bc8184SOlivier Moysan 		differential = false;
2072*d7705f35SNuno Sá 		ret = fwnode_property_read_u32_array(child, "diff-channels", vin, 2);
207395bc8184SOlivier Moysan 		/* diff-channels is optional */
207495bc8184SOlivier Moysan 		if (!ret) {
207595bc8184SOlivier Moysan 			differential = true;
207695bc8184SOlivier Moysan 			if (vin[0] != val || vin[1] >= adc_info->max_channels) {
207795bc8184SOlivier Moysan 				dev_err(&indio_dev->dev, "Invalid channel in%d-in%d\n",
207895bc8184SOlivier Moysan 					vin[0], vin[1]);
207995bc8184SOlivier Moysan 				goto err;
208095bc8184SOlivier Moysan 			}
208195bc8184SOlivier Moysan 		} else if (ret != -EINVAL) {
208295bc8184SOlivier Moysan 			dev_err(&indio_dev->dev, "Invalid diff-channels property %d\n", ret);
208395bc8184SOlivier Moysan 			goto err;
208495bc8184SOlivier Moysan 		}
208595bc8184SOlivier Moysan 
208695bc8184SOlivier Moysan 		stm32_adc_chan_init_one(indio_dev, &channels[scan_index], val,
208795bc8184SOlivier Moysan 					vin[1], scan_index, differential);
2088796e5d0bSOlivier Moysan 
2089*d7705f35SNuno Sá 		ret = fwnode_property_read_u32(child, "st,min-sample-time-ns", &val);
2090796e5d0bSOlivier Moysan 		/* st,min-sample-time-ns is optional */
2091796e5d0bSOlivier Moysan 		if (!ret) {
2092796e5d0bSOlivier Moysan 			stm32_adc_smpr_init(adc, channels[scan_index].channel, val);
2093796e5d0bSOlivier Moysan 			if (differential)
2094796e5d0bSOlivier Moysan 				stm32_adc_smpr_init(adc, vin[1], val);
2095796e5d0bSOlivier Moysan 		} else if (ret != -EINVAL) {
2096796e5d0bSOlivier Moysan 			dev_err(&indio_dev->dev, "Invalid st,min-sample-time-ns property %d\n",
2097796e5d0bSOlivier Moysan 				ret);
2098796e5d0bSOlivier Moysan 			goto err;
2099796e5d0bSOlivier Moysan 		}
2100796e5d0bSOlivier Moysan 
210195bc8184SOlivier Moysan 		scan_index++;
210295bc8184SOlivier Moysan 	}
210395bc8184SOlivier Moysan 
210495bc8184SOlivier Moysan 	return scan_index;
210595bc8184SOlivier Moysan 
210695bc8184SOlivier Moysan err:
2107*d7705f35SNuno Sá 	fwnode_handle_put(child);
210895bc8184SOlivier Moysan 
210995bc8184SOlivier Moysan 	return ret;
211095bc8184SOlivier Moysan }
211195bc8184SOlivier Moysan 
2112*d7705f35SNuno Sá static int stm32_adc_chan_fw_init(struct iio_dev *indio_dev, bool timestamping)
211345571a36SOlivier Moysan {
211445571a36SOlivier Moysan 	struct stm32_adc *adc = iio_priv(indio_dev);
211545571a36SOlivier Moysan 	const struct stm32_adc_info *adc_info = adc->cfg->adc_info;
211645571a36SOlivier Moysan 	struct iio_chan_spec *channels;
211745571a36SOlivier Moysan 	int scan_index = 0, num_channels = 0, ret, i;
211895bc8184SOlivier Moysan 	bool legacy = false;
211995bc8184SOlivier Moysan 
2120aec6e0d8SOlivier Moysan 	for (i = 0; i < STM32_ADC_INT_CH_NB; i++)
2121aec6e0d8SOlivier Moysan 		adc->int_ch[i] = STM32_ADC_INT_CH_NONE;
2122aec6e0d8SOlivier Moysan 
2123*d7705f35SNuno Sá 	num_channels = device_get_child_node_count(&indio_dev->dev);
212495bc8184SOlivier Moysan 	/* If no channels have been found, fallback to channels legacy properties. */
212595bc8184SOlivier Moysan 	if (!num_channels) {
212695bc8184SOlivier Moysan 		legacy = true;
212745571a36SOlivier Moysan 
212845571a36SOlivier Moysan 		ret = stm32_adc_get_legacy_chan_count(indio_dev, adc);
212995bc8184SOlivier Moysan 		if (!ret) {
213095bc8184SOlivier Moysan 			dev_err(indio_dev->dev.parent, "No channel found\n");
21313fb2e24eSFabrice Gasnier 			return -ENODATA;
213295bc8184SOlivier Moysan 		} else if (ret < 0) {
213395bc8184SOlivier Moysan 			return ret;
213495bc8184SOlivier Moysan 		}
213595bc8184SOlivier Moysan 
213695bc8184SOlivier Moysan 		num_channels = ret;
21370f883b22SFabrice Gasnier 	}
21380f883b22SFabrice Gasnier 
213945571a36SOlivier Moysan 	if (num_channels > adc_info->max_channels) {
214045571a36SOlivier Moysan 		dev_err(&indio_dev->dev, "Channel number [%d] exceeds %d\n",
214145571a36SOlivier Moysan 			num_channels, adc_info->max_channels);
214245571a36SOlivier Moysan 		return -EINVAL;
214345571a36SOlivier Moysan 	}
214445571a36SOlivier Moysan 
21458a09054fSAhmad Fatoum 	if (timestamping)
21468a09054fSAhmad Fatoum 		num_channels++;
21478a09054fSAhmad Fatoum 
21480f883b22SFabrice Gasnier 	channels = devm_kcalloc(&indio_dev->dev, num_channels,
21490f883b22SFabrice Gasnier 				sizeof(struct iio_chan_spec), GFP_KERNEL);
21500f883b22SFabrice Gasnier 	if (!channels)
21510f883b22SFabrice Gasnier 		return -ENOMEM;
21520f883b22SFabrice Gasnier 
215395bc8184SOlivier Moysan 	if (legacy)
2154*d7705f35SNuno Sá 		ret = stm32_adc_legacy_chan_init(indio_dev, adc, channels,
2155*d7705f35SNuno Sá 						 num_channels);
215695bc8184SOlivier Moysan 	else
215795bc8184SOlivier Moysan 		ret = stm32_adc_generic_chan_init(indio_dev, adc, channels);
215845571a36SOlivier Moysan 	if (ret < 0)
215945571a36SOlivier Moysan 		return ret;
216045571a36SOlivier Moysan 	scan_index = ret;
21613fb2e24eSFabrice Gasnier 
21628a09054fSAhmad Fatoum 	if (timestamping) {
21638a09054fSAhmad Fatoum 		struct iio_chan_spec *timestamp = &channels[scan_index];
21648a09054fSAhmad Fatoum 
21658a09054fSAhmad Fatoum 		timestamp->type = IIO_TIMESTAMP;
21668a09054fSAhmad Fatoum 		timestamp->channel = -1;
21678a09054fSAhmad Fatoum 		timestamp->scan_index = scan_index;
21688a09054fSAhmad Fatoum 		timestamp->scan_type.sign = 's';
21698a09054fSAhmad Fatoum 		timestamp->scan_type.realbits = 64;
21708a09054fSAhmad Fatoum 		timestamp->scan_type.storagebits = 64;
21718a09054fSAhmad Fatoum 
21728a09054fSAhmad Fatoum 		scan_index++;
21738a09054fSAhmad Fatoum 	}
21748a09054fSAhmad Fatoum 
21750f883b22SFabrice Gasnier 	indio_dev->num_channels = scan_index;
21760f883b22SFabrice Gasnier 	indio_dev->channels = channels;
21770f883b22SFabrice Gasnier 
21780f883b22SFabrice Gasnier 	return 0;
21790f883b22SFabrice Gasnier }
21800f883b22SFabrice Gasnier 
218152cd91c2SFabrice Gasnier static int stm32_adc_dma_request(struct device *dev, struct iio_dev *indio_dev)
21822763ea05SFabrice Gasnier {
21832763ea05SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
21842763ea05SFabrice Gasnier 	struct dma_slave_config config;
21852763ea05SFabrice Gasnier 	int ret;
21862763ea05SFabrice Gasnier 
218752cd91c2SFabrice Gasnier 	adc->dma_chan = dma_request_chan(dev, "rx");
2188735404b8SPeter Ujfalusi 	if (IS_ERR(adc->dma_chan)) {
2189735404b8SPeter Ujfalusi 		ret = PTR_ERR(adc->dma_chan);
2190ce30eeb6SKrzysztof Kozlowski 		if (ret != -ENODEV)
2191ce30eeb6SKrzysztof Kozlowski 			return dev_err_probe(dev, ret,
2192ce30eeb6SKrzysztof Kozlowski 					     "DMA channel request failed with\n");
2193735404b8SPeter Ujfalusi 
2194735404b8SPeter Ujfalusi 		/* DMA is optional: fall back to IRQ mode */
2195735404b8SPeter Ujfalusi 		adc->dma_chan = NULL;
21962763ea05SFabrice Gasnier 		return 0;
2197735404b8SPeter Ujfalusi 	}
21982763ea05SFabrice Gasnier 
21992763ea05SFabrice Gasnier 	adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev,
22002763ea05SFabrice Gasnier 					 STM32_DMA_BUFFER_SIZE,
22012763ea05SFabrice Gasnier 					 &adc->rx_dma_buf, GFP_KERNEL);
22022763ea05SFabrice Gasnier 	if (!adc->rx_buf) {
22032763ea05SFabrice Gasnier 		ret = -ENOMEM;
22042763ea05SFabrice Gasnier 		goto err_release;
22052763ea05SFabrice Gasnier 	}
22062763ea05SFabrice Gasnier 
22072763ea05SFabrice Gasnier 	/* Configure DMA channel to read data register */
22082763ea05SFabrice Gasnier 	memset(&config, 0, sizeof(config));
22092763ea05SFabrice Gasnier 	config.src_addr = (dma_addr_t)adc->common->phys_base;
221064ad7f64SFabrice Gasnier 	config.src_addr += adc->offset + adc->cfg->regs->dr;
22112763ea05SFabrice Gasnier 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
22122763ea05SFabrice Gasnier 
22132763ea05SFabrice Gasnier 	ret = dmaengine_slave_config(adc->dma_chan, &config);
22142763ea05SFabrice Gasnier 	if (ret)
22152763ea05SFabrice Gasnier 		goto err_free;
22162763ea05SFabrice Gasnier 
22172763ea05SFabrice Gasnier 	return 0;
22182763ea05SFabrice Gasnier 
22192763ea05SFabrice Gasnier err_free:
22202763ea05SFabrice Gasnier 	dma_free_coherent(adc->dma_chan->device->dev, STM32_DMA_BUFFER_SIZE,
22212763ea05SFabrice Gasnier 			  adc->rx_buf, adc->rx_dma_buf);
22222763ea05SFabrice Gasnier err_release:
22232763ea05SFabrice Gasnier 	dma_release_channel(adc->dma_chan);
22242763ea05SFabrice Gasnier 
22252763ea05SFabrice Gasnier 	return ret;
22262763ea05SFabrice Gasnier }
22272763ea05SFabrice Gasnier 
22280f883b22SFabrice Gasnier static int stm32_adc_probe(struct platform_device *pdev)
22290f883b22SFabrice Gasnier {
22300f883b22SFabrice Gasnier 	struct iio_dev *indio_dev;
223164ad7f64SFabrice Gasnier 	struct device *dev = &pdev->dev;
2232e2042d29SOlivier Moysan 	irqreturn_t (*handler)(int irq, void *p) = NULL;
22330f883b22SFabrice Gasnier 	struct stm32_adc *adc;
22348a09054fSAhmad Fatoum 	bool timestamping = false;
22350f883b22SFabrice Gasnier 	int ret;
22360f883b22SFabrice Gasnier 
22370f883b22SFabrice Gasnier 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
22380f883b22SFabrice Gasnier 	if (!indio_dev)
22390f883b22SFabrice Gasnier 		return -ENOMEM;
22400f883b22SFabrice Gasnier 
22410f883b22SFabrice Gasnier 	adc = iio_priv(indio_dev);
22420f883b22SFabrice Gasnier 	adc->common = dev_get_drvdata(pdev->dev.parent);
22430f883b22SFabrice Gasnier 	spin_lock_init(&adc->lock);
22440f883b22SFabrice Gasnier 	init_completion(&adc->completion);
2245*d7705f35SNuno Sá 	adc->cfg = device_get_match_data(dev);
22460f883b22SFabrice Gasnier 
22470f883b22SFabrice Gasnier 	indio_dev->name = dev_name(&pdev->dev);
2248*d7705f35SNuno Sá 	device_set_node(&indio_dev->dev, dev_fwnode(&pdev->dev));
22490f883b22SFabrice Gasnier 	indio_dev->info = &stm32_adc_iio_info;
2250f0b638a7SFabrice Gasnier 	indio_dev->modes = INDIO_DIRECT_MODE | INDIO_HARDWARE_TRIGGERED;
22510f883b22SFabrice Gasnier 
2252cd64d357SAlexandru Ardelean 	platform_set_drvdata(pdev, indio_dev);
22530f883b22SFabrice Gasnier 
2254*d7705f35SNuno Sá 	ret = device_property_read_u32(dev, "reg", &adc->offset);
22550f883b22SFabrice Gasnier 	if (ret != 0) {
22560f883b22SFabrice Gasnier 		dev_err(&pdev->dev, "missing reg property\n");
22570f883b22SFabrice Gasnier 		return -EINVAL;
22580f883b22SFabrice Gasnier 	}
22590f883b22SFabrice Gasnier 
22600f883b22SFabrice Gasnier 	adc->irq = platform_get_irq(pdev, 0);
22617c279229SStephen Boyd 	if (adc->irq < 0)
22620f883b22SFabrice Gasnier 		return adc->irq;
22630f883b22SFabrice Gasnier 
2264cc06e67dSFabrice Gasnier 	ret = devm_request_threaded_irq(&pdev->dev, adc->irq, stm32_adc_isr,
2265cc06e67dSFabrice Gasnier 					stm32_adc_threaded_isr,
2266cd64d357SAlexandru Ardelean 					0, pdev->name, indio_dev);
22670f883b22SFabrice Gasnier 	if (ret) {
22680f883b22SFabrice Gasnier 		dev_err(&pdev->dev, "failed to request IRQ\n");
22690f883b22SFabrice Gasnier 		return ret;
22700f883b22SFabrice Gasnier 	}
22710f883b22SFabrice Gasnier 
22720f883b22SFabrice Gasnier 	adc->clk = devm_clk_get(&pdev->dev, NULL);
22730f883b22SFabrice Gasnier 	if (IS_ERR(adc->clk)) {
2274204a6a25SFabrice Gasnier 		ret = PTR_ERR(adc->clk);
2275204a6a25SFabrice Gasnier 		if (ret == -ENOENT && !adc->cfg->clk_required) {
2276204a6a25SFabrice Gasnier 			adc->clk = NULL;
2277204a6a25SFabrice Gasnier 		} else {
22780f883b22SFabrice Gasnier 			dev_err(&pdev->dev, "Can't get clock\n");
2279204a6a25SFabrice Gasnier 			return ret;
2280204a6a25SFabrice Gasnier 		}
22810f883b22SFabrice Gasnier 	}
22820f883b22SFabrice Gasnier 
2283*d7705f35SNuno Sá 	ret = stm32_adc_fw_get_resolution(indio_dev);
228425a85bedSFabrice Gasnier 	if (ret < 0)
22859bdbb113SFabrice Gasnier 		return ret;
228625a85bedSFabrice Gasnier 
228752cd91c2SFabrice Gasnier 	ret = stm32_adc_dma_request(dev, indio_dev);
22882763ea05SFabrice Gasnier 	if (ret < 0)
22899bdbb113SFabrice Gasnier 		return ret;
22902763ea05SFabrice Gasnier 
22918a09054fSAhmad Fatoum 	if (!adc->dma_chan) {
22928a09054fSAhmad Fatoum 		/* For PIO mode only, iio_pollfunc_store_time stores a timestamp
22938a09054fSAhmad Fatoum 		 * in the primary trigger IRQ handler and stm32_adc_trigger_handler
22948a09054fSAhmad Fatoum 		 * runs in the IRQ thread to push out buffer along with timestamp.
22958a09054fSAhmad Fatoum 		 */
2296e2042d29SOlivier Moysan 		handler = &stm32_adc_trigger_handler;
22978a09054fSAhmad Fatoum 		timestamping = true;
22988a09054fSAhmad Fatoum 	}
22998a09054fSAhmad Fatoum 
2300*d7705f35SNuno Sá 	ret = stm32_adc_chan_fw_init(indio_dev, timestamping);
23018a09054fSAhmad Fatoum 	if (ret < 0)
23028a09054fSAhmad Fatoum 		goto err_dma_disable;
2303e2042d29SOlivier Moysan 
2304da9b9485SFabrice Gasnier 	ret = iio_triggered_buffer_setup(indio_dev,
2305e2042d29SOlivier Moysan 					 &iio_pollfunc_store_time, handler,
2306da9b9485SFabrice Gasnier 					 &stm32_adc_buffer_setup_ops);
23070f883b22SFabrice Gasnier 	if (ret) {
2308da9b9485SFabrice Gasnier 		dev_err(&pdev->dev, "buffer setup failed\n");
23092763ea05SFabrice Gasnier 		goto err_dma_disable;
23100f883b22SFabrice Gasnier 	}
23110f883b22SFabrice Gasnier 
23129bdbb113SFabrice Gasnier 	/* Get stm32-adc-core PM online */
23139bdbb113SFabrice Gasnier 	pm_runtime_get_noresume(dev);
23149bdbb113SFabrice Gasnier 	pm_runtime_set_active(dev);
23159bdbb113SFabrice Gasnier 	pm_runtime_set_autosuspend_delay(dev, STM32_ADC_HW_STOP_DELAY_MS);
23169bdbb113SFabrice Gasnier 	pm_runtime_use_autosuspend(dev);
23179bdbb113SFabrice Gasnier 	pm_runtime_enable(dev);
23189bdbb113SFabrice Gasnier 
23199bdbb113SFabrice Gasnier 	ret = stm32_adc_hw_start(dev);
23209bdbb113SFabrice Gasnier 	if (ret)
23219bdbb113SFabrice Gasnier 		goto err_buffer_cleanup;
23229bdbb113SFabrice Gasnier 
2323da9b9485SFabrice Gasnier 	ret = iio_device_register(indio_dev);
2324da9b9485SFabrice Gasnier 	if (ret) {
2325da9b9485SFabrice Gasnier 		dev_err(&pdev->dev, "iio dev register failed\n");
23269bdbb113SFabrice Gasnier 		goto err_hw_stop;
2327da9b9485SFabrice Gasnier 	}
2328da9b9485SFabrice Gasnier 
23299bdbb113SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
23309bdbb113SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
23319bdbb113SFabrice Gasnier 
23320f883b22SFabrice Gasnier 	return 0;
23330f883b22SFabrice Gasnier 
23349bdbb113SFabrice Gasnier err_hw_stop:
23359bdbb113SFabrice Gasnier 	stm32_adc_hw_stop(dev);
23369bdbb113SFabrice Gasnier 
2337da9b9485SFabrice Gasnier err_buffer_cleanup:
23389bdbb113SFabrice Gasnier 	pm_runtime_disable(dev);
23399bdbb113SFabrice Gasnier 	pm_runtime_set_suspended(dev);
23409bdbb113SFabrice Gasnier 	pm_runtime_put_noidle(dev);
2341da9b9485SFabrice Gasnier 	iio_triggered_buffer_cleanup(indio_dev);
2342da9b9485SFabrice Gasnier 
23432763ea05SFabrice Gasnier err_dma_disable:
23442763ea05SFabrice Gasnier 	if (adc->dma_chan) {
23452763ea05SFabrice Gasnier 		dma_free_coherent(adc->dma_chan->device->dev,
23462763ea05SFabrice Gasnier 				  STM32_DMA_BUFFER_SIZE,
23472763ea05SFabrice Gasnier 				  adc->rx_buf, adc->rx_dma_buf);
23482763ea05SFabrice Gasnier 		dma_release_channel(adc->dma_chan);
23492763ea05SFabrice Gasnier 	}
23500f883b22SFabrice Gasnier 
23510f883b22SFabrice Gasnier 	return ret;
23520f883b22SFabrice Gasnier }
23530f883b22SFabrice Gasnier 
23540f883b22SFabrice Gasnier static int stm32_adc_remove(struct platform_device *pdev)
23550f883b22SFabrice Gasnier {
2356cd64d357SAlexandru Ardelean 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
2357cd64d357SAlexandru Ardelean 	struct stm32_adc *adc = iio_priv(indio_dev);
23580f883b22SFabrice Gasnier 
23599bdbb113SFabrice Gasnier 	pm_runtime_get_sync(&pdev->dev);
23600f883b22SFabrice Gasnier 	iio_device_unregister(indio_dev);
23619bdbb113SFabrice Gasnier 	stm32_adc_hw_stop(&pdev->dev);
23629bdbb113SFabrice Gasnier 	pm_runtime_disable(&pdev->dev);
23639bdbb113SFabrice Gasnier 	pm_runtime_set_suspended(&pdev->dev);
23649bdbb113SFabrice Gasnier 	pm_runtime_put_noidle(&pdev->dev);
2365da9b9485SFabrice Gasnier 	iio_triggered_buffer_cleanup(indio_dev);
23662763ea05SFabrice Gasnier 	if (adc->dma_chan) {
23672763ea05SFabrice Gasnier 		dma_free_coherent(adc->dma_chan->device->dev,
23682763ea05SFabrice Gasnier 				  STM32_DMA_BUFFER_SIZE,
23692763ea05SFabrice Gasnier 				  adc->rx_buf, adc->rx_dma_buf);
23702763ea05SFabrice Gasnier 		dma_release_channel(adc->dma_chan);
23712763ea05SFabrice Gasnier 	}
23720f883b22SFabrice Gasnier 
23730f883b22SFabrice Gasnier 	return 0;
23740f883b22SFabrice Gasnier }
23750f883b22SFabrice Gasnier 
237649ad8d28SFabrice Gasnier static int stm32_adc_suspend(struct device *dev)
237749ad8d28SFabrice Gasnier {
2378cd64d357SAlexandru Ardelean 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
237949ad8d28SFabrice Gasnier 
238049ad8d28SFabrice Gasnier 	if (iio_buffer_enabled(indio_dev))
2381f11d59d8SLars-Peter Clausen 		stm32_adc_buffer_predisable(indio_dev);
238249ad8d28SFabrice Gasnier 
238349ad8d28SFabrice Gasnier 	return pm_runtime_force_suspend(dev);
238449ad8d28SFabrice Gasnier }
238549ad8d28SFabrice Gasnier 
238649ad8d28SFabrice Gasnier static int stm32_adc_resume(struct device *dev)
238749ad8d28SFabrice Gasnier {
2388cd64d357SAlexandru Ardelean 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
238949ad8d28SFabrice Gasnier 	int ret;
239049ad8d28SFabrice Gasnier 
239149ad8d28SFabrice Gasnier 	ret = pm_runtime_force_resume(dev);
239249ad8d28SFabrice Gasnier 	if (ret < 0)
239349ad8d28SFabrice Gasnier 		return ret;
239449ad8d28SFabrice Gasnier 
239549ad8d28SFabrice Gasnier 	if (!iio_buffer_enabled(indio_dev))
239649ad8d28SFabrice Gasnier 		return 0;
239749ad8d28SFabrice Gasnier 
239849ad8d28SFabrice Gasnier 	ret = stm32_adc_update_scan_mode(indio_dev,
239949ad8d28SFabrice Gasnier 					 indio_dev->active_scan_mask);
240049ad8d28SFabrice Gasnier 	if (ret < 0)
240149ad8d28SFabrice Gasnier 		return ret;
240249ad8d28SFabrice Gasnier 
2403f11d59d8SLars-Peter Clausen 	return stm32_adc_buffer_postenable(indio_dev);
240449ad8d28SFabrice Gasnier }
240549ad8d28SFabrice Gasnier 
24069bdbb113SFabrice Gasnier static int stm32_adc_runtime_suspend(struct device *dev)
24079bdbb113SFabrice Gasnier {
24089bdbb113SFabrice Gasnier 	return stm32_adc_hw_stop(dev);
24099bdbb113SFabrice Gasnier }
24109bdbb113SFabrice Gasnier 
24119bdbb113SFabrice Gasnier static int stm32_adc_runtime_resume(struct device *dev)
24129bdbb113SFabrice Gasnier {
24139bdbb113SFabrice Gasnier 	return stm32_adc_hw_start(dev);
24149bdbb113SFabrice Gasnier }
24159bdbb113SFabrice Gasnier 
24169bdbb113SFabrice Gasnier static const struct dev_pm_ops stm32_adc_pm_ops = {
2417d65daa79SJonathan Cameron 	SYSTEM_SLEEP_PM_OPS(stm32_adc_suspend, stm32_adc_resume)
2418d65daa79SJonathan Cameron 	RUNTIME_PM_OPS(stm32_adc_runtime_suspend, stm32_adc_runtime_resume,
24199bdbb113SFabrice Gasnier 		       NULL)
24209bdbb113SFabrice Gasnier };
24219bdbb113SFabrice Gasnier 
242264ad7f64SFabrice Gasnier static const struct stm32_adc_cfg stm32f4_adc_cfg = {
242364ad7f64SFabrice Gasnier 	.regs = &stm32f4_adc_regspec,
242464ad7f64SFabrice Gasnier 	.adc_info = &stm32f4_adc_info,
242564ad7f64SFabrice Gasnier 	.trigs = stm32f4_adc_trigs,
2426204a6a25SFabrice Gasnier 	.clk_required = true,
242764ad7f64SFabrice Gasnier 	.start_conv = stm32f4_adc_start_conv,
242864ad7f64SFabrice Gasnier 	.stop_conv = stm32f4_adc_stop_conv,
2429ee2ac1cdSFabrice Gasnier 	.smp_cycles = stm32f4_adc_smp_cycles,
2430695e2f5cSOlivier Moysan 	.irq_clear = stm32f4_adc_irq_clear,
243164ad7f64SFabrice Gasnier };
243264ad7f64SFabrice Gasnier 
243395e339b6SFabrice Gasnier static const struct stm32_adc_cfg stm32h7_adc_cfg = {
243495e339b6SFabrice Gasnier 	.regs = &stm32h7_adc_regspec,
243595e339b6SFabrice Gasnier 	.adc_info = &stm32h7_adc_info,
243695e339b6SFabrice Gasnier 	.trigs = stm32h7_adc_trigs,
243795e339b6SFabrice Gasnier 	.start_conv = stm32h7_adc_start_conv,
243895e339b6SFabrice Gasnier 	.stop_conv = stm32h7_adc_stop_conv,
243995e339b6SFabrice Gasnier 	.prepare = stm32h7_adc_prepare,
244095e339b6SFabrice Gasnier 	.unprepare = stm32h7_adc_unprepare,
2441ee2ac1cdSFabrice Gasnier 	.smp_cycles = stm32h7_adc_smp_cycles,
2442695e2f5cSOlivier Moysan 	.irq_clear = stm32h7_adc_irq_clear,
244395e339b6SFabrice Gasnier };
244495e339b6SFabrice Gasnier 
2445d58c67d1SFabrice Gasnier static const struct stm32_adc_cfg stm32mp1_adc_cfg = {
2446aec6e0d8SOlivier Moysan 	.regs = &stm32mp1_adc_regspec,
2447d58c67d1SFabrice Gasnier 	.adc_info = &stm32h7_adc_info,
2448d58c67d1SFabrice Gasnier 	.trigs = stm32h7_adc_trigs,
2449d58c67d1SFabrice Gasnier 	.has_vregready = true,
2450d58c67d1SFabrice Gasnier 	.start_conv = stm32h7_adc_start_conv,
2451d58c67d1SFabrice Gasnier 	.stop_conv = stm32h7_adc_stop_conv,
2452d58c67d1SFabrice Gasnier 	.prepare = stm32h7_adc_prepare,
2453d58c67d1SFabrice Gasnier 	.unprepare = stm32h7_adc_unprepare,
2454d58c67d1SFabrice Gasnier 	.smp_cycles = stm32h7_adc_smp_cycles,
2455695e2f5cSOlivier Moysan 	.irq_clear = stm32h7_adc_irq_clear,
24560e346b2cSOlivier Moysan 	.ts_vrefint_ns = 4300,
2457d58c67d1SFabrice Gasnier };
2458d58c67d1SFabrice Gasnier 
24590f883b22SFabrice Gasnier static const struct of_device_id stm32_adc_of_match[] = {
246064ad7f64SFabrice Gasnier 	{ .compatible = "st,stm32f4-adc", .data = (void *)&stm32f4_adc_cfg },
246195e339b6SFabrice Gasnier 	{ .compatible = "st,stm32h7-adc", .data = (void *)&stm32h7_adc_cfg },
2462d58c67d1SFabrice Gasnier 	{ .compatible = "st,stm32mp1-adc", .data = (void *)&stm32mp1_adc_cfg },
24630f883b22SFabrice Gasnier 	{},
24640f883b22SFabrice Gasnier };
24650f883b22SFabrice Gasnier MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
24660f883b22SFabrice Gasnier 
24670f883b22SFabrice Gasnier static struct platform_driver stm32_adc_driver = {
24680f883b22SFabrice Gasnier 	.probe = stm32_adc_probe,
24690f883b22SFabrice Gasnier 	.remove = stm32_adc_remove,
24700f883b22SFabrice Gasnier 	.driver = {
24710f883b22SFabrice Gasnier 		.name = "stm32-adc",
24720f883b22SFabrice Gasnier 		.of_match_table = stm32_adc_of_match,
2473d65daa79SJonathan Cameron 		.pm = pm_ptr(&stm32_adc_pm_ops),
24740f883b22SFabrice Gasnier 	},
24750f883b22SFabrice Gasnier };
24760f883b22SFabrice Gasnier module_platform_driver(stm32_adc_driver);
24770f883b22SFabrice Gasnier 
24780f883b22SFabrice Gasnier MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
24790f883b22SFabrice Gasnier MODULE_DESCRIPTION("STMicroelectronics STM32 ADC IIO driver");
24800f883b22SFabrice Gasnier MODULE_LICENSE("GPL v2");
24810f883b22SFabrice Gasnier MODULE_ALIAS("platform:stm32-adc");
2482