xref: /linux/drivers/iio/adc/stm32-adc.c (revision e2042d2936dfc84e9c600fe9b9d0039ca0e54b7d)
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>
240f883b22SFabrice Gasnier #include <linux/platform_device.h>
259bdbb113SFabrice Gasnier #include <linux/pm_runtime.h>
260f883b22SFabrice Gasnier #include <linux/of.h>
2764ad7f64SFabrice Gasnier #include <linux/of_device.h>
280f883b22SFabrice Gasnier 
290f883b22SFabrice Gasnier #include "stm32-adc-core.h"
300f883b22SFabrice Gasnier 
3195e339b6SFabrice Gasnier /* Number of linear calibration shadow registers / LINCALRDYW control bits */
3295e339b6SFabrice Gasnier #define STM32H7_LINCALFACT_NUM		6
3395e339b6SFabrice Gasnier 
3495e339b6SFabrice Gasnier /* BOOST bit must be set on STM32H7 when ADC clock is above 20MHz */
3595e339b6SFabrice Gasnier #define STM32H7_BOOST_CLKRATE		20000000UL
3695e339b6SFabrice Gasnier 
370bae72aaSFabrice Gasnier #define STM32_ADC_CH_MAX		20	/* max number of channels */
383fb2e24eSFabrice Gasnier #define STM32_ADC_CH_SZ			10	/* max channel name size */
39da9b9485SFabrice Gasnier #define STM32_ADC_MAX_SQ		16	/* SQ1..SQ16 */
40ee2ac1cdSFabrice Gasnier #define STM32_ADC_MAX_SMP		7	/* SMPx range is [0..7] */
410f883b22SFabrice Gasnier #define STM32_ADC_TIMEOUT_US		100000
420f883b22SFabrice Gasnier #define STM32_ADC_TIMEOUT	(msecs_to_jiffies(STM32_ADC_TIMEOUT_US / 1000))
439bdbb113SFabrice Gasnier #define STM32_ADC_HW_STOP_DELAY_MS	100
440f883b22SFabrice Gasnier 
452763ea05SFabrice Gasnier #define STM32_DMA_BUFFER_SIZE		PAGE_SIZE
462763ea05SFabrice Gasnier 
47da9b9485SFabrice Gasnier /* External trigger enable */
48da9b9485SFabrice Gasnier enum stm32_adc_exten {
49da9b9485SFabrice Gasnier 	STM32_EXTEN_SWTRIG,
50da9b9485SFabrice Gasnier 	STM32_EXTEN_HWTRIG_RISING_EDGE,
51da9b9485SFabrice Gasnier 	STM32_EXTEN_HWTRIG_FALLING_EDGE,
52da9b9485SFabrice Gasnier 	STM32_EXTEN_HWTRIG_BOTH_EDGES,
53da9b9485SFabrice Gasnier };
54da9b9485SFabrice Gasnier 
55f24a33b3SFabrice Gasnier /* extsel - trigger mux selection value */
56f24a33b3SFabrice Gasnier enum stm32_adc_extsel {
57f24a33b3SFabrice Gasnier 	STM32_EXT0,
58f24a33b3SFabrice Gasnier 	STM32_EXT1,
59f24a33b3SFabrice Gasnier 	STM32_EXT2,
60f24a33b3SFabrice Gasnier 	STM32_EXT3,
61f24a33b3SFabrice Gasnier 	STM32_EXT4,
62f24a33b3SFabrice Gasnier 	STM32_EXT5,
63f24a33b3SFabrice Gasnier 	STM32_EXT6,
64f24a33b3SFabrice Gasnier 	STM32_EXT7,
65f24a33b3SFabrice Gasnier 	STM32_EXT8,
66f24a33b3SFabrice Gasnier 	STM32_EXT9,
67f24a33b3SFabrice Gasnier 	STM32_EXT10,
68f24a33b3SFabrice Gasnier 	STM32_EXT11,
69f24a33b3SFabrice Gasnier 	STM32_EXT12,
70f24a33b3SFabrice Gasnier 	STM32_EXT13,
71f24a33b3SFabrice Gasnier 	STM32_EXT14,
72f24a33b3SFabrice Gasnier 	STM32_EXT15,
73f0b638a7SFabrice Gasnier 	STM32_EXT16,
74f0b638a7SFabrice Gasnier 	STM32_EXT17,
75f0b638a7SFabrice Gasnier 	STM32_EXT18,
76f0b638a7SFabrice Gasnier 	STM32_EXT19,
77f0b638a7SFabrice Gasnier 	STM32_EXT20,
78f24a33b3SFabrice Gasnier };
79f24a33b3SFabrice Gasnier 
80f24a33b3SFabrice Gasnier /**
81f24a33b3SFabrice Gasnier  * struct stm32_adc_trig_info - ADC trigger info
82f24a33b3SFabrice Gasnier  * @name:		name of the trigger, corresponding to its source
83f24a33b3SFabrice Gasnier  * @extsel:		trigger selection
84f24a33b3SFabrice Gasnier  */
85f24a33b3SFabrice Gasnier struct stm32_adc_trig_info {
86f24a33b3SFabrice Gasnier 	const char *name;
87f24a33b3SFabrice Gasnier 	enum stm32_adc_extsel extsel;
88f24a33b3SFabrice Gasnier };
89f24a33b3SFabrice Gasnier 
90da9b9485SFabrice Gasnier /**
9195e339b6SFabrice Gasnier  * struct stm32_adc_calib - optional adc calibration data
9295e339b6SFabrice Gasnier  * @calfact_s: Calibration offset for single ended channels
9395e339b6SFabrice Gasnier  * @calfact_d: Calibration offset in differential
9495e339b6SFabrice Gasnier  * @lincalfact: Linearity calibration factor
950da98c7bSFabrice Gasnier  * @calibrated: Indicates calibration status
9695e339b6SFabrice Gasnier  */
9795e339b6SFabrice Gasnier struct stm32_adc_calib {
9895e339b6SFabrice Gasnier 	u32			calfact_s;
9995e339b6SFabrice Gasnier 	u32			calfact_d;
10095e339b6SFabrice Gasnier 	u32			lincalfact[STM32H7_LINCALFACT_NUM];
1010da98c7bSFabrice Gasnier 	bool			calibrated;
10295e339b6SFabrice Gasnier };
10395e339b6SFabrice Gasnier 
10495e339b6SFabrice Gasnier /**
1051cd92d42SFabrice Gasnier  * struct stm32_adc_regs - stm32 ADC misc registers & bitfield desc
106da9b9485SFabrice Gasnier  * @reg:		register offset
107da9b9485SFabrice Gasnier  * @mask:		bitfield mask
108da9b9485SFabrice Gasnier  * @shift:		left shift
109da9b9485SFabrice Gasnier  */
110da9b9485SFabrice Gasnier struct stm32_adc_regs {
111da9b9485SFabrice Gasnier 	int reg;
112da9b9485SFabrice Gasnier 	int mask;
113da9b9485SFabrice Gasnier 	int shift;
114da9b9485SFabrice Gasnier };
115da9b9485SFabrice Gasnier 
1160f883b22SFabrice Gasnier /**
1171cd92d42SFabrice Gasnier  * struct stm32_adc_regspec - stm32 registers definition
11864ad7f64SFabrice Gasnier  * @dr:			data register offset
11964ad7f64SFabrice Gasnier  * @ier_eoc:		interrupt enable register & eocie bitfield
120cc06e67dSFabrice Gasnier  * @ier_ovr:		interrupt enable register & overrun bitfield
12164ad7f64SFabrice Gasnier  * @isr_eoc:		interrupt status register & eoc bitfield
122cc06e67dSFabrice Gasnier  * @isr_ovr:		interrupt status register & overrun bitfield
12364ad7f64SFabrice Gasnier  * @sqr:		reference to sequence registers array
12464ad7f64SFabrice Gasnier  * @exten:		trigger control register & bitfield
12564ad7f64SFabrice Gasnier  * @extsel:		trigger selection register & bitfield
12664ad7f64SFabrice Gasnier  * @res:		resolution selection register & bitfield
127ee2ac1cdSFabrice Gasnier  * @smpr:		smpr1 & smpr2 registers offset array
128ee2ac1cdSFabrice Gasnier  * @smp_bits:		smpr1 & smpr2 index and bitfields
12964ad7f64SFabrice Gasnier  */
13064ad7f64SFabrice Gasnier struct stm32_adc_regspec {
13164ad7f64SFabrice Gasnier 	const u32 dr;
13264ad7f64SFabrice Gasnier 	const struct stm32_adc_regs ier_eoc;
133cc06e67dSFabrice Gasnier 	const struct stm32_adc_regs ier_ovr;
13464ad7f64SFabrice Gasnier 	const struct stm32_adc_regs isr_eoc;
135cc06e67dSFabrice Gasnier 	const struct stm32_adc_regs isr_ovr;
13664ad7f64SFabrice Gasnier 	const struct stm32_adc_regs *sqr;
13764ad7f64SFabrice Gasnier 	const struct stm32_adc_regs exten;
13864ad7f64SFabrice Gasnier 	const struct stm32_adc_regs extsel;
13964ad7f64SFabrice Gasnier 	const struct stm32_adc_regs res;
140ee2ac1cdSFabrice Gasnier 	const u32 smpr[2];
141ee2ac1cdSFabrice Gasnier 	const struct stm32_adc_regs *smp_bits;
14264ad7f64SFabrice Gasnier };
14364ad7f64SFabrice Gasnier 
14464ad7f64SFabrice Gasnier struct stm32_adc;
14564ad7f64SFabrice Gasnier 
14664ad7f64SFabrice Gasnier /**
1471cd92d42SFabrice Gasnier  * struct stm32_adc_cfg - stm32 compatible configuration data
14864ad7f64SFabrice Gasnier  * @regs:		registers descriptions
14964ad7f64SFabrice Gasnier  * @adc_info:		per instance input channels definitions
15064ad7f64SFabrice Gasnier  * @trigs:		external trigger sources
151204a6a25SFabrice Gasnier  * @clk_required:	clock is required
152d58c67d1SFabrice Gasnier  * @has_vregready:	vregready status flag presence
15395e339b6SFabrice Gasnier  * @prepare:		optional prepare routine (power-up, enable)
15464ad7f64SFabrice Gasnier  * @start_conv:		routine to start conversions
15564ad7f64SFabrice Gasnier  * @stop_conv:		routine to stop conversions
15695e339b6SFabrice Gasnier  * @unprepare:		optional unprepare routine (disable, power-down)
157ee2ac1cdSFabrice Gasnier  * @smp_cycles:		programmable sampling time (ADC clock cycles)
15864ad7f64SFabrice Gasnier  */
15964ad7f64SFabrice Gasnier struct stm32_adc_cfg {
16064ad7f64SFabrice Gasnier 	const struct stm32_adc_regspec	*regs;
16164ad7f64SFabrice Gasnier 	const struct stm32_adc_info	*adc_info;
16264ad7f64SFabrice Gasnier 	struct stm32_adc_trig_info	*trigs;
163204a6a25SFabrice Gasnier 	bool clk_required;
164d58c67d1SFabrice Gasnier 	bool has_vregready;
16595e339b6SFabrice Gasnier 	int (*prepare)(struct stm32_adc *);
16664ad7f64SFabrice Gasnier 	void (*start_conv)(struct stm32_adc *, bool dma);
16764ad7f64SFabrice Gasnier 	void (*stop_conv)(struct stm32_adc *);
16895e339b6SFabrice Gasnier 	void (*unprepare)(struct stm32_adc *);
169ee2ac1cdSFabrice Gasnier 	const unsigned int *smp_cycles;
17064ad7f64SFabrice Gasnier };
17164ad7f64SFabrice Gasnier 
17264ad7f64SFabrice Gasnier /**
1730f883b22SFabrice Gasnier  * struct stm32_adc - private data of each ADC IIO instance
1740f883b22SFabrice Gasnier  * @common:		reference to ADC block common data
1750f883b22SFabrice Gasnier  * @offset:		ADC instance register offset in ADC block
17664ad7f64SFabrice Gasnier  * @cfg:		compatible configuration data
1770f883b22SFabrice Gasnier  * @completion:		end of single conversion completion
1780f883b22SFabrice Gasnier  * @buffer:		data buffer
1790f883b22SFabrice Gasnier  * @clk:		clock for this adc instance
1800f883b22SFabrice Gasnier  * @irq:		interrupt for this adc instance
1810f883b22SFabrice Gasnier  * @lock:		spinlock
182da9b9485SFabrice Gasnier  * @bufi:		data buffer index
183da9b9485SFabrice Gasnier  * @num_conv:		expected number of scan conversions
18425a85bedSFabrice Gasnier  * @res:		data resolution (e.g. RES bitfield value)
185732f2dc4SFabrice Gasnier  * @trigger_polarity:	external trigger polarity (e.g. exten)
1862763ea05SFabrice Gasnier  * @dma_chan:		dma channel
1872763ea05SFabrice Gasnier  * @rx_buf:		dma rx buffer cpu address
1882763ea05SFabrice Gasnier  * @rx_dma_buf:		dma rx buffer bus address
1892763ea05SFabrice Gasnier  * @rx_buf_sz:		dma rx buffer size
1901cd92d42SFabrice Gasnier  * @difsel:		bitmask to set single-ended/differential channel
1911cd92d42SFabrice Gasnier  * @pcsel:		bitmask to preselect channels on some devices
192ee2ac1cdSFabrice Gasnier  * @smpr_val:		sampling time settings (e.g. smpr1 / smpr2)
19395e339b6SFabrice Gasnier  * @cal:		optional calibration data on some devices
1940bae72aaSFabrice Gasnier  * @chan_name:		channel name array
1950f883b22SFabrice Gasnier  */
1960f883b22SFabrice Gasnier struct stm32_adc {
1970f883b22SFabrice Gasnier 	struct stm32_adc_common	*common;
1980f883b22SFabrice Gasnier 	u32			offset;
19964ad7f64SFabrice Gasnier 	const struct stm32_adc_cfg	*cfg;
2000f883b22SFabrice Gasnier 	struct completion	completion;
201da9b9485SFabrice Gasnier 	u16			buffer[STM32_ADC_MAX_SQ];
2020f883b22SFabrice Gasnier 	struct clk		*clk;
2030f883b22SFabrice Gasnier 	int			irq;
2040f883b22SFabrice Gasnier 	spinlock_t		lock;		/* interrupt lock */
205da9b9485SFabrice Gasnier 	unsigned int		bufi;
206da9b9485SFabrice Gasnier 	unsigned int		num_conv;
20725a85bedSFabrice Gasnier 	u32			res;
208732f2dc4SFabrice Gasnier 	u32			trigger_polarity;
2092763ea05SFabrice Gasnier 	struct dma_chan		*dma_chan;
2102763ea05SFabrice Gasnier 	u8			*rx_buf;
2112763ea05SFabrice Gasnier 	dma_addr_t		rx_dma_buf;
2122763ea05SFabrice Gasnier 	unsigned int		rx_buf_sz;
2133fb2e24eSFabrice Gasnier 	u32			difsel;
21495e339b6SFabrice Gasnier 	u32			pcsel;
215ee2ac1cdSFabrice Gasnier 	u32			smpr_val[2];
21695e339b6SFabrice Gasnier 	struct stm32_adc_calib	cal;
2170bae72aaSFabrice Gasnier 	char			chan_name[STM32_ADC_CH_MAX][STM32_ADC_CH_SZ];
2180f883b22SFabrice Gasnier };
2190f883b22SFabrice Gasnier 
2203fb2e24eSFabrice Gasnier struct stm32_adc_diff_channel {
2213fb2e24eSFabrice Gasnier 	u32 vinp;
2223fb2e24eSFabrice Gasnier 	u32 vinn;
2233fb2e24eSFabrice Gasnier };
2243fb2e24eSFabrice Gasnier 
22564ad7f64SFabrice Gasnier /**
22664ad7f64SFabrice Gasnier  * struct stm32_adc_info - stm32 ADC, per instance config data
22764ad7f64SFabrice Gasnier  * @max_channels:	Number of channels
22864ad7f64SFabrice Gasnier  * @resolutions:	available resolutions
22964ad7f64SFabrice Gasnier  * @num_res:		number of available resolutions
23064ad7f64SFabrice Gasnier  */
23164ad7f64SFabrice Gasnier struct stm32_adc_info {
23264ad7f64SFabrice Gasnier 	int max_channels;
23364ad7f64SFabrice Gasnier 	const unsigned int *resolutions;
23464ad7f64SFabrice Gasnier 	const unsigned int num_res;
23564ad7f64SFabrice Gasnier };
23664ad7f64SFabrice Gasnier 
23725a85bedSFabrice Gasnier static const unsigned int stm32f4_adc_resolutions[] = {
23825a85bedSFabrice Gasnier 	/* sorted values so the index matches RES[1:0] in STM32F4_ADC_CR1 */
23925a85bedSFabrice Gasnier 	12, 10, 8, 6,
24025a85bedSFabrice Gasnier };
24125a85bedSFabrice Gasnier 
2420bae72aaSFabrice Gasnier /* stm32f4 can have up to 16 channels */
24364ad7f64SFabrice Gasnier static const struct stm32_adc_info stm32f4_adc_info = {
24464ad7f64SFabrice Gasnier 	.max_channels = 16,
24564ad7f64SFabrice Gasnier 	.resolutions = stm32f4_adc_resolutions,
24664ad7f64SFabrice Gasnier 	.num_res = ARRAY_SIZE(stm32f4_adc_resolutions),
24764ad7f64SFabrice Gasnier };
24864ad7f64SFabrice Gasnier 
24995e339b6SFabrice Gasnier static const unsigned int stm32h7_adc_resolutions[] = {
25095e339b6SFabrice Gasnier 	/* sorted values so the index matches RES[2:0] in STM32H7_ADC_CFGR */
25195e339b6SFabrice Gasnier 	16, 14, 12, 10, 8,
25295e339b6SFabrice Gasnier };
25395e339b6SFabrice Gasnier 
2540bae72aaSFabrice Gasnier /* stm32h7 can have up to 20 channels */
25595e339b6SFabrice Gasnier static const struct stm32_adc_info stm32h7_adc_info = {
2560bae72aaSFabrice Gasnier 	.max_channels = STM32_ADC_CH_MAX,
25795e339b6SFabrice Gasnier 	.resolutions = stm32h7_adc_resolutions,
25895e339b6SFabrice Gasnier 	.num_res = ARRAY_SIZE(stm32h7_adc_resolutions),
25995e339b6SFabrice Gasnier };
26095e339b6SFabrice Gasnier 
2611cd92d42SFabrice Gasnier /*
262da9b9485SFabrice Gasnier  * stm32f4_sq - describe regular sequence registers
263da9b9485SFabrice Gasnier  * - L: sequence len (register & bit field)
264da9b9485SFabrice Gasnier  * - SQ1..SQ16: sequence entries (register & bit field)
265da9b9485SFabrice Gasnier  */
266da9b9485SFabrice Gasnier static const struct stm32_adc_regs stm32f4_sq[STM32_ADC_MAX_SQ + 1] = {
267da9b9485SFabrice Gasnier 	/* L: len bit field description to be kept as first element */
268da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(23, 20), 20 },
269da9b9485SFabrice Gasnier 	/* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
270da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(4, 0), 0 },
271da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(9, 5), 5 },
272da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(14, 10), 10 },
273da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(19, 15), 15 },
274da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(24, 20), 20 },
275da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(29, 25), 25 },
276da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(4, 0), 0 },
277da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(9, 5), 5 },
278da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(14, 10), 10 },
279da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(19, 15), 15 },
280da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(24, 20), 20 },
281da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(29, 25), 25 },
282da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(4, 0), 0 },
283da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(9, 5), 5 },
284da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(14, 10), 10 },
285da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(19, 15), 15 },
286da9b9485SFabrice Gasnier };
287da9b9485SFabrice Gasnier 
288f24a33b3SFabrice Gasnier /* STM32F4 external trigger sources for all instances */
289f24a33b3SFabrice Gasnier static struct stm32_adc_trig_info stm32f4_adc_trigs[] = {
290f24a33b3SFabrice Gasnier 	{ TIM1_CH1, STM32_EXT0 },
291f24a33b3SFabrice Gasnier 	{ TIM1_CH2, STM32_EXT1 },
292f24a33b3SFabrice Gasnier 	{ TIM1_CH3, STM32_EXT2 },
293f24a33b3SFabrice Gasnier 	{ TIM2_CH2, STM32_EXT3 },
294f24a33b3SFabrice Gasnier 	{ TIM2_CH3, STM32_EXT4 },
295f24a33b3SFabrice Gasnier 	{ TIM2_CH4, STM32_EXT5 },
296f24a33b3SFabrice Gasnier 	{ TIM2_TRGO, STM32_EXT6 },
297f24a33b3SFabrice Gasnier 	{ TIM3_CH1, STM32_EXT7 },
298f24a33b3SFabrice Gasnier 	{ TIM3_TRGO, STM32_EXT8 },
299f24a33b3SFabrice Gasnier 	{ TIM4_CH4, STM32_EXT9 },
300f24a33b3SFabrice Gasnier 	{ TIM5_CH1, STM32_EXT10 },
301f24a33b3SFabrice Gasnier 	{ TIM5_CH2, STM32_EXT11 },
302f24a33b3SFabrice Gasnier 	{ TIM5_CH3, STM32_EXT12 },
303f24a33b3SFabrice Gasnier 	{ TIM8_CH1, STM32_EXT13 },
304f24a33b3SFabrice Gasnier 	{ TIM8_TRGO, STM32_EXT14 },
305f24a33b3SFabrice Gasnier 	{}, /* sentinel */
306f24a33b3SFabrice Gasnier };
307f24a33b3SFabrice Gasnier 
3081cd92d42SFabrice Gasnier /*
309ee2ac1cdSFabrice Gasnier  * stm32f4_smp_bits[] - describe sampling time register index & bit fields
310ee2ac1cdSFabrice Gasnier  * Sorted so it can be indexed by channel number.
311ee2ac1cdSFabrice Gasnier  */
312ee2ac1cdSFabrice Gasnier static const struct stm32_adc_regs stm32f4_smp_bits[] = {
313ee2ac1cdSFabrice Gasnier 	/* STM32F4_ADC_SMPR2: smpr[] index, mask, shift for SMP0 to SMP9 */
314ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(2, 0), 0 },
315ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(5, 3), 3 },
316ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(8, 6), 6 },
317ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(11, 9), 9 },
318ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(14, 12), 12 },
319ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(17, 15), 15 },
320ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(20, 18), 18 },
321ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(23, 21), 21 },
322ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(26, 24), 24 },
323ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(29, 27), 27 },
324ee2ac1cdSFabrice Gasnier 	/* STM32F4_ADC_SMPR1, smpr[] index, mask, shift for SMP10 to SMP18 */
325ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(2, 0), 0 },
326ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(5, 3), 3 },
327ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(8, 6), 6 },
328ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(11, 9), 9 },
329ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(14, 12), 12 },
330ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(17, 15), 15 },
331ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(20, 18), 18 },
332ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(23, 21), 21 },
333ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(26, 24), 24 },
334ee2ac1cdSFabrice Gasnier };
335ee2ac1cdSFabrice Gasnier 
336ee2ac1cdSFabrice Gasnier /* STM32F4 programmable sampling time (ADC clock cycles) */
337ee2ac1cdSFabrice Gasnier static const unsigned int stm32f4_adc_smp_cycles[STM32_ADC_MAX_SMP + 1] = {
338ee2ac1cdSFabrice Gasnier 	3, 15, 28, 56, 84, 112, 144, 480,
339ee2ac1cdSFabrice Gasnier };
340ee2ac1cdSFabrice Gasnier 
34164ad7f64SFabrice Gasnier static const struct stm32_adc_regspec stm32f4_adc_regspec = {
34264ad7f64SFabrice Gasnier 	.dr = STM32F4_ADC_DR,
34364ad7f64SFabrice Gasnier 	.ier_eoc = { STM32F4_ADC_CR1, STM32F4_EOCIE },
344cc06e67dSFabrice Gasnier 	.ier_ovr = { STM32F4_ADC_CR1, STM32F4_OVRIE },
34564ad7f64SFabrice Gasnier 	.isr_eoc = { STM32F4_ADC_SR, STM32F4_EOC },
346cc06e67dSFabrice Gasnier 	.isr_ovr = { STM32F4_ADC_SR, STM32F4_OVR },
34764ad7f64SFabrice Gasnier 	.sqr = stm32f4_sq,
34864ad7f64SFabrice Gasnier 	.exten = { STM32F4_ADC_CR2, STM32F4_EXTEN_MASK, STM32F4_EXTEN_SHIFT },
34964ad7f64SFabrice Gasnier 	.extsel = { STM32F4_ADC_CR2, STM32F4_EXTSEL_MASK,
35064ad7f64SFabrice Gasnier 		    STM32F4_EXTSEL_SHIFT },
35164ad7f64SFabrice Gasnier 	.res = { STM32F4_ADC_CR1, STM32F4_RES_MASK, STM32F4_RES_SHIFT },
352ee2ac1cdSFabrice Gasnier 	.smpr = { STM32F4_ADC_SMPR1, STM32F4_ADC_SMPR2 },
353ee2ac1cdSFabrice Gasnier 	.smp_bits = stm32f4_smp_bits,
35464ad7f64SFabrice Gasnier };
35564ad7f64SFabrice Gasnier 
35695e339b6SFabrice Gasnier static const struct stm32_adc_regs stm32h7_sq[STM32_ADC_MAX_SQ + 1] = {
35795e339b6SFabrice Gasnier 	/* L: len bit field description to be kept as first element */
35895e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR1, GENMASK(3, 0), 0 },
35995e339b6SFabrice Gasnier 	/* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
36095e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR1, GENMASK(10, 6), 6 },
36195e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR1, GENMASK(16, 12), 12 },
36295e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR1, GENMASK(22, 18), 18 },
36395e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR1, GENMASK(28, 24), 24 },
36495e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR2, GENMASK(4, 0), 0 },
36595e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR2, GENMASK(10, 6), 6 },
36695e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR2, GENMASK(16, 12), 12 },
36795e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR2, GENMASK(22, 18), 18 },
36895e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR2, GENMASK(28, 24), 24 },
36995e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR3, GENMASK(4, 0), 0 },
37095e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR3, GENMASK(10, 6), 6 },
37195e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR3, GENMASK(16, 12), 12 },
37295e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR3, GENMASK(22, 18), 18 },
37395e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR3, GENMASK(28, 24), 24 },
37495e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR4, GENMASK(4, 0), 0 },
37595e339b6SFabrice Gasnier 	{ STM32H7_ADC_SQR4, GENMASK(10, 6), 6 },
37695e339b6SFabrice Gasnier };
37795e339b6SFabrice Gasnier 
37895e339b6SFabrice Gasnier /* STM32H7 external trigger sources for all instances */
37995e339b6SFabrice Gasnier static struct stm32_adc_trig_info stm32h7_adc_trigs[] = {
38095e339b6SFabrice Gasnier 	{ TIM1_CH1, STM32_EXT0 },
38195e339b6SFabrice Gasnier 	{ TIM1_CH2, STM32_EXT1 },
38295e339b6SFabrice Gasnier 	{ TIM1_CH3, STM32_EXT2 },
38395e339b6SFabrice Gasnier 	{ TIM2_CH2, STM32_EXT3 },
38495e339b6SFabrice Gasnier 	{ TIM3_TRGO, STM32_EXT4 },
38595e339b6SFabrice Gasnier 	{ TIM4_CH4, STM32_EXT5 },
38695e339b6SFabrice Gasnier 	{ TIM8_TRGO, STM32_EXT7 },
38795e339b6SFabrice Gasnier 	{ TIM8_TRGO2, STM32_EXT8 },
38895e339b6SFabrice Gasnier 	{ TIM1_TRGO, STM32_EXT9 },
38995e339b6SFabrice Gasnier 	{ TIM1_TRGO2, STM32_EXT10 },
39095e339b6SFabrice Gasnier 	{ TIM2_TRGO, STM32_EXT11 },
39195e339b6SFabrice Gasnier 	{ TIM4_TRGO, STM32_EXT12 },
39295e339b6SFabrice Gasnier 	{ TIM6_TRGO, STM32_EXT13 },
3933a069904SFabrice Gasnier 	{ TIM15_TRGO, STM32_EXT14 },
39495e339b6SFabrice Gasnier 	{ TIM3_CH4, STM32_EXT15 },
395f0b638a7SFabrice Gasnier 	{ LPTIM1_OUT, STM32_EXT18 },
396f0b638a7SFabrice Gasnier 	{ LPTIM2_OUT, STM32_EXT19 },
397f0b638a7SFabrice Gasnier 	{ LPTIM3_OUT, STM32_EXT20 },
39895e339b6SFabrice Gasnier 	{},
39995e339b6SFabrice Gasnier };
40095e339b6SFabrice Gasnier 
4011cd92d42SFabrice Gasnier /*
402ee2ac1cdSFabrice Gasnier  * stm32h7_smp_bits - describe sampling time register index & bit fields
403ee2ac1cdSFabrice Gasnier  * Sorted so it can be indexed by channel number.
404ee2ac1cdSFabrice Gasnier  */
405ee2ac1cdSFabrice Gasnier static const struct stm32_adc_regs stm32h7_smp_bits[] = {
406ee2ac1cdSFabrice Gasnier 	/* STM32H7_ADC_SMPR1, smpr[] index, mask, shift for SMP0 to SMP9 */
407ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(2, 0), 0 },
408ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(5, 3), 3 },
409ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(8, 6), 6 },
410ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(11, 9), 9 },
411ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(14, 12), 12 },
412ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(17, 15), 15 },
413ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(20, 18), 18 },
414ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(23, 21), 21 },
415ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(26, 24), 24 },
416ee2ac1cdSFabrice Gasnier 	{ 0, GENMASK(29, 27), 27 },
417ee2ac1cdSFabrice Gasnier 	/* STM32H7_ADC_SMPR2, smpr[] index, mask, shift for SMP10 to SMP19 */
418ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(2, 0), 0 },
419ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(5, 3), 3 },
420ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(8, 6), 6 },
421ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(11, 9), 9 },
422ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(14, 12), 12 },
423ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(17, 15), 15 },
424ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(20, 18), 18 },
425ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(23, 21), 21 },
426ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(26, 24), 24 },
427ee2ac1cdSFabrice Gasnier 	{ 1, GENMASK(29, 27), 27 },
428ee2ac1cdSFabrice Gasnier };
429ee2ac1cdSFabrice Gasnier 
430ee2ac1cdSFabrice Gasnier /* STM32H7 programmable sampling time (ADC clock cycles, rounded down) */
431ee2ac1cdSFabrice Gasnier static const unsigned int stm32h7_adc_smp_cycles[STM32_ADC_MAX_SMP + 1] = {
432ee2ac1cdSFabrice Gasnier 	1, 2, 8, 16, 32, 64, 387, 810,
433ee2ac1cdSFabrice Gasnier };
434ee2ac1cdSFabrice Gasnier 
43595e339b6SFabrice Gasnier static const struct stm32_adc_regspec stm32h7_adc_regspec = {
43695e339b6SFabrice Gasnier 	.dr = STM32H7_ADC_DR,
43795e339b6SFabrice Gasnier 	.ier_eoc = { STM32H7_ADC_IER, STM32H7_EOCIE },
438cc06e67dSFabrice Gasnier 	.ier_ovr = { STM32H7_ADC_IER, STM32H7_OVRIE },
43995e339b6SFabrice Gasnier 	.isr_eoc = { STM32H7_ADC_ISR, STM32H7_EOC },
440cc06e67dSFabrice Gasnier 	.isr_ovr = { STM32H7_ADC_ISR, STM32H7_OVR },
44195e339b6SFabrice Gasnier 	.sqr = stm32h7_sq,
44295e339b6SFabrice Gasnier 	.exten = { STM32H7_ADC_CFGR, STM32H7_EXTEN_MASK, STM32H7_EXTEN_SHIFT },
44395e339b6SFabrice Gasnier 	.extsel = { STM32H7_ADC_CFGR, STM32H7_EXTSEL_MASK,
44495e339b6SFabrice Gasnier 		    STM32H7_EXTSEL_SHIFT },
44595e339b6SFabrice Gasnier 	.res = { STM32H7_ADC_CFGR, STM32H7_RES_MASK, STM32H7_RES_SHIFT },
446ee2ac1cdSFabrice Gasnier 	.smpr = { STM32H7_ADC_SMPR1, STM32H7_ADC_SMPR2 },
447ee2ac1cdSFabrice Gasnier 	.smp_bits = stm32h7_smp_bits,
44895e339b6SFabrice Gasnier };
44995e339b6SFabrice Gasnier 
450da9b9485SFabrice Gasnier /**
4510f883b22SFabrice Gasnier  * STM32 ADC registers access routines
4520f883b22SFabrice Gasnier  * @adc: stm32 adc instance
4530f883b22SFabrice Gasnier  * @reg: reg offset in adc instance
4540f883b22SFabrice Gasnier  *
4550f883b22SFabrice Gasnier  * Note: All instances share same base, with 0x0, 0x100 or 0x200 offset resp.
4560f883b22SFabrice Gasnier  * for adc1, adc2 and adc3.
4570f883b22SFabrice Gasnier  */
4580f883b22SFabrice Gasnier static u32 stm32_adc_readl(struct stm32_adc *adc, u32 reg)
4590f883b22SFabrice Gasnier {
4600f883b22SFabrice Gasnier 	return readl_relaxed(adc->common->base + adc->offset + reg);
4610f883b22SFabrice Gasnier }
4620f883b22SFabrice Gasnier 
46395e339b6SFabrice Gasnier #define stm32_adc_readl_addr(addr)	stm32_adc_readl(adc, addr)
46495e339b6SFabrice Gasnier 
46595e339b6SFabrice Gasnier #define stm32_adc_readl_poll_timeout(reg, val, cond, sleep_us, timeout_us) \
46695e339b6SFabrice Gasnier 	readx_poll_timeout(stm32_adc_readl_addr, reg, val, \
46795e339b6SFabrice Gasnier 			   cond, sleep_us, timeout_us)
46895e339b6SFabrice Gasnier 
4690f883b22SFabrice Gasnier static u16 stm32_adc_readw(struct stm32_adc *adc, u32 reg)
4700f883b22SFabrice Gasnier {
4710f883b22SFabrice Gasnier 	return readw_relaxed(adc->common->base + adc->offset + reg);
4720f883b22SFabrice Gasnier }
4730f883b22SFabrice Gasnier 
4740f883b22SFabrice Gasnier static void stm32_adc_writel(struct stm32_adc *adc, u32 reg, u32 val)
4750f883b22SFabrice Gasnier {
4760f883b22SFabrice Gasnier 	writel_relaxed(val, adc->common->base + adc->offset + reg);
4770f883b22SFabrice Gasnier }
4780f883b22SFabrice Gasnier 
4790f883b22SFabrice Gasnier static void stm32_adc_set_bits(struct stm32_adc *adc, u32 reg, u32 bits)
4800f883b22SFabrice Gasnier {
4810f883b22SFabrice Gasnier 	unsigned long flags;
4820f883b22SFabrice Gasnier 
4830f883b22SFabrice Gasnier 	spin_lock_irqsave(&adc->lock, flags);
4840f883b22SFabrice Gasnier 	stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) | bits);
4850f883b22SFabrice Gasnier 	spin_unlock_irqrestore(&adc->lock, flags);
4860f883b22SFabrice Gasnier }
4870f883b22SFabrice Gasnier 
4880f883b22SFabrice Gasnier static void stm32_adc_clr_bits(struct stm32_adc *adc, u32 reg, u32 bits)
4890f883b22SFabrice Gasnier {
4900f883b22SFabrice Gasnier 	unsigned long flags;
4910f883b22SFabrice Gasnier 
4920f883b22SFabrice Gasnier 	spin_lock_irqsave(&adc->lock, flags);
4930f883b22SFabrice Gasnier 	stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) & ~bits);
4940f883b22SFabrice Gasnier 	spin_unlock_irqrestore(&adc->lock, flags);
4950f883b22SFabrice Gasnier }
4960f883b22SFabrice Gasnier 
4970f883b22SFabrice Gasnier /**
4980f883b22SFabrice Gasnier  * stm32_adc_conv_irq_enable() - Enable end of conversion interrupt
4990f883b22SFabrice Gasnier  * @adc: stm32 adc instance
5000f883b22SFabrice Gasnier  */
5010f883b22SFabrice Gasnier static void stm32_adc_conv_irq_enable(struct stm32_adc *adc)
5020f883b22SFabrice Gasnier {
50364ad7f64SFabrice Gasnier 	stm32_adc_set_bits(adc, adc->cfg->regs->ier_eoc.reg,
50464ad7f64SFabrice Gasnier 			   adc->cfg->regs->ier_eoc.mask);
5050f883b22SFabrice Gasnier };
5060f883b22SFabrice Gasnier 
5070f883b22SFabrice Gasnier /**
5080f883b22SFabrice Gasnier  * stm32_adc_conv_irq_disable() - Disable end of conversion interrupt
5090f883b22SFabrice Gasnier  * @adc: stm32 adc instance
5100f883b22SFabrice Gasnier  */
5110f883b22SFabrice Gasnier static void stm32_adc_conv_irq_disable(struct stm32_adc *adc)
5120f883b22SFabrice Gasnier {
51364ad7f64SFabrice Gasnier 	stm32_adc_clr_bits(adc, adc->cfg->regs->ier_eoc.reg,
51464ad7f64SFabrice Gasnier 			   adc->cfg->regs->ier_eoc.mask);
5150f883b22SFabrice Gasnier }
5160f883b22SFabrice Gasnier 
517cc06e67dSFabrice Gasnier static void stm32_adc_ovr_irq_enable(struct stm32_adc *adc)
518cc06e67dSFabrice Gasnier {
519cc06e67dSFabrice Gasnier 	stm32_adc_set_bits(adc, adc->cfg->regs->ier_ovr.reg,
520cc06e67dSFabrice Gasnier 			   adc->cfg->regs->ier_ovr.mask);
521cc06e67dSFabrice Gasnier }
522cc06e67dSFabrice Gasnier 
523cc06e67dSFabrice Gasnier static void stm32_adc_ovr_irq_disable(struct stm32_adc *adc)
524cc06e67dSFabrice Gasnier {
525cc06e67dSFabrice Gasnier 	stm32_adc_clr_bits(adc, adc->cfg->regs->ier_ovr.reg,
526cc06e67dSFabrice Gasnier 			   adc->cfg->regs->ier_ovr.mask);
527cc06e67dSFabrice Gasnier }
528cc06e67dSFabrice Gasnier 
52925a85bedSFabrice Gasnier static void stm32_adc_set_res(struct stm32_adc *adc)
53025a85bedSFabrice Gasnier {
53164ad7f64SFabrice Gasnier 	const struct stm32_adc_regs *res = &adc->cfg->regs->res;
53264ad7f64SFabrice Gasnier 	u32 val;
53325a85bedSFabrice Gasnier 
53464ad7f64SFabrice Gasnier 	val = stm32_adc_readl(adc, res->reg);
53564ad7f64SFabrice Gasnier 	val = (val & ~res->mask) | (adc->res << res->shift);
53664ad7f64SFabrice Gasnier 	stm32_adc_writel(adc, res->reg, val);
53725a85bedSFabrice Gasnier }
53825a85bedSFabrice Gasnier 
5399bdbb113SFabrice Gasnier static int stm32_adc_hw_stop(struct device *dev)
5409bdbb113SFabrice Gasnier {
5419bdbb113SFabrice Gasnier 	struct stm32_adc *adc = dev_get_drvdata(dev);
5429bdbb113SFabrice Gasnier 
5439bdbb113SFabrice Gasnier 	if (adc->cfg->unprepare)
5449bdbb113SFabrice Gasnier 		adc->cfg->unprepare(adc);
5459bdbb113SFabrice Gasnier 
5469bdbb113SFabrice Gasnier 	if (adc->clk)
5479bdbb113SFabrice Gasnier 		clk_disable_unprepare(adc->clk);
5489bdbb113SFabrice Gasnier 
5499bdbb113SFabrice Gasnier 	return 0;
5509bdbb113SFabrice Gasnier }
5519bdbb113SFabrice Gasnier 
5529bdbb113SFabrice Gasnier static int stm32_adc_hw_start(struct device *dev)
5539bdbb113SFabrice Gasnier {
5549bdbb113SFabrice Gasnier 	struct stm32_adc *adc = dev_get_drvdata(dev);
5559bdbb113SFabrice Gasnier 	int ret;
5569bdbb113SFabrice Gasnier 
5579bdbb113SFabrice Gasnier 	if (adc->clk) {
5589bdbb113SFabrice Gasnier 		ret = clk_prepare_enable(adc->clk);
5599bdbb113SFabrice Gasnier 		if (ret)
5609bdbb113SFabrice Gasnier 			return ret;
5619bdbb113SFabrice Gasnier 	}
5629bdbb113SFabrice Gasnier 
5639bdbb113SFabrice Gasnier 	stm32_adc_set_res(adc);
5649bdbb113SFabrice Gasnier 
5659bdbb113SFabrice Gasnier 	if (adc->cfg->prepare) {
5669bdbb113SFabrice Gasnier 		ret = adc->cfg->prepare(adc);
5679bdbb113SFabrice Gasnier 		if (ret)
5689bdbb113SFabrice Gasnier 			goto err_clk_dis;
5699bdbb113SFabrice Gasnier 	}
5709bdbb113SFabrice Gasnier 
5719bdbb113SFabrice Gasnier 	return 0;
5729bdbb113SFabrice Gasnier 
5739bdbb113SFabrice Gasnier err_clk_dis:
5749bdbb113SFabrice Gasnier 	if (adc->clk)
5759bdbb113SFabrice Gasnier 		clk_disable_unprepare(adc->clk);
5769bdbb113SFabrice Gasnier 
5779bdbb113SFabrice Gasnier 	return ret;
5789bdbb113SFabrice Gasnier }
5799bdbb113SFabrice Gasnier 
5800f883b22SFabrice Gasnier /**
58164ad7f64SFabrice Gasnier  * stm32f4_adc_start_conv() - Start conversions for regular channels.
5820f883b22SFabrice Gasnier  * @adc: stm32 adc instance
5832763ea05SFabrice Gasnier  * @dma: use dma to transfer conversion result
5842763ea05SFabrice Gasnier  *
5852763ea05SFabrice Gasnier  * Start conversions for regular channels.
5862763ea05SFabrice Gasnier  * Also take care of normal or DMA mode. Circular DMA may be used for regular
5872763ea05SFabrice Gasnier  * conversions, in IIO buffer modes. Otherwise, use ADC interrupt with direct
5882763ea05SFabrice Gasnier  * DR read instead (e.g. read_raw, or triggered buffer mode without DMA).
5890f883b22SFabrice Gasnier  */
59064ad7f64SFabrice Gasnier static void stm32f4_adc_start_conv(struct stm32_adc *adc, bool dma)
5910f883b22SFabrice Gasnier {
5920f883b22SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN);
5932763ea05SFabrice Gasnier 
5942763ea05SFabrice Gasnier 	if (dma)
5952763ea05SFabrice Gasnier 		stm32_adc_set_bits(adc, STM32F4_ADC_CR2,
5962763ea05SFabrice Gasnier 				   STM32F4_DMA | STM32F4_DDS);
5972763ea05SFabrice Gasnier 
5980f883b22SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_EOCS | STM32F4_ADON);
5990f883b22SFabrice Gasnier 
6000f883b22SFabrice Gasnier 	/* Wait for Power-up time (tSTAB from datasheet) */
6010f883b22SFabrice Gasnier 	usleep_range(2, 3);
6020f883b22SFabrice Gasnier 
6030f883b22SFabrice Gasnier 	/* Software start ? (e.g. trigger detection disabled ?) */
6040f883b22SFabrice Gasnier 	if (!(stm32_adc_readl(adc, STM32F4_ADC_CR2) & STM32F4_EXTEN_MASK))
6050f883b22SFabrice Gasnier 		stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_SWSTART);
6060f883b22SFabrice Gasnier }
6070f883b22SFabrice Gasnier 
60864ad7f64SFabrice Gasnier static void stm32f4_adc_stop_conv(struct stm32_adc *adc)
6090f883b22SFabrice Gasnier {
6100f883b22SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK);
6110f883b22SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_SR, STM32F4_STRT);
6120f883b22SFabrice Gasnier 
6130f883b22SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN);
6142763ea05SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_CR2,
6152763ea05SFabrice Gasnier 			   STM32F4_ADON | STM32F4_DMA | STM32F4_DDS);
6160f883b22SFabrice Gasnier }
6170f883b22SFabrice Gasnier 
61895e339b6SFabrice Gasnier static void stm32h7_adc_start_conv(struct stm32_adc *adc, bool dma)
61995e339b6SFabrice Gasnier {
62095e339b6SFabrice Gasnier 	enum stm32h7_adc_dmngt dmngt;
62195e339b6SFabrice Gasnier 	unsigned long flags;
62295e339b6SFabrice Gasnier 	u32 val;
62395e339b6SFabrice Gasnier 
62495e339b6SFabrice Gasnier 	if (dma)
62595e339b6SFabrice Gasnier 		dmngt = STM32H7_DMNGT_DMA_CIRC;
62695e339b6SFabrice Gasnier 	else
62795e339b6SFabrice Gasnier 		dmngt = STM32H7_DMNGT_DR_ONLY;
62895e339b6SFabrice Gasnier 
62995e339b6SFabrice Gasnier 	spin_lock_irqsave(&adc->lock, flags);
63095e339b6SFabrice Gasnier 	val = stm32_adc_readl(adc, STM32H7_ADC_CFGR);
63195e339b6SFabrice Gasnier 	val = (val & ~STM32H7_DMNGT_MASK) | (dmngt << STM32H7_DMNGT_SHIFT);
63295e339b6SFabrice Gasnier 	stm32_adc_writel(adc, STM32H7_ADC_CFGR, val);
63395e339b6SFabrice Gasnier 	spin_unlock_irqrestore(&adc->lock, flags);
63495e339b6SFabrice Gasnier 
63595e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTART);
63695e339b6SFabrice Gasnier }
63795e339b6SFabrice Gasnier 
63895e339b6SFabrice Gasnier static void stm32h7_adc_stop_conv(struct stm32_adc *adc)
63995e339b6SFabrice Gasnier {
64095e339b6SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
64195e339b6SFabrice Gasnier 	int ret;
64295e339b6SFabrice Gasnier 	u32 val;
64395e339b6SFabrice Gasnier 
64495e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTP);
64595e339b6SFabrice Gasnier 
64695e339b6SFabrice Gasnier 	ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
64795e339b6SFabrice Gasnier 					   !(val & (STM32H7_ADSTART)),
64895e339b6SFabrice Gasnier 					   100, STM32_ADC_TIMEOUT_US);
64995e339b6SFabrice Gasnier 	if (ret)
65095e339b6SFabrice Gasnier 		dev_warn(&indio_dev->dev, "stop failed\n");
65195e339b6SFabrice Gasnier 
65295e339b6SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32H7_ADC_CFGR, STM32H7_DMNGT_MASK);
65395e339b6SFabrice Gasnier }
65495e339b6SFabrice Gasnier 
655d58c67d1SFabrice Gasnier static int stm32h7_adc_exit_pwr_down(struct stm32_adc *adc)
65695e339b6SFabrice Gasnier {
657d58c67d1SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
658d58c67d1SFabrice Gasnier 	int ret;
659d58c67d1SFabrice Gasnier 	u32 val;
660d58c67d1SFabrice Gasnier 
66195e339b6SFabrice Gasnier 	/* Exit deep power down, then enable ADC voltage regulator */
66295e339b6SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD);
66395e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADVREGEN);
66495e339b6SFabrice Gasnier 
66595e339b6SFabrice Gasnier 	if (adc->common->rate > STM32H7_BOOST_CLKRATE)
66695e339b6SFabrice Gasnier 		stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_BOOST);
66795e339b6SFabrice Gasnier 
66895e339b6SFabrice Gasnier 	/* Wait for startup time */
669d58c67d1SFabrice Gasnier 	if (!adc->cfg->has_vregready) {
67095e339b6SFabrice Gasnier 		usleep_range(10, 20);
671d58c67d1SFabrice Gasnier 		return 0;
672d58c67d1SFabrice Gasnier 	}
673d58c67d1SFabrice Gasnier 
674d58c67d1SFabrice Gasnier 	ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR, val,
675d58c67d1SFabrice Gasnier 					   val & STM32MP1_VREGREADY, 100,
676d58c67d1SFabrice Gasnier 					   STM32_ADC_TIMEOUT_US);
677d58c67d1SFabrice Gasnier 	if (ret) {
678d58c67d1SFabrice Gasnier 		stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD);
679d58c67d1SFabrice Gasnier 		dev_err(&indio_dev->dev, "Failed to exit power down\n");
680d58c67d1SFabrice Gasnier 	}
681d58c67d1SFabrice Gasnier 
682d58c67d1SFabrice Gasnier 	return ret;
68395e339b6SFabrice Gasnier }
68495e339b6SFabrice Gasnier 
68595e339b6SFabrice Gasnier static void stm32h7_adc_enter_pwr_down(struct stm32_adc *adc)
68695e339b6SFabrice Gasnier {
68795e339b6SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_BOOST);
68895e339b6SFabrice Gasnier 
68995e339b6SFabrice Gasnier 	/* Setting DEEPPWD disables ADC vreg and clears ADVREGEN */
69095e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD);
69195e339b6SFabrice Gasnier }
69295e339b6SFabrice Gasnier 
69395e339b6SFabrice Gasnier static int stm32h7_adc_enable(struct stm32_adc *adc)
69495e339b6SFabrice Gasnier {
69595e339b6SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
69695e339b6SFabrice Gasnier 	int ret;
69795e339b6SFabrice Gasnier 	u32 val;
69895e339b6SFabrice Gasnier 
69995e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADEN);
70095e339b6SFabrice Gasnier 
70195e339b6SFabrice Gasnier 	/* Poll for ADRDY to be set (after adc startup time) */
70295e339b6SFabrice Gasnier 	ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR, val,
70395e339b6SFabrice Gasnier 					   val & STM32H7_ADRDY,
70495e339b6SFabrice Gasnier 					   100, STM32_ADC_TIMEOUT_US);
70595e339b6SFabrice Gasnier 	if (ret) {
706a3b5655eSFabrice Gasnier 		stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADDIS);
70795e339b6SFabrice Gasnier 		dev_err(&indio_dev->dev, "Failed to enable ADC\n");
708a3b5655eSFabrice Gasnier 	} else {
709a3b5655eSFabrice Gasnier 		/* Clear ADRDY by writing one */
710a3b5655eSFabrice Gasnier 		stm32_adc_set_bits(adc, STM32H7_ADC_ISR, STM32H7_ADRDY);
71195e339b6SFabrice Gasnier 	}
71295e339b6SFabrice Gasnier 
71395e339b6SFabrice Gasnier 	return ret;
71495e339b6SFabrice Gasnier }
71595e339b6SFabrice Gasnier 
71695e339b6SFabrice Gasnier static void stm32h7_adc_disable(struct stm32_adc *adc)
71795e339b6SFabrice Gasnier {
71895e339b6SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
71995e339b6SFabrice Gasnier 	int ret;
72095e339b6SFabrice Gasnier 	u32 val;
72195e339b6SFabrice Gasnier 
72295e339b6SFabrice Gasnier 	/* Disable ADC and wait until it's effectively disabled */
72395e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADDIS);
72495e339b6SFabrice Gasnier 	ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
72595e339b6SFabrice Gasnier 					   !(val & STM32H7_ADEN), 100,
72695e339b6SFabrice Gasnier 					   STM32_ADC_TIMEOUT_US);
72795e339b6SFabrice Gasnier 	if (ret)
72895e339b6SFabrice Gasnier 		dev_warn(&indio_dev->dev, "Failed to disable\n");
72995e339b6SFabrice Gasnier }
73095e339b6SFabrice Gasnier 
73195e339b6SFabrice Gasnier /**
73295e339b6SFabrice Gasnier  * stm32h7_adc_read_selfcalib() - read calibration shadow regs, save result
73395e339b6SFabrice Gasnier  * @adc: stm32 adc instance
7340da98c7bSFabrice Gasnier  * Note: Must be called once ADC is enabled, so LINCALRDYW[1..6] are writable
73595e339b6SFabrice Gasnier  */
73695e339b6SFabrice Gasnier static int stm32h7_adc_read_selfcalib(struct stm32_adc *adc)
73795e339b6SFabrice Gasnier {
73895e339b6SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
73995e339b6SFabrice Gasnier 	int i, ret;
74095e339b6SFabrice Gasnier 	u32 lincalrdyw_mask, val;
74195e339b6SFabrice Gasnier 
74295e339b6SFabrice Gasnier 	/* Read linearity calibration */
74395e339b6SFabrice Gasnier 	lincalrdyw_mask = STM32H7_LINCALRDYW6;
74495e339b6SFabrice Gasnier 	for (i = STM32H7_LINCALFACT_NUM - 1; i >= 0; i--) {
74595e339b6SFabrice Gasnier 		/* Clear STM32H7_LINCALRDYW[6..1]: transfer calib to CALFACT2 */
74695e339b6SFabrice Gasnier 		stm32_adc_clr_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask);
74795e339b6SFabrice Gasnier 
74895e339b6SFabrice Gasnier 		/* Poll: wait calib data to be ready in CALFACT2 register */
74995e339b6SFabrice Gasnier 		ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
75095e339b6SFabrice Gasnier 						   !(val & lincalrdyw_mask),
75195e339b6SFabrice Gasnier 						   100, STM32_ADC_TIMEOUT_US);
75295e339b6SFabrice Gasnier 		if (ret) {
75395e339b6SFabrice Gasnier 			dev_err(&indio_dev->dev, "Failed to read calfact\n");
7540da98c7bSFabrice Gasnier 			return ret;
75595e339b6SFabrice Gasnier 		}
75695e339b6SFabrice Gasnier 
75795e339b6SFabrice Gasnier 		val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2);
75895e339b6SFabrice Gasnier 		adc->cal.lincalfact[i] = (val & STM32H7_LINCALFACT_MASK);
75995e339b6SFabrice Gasnier 		adc->cal.lincalfact[i] >>= STM32H7_LINCALFACT_SHIFT;
76095e339b6SFabrice Gasnier 
76195e339b6SFabrice Gasnier 		lincalrdyw_mask >>= 1;
76295e339b6SFabrice Gasnier 	}
76395e339b6SFabrice Gasnier 
76495e339b6SFabrice Gasnier 	/* Read offset calibration */
76595e339b6SFabrice Gasnier 	val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT);
76695e339b6SFabrice Gasnier 	adc->cal.calfact_s = (val & STM32H7_CALFACT_S_MASK);
76795e339b6SFabrice Gasnier 	adc->cal.calfact_s >>= STM32H7_CALFACT_S_SHIFT;
76895e339b6SFabrice Gasnier 	adc->cal.calfact_d = (val & STM32H7_CALFACT_D_MASK);
76995e339b6SFabrice Gasnier 	adc->cal.calfact_d >>= STM32H7_CALFACT_D_SHIFT;
7700da98c7bSFabrice Gasnier 	adc->cal.calibrated = true;
77195e339b6SFabrice Gasnier 
7720da98c7bSFabrice Gasnier 	return 0;
77395e339b6SFabrice Gasnier }
77495e339b6SFabrice Gasnier 
77595e339b6SFabrice Gasnier /**
77695e339b6SFabrice Gasnier  * stm32h7_adc_restore_selfcalib() - Restore saved self-calibration result
77795e339b6SFabrice Gasnier  * @adc: stm32 adc instance
77895e339b6SFabrice Gasnier  * Note: ADC must be enabled, with no on-going conversions.
77995e339b6SFabrice Gasnier  */
78095e339b6SFabrice Gasnier static int stm32h7_adc_restore_selfcalib(struct stm32_adc *adc)
78195e339b6SFabrice Gasnier {
78295e339b6SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
78395e339b6SFabrice Gasnier 	int i, ret;
78495e339b6SFabrice Gasnier 	u32 lincalrdyw_mask, val;
78595e339b6SFabrice Gasnier 
78695e339b6SFabrice Gasnier 	val = (adc->cal.calfact_s << STM32H7_CALFACT_S_SHIFT) |
78795e339b6SFabrice Gasnier 		(adc->cal.calfact_d << STM32H7_CALFACT_D_SHIFT);
78895e339b6SFabrice Gasnier 	stm32_adc_writel(adc, STM32H7_ADC_CALFACT, val);
78995e339b6SFabrice Gasnier 
79095e339b6SFabrice Gasnier 	lincalrdyw_mask = STM32H7_LINCALRDYW6;
79195e339b6SFabrice Gasnier 	for (i = STM32H7_LINCALFACT_NUM - 1; i >= 0; i--) {
79295e339b6SFabrice Gasnier 		/*
79395e339b6SFabrice Gasnier 		 * Write saved calibration data to shadow registers:
79495e339b6SFabrice Gasnier 		 * Write CALFACT2, and set LINCALRDYW[6..1] bit to trigger
79595e339b6SFabrice Gasnier 		 * data write. Then poll to wait for complete transfer.
79695e339b6SFabrice Gasnier 		 */
79795e339b6SFabrice Gasnier 		val = adc->cal.lincalfact[i] << STM32H7_LINCALFACT_SHIFT;
79895e339b6SFabrice Gasnier 		stm32_adc_writel(adc, STM32H7_ADC_CALFACT2, val);
79995e339b6SFabrice Gasnier 		stm32_adc_set_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask);
80095e339b6SFabrice Gasnier 		ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
80195e339b6SFabrice Gasnier 						   val & lincalrdyw_mask,
80295e339b6SFabrice Gasnier 						   100, STM32_ADC_TIMEOUT_US);
80395e339b6SFabrice Gasnier 		if (ret) {
80495e339b6SFabrice Gasnier 			dev_err(&indio_dev->dev, "Failed to write calfact\n");
80595e339b6SFabrice Gasnier 			return ret;
80695e339b6SFabrice Gasnier 		}
80795e339b6SFabrice Gasnier 
80895e339b6SFabrice Gasnier 		/*
80995e339b6SFabrice Gasnier 		 * Read back calibration data, has two effects:
81095e339b6SFabrice Gasnier 		 * - It ensures bits LINCALRDYW[6..1] are kept cleared
81195e339b6SFabrice Gasnier 		 *   for next time calibration needs to be restored.
81295e339b6SFabrice Gasnier 		 * - BTW, bit clear triggers a read, then check data has been
81395e339b6SFabrice Gasnier 		 *   correctly written.
81495e339b6SFabrice Gasnier 		 */
81595e339b6SFabrice Gasnier 		stm32_adc_clr_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask);
81695e339b6SFabrice Gasnier 		ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
81795e339b6SFabrice Gasnier 						   !(val & lincalrdyw_mask),
81895e339b6SFabrice Gasnier 						   100, STM32_ADC_TIMEOUT_US);
81995e339b6SFabrice Gasnier 		if (ret) {
82095e339b6SFabrice Gasnier 			dev_err(&indio_dev->dev, "Failed to read calfact\n");
82195e339b6SFabrice Gasnier 			return ret;
82295e339b6SFabrice Gasnier 		}
82395e339b6SFabrice Gasnier 		val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2);
82495e339b6SFabrice Gasnier 		if (val != adc->cal.lincalfact[i] << STM32H7_LINCALFACT_SHIFT) {
82595e339b6SFabrice Gasnier 			dev_err(&indio_dev->dev, "calfact not consistent\n");
82695e339b6SFabrice Gasnier 			return -EIO;
82795e339b6SFabrice Gasnier 		}
82895e339b6SFabrice Gasnier 
82995e339b6SFabrice Gasnier 		lincalrdyw_mask >>= 1;
83095e339b6SFabrice Gasnier 	}
83195e339b6SFabrice Gasnier 
83295e339b6SFabrice Gasnier 	return 0;
83395e339b6SFabrice Gasnier }
83495e339b6SFabrice Gasnier 
83595e339b6SFabrice Gasnier /**
83695e339b6SFabrice Gasnier  * Fixed timeout value for ADC calibration.
83795e339b6SFabrice Gasnier  * worst cases:
83895e339b6SFabrice Gasnier  * - low clock frequency
83995e339b6SFabrice Gasnier  * - maximum prescalers
84095e339b6SFabrice Gasnier  * Calibration requires:
84195e339b6SFabrice Gasnier  * - 131,072 ADC clock cycle for the linear calibration
84295e339b6SFabrice Gasnier  * - 20 ADC clock cycle for the offset calibration
84395e339b6SFabrice Gasnier  *
84495e339b6SFabrice Gasnier  * Set to 100ms for now
84595e339b6SFabrice Gasnier  */
84695e339b6SFabrice Gasnier #define STM32H7_ADC_CALIB_TIMEOUT_US		100000
84795e339b6SFabrice Gasnier 
84895e339b6SFabrice Gasnier /**
8490da98c7bSFabrice Gasnier  * stm32h7_adc_selfcalib() - Procedure to calibrate ADC
85095e339b6SFabrice Gasnier  * @adc: stm32 adc instance
8510da98c7bSFabrice Gasnier  * Note: Must be called once ADC is out of power down.
85295e339b6SFabrice Gasnier  */
85395e339b6SFabrice Gasnier static int stm32h7_adc_selfcalib(struct stm32_adc *adc)
85495e339b6SFabrice Gasnier {
85595e339b6SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
85695e339b6SFabrice Gasnier 	int ret;
85795e339b6SFabrice Gasnier 	u32 val;
85895e339b6SFabrice Gasnier 
8590da98c7bSFabrice Gasnier 	if (adc->cal.calibrated)
8600da98c7bSFabrice Gasnier 		return true;
86195e339b6SFabrice Gasnier 
86295e339b6SFabrice Gasnier 	/*
86395e339b6SFabrice Gasnier 	 * Select calibration mode:
86495e339b6SFabrice Gasnier 	 * - Offset calibration for single ended inputs
86595e339b6SFabrice Gasnier 	 * - No linearity calibration (do it later, before reading it)
86695e339b6SFabrice Gasnier 	 */
86795e339b6SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADCALDIF);
86895e339b6SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADCALLIN);
86995e339b6SFabrice Gasnier 
87095e339b6SFabrice Gasnier 	/* Start calibration, then wait for completion */
87195e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADCAL);
87295e339b6SFabrice Gasnier 	ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
87395e339b6SFabrice Gasnier 					   !(val & STM32H7_ADCAL), 100,
87495e339b6SFabrice Gasnier 					   STM32H7_ADC_CALIB_TIMEOUT_US);
87595e339b6SFabrice Gasnier 	if (ret) {
87695e339b6SFabrice Gasnier 		dev_err(&indio_dev->dev, "calibration failed\n");
8770da98c7bSFabrice Gasnier 		goto out;
87895e339b6SFabrice Gasnier 	}
87995e339b6SFabrice Gasnier 
88095e339b6SFabrice Gasnier 	/*
88195e339b6SFabrice Gasnier 	 * Select calibration mode, then start calibration:
88295e339b6SFabrice Gasnier 	 * - Offset calibration for differential input
88395e339b6SFabrice Gasnier 	 * - Linearity calibration (needs to be done only once for single/diff)
88495e339b6SFabrice Gasnier 	 *   will run simultaneously with offset calibration.
88595e339b6SFabrice Gasnier 	 */
88695e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR,
88795e339b6SFabrice Gasnier 			   STM32H7_ADCALDIF | STM32H7_ADCALLIN);
88895e339b6SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADCAL);
88995e339b6SFabrice Gasnier 	ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
89095e339b6SFabrice Gasnier 					   !(val & STM32H7_ADCAL), 100,
89195e339b6SFabrice Gasnier 					   STM32H7_ADC_CALIB_TIMEOUT_US);
89295e339b6SFabrice Gasnier 	if (ret) {
89395e339b6SFabrice Gasnier 		dev_err(&indio_dev->dev, "calibration failed\n");
8940da98c7bSFabrice Gasnier 		goto out;
89595e339b6SFabrice Gasnier 	}
89695e339b6SFabrice Gasnier 
8970da98c7bSFabrice Gasnier out:
89895e339b6SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32H7_ADC_CR,
89995e339b6SFabrice Gasnier 			   STM32H7_ADCALDIF | STM32H7_ADCALLIN);
90095e339b6SFabrice Gasnier 
90195e339b6SFabrice Gasnier 	return ret;
90295e339b6SFabrice Gasnier }
90395e339b6SFabrice Gasnier 
90495e339b6SFabrice Gasnier /**
90595e339b6SFabrice Gasnier  * stm32h7_adc_prepare() - Leave power down mode to enable ADC.
90695e339b6SFabrice Gasnier  * @adc: stm32 adc instance
90795e339b6SFabrice Gasnier  * Leave power down mode.
9083fb2e24eSFabrice Gasnier  * Configure channels as single ended or differential before enabling ADC.
90995e339b6SFabrice Gasnier  * Enable ADC.
91095e339b6SFabrice Gasnier  * Restore calibration data.
9113fb2e24eSFabrice Gasnier  * Pre-select channels that may be used in PCSEL (required by input MUX / IO):
9123fb2e24eSFabrice Gasnier  * - Only one input is selected for single ended (e.g. 'vinp')
9133fb2e24eSFabrice Gasnier  * - Two inputs are selected for differential channels (e.g. 'vinp' & 'vinn')
91495e339b6SFabrice Gasnier  */
91595e339b6SFabrice Gasnier static int stm32h7_adc_prepare(struct stm32_adc *adc)
91695e339b6SFabrice Gasnier {
9170da98c7bSFabrice Gasnier 	int calib, ret;
91895e339b6SFabrice Gasnier 
919d58c67d1SFabrice Gasnier 	ret = stm32h7_adc_exit_pwr_down(adc);
920d58c67d1SFabrice Gasnier 	if (ret)
921d58c67d1SFabrice Gasnier 		return ret;
922d58c67d1SFabrice Gasnier 
9230da98c7bSFabrice Gasnier 	ret = stm32h7_adc_selfcalib(adc);
9240da98c7bSFabrice Gasnier 	if (ret < 0)
9250da98c7bSFabrice Gasnier 		goto pwr_dwn;
9260da98c7bSFabrice Gasnier 	calib = ret;
9270da98c7bSFabrice Gasnier 
9283fb2e24eSFabrice Gasnier 	stm32_adc_writel(adc, STM32H7_ADC_DIFSEL, adc->difsel);
92995e339b6SFabrice Gasnier 
93095e339b6SFabrice Gasnier 	ret = stm32h7_adc_enable(adc);
93195e339b6SFabrice Gasnier 	if (ret)
93295e339b6SFabrice Gasnier 		goto pwr_dwn;
93395e339b6SFabrice Gasnier 
9340da98c7bSFabrice Gasnier 	/* Either restore or read calibration result for future reference */
9350da98c7bSFabrice Gasnier 	if (calib)
93695e339b6SFabrice Gasnier 		ret = stm32h7_adc_restore_selfcalib(adc);
9370da98c7bSFabrice Gasnier 	else
9380da98c7bSFabrice Gasnier 		ret = stm32h7_adc_read_selfcalib(adc);
93995e339b6SFabrice Gasnier 	if (ret)
94095e339b6SFabrice Gasnier 		goto disable;
94195e339b6SFabrice Gasnier 
94295e339b6SFabrice Gasnier 	stm32_adc_writel(adc, STM32H7_ADC_PCSEL, adc->pcsel);
94395e339b6SFabrice Gasnier 
94495e339b6SFabrice Gasnier 	return 0;
94595e339b6SFabrice Gasnier 
94695e339b6SFabrice Gasnier disable:
94795e339b6SFabrice Gasnier 	stm32h7_adc_disable(adc);
94895e339b6SFabrice Gasnier pwr_dwn:
94995e339b6SFabrice Gasnier 	stm32h7_adc_enter_pwr_down(adc);
95095e339b6SFabrice Gasnier 
95195e339b6SFabrice Gasnier 	return ret;
95295e339b6SFabrice Gasnier }
95395e339b6SFabrice Gasnier 
95495e339b6SFabrice Gasnier static void stm32h7_adc_unprepare(struct stm32_adc *adc)
95595e339b6SFabrice Gasnier {
95695e339b6SFabrice Gasnier 	stm32h7_adc_disable(adc);
95795e339b6SFabrice Gasnier 	stm32h7_adc_enter_pwr_down(adc);
95895e339b6SFabrice Gasnier }
95995e339b6SFabrice Gasnier 
9600f883b22SFabrice Gasnier /**
961da9b9485SFabrice Gasnier  * stm32_adc_conf_scan_seq() - Build regular channels scan sequence
962da9b9485SFabrice Gasnier  * @indio_dev: IIO device
963da9b9485SFabrice Gasnier  * @scan_mask: channels to be converted
964da9b9485SFabrice Gasnier  *
965da9b9485SFabrice Gasnier  * Conversion sequence :
966ee2ac1cdSFabrice Gasnier  * Apply sampling time settings for all channels.
967da9b9485SFabrice Gasnier  * Configure ADC scan sequence based on selected channels in scan_mask.
968da9b9485SFabrice Gasnier  * Add channels to SQR registers, from scan_mask LSB to MSB, then
969da9b9485SFabrice Gasnier  * program sequence len.
970da9b9485SFabrice Gasnier  */
971da9b9485SFabrice Gasnier static int stm32_adc_conf_scan_seq(struct iio_dev *indio_dev,
972da9b9485SFabrice Gasnier 				   const unsigned long *scan_mask)
973da9b9485SFabrice Gasnier {
974da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
97564ad7f64SFabrice Gasnier 	const struct stm32_adc_regs *sqr = adc->cfg->regs->sqr;
976da9b9485SFabrice Gasnier 	const struct iio_chan_spec *chan;
977da9b9485SFabrice Gasnier 	u32 val, bit;
978da9b9485SFabrice Gasnier 	int i = 0;
979da9b9485SFabrice Gasnier 
980ee2ac1cdSFabrice Gasnier 	/* Apply sampling time settings */
981ee2ac1cdSFabrice Gasnier 	stm32_adc_writel(adc, adc->cfg->regs->smpr[0], adc->smpr_val[0]);
982ee2ac1cdSFabrice Gasnier 	stm32_adc_writel(adc, adc->cfg->regs->smpr[1], adc->smpr_val[1]);
983ee2ac1cdSFabrice Gasnier 
984da9b9485SFabrice Gasnier 	for_each_set_bit(bit, scan_mask, indio_dev->masklength) {
985da9b9485SFabrice Gasnier 		chan = indio_dev->channels + bit;
986da9b9485SFabrice Gasnier 		/*
987da9b9485SFabrice Gasnier 		 * Assign one channel per SQ entry in regular
988da9b9485SFabrice Gasnier 		 * sequence, starting with SQ1.
989da9b9485SFabrice Gasnier 		 */
990da9b9485SFabrice Gasnier 		i++;
991da9b9485SFabrice Gasnier 		if (i > STM32_ADC_MAX_SQ)
992da9b9485SFabrice Gasnier 			return -EINVAL;
993da9b9485SFabrice Gasnier 
994da9b9485SFabrice Gasnier 		dev_dbg(&indio_dev->dev, "%s chan %d to SQ%d\n",
995da9b9485SFabrice Gasnier 			__func__, chan->channel, i);
996da9b9485SFabrice Gasnier 
99764ad7f64SFabrice Gasnier 		val = stm32_adc_readl(adc, sqr[i].reg);
99864ad7f64SFabrice Gasnier 		val &= ~sqr[i].mask;
99964ad7f64SFabrice Gasnier 		val |= chan->channel << sqr[i].shift;
100064ad7f64SFabrice Gasnier 		stm32_adc_writel(adc, sqr[i].reg, val);
1001da9b9485SFabrice Gasnier 	}
1002da9b9485SFabrice Gasnier 
1003da9b9485SFabrice Gasnier 	if (!i)
1004da9b9485SFabrice Gasnier 		return -EINVAL;
1005da9b9485SFabrice Gasnier 
1006da9b9485SFabrice Gasnier 	/* Sequence len */
100764ad7f64SFabrice Gasnier 	val = stm32_adc_readl(adc, sqr[0].reg);
100864ad7f64SFabrice Gasnier 	val &= ~sqr[0].mask;
100964ad7f64SFabrice Gasnier 	val |= ((i - 1) << sqr[0].shift);
101064ad7f64SFabrice Gasnier 	stm32_adc_writel(adc, sqr[0].reg, val);
1011da9b9485SFabrice Gasnier 
1012da9b9485SFabrice Gasnier 	return 0;
1013da9b9485SFabrice Gasnier }
1014da9b9485SFabrice Gasnier 
1015da9b9485SFabrice Gasnier /**
1016da9b9485SFabrice Gasnier  * stm32_adc_get_trig_extsel() - Get external trigger selection
10171cd92d42SFabrice Gasnier  * @indio_dev: IIO device structure
1018da9b9485SFabrice Gasnier  * @trig: trigger
1019da9b9485SFabrice Gasnier  *
1020da9b9485SFabrice Gasnier  * Returns trigger extsel value, if trig matches, -EINVAL otherwise.
1021da9b9485SFabrice Gasnier  */
102264ad7f64SFabrice Gasnier static int stm32_adc_get_trig_extsel(struct iio_dev *indio_dev,
102364ad7f64SFabrice Gasnier 				     struct iio_trigger *trig)
1024da9b9485SFabrice Gasnier {
102564ad7f64SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
1026f24a33b3SFabrice Gasnier 	int i;
1027f24a33b3SFabrice Gasnier 
1028f24a33b3SFabrice Gasnier 	/* lookup triggers registered by stm32 timer trigger driver */
102964ad7f64SFabrice Gasnier 	for (i = 0; adc->cfg->trigs[i].name; i++) {
1030f24a33b3SFabrice Gasnier 		/**
1031f24a33b3SFabrice Gasnier 		 * Checking both stm32 timer trigger type and trig name
1032f24a33b3SFabrice Gasnier 		 * should be safe against arbitrary trigger names.
1033f24a33b3SFabrice Gasnier 		 */
1034f0b638a7SFabrice Gasnier 		if ((is_stm32_timer_trigger(trig) ||
1035f0b638a7SFabrice Gasnier 		     is_stm32_lptim_trigger(trig)) &&
103664ad7f64SFabrice Gasnier 		    !strcmp(adc->cfg->trigs[i].name, trig->name)) {
103764ad7f64SFabrice Gasnier 			return adc->cfg->trigs[i].extsel;
1038f24a33b3SFabrice Gasnier 		}
1039f24a33b3SFabrice Gasnier 	}
1040f24a33b3SFabrice Gasnier 
1041da9b9485SFabrice Gasnier 	return -EINVAL;
1042da9b9485SFabrice Gasnier }
1043da9b9485SFabrice Gasnier 
1044da9b9485SFabrice Gasnier /**
1045da9b9485SFabrice Gasnier  * stm32_adc_set_trig() - Set a regular trigger
1046da9b9485SFabrice Gasnier  * @indio_dev: IIO device
1047da9b9485SFabrice Gasnier  * @trig: IIO trigger
1048da9b9485SFabrice Gasnier  *
1049da9b9485SFabrice Gasnier  * Set trigger source/polarity (e.g. SW, or HW with polarity) :
1050da9b9485SFabrice Gasnier  * - if HW trigger disabled (e.g. trig == NULL, conversion launched by sw)
1051da9b9485SFabrice Gasnier  * - if HW trigger enabled, set source & polarity
1052da9b9485SFabrice Gasnier  */
1053da9b9485SFabrice Gasnier static int stm32_adc_set_trig(struct iio_dev *indio_dev,
1054da9b9485SFabrice Gasnier 			      struct iio_trigger *trig)
1055da9b9485SFabrice Gasnier {
1056da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
1057da9b9485SFabrice Gasnier 	u32 val, extsel = 0, exten = STM32_EXTEN_SWTRIG;
1058da9b9485SFabrice Gasnier 	unsigned long flags;
1059da9b9485SFabrice Gasnier 	int ret;
1060da9b9485SFabrice Gasnier 
1061da9b9485SFabrice Gasnier 	if (trig) {
106264ad7f64SFabrice Gasnier 		ret = stm32_adc_get_trig_extsel(indio_dev, trig);
1063da9b9485SFabrice Gasnier 		if (ret < 0)
1064da9b9485SFabrice Gasnier 			return ret;
1065da9b9485SFabrice Gasnier 
1066da9b9485SFabrice Gasnier 		/* set trigger source and polarity (default to rising edge) */
1067da9b9485SFabrice Gasnier 		extsel = ret;
1068732f2dc4SFabrice Gasnier 		exten = adc->trigger_polarity + STM32_EXTEN_HWTRIG_RISING_EDGE;
1069da9b9485SFabrice Gasnier 	}
1070da9b9485SFabrice Gasnier 
1071da9b9485SFabrice Gasnier 	spin_lock_irqsave(&adc->lock, flags);
107264ad7f64SFabrice Gasnier 	val = stm32_adc_readl(adc, adc->cfg->regs->exten.reg);
107364ad7f64SFabrice Gasnier 	val &= ~(adc->cfg->regs->exten.mask | adc->cfg->regs->extsel.mask);
107464ad7f64SFabrice Gasnier 	val |= exten << adc->cfg->regs->exten.shift;
107564ad7f64SFabrice Gasnier 	val |= extsel << adc->cfg->regs->extsel.shift;
107664ad7f64SFabrice Gasnier 	stm32_adc_writel(adc,  adc->cfg->regs->exten.reg, val);
1077da9b9485SFabrice Gasnier 	spin_unlock_irqrestore(&adc->lock, flags);
1078da9b9485SFabrice Gasnier 
1079da9b9485SFabrice Gasnier 	return 0;
1080da9b9485SFabrice Gasnier }
1081da9b9485SFabrice Gasnier 
1082732f2dc4SFabrice Gasnier static int stm32_adc_set_trig_pol(struct iio_dev *indio_dev,
1083732f2dc4SFabrice Gasnier 				  const struct iio_chan_spec *chan,
1084732f2dc4SFabrice Gasnier 				  unsigned int type)
1085732f2dc4SFabrice Gasnier {
1086732f2dc4SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
1087732f2dc4SFabrice Gasnier 
1088732f2dc4SFabrice Gasnier 	adc->trigger_polarity = type;
1089732f2dc4SFabrice Gasnier 
1090732f2dc4SFabrice Gasnier 	return 0;
1091732f2dc4SFabrice Gasnier }
1092732f2dc4SFabrice Gasnier 
1093732f2dc4SFabrice Gasnier static int stm32_adc_get_trig_pol(struct iio_dev *indio_dev,
1094732f2dc4SFabrice Gasnier 				  const struct iio_chan_spec *chan)
1095732f2dc4SFabrice Gasnier {
1096732f2dc4SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
1097732f2dc4SFabrice Gasnier 
1098732f2dc4SFabrice Gasnier 	return adc->trigger_polarity;
1099732f2dc4SFabrice Gasnier }
1100732f2dc4SFabrice Gasnier 
1101732f2dc4SFabrice Gasnier static const char * const stm32_trig_pol_items[] = {
1102732f2dc4SFabrice Gasnier 	"rising-edge", "falling-edge", "both-edges",
1103732f2dc4SFabrice Gasnier };
1104732f2dc4SFabrice Gasnier 
11052763ea05SFabrice Gasnier static const struct iio_enum stm32_adc_trig_pol = {
1106732f2dc4SFabrice Gasnier 	.items = stm32_trig_pol_items,
1107732f2dc4SFabrice Gasnier 	.num_items = ARRAY_SIZE(stm32_trig_pol_items),
1108732f2dc4SFabrice Gasnier 	.get = stm32_adc_get_trig_pol,
1109732f2dc4SFabrice Gasnier 	.set = stm32_adc_set_trig_pol,
1110732f2dc4SFabrice Gasnier };
1111732f2dc4SFabrice Gasnier 
1112da9b9485SFabrice Gasnier /**
11130f883b22SFabrice Gasnier  * stm32_adc_single_conv() - Performs a single conversion
11140f883b22SFabrice Gasnier  * @indio_dev: IIO device
11150f883b22SFabrice Gasnier  * @chan: IIO channel
11160f883b22SFabrice Gasnier  * @res: conversion result
11170f883b22SFabrice Gasnier  *
11180f883b22SFabrice Gasnier  * The function performs a single conversion on a given channel:
1119ee2ac1cdSFabrice Gasnier  * - Apply sampling time settings
11200f883b22SFabrice Gasnier  * - Program sequencer with one channel (e.g. in SQ1 with len = 1)
11210f883b22SFabrice Gasnier  * - Use SW trigger
11220f883b22SFabrice Gasnier  * - Start conversion, then wait for interrupt completion.
11230f883b22SFabrice Gasnier  */
11240f883b22SFabrice Gasnier static int stm32_adc_single_conv(struct iio_dev *indio_dev,
11250f883b22SFabrice Gasnier 				 const struct iio_chan_spec *chan,
11260f883b22SFabrice Gasnier 				 int *res)
11270f883b22SFabrice Gasnier {
11280f883b22SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
11299bdbb113SFabrice Gasnier 	struct device *dev = indio_dev->dev.parent;
113064ad7f64SFabrice Gasnier 	const struct stm32_adc_regspec *regs = adc->cfg->regs;
11310f883b22SFabrice Gasnier 	long timeout;
11320f883b22SFabrice Gasnier 	u32 val;
11330f883b22SFabrice Gasnier 	int ret;
11340f883b22SFabrice Gasnier 
11350f883b22SFabrice Gasnier 	reinit_completion(&adc->completion);
11360f883b22SFabrice Gasnier 
1137da9b9485SFabrice Gasnier 	adc->bufi = 0;
11380f883b22SFabrice Gasnier 
11399bdbb113SFabrice Gasnier 	ret = pm_runtime_get_sync(dev);
11409bdbb113SFabrice Gasnier 	if (ret < 0) {
11419bdbb113SFabrice Gasnier 		pm_runtime_put_noidle(dev);
114295e339b6SFabrice Gasnier 		return ret;
114395e339b6SFabrice Gasnier 	}
114495e339b6SFabrice Gasnier 
1145ee2ac1cdSFabrice Gasnier 	/* Apply sampling time settings */
1146ee2ac1cdSFabrice Gasnier 	stm32_adc_writel(adc, regs->smpr[0], adc->smpr_val[0]);
1147ee2ac1cdSFabrice Gasnier 	stm32_adc_writel(adc, regs->smpr[1], adc->smpr_val[1]);
1148ee2ac1cdSFabrice Gasnier 
1149da9b9485SFabrice Gasnier 	/* Program chan number in regular sequence (SQ1) */
115064ad7f64SFabrice Gasnier 	val = stm32_adc_readl(adc, regs->sqr[1].reg);
115164ad7f64SFabrice Gasnier 	val &= ~regs->sqr[1].mask;
115264ad7f64SFabrice Gasnier 	val |= chan->channel << regs->sqr[1].shift;
115364ad7f64SFabrice Gasnier 	stm32_adc_writel(adc, regs->sqr[1].reg, val);
11540f883b22SFabrice Gasnier 
11550f883b22SFabrice Gasnier 	/* Set regular sequence len (0 for 1 conversion) */
115664ad7f64SFabrice Gasnier 	stm32_adc_clr_bits(adc, regs->sqr[0].reg, regs->sqr[0].mask);
11570f883b22SFabrice Gasnier 
11580f883b22SFabrice Gasnier 	/* Trigger detection disabled (conversion can be launched in SW) */
115964ad7f64SFabrice Gasnier 	stm32_adc_clr_bits(adc, regs->exten.reg, regs->exten.mask);
11600f883b22SFabrice Gasnier 
11610f883b22SFabrice Gasnier 	stm32_adc_conv_irq_enable(adc);
11620f883b22SFabrice Gasnier 
116364ad7f64SFabrice Gasnier 	adc->cfg->start_conv(adc, false);
11640f883b22SFabrice Gasnier 
11650f883b22SFabrice Gasnier 	timeout = wait_for_completion_interruptible_timeout(
11660f883b22SFabrice Gasnier 					&adc->completion, STM32_ADC_TIMEOUT);
11670f883b22SFabrice Gasnier 	if (timeout == 0) {
11680f883b22SFabrice Gasnier 		ret = -ETIMEDOUT;
11690f883b22SFabrice Gasnier 	} else if (timeout < 0) {
11700f883b22SFabrice Gasnier 		ret = timeout;
11710f883b22SFabrice Gasnier 	} else {
1172da9b9485SFabrice Gasnier 		*res = adc->buffer[0];
11730f883b22SFabrice Gasnier 		ret = IIO_VAL_INT;
11740f883b22SFabrice Gasnier 	}
11750f883b22SFabrice Gasnier 
117664ad7f64SFabrice Gasnier 	adc->cfg->stop_conv(adc);
11770f883b22SFabrice Gasnier 
11780f883b22SFabrice Gasnier 	stm32_adc_conv_irq_disable(adc);
11790f883b22SFabrice Gasnier 
11809bdbb113SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
11819bdbb113SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
118295e339b6SFabrice Gasnier 
11830f883b22SFabrice Gasnier 	return ret;
11840f883b22SFabrice Gasnier }
11850f883b22SFabrice Gasnier 
11860f883b22SFabrice Gasnier static int stm32_adc_read_raw(struct iio_dev *indio_dev,
11870f883b22SFabrice Gasnier 			      struct iio_chan_spec const *chan,
11880f883b22SFabrice Gasnier 			      int *val, int *val2, long mask)
11890f883b22SFabrice Gasnier {
11900f883b22SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
11910f883b22SFabrice Gasnier 	int ret;
11920f883b22SFabrice Gasnier 
11930f883b22SFabrice Gasnier 	switch (mask) {
11940f883b22SFabrice Gasnier 	case IIO_CHAN_INFO_RAW:
11950f883b22SFabrice Gasnier 		ret = iio_device_claim_direct_mode(indio_dev);
11960f883b22SFabrice Gasnier 		if (ret)
11970f883b22SFabrice Gasnier 			return ret;
11980f883b22SFabrice Gasnier 		if (chan->type == IIO_VOLTAGE)
11990f883b22SFabrice Gasnier 			ret = stm32_adc_single_conv(indio_dev, chan, val);
12000f883b22SFabrice Gasnier 		else
12010f883b22SFabrice Gasnier 			ret = -EINVAL;
12020f883b22SFabrice Gasnier 		iio_device_release_direct_mode(indio_dev);
12030f883b22SFabrice Gasnier 		return ret;
12040f883b22SFabrice Gasnier 
12050f883b22SFabrice Gasnier 	case IIO_CHAN_INFO_SCALE:
12063fb2e24eSFabrice Gasnier 		if (chan->differential) {
12073fb2e24eSFabrice Gasnier 			*val = adc->common->vref_mv * 2;
12083fb2e24eSFabrice Gasnier 			*val2 = chan->scan_type.realbits;
12093fb2e24eSFabrice Gasnier 		} else {
12100f883b22SFabrice Gasnier 			*val = adc->common->vref_mv;
12110f883b22SFabrice Gasnier 			*val2 = chan->scan_type.realbits;
12123fb2e24eSFabrice Gasnier 		}
12130f883b22SFabrice Gasnier 		return IIO_VAL_FRACTIONAL_LOG2;
12140f883b22SFabrice Gasnier 
12153fb2e24eSFabrice Gasnier 	case IIO_CHAN_INFO_OFFSET:
12163fb2e24eSFabrice Gasnier 		if (chan->differential)
12173fb2e24eSFabrice Gasnier 			/* ADC_full_scale / 2 */
12183fb2e24eSFabrice Gasnier 			*val = -((1 << chan->scan_type.realbits) / 2);
12193fb2e24eSFabrice Gasnier 		else
12203fb2e24eSFabrice Gasnier 			*val = 0;
12213fb2e24eSFabrice Gasnier 		return IIO_VAL_INT;
12223fb2e24eSFabrice Gasnier 
12230f883b22SFabrice Gasnier 	default:
12240f883b22SFabrice Gasnier 		return -EINVAL;
12250f883b22SFabrice Gasnier 	}
12260f883b22SFabrice Gasnier }
12270f883b22SFabrice Gasnier 
1228cc06e67dSFabrice Gasnier static irqreturn_t stm32_adc_threaded_isr(int irq, void *data)
1229cc06e67dSFabrice Gasnier {
1230cc06e67dSFabrice Gasnier 	struct stm32_adc *adc = data;
1231cc06e67dSFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
1232cc06e67dSFabrice Gasnier 	const struct stm32_adc_regspec *regs = adc->cfg->regs;
1233cc06e67dSFabrice Gasnier 	u32 status = stm32_adc_readl(adc, regs->isr_eoc.reg);
1234cc06e67dSFabrice Gasnier 
1235cc06e67dSFabrice Gasnier 	if (status & regs->isr_ovr.mask)
1236cc06e67dSFabrice Gasnier 		dev_err(&indio_dev->dev, "Overrun, stopping: restart needed\n");
1237cc06e67dSFabrice Gasnier 
1238cc06e67dSFabrice Gasnier 	return IRQ_HANDLED;
1239cc06e67dSFabrice Gasnier }
1240cc06e67dSFabrice Gasnier 
12410f883b22SFabrice Gasnier static irqreturn_t stm32_adc_isr(int irq, void *data)
12420f883b22SFabrice Gasnier {
12430f883b22SFabrice Gasnier 	struct stm32_adc *adc = data;
1244da9b9485SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
124564ad7f64SFabrice Gasnier 	const struct stm32_adc_regspec *regs = adc->cfg->regs;
124664ad7f64SFabrice Gasnier 	u32 status = stm32_adc_readl(adc, regs->isr_eoc.reg);
12470f883b22SFabrice Gasnier 
1248cc06e67dSFabrice Gasnier 	if (status & regs->isr_ovr.mask) {
1249cc06e67dSFabrice Gasnier 		/*
1250cc06e67dSFabrice Gasnier 		 * Overrun occurred on regular conversions: data for wrong
1251cc06e67dSFabrice Gasnier 		 * channel may be read. Unconditionally disable interrupts
1252cc06e67dSFabrice Gasnier 		 * to stop processing data and print error message.
1253cc06e67dSFabrice Gasnier 		 * Restarting the capture can be done by disabling, then
1254cc06e67dSFabrice Gasnier 		 * re-enabling it (e.g. write 0, then 1 to buffer/enable).
1255cc06e67dSFabrice Gasnier 		 */
1256cc06e67dSFabrice Gasnier 		stm32_adc_ovr_irq_disable(adc);
1257cc06e67dSFabrice Gasnier 		stm32_adc_conv_irq_disable(adc);
1258cc06e67dSFabrice Gasnier 		return IRQ_WAKE_THREAD;
1259cc06e67dSFabrice Gasnier 	}
1260cc06e67dSFabrice Gasnier 
126164ad7f64SFabrice Gasnier 	if (status & regs->isr_eoc.mask) {
1262da9b9485SFabrice Gasnier 		/* Reading DR also clears EOC status flag */
126364ad7f64SFabrice Gasnier 		adc->buffer[adc->bufi] = stm32_adc_readw(adc, regs->dr);
1264da9b9485SFabrice Gasnier 		if (iio_buffer_enabled(indio_dev)) {
1265da9b9485SFabrice Gasnier 			adc->bufi++;
1266da9b9485SFabrice Gasnier 			if (adc->bufi >= adc->num_conv) {
1267da9b9485SFabrice Gasnier 				stm32_adc_conv_irq_disable(adc);
1268da9b9485SFabrice Gasnier 				iio_trigger_poll(indio_dev->trig);
1269da9b9485SFabrice Gasnier 			}
1270da9b9485SFabrice Gasnier 		} else {
12710f883b22SFabrice Gasnier 			complete(&adc->completion);
1272da9b9485SFabrice Gasnier 		}
12730f883b22SFabrice Gasnier 		return IRQ_HANDLED;
12740f883b22SFabrice Gasnier 	}
12750f883b22SFabrice Gasnier 
12760f883b22SFabrice Gasnier 	return IRQ_NONE;
12770f883b22SFabrice Gasnier }
12780f883b22SFabrice Gasnier 
1279da9b9485SFabrice Gasnier /**
1280da9b9485SFabrice Gasnier  * stm32_adc_validate_trigger() - validate trigger for stm32 adc
1281da9b9485SFabrice Gasnier  * @indio_dev: IIO device
1282da9b9485SFabrice Gasnier  * @trig: new trigger
1283da9b9485SFabrice Gasnier  *
1284da9b9485SFabrice Gasnier  * Returns: 0 if trig matches one of the triggers registered by stm32 adc
1285da9b9485SFabrice Gasnier  * driver, -EINVAL otherwise.
1286da9b9485SFabrice Gasnier  */
1287da9b9485SFabrice Gasnier static int stm32_adc_validate_trigger(struct iio_dev *indio_dev,
1288da9b9485SFabrice Gasnier 				      struct iio_trigger *trig)
1289da9b9485SFabrice Gasnier {
129064ad7f64SFabrice Gasnier 	return stm32_adc_get_trig_extsel(indio_dev, trig) < 0 ? -EINVAL : 0;
1291da9b9485SFabrice Gasnier }
1292da9b9485SFabrice Gasnier 
12932763ea05SFabrice Gasnier static int stm32_adc_set_watermark(struct iio_dev *indio_dev, unsigned int val)
12942763ea05SFabrice Gasnier {
12952763ea05SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
12962763ea05SFabrice Gasnier 	unsigned int watermark = STM32_DMA_BUFFER_SIZE / 2;
129704e491caSFabrice Gasnier 	unsigned int rx_buf_sz = STM32_DMA_BUFFER_SIZE;
12982763ea05SFabrice Gasnier 
12992763ea05SFabrice Gasnier 	/*
13002763ea05SFabrice Gasnier 	 * dma cyclic transfers are used, buffer is split into two periods.
13012763ea05SFabrice Gasnier 	 * There should be :
13022763ea05SFabrice Gasnier 	 * - always one buffer (period) dma is working on
13032763ea05SFabrice Gasnier 	 * - one buffer (period) driver can push with iio_trigger_poll().
13042763ea05SFabrice Gasnier 	 */
13052763ea05SFabrice Gasnier 	watermark = min(watermark, val * (unsigned)(sizeof(u16)));
130604e491caSFabrice Gasnier 	adc->rx_buf_sz = min(rx_buf_sz, watermark * 2 * adc->num_conv);
13072763ea05SFabrice Gasnier 
13082763ea05SFabrice Gasnier 	return 0;
13092763ea05SFabrice Gasnier }
13102763ea05SFabrice Gasnier 
1311da9b9485SFabrice Gasnier static int stm32_adc_update_scan_mode(struct iio_dev *indio_dev,
1312da9b9485SFabrice Gasnier 				      const unsigned long *scan_mask)
1313da9b9485SFabrice Gasnier {
1314da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
13159bdbb113SFabrice Gasnier 	struct device *dev = indio_dev->dev.parent;
1316da9b9485SFabrice Gasnier 	int ret;
1317da9b9485SFabrice Gasnier 
13189bdbb113SFabrice Gasnier 	ret = pm_runtime_get_sync(dev);
13199bdbb113SFabrice Gasnier 	if (ret < 0) {
13209bdbb113SFabrice Gasnier 		pm_runtime_put_noidle(dev);
13219bdbb113SFabrice Gasnier 		return ret;
13229bdbb113SFabrice Gasnier 	}
13239bdbb113SFabrice Gasnier 
1324da9b9485SFabrice Gasnier 	adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength);
1325da9b9485SFabrice Gasnier 
1326da9b9485SFabrice Gasnier 	ret = stm32_adc_conf_scan_seq(indio_dev, scan_mask);
13279bdbb113SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
13289bdbb113SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
1329da9b9485SFabrice Gasnier 
13309bdbb113SFabrice Gasnier 	return ret;
1331da9b9485SFabrice Gasnier }
1332da9b9485SFabrice Gasnier 
13330f883b22SFabrice Gasnier static int stm32_adc_of_xlate(struct iio_dev *indio_dev,
13340f883b22SFabrice Gasnier 			      const struct of_phandle_args *iiospec)
13350f883b22SFabrice Gasnier {
13360f883b22SFabrice Gasnier 	int i;
13370f883b22SFabrice Gasnier 
13380f883b22SFabrice Gasnier 	for (i = 0; i < indio_dev->num_channels; i++)
13390f883b22SFabrice Gasnier 		if (indio_dev->channels[i].channel == iiospec->args[0])
13400f883b22SFabrice Gasnier 			return i;
13410f883b22SFabrice Gasnier 
13420f883b22SFabrice Gasnier 	return -EINVAL;
13430f883b22SFabrice Gasnier }
13440f883b22SFabrice Gasnier 
13450f883b22SFabrice Gasnier /**
13460f883b22SFabrice Gasnier  * stm32_adc_debugfs_reg_access - read or write register value
13471cd92d42SFabrice Gasnier  * @indio_dev: IIO device structure
13481cd92d42SFabrice Gasnier  * @reg: register offset
13491cd92d42SFabrice Gasnier  * @writeval: value to write
13501cd92d42SFabrice Gasnier  * @readval: value to read
13510f883b22SFabrice Gasnier  *
13520f883b22SFabrice Gasnier  * To read a value from an ADC register:
13530f883b22SFabrice Gasnier  *   echo [ADC reg offset] > direct_reg_access
13540f883b22SFabrice Gasnier  *   cat direct_reg_access
13550f883b22SFabrice Gasnier  *
13560f883b22SFabrice Gasnier  * To write a value in a ADC register:
13570f883b22SFabrice Gasnier  *   echo [ADC_reg_offset] [value] > direct_reg_access
13580f883b22SFabrice Gasnier  */
13590f883b22SFabrice Gasnier static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev,
13600f883b22SFabrice Gasnier 					unsigned reg, unsigned writeval,
13610f883b22SFabrice Gasnier 					unsigned *readval)
13620f883b22SFabrice Gasnier {
13630f883b22SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
13649bdbb113SFabrice Gasnier 	struct device *dev = indio_dev->dev.parent;
13659bdbb113SFabrice Gasnier 	int ret;
13669bdbb113SFabrice Gasnier 
13679bdbb113SFabrice Gasnier 	ret = pm_runtime_get_sync(dev);
13689bdbb113SFabrice Gasnier 	if (ret < 0) {
13699bdbb113SFabrice Gasnier 		pm_runtime_put_noidle(dev);
13709bdbb113SFabrice Gasnier 		return ret;
13719bdbb113SFabrice Gasnier 	}
13720f883b22SFabrice Gasnier 
13730f883b22SFabrice Gasnier 	if (!readval)
13740f883b22SFabrice Gasnier 		stm32_adc_writel(adc, reg, writeval);
13750f883b22SFabrice Gasnier 	else
13760f883b22SFabrice Gasnier 		*readval = stm32_adc_readl(adc, reg);
13770f883b22SFabrice Gasnier 
13789bdbb113SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
13799bdbb113SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
13809bdbb113SFabrice Gasnier 
13810f883b22SFabrice Gasnier 	return 0;
13820f883b22SFabrice Gasnier }
13830f883b22SFabrice Gasnier 
13840f883b22SFabrice Gasnier static const struct iio_info stm32_adc_iio_info = {
13850f883b22SFabrice Gasnier 	.read_raw = stm32_adc_read_raw,
1386da9b9485SFabrice Gasnier 	.validate_trigger = stm32_adc_validate_trigger,
13872763ea05SFabrice Gasnier 	.hwfifo_set_watermark = stm32_adc_set_watermark,
1388da9b9485SFabrice Gasnier 	.update_scan_mode = stm32_adc_update_scan_mode,
13890f883b22SFabrice Gasnier 	.debugfs_reg_access = stm32_adc_debugfs_reg_access,
13900f883b22SFabrice Gasnier 	.of_xlate = stm32_adc_of_xlate,
13910f883b22SFabrice Gasnier };
13920f883b22SFabrice Gasnier 
13932763ea05SFabrice Gasnier static unsigned int stm32_adc_dma_residue(struct stm32_adc *adc)
13942763ea05SFabrice Gasnier {
13952763ea05SFabrice Gasnier 	struct dma_tx_state state;
13962763ea05SFabrice Gasnier 	enum dma_status status;
13972763ea05SFabrice Gasnier 
13982763ea05SFabrice Gasnier 	status = dmaengine_tx_status(adc->dma_chan,
13992763ea05SFabrice Gasnier 				     adc->dma_chan->cookie,
14002763ea05SFabrice Gasnier 				     &state);
14012763ea05SFabrice Gasnier 	if (status == DMA_IN_PROGRESS) {
14022763ea05SFabrice Gasnier 		/* Residue is size in bytes from end of buffer */
14032763ea05SFabrice Gasnier 		unsigned int i = adc->rx_buf_sz - state.residue;
14042763ea05SFabrice Gasnier 		unsigned int size;
14052763ea05SFabrice Gasnier 
14062763ea05SFabrice Gasnier 		/* Return available bytes */
14072763ea05SFabrice Gasnier 		if (i >= adc->bufi)
14082763ea05SFabrice Gasnier 			size = i - adc->bufi;
14092763ea05SFabrice Gasnier 		else
14102763ea05SFabrice Gasnier 			size = adc->rx_buf_sz + i - adc->bufi;
14112763ea05SFabrice Gasnier 
14122763ea05SFabrice Gasnier 		return size;
14132763ea05SFabrice Gasnier 	}
14142763ea05SFabrice Gasnier 
14152763ea05SFabrice Gasnier 	return 0;
14162763ea05SFabrice Gasnier }
14172763ea05SFabrice Gasnier 
14182763ea05SFabrice Gasnier static void stm32_adc_dma_buffer_done(void *data)
14192763ea05SFabrice Gasnier {
14202763ea05SFabrice Gasnier 	struct iio_dev *indio_dev = data;
1421*e2042d29SOlivier Moysan 	struct stm32_adc *adc = iio_priv(indio_dev);
1422*e2042d29SOlivier Moysan 	int residue = stm32_adc_dma_residue(adc);
14232763ea05SFabrice Gasnier 
1424*e2042d29SOlivier Moysan 	/*
1425*e2042d29SOlivier Moysan 	 * In DMA mode the trigger services of IIO are not used
1426*e2042d29SOlivier Moysan 	 * (e.g. no call to iio_trigger_poll).
1427*e2042d29SOlivier Moysan 	 * Calling irq handler associated to the hardware trigger is not
1428*e2042d29SOlivier Moysan 	 * relevant as the conversions have already been done. Data
1429*e2042d29SOlivier Moysan 	 * transfers are performed directly in DMA callback instead.
1430*e2042d29SOlivier Moysan 	 * This implementation avoids to call trigger irq handler that
1431*e2042d29SOlivier Moysan 	 * may sleep, in an atomic context (DMA irq handler context).
1432*e2042d29SOlivier Moysan 	 */
1433*e2042d29SOlivier Moysan 	dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi);
1434*e2042d29SOlivier Moysan 
1435*e2042d29SOlivier Moysan 	while (residue >= indio_dev->scan_bytes) {
1436*e2042d29SOlivier Moysan 		u16 *buffer = (u16 *)&adc->rx_buf[adc->bufi];
1437*e2042d29SOlivier Moysan 
1438*e2042d29SOlivier Moysan 		iio_push_to_buffers(indio_dev, buffer);
1439*e2042d29SOlivier Moysan 
1440*e2042d29SOlivier Moysan 		residue -= indio_dev->scan_bytes;
1441*e2042d29SOlivier Moysan 		adc->bufi += indio_dev->scan_bytes;
1442*e2042d29SOlivier Moysan 		if (adc->bufi >= adc->rx_buf_sz)
1443*e2042d29SOlivier Moysan 			adc->bufi = 0;
1444*e2042d29SOlivier Moysan 	}
14452763ea05SFabrice Gasnier }
14462763ea05SFabrice Gasnier 
14472763ea05SFabrice Gasnier static int stm32_adc_dma_start(struct iio_dev *indio_dev)
14482763ea05SFabrice Gasnier {
14492763ea05SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
14502763ea05SFabrice Gasnier 	struct dma_async_tx_descriptor *desc;
14512763ea05SFabrice Gasnier 	dma_cookie_t cookie;
14522763ea05SFabrice Gasnier 	int ret;
14532763ea05SFabrice Gasnier 
14542763ea05SFabrice Gasnier 	if (!adc->dma_chan)
14552763ea05SFabrice Gasnier 		return 0;
14562763ea05SFabrice Gasnier 
14572763ea05SFabrice Gasnier 	dev_dbg(&indio_dev->dev, "%s size=%d watermark=%d\n", __func__,
14582763ea05SFabrice Gasnier 		adc->rx_buf_sz, adc->rx_buf_sz / 2);
14592763ea05SFabrice Gasnier 
14602763ea05SFabrice Gasnier 	/* Prepare a DMA cyclic transaction */
14612763ea05SFabrice Gasnier 	desc = dmaengine_prep_dma_cyclic(adc->dma_chan,
14622763ea05SFabrice Gasnier 					 adc->rx_dma_buf,
14632763ea05SFabrice Gasnier 					 adc->rx_buf_sz, adc->rx_buf_sz / 2,
14642763ea05SFabrice Gasnier 					 DMA_DEV_TO_MEM,
14652763ea05SFabrice Gasnier 					 DMA_PREP_INTERRUPT);
14662763ea05SFabrice Gasnier 	if (!desc)
14672763ea05SFabrice Gasnier 		return -EBUSY;
14682763ea05SFabrice Gasnier 
14692763ea05SFabrice Gasnier 	desc->callback = stm32_adc_dma_buffer_done;
14702763ea05SFabrice Gasnier 	desc->callback_param = indio_dev;
14712763ea05SFabrice Gasnier 
14722763ea05SFabrice Gasnier 	cookie = dmaengine_submit(desc);
14732763ea05SFabrice Gasnier 	ret = dma_submit_error(cookie);
14742763ea05SFabrice Gasnier 	if (ret) {
1475e6afcf6cSFabrice Gasnier 		dmaengine_terminate_sync(adc->dma_chan);
14762763ea05SFabrice Gasnier 		return ret;
14772763ea05SFabrice Gasnier 	}
14782763ea05SFabrice Gasnier 
14792763ea05SFabrice Gasnier 	/* Issue pending DMA requests */
14802763ea05SFabrice Gasnier 	dma_async_issue_pending(adc->dma_chan);
14812763ea05SFabrice Gasnier 
14822763ea05SFabrice Gasnier 	return 0;
14832763ea05SFabrice Gasnier }
14842763ea05SFabrice Gasnier 
148549ad8d28SFabrice Gasnier static int __stm32_adc_buffer_postenable(struct iio_dev *indio_dev)
1486da9b9485SFabrice Gasnier {
1487da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
14889bdbb113SFabrice Gasnier 	struct device *dev = indio_dev->dev.parent;
1489da9b9485SFabrice Gasnier 	int ret;
1490da9b9485SFabrice Gasnier 
14919bdbb113SFabrice Gasnier 	ret = pm_runtime_get_sync(dev);
14929bdbb113SFabrice Gasnier 	if (ret < 0) {
14939bdbb113SFabrice Gasnier 		pm_runtime_put_noidle(dev);
149495e339b6SFabrice Gasnier 		return ret;
149595e339b6SFabrice Gasnier 	}
149695e339b6SFabrice Gasnier 
1497da9b9485SFabrice Gasnier 	ret = stm32_adc_set_trig(indio_dev, indio_dev->trig);
1498da9b9485SFabrice Gasnier 	if (ret) {
1499da9b9485SFabrice Gasnier 		dev_err(&indio_dev->dev, "Can't set trigger\n");
15009bdbb113SFabrice Gasnier 		goto err_pm_put;
1501da9b9485SFabrice Gasnier 	}
1502da9b9485SFabrice Gasnier 
15032763ea05SFabrice Gasnier 	ret = stm32_adc_dma_start(indio_dev);
15042763ea05SFabrice Gasnier 	if (ret) {
15052763ea05SFabrice Gasnier 		dev_err(&indio_dev->dev, "Can't start dma\n");
15062763ea05SFabrice Gasnier 		goto err_clr_trig;
15072763ea05SFabrice Gasnier 	}
15082763ea05SFabrice Gasnier 
1509da9b9485SFabrice Gasnier 	/* Reset adc buffer index */
1510da9b9485SFabrice Gasnier 	adc->bufi = 0;
1511da9b9485SFabrice Gasnier 
1512cc06e67dSFabrice Gasnier 	stm32_adc_ovr_irq_enable(adc);
1513cc06e67dSFabrice Gasnier 
15142763ea05SFabrice Gasnier 	if (!adc->dma_chan)
1515da9b9485SFabrice Gasnier 		stm32_adc_conv_irq_enable(adc);
15162763ea05SFabrice Gasnier 
151764ad7f64SFabrice Gasnier 	adc->cfg->start_conv(adc, !!adc->dma_chan);
1518da9b9485SFabrice Gasnier 
1519da9b9485SFabrice Gasnier 	return 0;
1520da9b9485SFabrice Gasnier 
1521da9b9485SFabrice Gasnier err_clr_trig:
1522da9b9485SFabrice Gasnier 	stm32_adc_set_trig(indio_dev, NULL);
15239bdbb113SFabrice Gasnier err_pm_put:
15249bdbb113SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
15259bdbb113SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
1526da9b9485SFabrice Gasnier 
1527da9b9485SFabrice Gasnier 	return ret;
1528da9b9485SFabrice Gasnier }
1529da9b9485SFabrice Gasnier 
153049ad8d28SFabrice Gasnier static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev)
153149ad8d28SFabrice Gasnier {
153249ad8d28SFabrice Gasnier 	int ret;
153349ad8d28SFabrice Gasnier 
153449ad8d28SFabrice Gasnier 	ret = iio_triggered_buffer_postenable(indio_dev);
153549ad8d28SFabrice Gasnier 	if (ret < 0)
153649ad8d28SFabrice Gasnier 		return ret;
153749ad8d28SFabrice Gasnier 
153849ad8d28SFabrice Gasnier 	ret = __stm32_adc_buffer_postenable(indio_dev);
153949ad8d28SFabrice Gasnier 	if (ret < 0)
154049ad8d28SFabrice Gasnier 		iio_triggered_buffer_predisable(indio_dev);
154149ad8d28SFabrice Gasnier 
154249ad8d28SFabrice Gasnier 	return ret;
154349ad8d28SFabrice Gasnier }
154449ad8d28SFabrice Gasnier 
154549ad8d28SFabrice Gasnier static void __stm32_adc_buffer_predisable(struct iio_dev *indio_dev)
1546da9b9485SFabrice Gasnier {
1547da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
15489bdbb113SFabrice Gasnier 	struct device *dev = indio_dev->dev.parent;
1549da9b9485SFabrice Gasnier 
155064ad7f64SFabrice Gasnier 	adc->cfg->stop_conv(adc);
15512763ea05SFabrice Gasnier 	if (!adc->dma_chan)
1552da9b9485SFabrice Gasnier 		stm32_adc_conv_irq_disable(adc);
1553da9b9485SFabrice Gasnier 
1554cc06e67dSFabrice Gasnier 	stm32_adc_ovr_irq_disable(adc);
1555cc06e67dSFabrice Gasnier 
15562763ea05SFabrice Gasnier 	if (adc->dma_chan)
1557e6afcf6cSFabrice Gasnier 		dmaengine_terminate_sync(adc->dma_chan);
15582763ea05SFabrice Gasnier 
1559da9b9485SFabrice Gasnier 	if (stm32_adc_set_trig(indio_dev, NULL))
1560da9b9485SFabrice Gasnier 		dev_err(&indio_dev->dev, "Can't clear trigger\n");
1561da9b9485SFabrice Gasnier 
15629bdbb113SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
15639bdbb113SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
156449ad8d28SFabrice Gasnier }
156549ad8d28SFabrice Gasnier 
156649ad8d28SFabrice Gasnier static int stm32_adc_buffer_predisable(struct iio_dev *indio_dev)
156749ad8d28SFabrice Gasnier {
156849ad8d28SFabrice Gasnier 	int ret;
156949ad8d28SFabrice Gasnier 
157049ad8d28SFabrice Gasnier 	__stm32_adc_buffer_predisable(indio_dev);
157149ad8d28SFabrice Gasnier 
157249ad8d28SFabrice Gasnier 	ret = iio_triggered_buffer_predisable(indio_dev);
157349ad8d28SFabrice Gasnier 	if (ret < 0)
157449ad8d28SFabrice Gasnier 		dev_err(&indio_dev->dev, "predisable failed\n");
157595e339b6SFabrice Gasnier 
1576da9b9485SFabrice Gasnier 	return ret;
1577da9b9485SFabrice Gasnier }
1578da9b9485SFabrice Gasnier 
1579da9b9485SFabrice Gasnier static const struct iio_buffer_setup_ops stm32_adc_buffer_setup_ops = {
1580da9b9485SFabrice Gasnier 	.postenable = &stm32_adc_buffer_postenable,
1581da9b9485SFabrice Gasnier 	.predisable = &stm32_adc_buffer_predisable,
1582da9b9485SFabrice Gasnier };
1583da9b9485SFabrice Gasnier 
1584da9b9485SFabrice Gasnier static irqreturn_t stm32_adc_trigger_handler(int irq, void *p)
1585da9b9485SFabrice Gasnier {
1586da9b9485SFabrice Gasnier 	struct iio_poll_func *pf = p;
1587da9b9485SFabrice Gasnier 	struct iio_dev *indio_dev = pf->indio_dev;
1588da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
1589da9b9485SFabrice Gasnier 
1590da9b9485SFabrice Gasnier 	dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi);
1591da9b9485SFabrice Gasnier 
15922763ea05SFabrice Gasnier 	if (!adc->dma_chan) {
1593da9b9485SFabrice Gasnier 		/* reset buffer index */
1594da9b9485SFabrice Gasnier 		adc->bufi = 0;
1595da9b9485SFabrice Gasnier 		iio_push_to_buffers_with_timestamp(indio_dev, adc->buffer,
1596da9b9485SFabrice Gasnier 						   pf->timestamp);
15972763ea05SFabrice Gasnier 	} else {
15982763ea05SFabrice Gasnier 		int residue = stm32_adc_dma_residue(adc);
15992763ea05SFabrice Gasnier 
16002763ea05SFabrice Gasnier 		while (residue >= indio_dev->scan_bytes) {
16012763ea05SFabrice Gasnier 			u16 *buffer = (u16 *)&adc->rx_buf[adc->bufi];
16022763ea05SFabrice Gasnier 
16032763ea05SFabrice Gasnier 			iio_push_to_buffers_with_timestamp(indio_dev, buffer,
16042763ea05SFabrice Gasnier 							   pf->timestamp);
16052763ea05SFabrice Gasnier 			residue -= indio_dev->scan_bytes;
16062763ea05SFabrice Gasnier 			adc->bufi += indio_dev->scan_bytes;
16072763ea05SFabrice Gasnier 			if (adc->bufi >= adc->rx_buf_sz)
16082763ea05SFabrice Gasnier 				adc->bufi = 0;
16092763ea05SFabrice Gasnier 		}
16102763ea05SFabrice Gasnier 	}
1611da9b9485SFabrice Gasnier 
1612da9b9485SFabrice Gasnier 	iio_trigger_notify_done(indio_dev->trig);
1613da9b9485SFabrice Gasnier 
1614da9b9485SFabrice Gasnier 	/* re-enable eoc irq */
16152763ea05SFabrice Gasnier 	if (!adc->dma_chan)
1616da9b9485SFabrice Gasnier 		stm32_adc_conv_irq_enable(adc);
1617da9b9485SFabrice Gasnier 
1618da9b9485SFabrice Gasnier 	return IRQ_HANDLED;
1619da9b9485SFabrice Gasnier }
1620da9b9485SFabrice Gasnier 
1621732f2dc4SFabrice Gasnier static const struct iio_chan_spec_ext_info stm32_adc_ext_info[] = {
1622732f2dc4SFabrice Gasnier 	IIO_ENUM("trigger_polarity", IIO_SHARED_BY_ALL, &stm32_adc_trig_pol),
1623732f2dc4SFabrice Gasnier 	{
1624732f2dc4SFabrice Gasnier 		.name = "trigger_polarity_available",
1625732f2dc4SFabrice Gasnier 		.shared = IIO_SHARED_BY_ALL,
1626732f2dc4SFabrice Gasnier 		.read = iio_enum_available_read,
1627732f2dc4SFabrice Gasnier 		.private = (uintptr_t)&stm32_adc_trig_pol,
1628732f2dc4SFabrice Gasnier 	},
1629732f2dc4SFabrice Gasnier 	{},
1630732f2dc4SFabrice Gasnier };
1631732f2dc4SFabrice Gasnier 
163225a85bedSFabrice Gasnier static int stm32_adc_of_get_resolution(struct iio_dev *indio_dev)
163325a85bedSFabrice Gasnier {
163425a85bedSFabrice Gasnier 	struct device_node *node = indio_dev->dev.of_node;
163525a85bedSFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
163625a85bedSFabrice Gasnier 	unsigned int i;
163725a85bedSFabrice Gasnier 	u32 res;
163825a85bedSFabrice Gasnier 
163925a85bedSFabrice Gasnier 	if (of_property_read_u32(node, "assigned-resolution-bits", &res))
164064ad7f64SFabrice Gasnier 		res = adc->cfg->adc_info->resolutions[0];
164125a85bedSFabrice Gasnier 
164264ad7f64SFabrice Gasnier 	for (i = 0; i < adc->cfg->adc_info->num_res; i++)
164364ad7f64SFabrice Gasnier 		if (res == adc->cfg->adc_info->resolutions[i])
164425a85bedSFabrice Gasnier 			break;
164564ad7f64SFabrice Gasnier 	if (i >= adc->cfg->adc_info->num_res) {
164625a85bedSFabrice Gasnier 		dev_err(&indio_dev->dev, "Bad resolution: %u bits\n", res);
164725a85bedSFabrice Gasnier 		return -EINVAL;
164825a85bedSFabrice Gasnier 	}
164925a85bedSFabrice Gasnier 
165025a85bedSFabrice Gasnier 	dev_dbg(&indio_dev->dev, "Using %u bits resolution\n", res);
165125a85bedSFabrice Gasnier 	adc->res = i;
165225a85bedSFabrice Gasnier 
165325a85bedSFabrice Gasnier 	return 0;
165425a85bedSFabrice Gasnier }
165525a85bedSFabrice Gasnier 
1656ee2ac1cdSFabrice Gasnier static void stm32_adc_smpr_init(struct stm32_adc *adc, int channel, u32 smp_ns)
1657ee2ac1cdSFabrice Gasnier {
1658ee2ac1cdSFabrice Gasnier 	const struct stm32_adc_regs *smpr = &adc->cfg->regs->smp_bits[channel];
1659ee2ac1cdSFabrice Gasnier 	u32 period_ns, shift = smpr->shift, mask = smpr->mask;
1660ee2ac1cdSFabrice Gasnier 	unsigned int smp, r = smpr->reg;
1661ee2ac1cdSFabrice Gasnier 
1662ee2ac1cdSFabrice Gasnier 	/* Determine sampling time (ADC clock cycles) */
1663ee2ac1cdSFabrice Gasnier 	period_ns = NSEC_PER_SEC / adc->common->rate;
1664ee2ac1cdSFabrice Gasnier 	for (smp = 0; smp <= STM32_ADC_MAX_SMP; smp++)
1665ee2ac1cdSFabrice Gasnier 		if ((period_ns * adc->cfg->smp_cycles[smp]) >= smp_ns)
1666ee2ac1cdSFabrice Gasnier 			break;
1667ee2ac1cdSFabrice Gasnier 	if (smp > STM32_ADC_MAX_SMP)
1668ee2ac1cdSFabrice Gasnier 		smp = STM32_ADC_MAX_SMP;
1669ee2ac1cdSFabrice Gasnier 
1670ee2ac1cdSFabrice Gasnier 	/* pre-build sampling time registers (e.g. smpr1, smpr2) */
1671ee2ac1cdSFabrice Gasnier 	adc->smpr_val[r] = (adc->smpr_val[r] & ~mask) | (smp << shift);
1672ee2ac1cdSFabrice Gasnier }
1673ee2ac1cdSFabrice Gasnier 
16740f883b22SFabrice Gasnier static void stm32_adc_chan_init_one(struct iio_dev *indio_dev,
16750bae72aaSFabrice Gasnier 				    struct iio_chan_spec *chan, u32 vinp,
16763fb2e24eSFabrice Gasnier 				    u32 vinn, int scan_index, bool differential)
16770f883b22SFabrice Gasnier {
167825a85bedSFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
16790bae72aaSFabrice Gasnier 	char *name = adc->chan_name[vinp];
168025a85bedSFabrice Gasnier 
16810bae72aaSFabrice Gasnier 	chan->type = IIO_VOLTAGE;
16820bae72aaSFabrice Gasnier 	chan->channel = vinp;
16833fb2e24eSFabrice Gasnier 	if (differential) {
16843fb2e24eSFabrice Gasnier 		chan->differential = 1;
16853fb2e24eSFabrice Gasnier 		chan->channel2 = vinn;
16863fb2e24eSFabrice Gasnier 		snprintf(name, STM32_ADC_CH_SZ, "in%d-in%d", vinp, vinn);
16873fb2e24eSFabrice Gasnier 	} else {
16880bae72aaSFabrice Gasnier 		snprintf(name, STM32_ADC_CH_SZ, "in%d", vinp);
16893fb2e24eSFabrice Gasnier 	}
16900bae72aaSFabrice Gasnier 	chan->datasheet_name = name;
16910f883b22SFabrice Gasnier 	chan->scan_index = scan_index;
16920f883b22SFabrice Gasnier 	chan->indexed = 1;
16930f883b22SFabrice Gasnier 	chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
16943fb2e24eSFabrice Gasnier 	chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
16953fb2e24eSFabrice Gasnier 					 BIT(IIO_CHAN_INFO_OFFSET);
16960f883b22SFabrice Gasnier 	chan->scan_type.sign = 'u';
169764ad7f64SFabrice Gasnier 	chan->scan_type.realbits = adc->cfg->adc_info->resolutions[adc->res];
16980f883b22SFabrice Gasnier 	chan->scan_type.storagebits = 16;
1699732f2dc4SFabrice Gasnier 	chan->ext_info = stm32_adc_ext_info;
170095e339b6SFabrice Gasnier 
170195e339b6SFabrice Gasnier 	/* pre-build selected channels mask */
170295e339b6SFabrice Gasnier 	adc->pcsel |= BIT(chan->channel);
17033fb2e24eSFabrice Gasnier 	if (differential) {
17043fb2e24eSFabrice Gasnier 		/* pre-build diff channels mask */
17053fb2e24eSFabrice Gasnier 		adc->difsel |= BIT(chan->channel);
17063fb2e24eSFabrice Gasnier 		/* Also add negative input to pre-selected channels */
17073fb2e24eSFabrice Gasnier 		adc->pcsel |= BIT(chan->channel2);
17083fb2e24eSFabrice Gasnier 	}
17090f883b22SFabrice Gasnier }
17100f883b22SFabrice Gasnier 
17110f883b22SFabrice Gasnier static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
17120f883b22SFabrice Gasnier {
17130f883b22SFabrice Gasnier 	struct device_node *node = indio_dev->dev.of_node;
171464ad7f64SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
171564ad7f64SFabrice Gasnier 	const struct stm32_adc_info *adc_info = adc->cfg->adc_info;
17163fb2e24eSFabrice Gasnier 	struct stm32_adc_diff_channel diff[STM32_ADC_CH_MAX];
17170f883b22SFabrice Gasnier 	struct property *prop;
17180f883b22SFabrice Gasnier 	const __be32 *cur;
17190f883b22SFabrice Gasnier 	struct iio_chan_spec *channels;
17203fb2e24eSFabrice Gasnier 	int scan_index = 0, num_channels = 0, num_diff = 0, ret, i;
1721ee2ac1cdSFabrice Gasnier 	u32 val, smp = 0;
17220f883b22SFabrice Gasnier 
17233fb2e24eSFabrice Gasnier 	ret = of_property_count_u32_elems(node, "st,adc-channels");
17243fb2e24eSFabrice Gasnier 	if (ret > adc_info->max_channels) {
17250f883b22SFabrice Gasnier 		dev_err(&indio_dev->dev, "Bad st,adc-channels?\n");
17263fb2e24eSFabrice Gasnier 		return -EINVAL;
17273fb2e24eSFabrice Gasnier 	} else if (ret > 0) {
17283fb2e24eSFabrice Gasnier 		num_channels += ret;
17293fb2e24eSFabrice Gasnier 	}
17303fb2e24eSFabrice Gasnier 
17313fb2e24eSFabrice Gasnier 	ret = of_property_count_elems_of_size(node, "st,adc-diff-channels",
17323fb2e24eSFabrice Gasnier 					      sizeof(*diff));
17333fb2e24eSFabrice Gasnier 	if (ret > adc_info->max_channels) {
17343fb2e24eSFabrice Gasnier 		dev_err(&indio_dev->dev, "Bad st,adc-diff-channels?\n");
17353fb2e24eSFabrice Gasnier 		return -EINVAL;
17363fb2e24eSFabrice Gasnier 	} else if (ret > 0) {
17373fb2e24eSFabrice Gasnier 		int size = ret * sizeof(*diff) / sizeof(u32);
17383fb2e24eSFabrice Gasnier 
17393fb2e24eSFabrice Gasnier 		num_diff = ret;
17403fb2e24eSFabrice Gasnier 		num_channels += ret;
17413fb2e24eSFabrice Gasnier 		ret = of_property_read_u32_array(node, "st,adc-diff-channels",
17423fb2e24eSFabrice Gasnier 						 (u32 *)diff, size);
17433fb2e24eSFabrice Gasnier 		if (ret)
17443fb2e24eSFabrice Gasnier 			return ret;
17453fb2e24eSFabrice Gasnier 	}
17463fb2e24eSFabrice Gasnier 
17473fb2e24eSFabrice Gasnier 	if (!num_channels) {
17483fb2e24eSFabrice Gasnier 		dev_err(&indio_dev->dev, "No channels configured\n");
17493fb2e24eSFabrice Gasnier 		return -ENODATA;
17500f883b22SFabrice Gasnier 	}
17510f883b22SFabrice Gasnier 
1752ee2ac1cdSFabrice Gasnier 	/* Optional sample time is provided either for each, or all channels */
1753ee2ac1cdSFabrice Gasnier 	ret = of_property_count_u32_elems(node, "st,min-sample-time-nsecs");
1754ee2ac1cdSFabrice Gasnier 	if (ret > 1 && ret != num_channels) {
1755ee2ac1cdSFabrice Gasnier 		dev_err(&indio_dev->dev, "Invalid st,min-sample-time-nsecs\n");
1756ee2ac1cdSFabrice Gasnier 		return -EINVAL;
1757ee2ac1cdSFabrice Gasnier 	}
1758ee2ac1cdSFabrice Gasnier 
17590f883b22SFabrice Gasnier 	channels = devm_kcalloc(&indio_dev->dev, num_channels,
17600f883b22SFabrice Gasnier 				sizeof(struct iio_chan_spec), GFP_KERNEL);
17610f883b22SFabrice Gasnier 	if (!channels)
17620f883b22SFabrice Gasnier 		return -ENOMEM;
17630f883b22SFabrice Gasnier 
17640f883b22SFabrice Gasnier 	of_property_for_each_u32(node, "st,adc-channels", prop, cur, val) {
176564ad7f64SFabrice Gasnier 		if (val >= adc_info->max_channels) {
17660f883b22SFabrice Gasnier 			dev_err(&indio_dev->dev, "Invalid channel %d\n", val);
17670f883b22SFabrice Gasnier 			return -EINVAL;
17680f883b22SFabrice Gasnier 		}
1769ee2ac1cdSFabrice Gasnier 
17703fb2e24eSFabrice Gasnier 		/* Channel can't be configured both as single-ended & diff */
17713fb2e24eSFabrice Gasnier 		for (i = 0; i < num_diff; i++) {
17723fb2e24eSFabrice Gasnier 			if (val == diff[i].vinp) {
17733fb2e24eSFabrice Gasnier 				dev_err(&indio_dev->dev,
17743fb2e24eSFabrice Gasnier 					"channel %d miss-configured\n",	val);
17753fb2e24eSFabrice Gasnier 				return -EINVAL;
17763fb2e24eSFabrice Gasnier 			}
17773fb2e24eSFabrice Gasnier 		}
17783fb2e24eSFabrice Gasnier 		stm32_adc_chan_init_one(indio_dev, &channels[scan_index], val,
17793fb2e24eSFabrice Gasnier 					0, scan_index, false);
17803fb2e24eSFabrice Gasnier 		scan_index++;
17813fb2e24eSFabrice Gasnier 	}
17823fb2e24eSFabrice Gasnier 
17833fb2e24eSFabrice Gasnier 	for (i = 0; i < num_diff; i++) {
17843fb2e24eSFabrice Gasnier 		if (diff[i].vinp >= adc_info->max_channels ||
17853fb2e24eSFabrice Gasnier 		    diff[i].vinn >= adc_info->max_channels) {
17863fb2e24eSFabrice Gasnier 			dev_err(&indio_dev->dev, "Invalid channel in%d-in%d\n",
17873fb2e24eSFabrice Gasnier 				diff[i].vinp, diff[i].vinn);
17883fb2e24eSFabrice Gasnier 			return -EINVAL;
17893fb2e24eSFabrice Gasnier 		}
17903fb2e24eSFabrice Gasnier 		stm32_adc_chan_init_one(indio_dev, &channels[scan_index],
17913fb2e24eSFabrice Gasnier 					diff[i].vinp, diff[i].vinn, scan_index,
17923fb2e24eSFabrice Gasnier 					true);
17933fb2e24eSFabrice Gasnier 		scan_index++;
17943fb2e24eSFabrice Gasnier 	}
17953fb2e24eSFabrice Gasnier 
17963fb2e24eSFabrice Gasnier 	for (i = 0; i < scan_index; i++) {
1797ee2ac1cdSFabrice Gasnier 		/*
1798ee2ac1cdSFabrice Gasnier 		 * Using of_property_read_u32_index(), smp value will only be
1799ee2ac1cdSFabrice Gasnier 		 * modified if valid u32 value can be decoded. This allows to
1800ee2ac1cdSFabrice Gasnier 		 * get either no value, 1 shared value for all indexes, or one
1801ee2ac1cdSFabrice Gasnier 		 * value per channel.
1802ee2ac1cdSFabrice Gasnier 		 */
1803ee2ac1cdSFabrice Gasnier 		of_property_read_u32_index(node, "st,min-sample-time-nsecs",
18043fb2e24eSFabrice Gasnier 					   i, &smp);
18053fb2e24eSFabrice Gasnier 		/* Prepare sampling time settings */
18063fb2e24eSFabrice Gasnier 		stm32_adc_smpr_init(adc, channels[i].channel, smp);
18070f883b22SFabrice Gasnier 	}
18080f883b22SFabrice Gasnier 
18090f883b22SFabrice Gasnier 	indio_dev->num_channels = scan_index;
18100f883b22SFabrice Gasnier 	indio_dev->channels = channels;
18110f883b22SFabrice Gasnier 
18120f883b22SFabrice Gasnier 	return 0;
18130f883b22SFabrice Gasnier }
18140f883b22SFabrice Gasnier 
18152763ea05SFabrice Gasnier static int stm32_adc_dma_request(struct iio_dev *indio_dev)
18162763ea05SFabrice Gasnier {
18172763ea05SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
18182763ea05SFabrice Gasnier 	struct dma_slave_config config;
18192763ea05SFabrice Gasnier 	int ret;
18202763ea05SFabrice Gasnier 
1821735404b8SPeter Ujfalusi 	adc->dma_chan = dma_request_chan(&indio_dev->dev, "rx");
1822735404b8SPeter Ujfalusi 	if (IS_ERR(adc->dma_chan)) {
1823735404b8SPeter Ujfalusi 		ret = PTR_ERR(adc->dma_chan);
1824735404b8SPeter Ujfalusi 		if (ret != -ENODEV) {
1825735404b8SPeter Ujfalusi 			if (ret != -EPROBE_DEFER)
1826735404b8SPeter Ujfalusi 				dev_err(&indio_dev->dev,
1827735404b8SPeter Ujfalusi 					"DMA channel request failed with %d\n",
1828735404b8SPeter Ujfalusi 					ret);
1829735404b8SPeter Ujfalusi 			return ret;
1830735404b8SPeter Ujfalusi 		}
1831735404b8SPeter Ujfalusi 
1832735404b8SPeter Ujfalusi 		/* DMA is optional: fall back to IRQ mode */
1833735404b8SPeter Ujfalusi 		adc->dma_chan = NULL;
18342763ea05SFabrice Gasnier 		return 0;
1835735404b8SPeter Ujfalusi 	}
18362763ea05SFabrice Gasnier 
18372763ea05SFabrice Gasnier 	adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev,
18382763ea05SFabrice Gasnier 					 STM32_DMA_BUFFER_SIZE,
18392763ea05SFabrice Gasnier 					 &adc->rx_dma_buf, GFP_KERNEL);
18402763ea05SFabrice Gasnier 	if (!adc->rx_buf) {
18412763ea05SFabrice Gasnier 		ret = -ENOMEM;
18422763ea05SFabrice Gasnier 		goto err_release;
18432763ea05SFabrice Gasnier 	}
18442763ea05SFabrice Gasnier 
18452763ea05SFabrice Gasnier 	/* Configure DMA channel to read data register */
18462763ea05SFabrice Gasnier 	memset(&config, 0, sizeof(config));
18472763ea05SFabrice Gasnier 	config.src_addr = (dma_addr_t)adc->common->phys_base;
184864ad7f64SFabrice Gasnier 	config.src_addr += adc->offset + adc->cfg->regs->dr;
18492763ea05SFabrice Gasnier 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
18502763ea05SFabrice Gasnier 
18512763ea05SFabrice Gasnier 	ret = dmaengine_slave_config(adc->dma_chan, &config);
18522763ea05SFabrice Gasnier 	if (ret)
18532763ea05SFabrice Gasnier 		goto err_free;
18542763ea05SFabrice Gasnier 
18552763ea05SFabrice Gasnier 	return 0;
18562763ea05SFabrice Gasnier 
18572763ea05SFabrice Gasnier err_free:
18582763ea05SFabrice Gasnier 	dma_free_coherent(adc->dma_chan->device->dev, STM32_DMA_BUFFER_SIZE,
18592763ea05SFabrice Gasnier 			  adc->rx_buf, adc->rx_dma_buf);
18602763ea05SFabrice Gasnier err_release:
18612763ea05SFabrice Gasnier 	dma_release_channel(adc->dma_chan);
18622763ea05SFabrice Gasnier 
18632763ea05SFabrice Gasnier 	return ret;
18642763ea05SFabrice Gasnier }
18652763ea05SFabrice Gasnier 
18660f883b22SFabrice Gasnier static int stm32_adc_probe(struct platform_device *pdev)
18670f883b22SFabrice Gasnier {
18680f883b22SFabrice Gasnier 	struct iio_dev *indio_dev;
186964ad7f64SFabrice Gasnier 	struct device *dev = &pdev->dev;
1870*e2042d29SOlivier Moysan 	irqreturn_t (*handler)(int irq, void *p) = NULL;
18710f883b22SFabrice Gasnier 	struct stm32_adc *adc;
18720f883b22SFabrice Gasnier 	int ret;
18730f883b22SFabrice Gasnier 
18740f883b22SFabrice Gasnier 	if (!pdev->dev.of_node)
18750f883b22SFabrice Gasnier 		return -ENODEV;
18760f883b22SFabrice Gasnier 
18770f883b22SFabrice Gasnier 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
18780f883b22SFabrice Gasnier 	if (!indio_dev)
18790f883b22SFabrice Gasnier 		return -ENOMEM;
18800f883b22SFabrice Gasnier 
18810f883b22SFabrice Gasnier 	adc = iio_priv(indio_dev);
18820f883b22SFabrice Gasnier 	adc->common = dev_get_drvdata(pdev->dev.parent);
18830f883b22SFabrice Gasnier 	spin_lock_init(&adc->lock);
18840f883b22SFabrice Gasnier 	init_completion(&adc->completion);
188564ad7f64SFabrice Gasnier 	adc->cfg = (const struct stm32_adc_cfg *)
188664ad7f64SFabrice Gasnier 		of_match_device(dev->driver->of_match_table, dev)->data;
18870f883b22SFabrice Gasnier 
18880f883b22SFabrice Gasnier 	indio_dev->name = dev_name(&pdev->dev);
18890f883b22SFabrice Gasnier 	indio_dev->dev.parent = &pdev->dev;
18900f883b22SFabrice Gasnier 	indio_dev->dev.of_node = pdev->dev.of_node;
18910f883b22SFabrice Gasnier 	indio_dev->info = &stm32_adc_iio_info;
1892f0b638a7SFabrice Gasnier 	indio_dev->modes = INDIO_DIRECT_MODE | INDIO_HARDWARE_TRIGGERED;
18930f883b22SFabrice Gasnier 
18940f883b22SFabrice Gasnier 	platform_set_drvdata(pdev, adc);
18950f883b22SFabrice Gasnier 
18960f883b22SFabrice Gasnier 	ret = of_property_read_u32(pdev->dev.of_node, "reg", &adc->offset);
18970f883b22SFabrice Gasnier 	if (ret != 0) {
18980f883b22SFabrice Gasnier 		dev_err(&pdev->dev, "missing reg property\n");
18990f883b22SFabrice Gasnier 		return -EINVAL;
19000f883b22SFabrice Gasnier 	}
19010f883b22SFabrice Gasnier 
19020f883b22SFabrice Gasnier 	adc->irq = platform_get_irq(pdev, 0);
19037c279229SStephen Boyd 	if (adc->irq < 0)
19040f883b22SFabrice Gasnier 		return adc->irq;
19050f883b22SFabrice Gasnier 
1906cc06e67dSFabrice Gasnier 	ret = devm_request_threaded_irq(&pdev->dev, adc->irq, stm32_adc_isr,
1907cc06e67dSFabrice Gasnier 					stm32_adc_threaded_isr,
19080f883b22SFabrice Gasnier 					0, pdev->name, adc);
19090f883b22SFabrice Gasnier 	if (ret) {
19100f883b22SFabrice Gasnier 		dev_err(&pdev->dev, "failed to request IRQ\n");
19110f883b22SFabrice Gasnier 		return ret;
19120f883b22SFabrice Gasnier 	}
19130f883b22SFabrice Gasnier 
19140f883b22SFabrice Gasnier 	adc->clk = devm_clk_get(&pdev->dev, NULL);
19150f883b22SFabrice Gasnier 	if (IS_ERR(adc->clk)) {
1916204a6a25SFabrice Gasnier 		ret = PTR_ERR(adc->clk);
1917204a6a25SFabrice Gasnier 		if (ret == -ENOENT && !adc->cfg->clk_required) {
1918204a6a25SFabrice Gasnier 			adc->clk = NULL;
1919204a6a25SFabrice Gasnier 		} else {
19200f883b22SFabrice Gasnier 			dev_err(&pdev->dev, "Can't get clock\n");
1921204a6a25SFabrice Gasnier 			return ret;
1922204a6a25SFabrice Gasnier 		}
19230f883b22SFabrice Gasnier 	}
19240f883b22SFabrice Gasnier 
192525a85bedSFabrice Gasnier 	ret = stm32_adc_of_get_resolution(indio_dev);
192625a85bedSFabrice Gasnier 	if (ret < 0)
19279bdbb113SFabrice Gasnier 		return ret;
192825a85bedSFabrice Gasnier 
19290f883b22SFabrice Gasnier 	ret = stm32_adc_chan_of_init(indio_dev);
19300f883b22SFabrice Gasnier 	if (ret < 0)
19319bdbb113SFabrice Gasnier 		return ret;
19320f883b22SFabrice Gasnier 
19332763ea05SFabrice Gasnier 	ret = stm32_adc_dma_request(indio_dev);
19342763ea05SFabrice Gasnier 	if (ret < 0)
19359bdbb113SFabrice Gasnier 		return ret;
19362763ea05SFabrice Gasnier 
1937*e2042d29SOlivier Moysan 	if (!adc->dma_chan)
1938*e2042d29SOlivier Moysan 		handler = &stm32_adc_trigger_handler;
1939*e2042d29SOlivier Moysan 
1940da9b9485SFabrice Gasnier 	ret = iio_triggered_buffer_setup(indio_dev,
1941*e2042d29SOlivier Moysan 					 &iio_pollfunc_store_time, handler,
1942da9b9485SFabrice Gasnier 					 &stm32_adc_buffer_setup_ops);
19430f883b22SFabrice Gasnier 	if (ret) {
1944da9b9485SFabrice Gasnier 		dev_err(&pdev->dev, "buffer setup failed\n");
19452763ea05SFabrice Gasnier 		goto err_dma_disable;
19460f883b22SFabrice Gasnier 	}
19470f883b22SFabrice Gasnier 
19489bdbb113SFabrice Gasnier 	/* Get stm32-adc-core PM online */
19499bdbb113SFabrice Gasnier 	pm_runtime_get_noresume(dev);
19509bdbb113SFabrice Gasnier 	pm_runtime_set_active(dev);
19519bdbb113SFabrice Gasnier 	pm_runtime_set_autosuspend_delay(dev, STM32_ADC_HW_STOP_DELAY_MS);
19529bdbb113SFabrice Gasnier 	pm_runtime_use_autosuspend(dev);
19539bdbb113SFabrice Gasnier 	pm_runtime_enable(dev);
19549bdbb113SFabrice Gasnier 
19559bdbb113SFabrice Gasnier 	ret = stm32_adc_hw_start(dev);
19569bdbb113SFabrice Gasnier 	if (ret)
19579bdbb113SFabrice Gasnier 		goto err_buffer_cleanup;
19589bdbb113SFabrice Gasnier 
1959da9b9485SFabrice Gasnier 	ret = iio_device_register(indio_dev);
1960da9b9485SFabrice Gasnier 	if (ret) {
1961da9b9485SFabrice Gasnier 		dev_err(&pdev->dev, "iio dev register failed\n");
19629bdbb113SFabrice Gasnier 		goto err_hw_stop;
1963da9b9485SFabrice Gasnier 	}
1964da9b9485SFabrice Gasnier 
19659bdbb113SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
19669bdbb113SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
19679bdbb113SFabrice Gasnier 
19680f883b22SFabrice Gasnier 	return 0;
19690f883b22SFabrice Gasnier 
19709bdbb113SFabrice Gasnier err_hw_stop:
19719bdbb113SFabrice Gasnier 	stm32_adc_hw_stop(dev);
19729bdbb113SFabrice Gasnier 
1973da9b9485SFabrice Gasnier err_buffer_cleanup:
19749bdbb113SFabrice Gasnier 	pm_runtime_disable(dev);
19759bdbb113SFabrice Gasnier 	pm_runtime_set_suspended(dev);
19769bdbb113SFabrice Gasnier 	pm_runtime_put_noidle(dev);
1977da9b9485SFabrice Gasnier 	iio_triggered_buffer_cleanup(indio_dev);
1978da9b9485SFabrice Gasnier 
19792763ea05SFabrice Gasnier err_dma_disable:
19802763ea05SFabrice Gasnier 	if (adc->dma_chan) {
19812763ea05SFabrice Gasnier 		dma_free_coherent(adc->dma_chan->device->dev,
19822763ea05SFabrice Gasnier 				  STM32_DMA_BUFFER_SIZE,
19832763ea05SFabrice Gasnier 				  adc->rx_buf, adc->rx_dma_buf);
19842763ea05SFabrice Gasnier 		dma_release_channel(adc->dma_chan);
19852763ea05SFabrice Gasnier 	}
19860f883b22SFabrice Gasnier 
19870f883b22SFabrice Gasnier 	return ret;
19880f883b22SFabrice Gasnier }
19890f883b22SFabrice Gasnier 
19900f883b22SFabrice Gasnier static int stm32_adc_remove(struct platform_device *pdev)
19910f883b22SFabrice Gasnier {
19920f883b22SFabrice Gasnier 	struct stm32_adc *adc = platform_get_drvdata(pdev);
19930f883b22SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
19940f883b22SFabrice Gasnier 
19959bdbb113SFabrice Gasnier 	pm_runtime_get_sync(&pdev->dev);
19960f883b22SFabrice Gasnier 	iio_device_unregister(indio_dev);
19979bdbb113SFabrice Gasnier 	stm32_adc_hw_stop(&pdev->dev);
19989bdbb113SFabrice Gasnier 	pm_runtime_disable(&pdev->dev);
19999bdbb113SFabrice Gasnier 	pm_runtime_set_suspended(&pdev->dev);
20009bdbb113SFabrice Gasnier 	pm_runtime_put_noidle(&pdev->dev);
2001da9b9485SFabrice Gasnier 	iio_triggered_buffer_cleanup(indio_dev);
20022763ea05SFabrice Gasnier 	if (adc->dma_chan) {
20032763ea05SFabrice Gasnier 		dma_free_coherent(adc->dma_chan->device->dev,
20042763ea05SFabrice Gasnier 				  STM32_DMA_BUFFER_SIZE,
20052763ea05SFabrice Gasnier 				  adc->rx_buf, adc->rx_dma_buf);
20062763ea05SFabrice Gasnier 		dma_release_channel(adc->dma_chan);
20072763ea05SFabrice Gasnier 	}
20080f883b22SFabrice Gasnier 
20090f883b22SFabrice Gasnier 	return 0;
20100f883b22SFabrice Gasnier }
20110f883b22SFabrice Gasnier 
201249ad8d28SFabrice Gasnier #if defined(CONFIG_PM_SLEEP)
201349ad8d28SFabrice Gasnier static int stm32_adc_suspend(struct device *dev)
201449ad8d28SFabrice Gasnier {
201549ad8d28SFabrice Gasnier 	struct stm32_adc *adc = dev_get_drvdata(dev);
201649ad8d28SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
201749ad8d28SFabrice Gasnier 
201849ad8d28SFabrice Gasnier 	if (iio_buffer_enabled(indio_dev))
201949ad8d28SFabrice Gasnier 		__stm32_adc_buffer_predisable(indio_dev);
202049ad8d28SFabrice Gasnier 
202149ad8d28SFabrice Gasnier 	return pm_runtime_force_suspend(dev);
202249ad8d28SFabrice Gasnier }
202349ad8d28SFabrice Gasnier 
202449ad8d28SFabrice Gasnier static int stm32_adc_resume(struct device *dev)
202549ad8d28SFabrice Gasnier {
202649ad8d28SFabrice Gasnier 	struct stm32_adc *adc = dev_get_drvdata(dev);
202749ad8d28SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
202849ad8d28SFabrice Gasnier 	int ret;
202949ad8d28SFabrice Gasnier 
203049ad8d28SFabrice Gasnier 	ret = pm_runtime_force_resume(dev);
203149ad8d28SFabrice Gasnier 	if (ret < 0)
203249ad8d28SFabrice Gasnier 		return ret;
203349ad8d28SFabrice Gasnier 
203449ad8d28SFabrice Gasnier 	if (!iio_buffer_enabled(indio_dev))
203549ad8d28SFabrice Gasnier 		return 0;
203649ad8d28SFabrice Gasnier 
203749ad8d28SFabrice Gasnier 	ret = stm32_adc_update_scan_mode(indio_dev,
203849ad8d28SFabrice Gasnier 					 indio_dev->active_scan_mask);
203949ad8d28SFabrice Gasnier 	if (ret < 0)
204049ad8d28SFabrice Gasnier 		return ret;
204149ad8d28SFabrice Gasnier 
204249ad8d28SFabrice Gasnier 	return __stm32_adc_buffer_postenable(indio_dev);
204349ad8d28SFabrice Gasnier }
204449ad8d28SFabrice Gasnier #endif
204549ad8d28SFabrice Gasnier 
20469bdbb113SFabrice Gasnier #if defined(CONFIG_PM)
20479bdbb113SFabrice Gasnier static int stm32_adc_runtime_suspend(struct device *dev)
20489bdbb113SFabrice Gasnier {
20499bdbb113SFabrice Gasnier 	return stm32_adc_hw_stop(dev);
20509bdbb113SFabrice Gasnier }
20519bdbb113SFabrice Gasnier 
20529bdbb113SFabrice Gasnier static int stm32_adc_runtime_resume(struct device *dev)
20539bdbb113SFabrice Gasnier {
20549bdbb113SFabrice Gasnier 	return stm32_adc_hw_start(dev);
20559bdbb113SFabrice Gasnier }
20569bdbb113SFabrice Gasnier #endif
20579bdbb113SFabrice Gasnier 
20589bdbb113SFabrice Gasnier static const struct dev_pm_ops stm32_adc_pm_ops = {
205949ad8d28SFabrice Gasnier 	SET_SYSTEM_SLEEP_PM_OPS(stm32_adc_suspend, stm32_adc_resume)
20609bdbb113SFabrice Gasnier 	SET_RUNTIME_PM_OPS(stm32_adc_runtime_suspend, stm32_adc_runtime_resume,
20619bdbb113SFabrice Gasnier 			   NULL)
20629bdbb113SFabrice Gasnier };
20639bdbb113SFabrice Gasnier 
206464ad7f64SFabrice Gasnier static const struct stm32_adc_cfg stm32f4_adc_cfg = {
206564ad7f64SFabrice Gasnier 	.regs = &stm32f4_adc_regspec,
206664ad7f64SFabrice Gasnier 	.adc_info = &stm32f4_adc_info,
206764ad7f64SFabrice Gasnier 	.trigs = stm32f4_adc_trigs,
2068204a6a25SFabrice Gasnier 	.clk_required = true,
206964ad7f64SFabrice Gasnier 	.start_conv = stm32f4_adc_start_conv,
207064ad7f64SFabrice Gasnier 	.stop_conv = stm32f4_adc_stop_conv,
2071ee2ac1cdSFabrice Gasnier 	.smp_cycles = stm32f4_adc_smp_cycles,
207264ad7f64SFabrice Gasnier };
207364ad7f64SFabrice Gasnier 
207495e339b6SFabrice Gasnier static const struct stm32_adc_cfg stm32h7_adc_cfg = {
207595e339b6SFabrice Gasnier 	.regs = &stm32h7_adc_regspec,
207695e339b6SFabrice Gasnier 	.adc_info = &stm32h7_adc_info,
207795e339b6SFabrice Gasnier 	.trigs = stm32h7_adc_trigs,
207895e339b6SFabrice Gasnier 	.start_conv = stm32h7_adc_start_conv,
207995e339b6SFabrice Gasnier 	.stop_conv = stm32h7_adc_stop_conv,
208095e339b6SFabrice Gasnier 	.prepare = stm32h7_adc_prepare,
208195e339b6SFabrice Gasnier 	.unprepare = stm32h7_adc_unprepare,
2082ee2ac1cdSFabrice Gasnier 	.smp_cycles = stm32h7_adc_smp_cycles,
208395e339b6SFabrice Gasnier };
208495e339b6SFabrice Gasnier 
2085d58c67d1SFabrice Gasnier static const struct stm32_adc_cfg stm32mp1_adc_cfg = {
2086d58c67d1SFabrice Gasnier 	.regs = &stm32h7_adc_regspec,
2087d58c67d1SFabrice Gasnier 	.adc_info = &stm32h7_adc_info,
2088d58c67d1SFabrice Gasnier 	.trigs = stm32h7_adc_trigs,
2089d58c67d1SFabrice Gasnier 	.has_vregready = true,
2090d58c67d1SFabrice Gasnier 	.start_conv = stm32h7_adc_start_conv,
2091d58c67d1SFabrice Gasnier 	.stop_conv = stm32h7_adc_stop_conv,
2092d58c67d1SFabrice Gasnier 	.prepare = stm32h7_adc_prepare,
2093d58c67d1SFabrice Gasnier 	.unprepare = stm32h7_adc_unprepare,
2094d58c67d1SFabrice Gasnier 	.smp_cycles = stm32h7_adc_smp_cycles,
2095d58c67d1SFabrice Gasnier };
2096d58c67d1SFabrice Gasnier 
20970f883b22SFabrice Gasnier static const struct of_device_id stm32_adc_of_match[] = {
209864ad7f64SFabrice Gasnier 	{ .compatible = "st,stm32f4-adc", .data = (void *)&stm32f4_adc_cfg },
209995e339b6SFabrice Gasnier 	{ .compatible = "st,stm32h7-adc", .data = (void *)&stm32h7_adc_cfg },
2100d58c67d1SFabrice Gasnier 	{ .compatible = "st,stm32mp1-adc", .data = (void *)&stm32mp1_adc_cfg },
21010f883b22SFabrice Gasnier 	{},
21020f883b22SFabrice Gasnier };
21030f883b22SFabrice Gasnier MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
21040f883b22SFabrice Gasnier 
21050f883b22SFabrice Gasnier static struct platform_driver stm32_adc_driver = {
21060f883b22SFabrice Gasnier 	.probe = stm32_adc_probe,
21070f883b22SFabrice Gasnier 	.remove = stm32_adc_remove,
21080f883b22SFabrice Gasnier 	.driver = {
21090f883b22SFabrice Gasnier 		.name = "stm32-adc",
21100f883b22SFabrice Gasnier 		.of_match_table = stm32_adc_of_match,
21119bdbb113SFabrice Gasnier 		.pm = &stm32_adc_pm_ops,
21120f883b22SFabrice Gasnier 	},
21130f883b22SFabrice Gasnier };
21140f883b22SFabrice Gasnier module_platform_driver(stm32_adc_driver);
21150f883b22SFabrice Gasnier 
21160f883b22SFabrice Gasnier MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
21170f883b22SFabrice Gasnier MODULE_DESCRIPTION("STMicroelectronics STM32 ADC IIO driver");
21180f883b22SFabrice Gasnier MODULE_LICENSE("GPL v2");
21190f883b22SFabrice Gasnier MODULE_ALIAS("platform:stm32-adc");
2120