xref: /linux/drivers/iio/adc/stm32-adc.c (revision da9b948514c36da480cc15f7c75f51417f882175)
10f883b22SFabrice Gasnier /*
20f883b22SFabrice Gasnier  * This file is part of STM32 ADC driver
30f883b22SFabrice Gasnier  *
40f883b22SFabrice Gasnier  * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
50f883b22SFabrice Gasnier  * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
60f883b22SFabrice Gasnier  *
70f883b22SFabrice Gasnier  * License type: GPLv2
80f883b22SFabrice Gasnier  *
90f883b22SFabrice Gasnier  * This program is free software; you can redistribute it and/or modify it
100f883b22SFabrice Gasnier  * under the terms of the GNU General Public License version 2 as published by
110f883b22SFabrice Gasnier  * the Free Software Foundation.
120f883b22SFabrice Gasnier  *
130f883b22SFabrice Gasnier  * This program is distributed in the hope that it will be useful, but
140f883b22SFabrice Gasnier  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
150f883b22SFabrice Gasnier  * or FITNESS FOR A PARTICULAR PURPOSE.
160f883b22SFabrice Gasnier  * See the GNU General Public License for more details.
170f883b22SFabrice Gasnier  *
180f883b22SFabrice Gasnier  * You should have received a copy of the GNU General Public License along with
190f883b22SFabrice Gasnier  * this program. If not, see <http://www.gnu.org/licenses/>.
200f883b22SFabrice Gasnier  */
210f883b22SFabrice Gasnier 
220f883b22SFabrice Gasnier #include <linux/clk.h>
230f883b22SFabrice Gasnier #include <linux/delay.h>
240f883b22SFabrice Gasnier #include <linux/iio/iio.h>
25*da9b9485SFabrice Gasnier #include <linux/iio/buffer.h>
26*da9b9485SFabrice Gasnier #include <linux/iio/trigger.h>
27*da9b9485SFabrice Gasnier #include <linux/iio/trigger_consumer.h>
28*da9b9485SFabrice Gasnier #include <linux/iio/triggered_buffer.h>
290f883b22SFabrice Gasnier #include <linux/interrupt.h>
300f883b22SFabrice Gasnier #include <linux/io.h>
310f883b22SFabrice Gasnier #include <linux/module.h>
320f883b22SFabrice Gasnier #include <linux/platform_device.h>
330f883b22SFabrice Gasnier #include <linux/of.h>
340f883b22SFabrice Gasnier 
350f883b22SFabrice Gasnier #include "stm32-adc-core.h"
360f883b22SFabrice Gasnier 
370f883b22SFabrice Gasnier /* STM32F4 - Registers for each ADC instance */
380f883b22SFabrice Gasnier #define STM32F4_ADC_SR			0x00
390f883b22SFabrice Gasnier #define STM32F4_ADC_CR1			0x04
400f883b22SFabrice Gasnier #define STM32F4_ADC_CR2			0x08
410f883b22SFabrice Gasnier #define STM32F4_ADC_SMPR1		0x0C
420f883b22SFabrice Gasnier #define STM32F4_ADC_SMPR2		0x10
430f883b22SFabrice Gasnier #define STM32F4_ADC_HTR			0x24
440f883b22SFabrice Gasnier #define STM32F4_ADC_LTR			0x28
450f883b22SFabrice Gasnier #define STM32F4_ADC_SQR1		0x2C
460f883b22SFabrice Gasnier #define STM32F4_ADC_SQR2		0x30
470f883b22SFabrice Gasnier #define STM32F4_ADC_SQR3		0x34
480f883b22SFabrice Gasnier #define STM32F4_ADC_JSQR		0x38
490f883b22SFabrice Gasnier #define STM32F4_ADC_JDR1		0x3C
500f883b22SFabrice Gasnier #define STM32F4_ADC_JDR2		0x40
510f883b22SFabrice Gasnier #define STM32F4_ADC_JDR3		0x44
520f883b22SFabrice Gasnier #define STM32F4_ADC_JDR4		0x48
530f883b22SFabrice Gasnier #define STM32F4_ADC_DR			0x4C
540f883b22SFabrice Gasnier 
550f883b22SFabrice Gasnier /* STM32F4_ADC_SR - bit fields */
560f883b22SFabrice Gasnier #define STM32F4_STRT			BIT(4)
570f883b22SFabrice Gasnier #define STM32F4_EOC			BIT(1)
580f883b22SFabrice Gasnier 
590f883b22SFabrice Gasnier /* STM32F4_ADC_CR1 - bit fields */
600f883b22SFabrice Gasnier #define STM32F4_SCAN			BIT(8)
610f883b22SFabrice Gasnier #define STM32F4_EOCIE			BIT(5)
620f883b22SFabrice Gasnier 
630f883b22SFabrice Gasnier /* STM32F4_ADC_CR2 - bit fields */
640f883b22SFabrice Gasnier #define STM32F4_SWSTART			BIT(30)
65*da9b9485SFabrice Gasnier #define STM32F4_EXTEN_SHIFT		28
660f883b22SFabrice Gasnier #define STM32F4_EXTEN_MASK		GENMASK(29, 28)
67*da9b9485SFabrice Gasnier #define STM32F4_EXTSEL_SHIFT		24
68*da9b9485SFabrice Gasnier #define STM32F4_EXTSEL_MASK		GENMASK(27, 24)
690f883b22SFabrice Gasnier #define STM32F4_EOCS			BIT(10)
700f883b22SFabrice Gasnier #define STM32F4_ADON			BIT(0)
710f883b22SFabrice Gasnier 
72*da9b9485SFabrice Gasnier #define STM32_ADC_MAX_SQ		16	/* SQ1..SQ16 */
730f883b22SFabrice Gasnier #define STM32_ADC_TIMEOUT_US		100000
740f883b22SFabrice Gasnier #define STM32_ADC_TIMEOUT	(msecs_to_jiffies(STM32_ADC_TIMEOUT_US / 1000))
750f883b22SFabrice Gasnier 
76*da9b9485SFabrice Gasnier /* External trigger enable */
77*da9b9485SFabrice Gasnier enum stm32_adc_exten {
78*da9b9485SFabrice Gasnier 	STM32_EXTEN_SWTRIG,
79*da9b9485SFabrice Gasnier 	STM32_EXTEN_HWTRIG_RISING_EDGE,
80*da9b9485SFabrice Gasnier 	STM32_EXTEN_HWTRIG_FALLING_EDGE,
81*da9b9485SFabrice Gasnier 	STM32_EXTEN_HWTRIG_BOTH_EDGES,
82*da9b9485SFabrice Gasnier };
83*da9b9485SFabrice Gasnier 
84*da9b9485SFabrice Gasnier /**
85*da9b9485SFabrice Gasnier  * stm32_adc_regs - stm32 ADC misc registers & bitfield desc
86*da9b9485SFabrice Gasnier  * @reg:		register offset
87*da9b9485SFabrice Gasnier  * @mask:		bitfield mask
88*da9b9485SFabrice Gasnier  * @shift:		left shift
89*da9b9485SFabrice Gasnier  */
90*da9b9485SFabrice Gasnier struct stm32_adc_regs {
91*da9b9485SFabrice Gasnier 	int reg;
92*da9b9485SFabrice Gasnier 	int mask;
93*da9b9485SFabrice Gasnier 	int shift;
94*da9b9485SFabrice Gasnier };
95*da9b9485SFabrice Gasnier 
960f883b22SFabrice Gasnier /**
970f883b22SFabrice Gasnier  * struct stm32_adc - private data of each ADC IIO instance
980f883b22SFabrice Gasnier  * @common:		reference to ADC block common data
990f883b22SFabrice Gasnier  * @offset:		ADC instance register offset in ADC block
1000f883b22SFabrice Gasnier  * @completion:		end of single conversion completion
1010f883b22SFabrice Gasnier  * @buffer:		data buffer
1020f883b22SFabrice Gasnier  * @clk:		clock for this adc instance
1030f883b22SFabrice Gasnier  * @irq:		interrupt for this adc instance
1040f883b22SFabrice Gasnier  * @lock:		spinlock
105*da9b9485SFabrice Gasnier  * @bufi:		data buffer index
106*da9b9485SFabrice Gasnier  * @num_conv:		expected number of scan conversions
1070f883b22SFabrice Gasnier  */
1080f883b22SFabrice Gasnier struct stm32_adc {
1090f883b22SFabrice Gasnier 	struct stm32_adc_common	*common;
1100f883b22SFabrice Gasnier 	u32			offset;
1110f883b22SFabrice Gasnier 	struct completion	completion;
112*da9b9485SFabrice Gasnier 	u16			buffer[STM32_ADC_MAX_SQ];
1130f883b22SFabrice Gasnier 	struct clk		*clk;
1140f883b22SFabrice Gasnier 	int			irq;
1150f883b22SFabrice Gasnier 	spinlock_t		lock;		/* interrupt lock */
116*da9b9485SFabrice Gasnier 	unsigned int		bufi;
117*da9b9485SFabrice Gasnier 	unsigned int		num_conv;
1180f883b22SFabrice Gasnier };
1190f883b22SFabrice Gasnier 
1200f883b22SFabrice Gasnier /**
1210f883b22SFabrice Gasnier  * struct stm32_adc_chan_spec - specification of stm32 adc channel
1220f883b22SFabrice Gasnier  * @type:	IIO channel type
1230f883b22SFabrice Gasnier  * @channel:	channel number (single ended)
1240f883b22SFabrice Gasnier  * @name:	channel name (single ended)
1250f883b22SFabrice Gasnier  */
1260f883b22SFabrice Gasnier struct stm32_adc_chan_spec {
1270f883b22SFabrice Gasnier 	enum iio_chan_type	type;
1280f883b22SFabrice Gasnier 	int			channel;
1290f883b22SFabrice Gasnier 	const char		*name;
1300f883b22SFabrice Gasnier };
1310f883b22SFabrice Gasnier 
1320f883b22SFabrice Gasnier /* Input definitions common for all STM32F4 instances */
1330f883b22SFabrice Gasnier static const struct stm32_adc_chan_spec stm32f4_adc123_channels[] = {
1340f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 0, "in0" },
1350f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 1, "in1" },
1360f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 2, "in2" },
1370f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 3, "in3" },
1380f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 4, "in4" },
1390f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 5, "in5" },
1400f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 6, "in6" },
1410f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 7, "in7" },
1420f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 8, "in8" },
1430f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 9, "in9" },
1440f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 10, "in10" },
1450f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 11, "in11" },
1460f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 12, "in12" },
1470f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 13, "in13" },
1480f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 14, "in14" },
1490f883b22SFabrice Gasnier 	{ IIO_VOLTAGE, 15, "in15" },
1500f883b22SFabrice Gasnier };
1510f883b22SFabrice Gasnier 
1520f883b22SFabrice Gasnier /**
153*da9b9485SFabrice Gasnier  * stm32f4_sq - describe regular sequence registers
154*da9b9485SFabrice Gasnier  * - L: sequence len (register & bit field)
155*da9b9485SFabrice Gasnier  * - SQ1..SQ16: sequence entries (register & bit field)
156*da9b9485SFabrice Gasnier  */
157*da9b9485SFabrice Gasnier static const struct stm32_adc_regs stm32f4_sq[STM32_ADC_MAX_SQ + 1] = {
158*da9b9485SFabrice Gasnier 	/* L: len bit field description to be kept as first element */
159*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(23, 20), 20 },
160*da9b9485SFabrice Gasnier 	/* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
161*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(4, 0), 0 },
162*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(9, 5), 5 },
163*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(14, 10), 10 },
164*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(19, 15), 15 },
165*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(24, 20), 20 },
166*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR3, GENMASK(29, 25), 25 },
167*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(4, 0), 0 },
168*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(9, 5), 5 },
169*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(14, 10), 10 },
170*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(19, 15), 15 },
171*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(24, 20), 20 },
172*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR2, GENMASK(29, 25), 25 },
173*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(4, 0), 0 },
174*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(9, 5), 5 },
175*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(14, 10), 10 },
176*da9b9485SFabrice Gasnier 	{ STM32F4_ADC_SQR1, GENMASK(19, 15), 15 },
177*da9b9485SFabrice Gasnier };
178*da9b9485SFabrice Gasnier 
179*da9b9485SFabrice Gasnier /**
1800f883b22SFabrice Gasnier  * STM32 ADC registers access routines
1810f883b22SFabrice Gasnier  * @adc: stm32 adc instance
1820f883b22SFabrice Gasnier  * @reg: reg offset in adc instance
1830f883b22SFabrice Gasnier  *
1840f883b22SFabrice Gasnier  * Note: All instances share same base, with 0x0, 0x100 or 0x200 offset resp.
1850f883b22SFabrice Gasnier  * for adc1, adc2 and adc3.
1860f883b22SFabrice Gasnier  */
1870f883b22SFabrice Gasnier static u32 stm32_adc_readl(struct stm32_adc *adc, u32 reg)
1880f883b22SFabrice Gasnier {
1890f883b22SFabrice Gasnier 	return readl_relaxed(adc->common->base + adc->offset + reg);
1900f883b22SFabrice Gasnier }
1910f883b22SFabrice Gasnier 
1920f883b22SFabrice Gasnier static u16 stm32_adc_readw(struct stm32_adc *adc, u32 reg)
1930f883b22SFabrice Gasnier {
1940f883b22SFabrice Gasnier 	return readw_relaxed(adc->common->base + adc->offset + reg);
1950f883b22SFabrice Gasnier }
1960f883b22SFabrice Gasnier 
1970f883b22SFabrice Gasnier static void stm32_adc_writel(struct stm32_adc *adc, u32 reg, u32 val)
1980f883b22SFabrice Gasnier {
1990f883b22SFabrice Gasnier 	writel_relaxed(val, adc->common->base + adc->offset + reg);
2000f883b22SFabrice Gasnier }
2010f883b22SFabrice Gasnier 
2020f883b22SFabrice Gasnier static void stm32_adc_set_bits(struct stm32_adc *adc, u32 reg, u32 bits)
2030f883b22SFabrice Gasnier {
2040f883b22SFabrice Gasnier 	unsigned long flags;
2050f883b22SFabrice Gasnier 
2060f883b22SFabrice Gasnier 	spin_lock_irqsave(&adc->lock, flags);
2070f883b22SFabrice Gasnier 	stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) | bits);
2080f883b22SFabrice Gasnier 	spin_unlock_irqrestore(&adc->lock, flags);
2090f883b22SFabrice Gasnier }
2100f883b22SFabrice Gasnier 
2110f883b22SFabrice Gasnier static void stm32_adc_clr_bits(struct stm32_adc *adc, u32 reg, u32 bits)
2120f883b22SFabrice Gasnier {
2130f883b22SFabrice Gasnier 	unsigned long flags;
2140f883b22SFabrice Gasnier 
2150f883b22SFabrice Gasnier 	spin_lock_irqsave(&adc->lock, flags);
2160f883b22SFabrice Gasnier 	stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) & ~bits);
2170f883b22SFabrice Gasnier 	spin_unlock_irqrestore(&adc->lock, flags);
2180f883b22SFabrice Gasnier }
2190f883b22SFabrice Gasnier 
2200f883b22SFabrice Gasnier /**
2210f883b22SFabrice Gasnier  * stm32_adc_conv_irq_enable() - Enable end of conversion interrupt
2220f883b22SFabrice Gasnier  * @adc: stm32 adc instance
2230f883b22SFabrice Gasnier  */
2240f883b22SFabrice Gasnier static void stm32_adc_conv_irq_enable(struct stm32_adc *adc)
2250f883b22SFabrice Gasnier {
2260f883b22SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_EOCIE);
2270f883b22SFabrice Gasnier };
2280f883b22SFabrice Gasnier 
2290f883b22SFabrice Gasnier /**
2300f883b22SFabrice Gasnier  * stm32_adc_conv_irq_disable() - Disable end of conversion interrupt
2310f883b22SFabrice Gasnier  * @adc: stm32 adc instance
2320f883b22SFabrice Gasnier  */
2330f883b22SFabrice Gasnier static void stm32_adc_conv_irq_disable(struct stm32_adc *adc)
2340f883b22SFabrice Gasnier {
2350f883b22SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_CR1, STM32F4_EOCIE);
2360f883b22SFabrice Gasnier }
2370f883b22SFabrice Gasnier 
2380f883b22SFabrice Gasnier /**
2390f883b22SFabrice Gasnier  * stm32_adc_start_conv() - Start conversions for regular channels.
2400f883b22SFabrice Gasnier  * @adc: stm32 adc instance
2410f883b22SFabrice Gasnier  */
2420f883b22SFabrice Gasnier static void stm32_adc_start_conv(struct stm32_adc *adc)
2430f883b22SFabrice Gasnier {
2440f883b22SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN);
2450f883b22SFabrice Gasnier 	stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_EOCS | STM32F4_ADON);
2460f883b22SFabrice Gasnier 
2470f883b22SFabrice Gasnier 	/* Wait for Power-up time (tSTAB from datasheet) */
2480f883b22SFabrice Gasnier 	usleep_range(2, 3);
2490f883b22SFabrice Gasnier 
2500f883b22SFabrice Gasnier 	/* Software start ? (e.g. trigger detection disabled ?) */
2510f883b22SFabrice Gasnier 	if (!(stm32_adc_readl(adc, STM32F4_ADC_CR2) & STM32F4_EXTEN_MASK))
2520f883b22SFabrice Gasnier 		stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_SWSTART);
2530f883b22SFabrice Gasnier }
2540f883b22SFabrice Gasnier 
2550f883b22SFabrice Gasnier static void stm32_adc_stop_conv(struct stm32_adc *adc)
2560f883b22SFabrice Gasnier {
2570f883b22SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK);
2580f883b22SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_SR, STM32F4_STRT);
2590f883b22SFabrice Gasnier 
2600f883b22SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN);
2610f883b22SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_ADON);
2620f883b22SFabrice Gasnier }
2630f883b22SFabrice Gasnier 
2640f883b22SFabrice Gasnier /**
265*da9b9485SFabrice Gasnier  * stm32_adc_conf_scan_seq() - Build regular channels scan sequence
266*da9b9485SFabrice Gasnier  * @indio_dev: IIO device
267*da9b9485SFabrice Gasnier  * @scan_mask: channels to be converted
268*da9b9485SFabrice Gasnier  *
269*da9b9485SFabrice Gasnier  * Conversion sequence :
270*da9b9485SFabrice Gasnier  * Configure ADC scan sequence based on selected channels in scan_mask.
271*da9b9485SFabrice Gasnier  * Add channels to SQR registers, from scan_mask LSB to MSB, then
272*da9b9485SFabrice Gasnier  * program sequence len.
273*da9b9485SFabrice Gasnier  */
274*da9b9485SFabrice Gasnier static int stm32_adc_conf_scan_seq(struct iio_dev *indio_dev,
275*da9b9485SFabrice Gasnier 				   const unsigned long *scan_mask)
276*da9b9485SFabrice Gasnier {
277*da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
278*da9b9485SFabrice Gasnier 	const struct iio_chan_spec *chan;
279*da9b9485SFabrice Gasnier 	u32 val, bit;
280*da9b9485SFabrice Gasnier 	int i = 0;
281*da9b9485SFabrice Gasnier 
282*da9b9485SFabrice Gasnier 	for_each_set_bit(bit, scan_mask, indio_dev->masklength) {
283*da9b9485SFabrice Gasnier 		chan = indio_dev->channels + bit;
284*da9b9485SFabrice Gasnier 		/*
285*da9b9485SFabrice Gasnier 		 * Assign one channel per SQ entry in regular
286*da9b9485SFabrice Gasnier 		 * sequence, starting with SQ1.
287*da9b9485SFabrice Gasnier 		 */
288*da9b9485SFabrice Gasnier 		i++;
289*da9b9485SFabrice Gasnier 		if (i > STM32_ADC_MAX_SQ)
290*da9b9485SFabrice Gasnier 			return -EINVAL;
291*da9b9485SFabrice Gasnier 
292*da9b9485SFabrice Gasnier 		dev_dbg(&indio_dev->dev, "%s chan %d to SQ%d\n",
293*da9b9485SFabrice Gasnier 			__func__, chan->channel, i);
294*da9b9485SFabrice Gasnier 
295*da9b9485SFabrice Gasnier 		val = stm32_adc_readl(adc, stm32f4_sq[i].reg);
296*da9b9485SFabrice Gasnier 		val &= ~stm32f4_sq[i].mask;
297*da9b9485SFabrice Gasnier 		val |= chan->channel << stm32f4_sq[i].shift;
298*da9b9485SFabrice Gasnier 		stm32_adc_writel(adc, stm32f4_sq[i].reg, val);
299*da9b9485SFabrice Gasnier 	}
300*da9b9485SFabrice Gasnier 
301*da9b9485SFabrice Gasnier 	if (!i)
302*da9b9485SFabrice Gasnier 		return -EINVAL;
303*da9b9485SFabrice Gasnier 
304*da9b9485SFabrice Gasnier 	/* Sequence len */
305*da9b9485SFabrice Gasnier 	val = stm32_adc_readl(adc, stm32f4_sq[0].reg);
306*da9b9485SFabrice Gasnier 	val &= ~stm32f4_sq[0].mask;
307*da9b9485SFabrice Gasnier 	val |= ((i - 1) << stm32f4_sq[0].shift);
308*da9b9485SFabrice Gasnier 	stm32_adc_writel(adc, stm32f4_sq[0].reg, val);
309*da9b9485SFabrice Gasnier 
310*da9b9485SFabrice Gasnier 	return 0;
311*da9b9485SFabrice Gasnier }
312*da9b9485SFabrice Gasnier 
313*da9b9485SFabrice Gasnier /**
314*da9b9485SFabrice Gasnier  * stm32_adc_get_trig_extsel() - Get external trigger selection
315*da9b9485SFabrice Gasnier  * @trig: trigger
316*da9b9485SFabrice Gasnier  *
317*da9b9485SFabrice Gasnier  * Returns trigger extsel value, if trig matches, -EINVAL otherwise.
318*da9b9485SFabrice Gasnier  */
319*da9b9485SFabrice Gasnier static int stm32_adc_get_trig_extsel(struct iio_trigger *trig)
320*da9b9485SFabrice Gasnier {
321*da9b9485SFabrice Gasnier 	return -EINVAL;
322*da9b9485SFabrice Gasnier }
323*da9b9485SFabrice Gasnier 
324*da9b9485SFabrice Gasnier /**
325*da9b9485SFabrice Gasnier  * stm32_adc_set_trig() - Set a regular trigger
326*da9b9485SFabrice Gasnier  * @indio_dev: IIO device
327*da9b9485SFabrice Gasnier  * @trig: IIO trigger
328*da9b9485SFabrice Gasnier  *
329*da9b9485SFabrice Gasnier  * Set trigger source/polarity (e.g. SW, or HW with polarity) :
330*da9b9485SFabrice Gasnier  * - if HW trigger disabled (e.g. trig == NULL, conversion launched by sw)
331*da9b9485SFabrice Gasnier  * - if HW trigger enabled, set source & polarity
332*da9b9485SFabrice Gasnier  */
333*da9b9485SFabrice Gasnier static int stm32_adc_set_trig(struct iio_dev *indio_dev,
334*da9b9485SFabrice Gasnier 			      struct iio_trigger *trig)
335*da9b9485SFabrice Gasnier {
336*da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
337*da9b9485SFabrice Gasnier 	u32 val, extsel = 0, exten = STM32_EXTEN_SWTRIG;
338*da9b9485SFabrice Gasnier 	unsigned long flags;
339*da9b9485SFabrice Gasnier 	int ret;
340*da9b9485SFabrice Gasnier 
341*da9b9485SFabrice Gasnier 	if (trig) {
342*da9b9485SFabrice Gasnier 		ret = stm32_adc_get_trig_extsel(trig);
343*da9b9485SFabrice Gasnier 		if (ret < 0)
344*da9b9485SFabrice Gasnier 			return ret;
345*da9b9485SFabrice Gasnier 
346*da9b9485SFabrice Gasnier 		/* set trigger source and polarity (default to rising edge) */
347*da9b9485SFabrice Gasnier 		extsel = ret;
348*da9b9485SFabrice Gasnier 		exten = STM32_EXTEN_HWTRIG_RISING_EDGE;
349*da9b9485SFabrice Gasnier 	}
350*da9b9485SFabrice Gasnier 
351*da9b9485SFabrice Gasnier 	spin_lock_irqsave(&adc->lock, flags);
352*da9b9485SFabrice Gasnier 	val = stm32_adc_readl(adc, STM32F4_ADC_CR2);
353*da9b9485SFabrice Gasnier 	val &= ~(STM32F4_EXTEN_MASK | STM32F4_EXTSEL_MASK);
354*da9b9485SFabrice Gasnier 	val |= exten << STM32F4_EXTEN_SHIFT;
355*da9b9485SFabrice Gasnier 	val |= extsel << STM32F4_EXTSEL_SHIFT;
356*da9b9485SFabrice Gasnier 	stm32_adc_writel(adc, STM32F4_ADC_CR2, val);
357*da9b9485SFabrice Gasnier 	spin_unlock_irqrestore(&adc->lock, flags);
358*da9b9485SFabrice Gasnier 
359*da9b9485SFabrice Gasnier 	return 0;
360*da9b9485SFabrice Gasnier }
361*da9b9485SFabrice Gasnier 
362*da9b9485SFabrice Gasnier /**
3630f883b22SFabrice Gasnier  * stm32_adc_single_conv() - Performs a single conversion
3640f883b22SFabrice Gasnier  * @indio_dev: IIO device
3650f883b22SFabrice Gasnier  * @chan: IIO channel
3660f883b22SFabrice Gasnier  * @res: conversion result
3670f883b22SFabrice Gasnier  *
3680f883b22SFabrice Gasnier  * The function performs a single conversion on a given channel:
3690f883b22SFabrice Gasnier  * - Program sequencer with one channel (e.g. in SQ1 with len = 1)
3700f883b22SFabrice Gasnier  * - Use SW trigger
3710f883b22SFabrice Gasnier  * - Start conversion, then wait for interrupt completion.
3720f883b22SFabrice Gasnier  */
3730f883b22SFabrice Gasnier static int stm32_adc_single_conv(struct iio_dev *indio_dev,
3740f883b22SFabrice Gasnier 				 const struct iio_chan_spec *chan,
3750f883b22SFabrice Gasnier 				 int *res)
3760f883b22SFabrice Gasnier {
3770f883b22SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
3780f883b22SFabrice Gasnier 	long timeout;
3790f883b22SFabrice Gasnier 	u32 val;
3800f883b22SFabrice Gasnier 	int ret;
3810f883b22SFabrice Gasnier 
3820f883b22SFabrice Gasnier 	reinit_completion(&adc->completion);
3830f883b22SFabrice Gasnier 
384*da9b9485SFabrice Gasnier 	adc->bufi = 0;
3850f883b22SFabrice Gasnier 
386*da9b9485SFabrice Gasnier 	/* Program chan number in regular sequence (SQ1) */
387*da9b9485SFabrice Gasnier 	val = stm32_adc_readl(adc, stm32f4_sq[1].reg);
388*da9b9485SFabrice Gasnier 	val &= ~stm32f4_sq[1].mask;
389*da9b9485SFabrice Gasnier 	val |= chan->channel << stm32f4_sq[1].shift;
390*da9b9485SFabrice Gasnier 	stm32_adc_writel(adc, stm32f4_sq[1].reg, val);
3910f883b22SFabrice Gasnier 
3920f883b22SFabrice Gasnier 	/* Set regular sequence len (0 for 1 conversion) */
393*da9b9485SFabrice Gasnier 	stm32_adc_clr_bits(adc, stm32f4_sq[0].reg, stm32f4_sq[0].mask);
3940f883b22SFabrice Gasnier 
3950f883b22SFabrice Gasnier 	/* Trigger detection disabled (conversion can be launched in SW) */
3960f883b22SFabrice Gasnier 	stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK);
3970f883b22SFabrice Gasnier 
3980f883b22SFabrice Gasnier 	stm32_adc_conv_irq_enable(adc);
3990f883b22SFabrice Gasnier 
4000f883b22SFabrice Gasnier 	stm32_adc_start_conv(adc);
4010f883b22SFabrice Gasnier 
4020f883b22SFabrice Gasnier 	timeout = wait_for_completion_interruptible_timeout(
4030f883b22SFabrice Gasnier 					&adc->completion, STM32_ADC_TIMEOUT);
4040f883b22SFabrice Gasnier 	if (timeout == 0) {
4050f883b22SFabrice Gasnier 		ret = -ETIMEDOUT;
4060f883b22SFabrice Gasnier 	} else if (timeout < 0) {
4070f883b22SFabrice Gasnier 		ret = timeout;
4080f883b22SFabrice Gasnier 	} else {
409*da9b9485SFabrice Gasnier 		*res = adc->buffer[0];
4100f883b22SFabrice Gasnier 		ret = IIO_VAL_INT;
4110f883b22SFabrice Gasnier 	}
4120f883b22SFabrice Gasnier 
4130f883b22SFabrice Gasnier 	stm32_adc_stop_conv(adc);
4140f883b22SFabrice Gasnier 
4150f883b22SFabrice Gasnier 	stm32_adc_conv_irq_disable(adc);
4160f883b22SFabrice Gasnier 
4170f883b22SFabrice Gasnier 	return ret;
4180f883b22SFabrice Gasnier }
4190f883b22SFabrice Gasnier 
4200f883b22SFabrice Gasnier static int stm32_adc_read_raw(struct iio_dev *indio_dev,
4210f883b22SFabrice Gasnier 			      struct iio_chan_spec const *chan,
4220f883b22SFabrice Gasnier 			      int *val, int *val2, long mask)
4230f883b22SFabrice Gasnier {
4240f883b22SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
4250f883b22SFabrice Gasnier 	int ret;
4260f883b22SFabrice Gasnier 
4270f883b22SFabrice Gasnier 	switch (mask) {
4280f883b22SFabrice Gasnier 	case IIO_CHAN_INFO_RAW:
4290f883b22SFabrice Gasnier 		ret = iio_device_claim_direct_mode(indio_dev);
4300f883b22SFabrice Gasnier 		if (ret)
4310f883b22SFabrice Gasnier 			return ret;
4320f883b22SFabrice Gasnier 		if (chan->type == IIO_VOLTAGE)
4330f883b22SFabrice Gasnier 			ret = stm32_adc_single_conv(indio_dev, chan, val);
4340f883b22SFabrice Gasnier 		else
4350f883b22SFabrice Gasnier 			ret = -EINVAL;
4360f883b22SFabrice Gasnier 		iio_device_release_direct_mode(indio_dev);
4370f883b22SFabrice Gasnier 		return ret;
4380f883b22SFabrice Gasnier 
4390f883b22SFabrice Gasnier 	case IIO_CHAN_INFO_SCALE:
4400f883b22SFabrice Gasnier 		*val = adc->common->vref_mv;
4410f883b22SFabrice Gasnier 		*val2 = chan->scan_type.realbits;
4420f883b22SFabrice Gasnier 		return IIO_VAL_FRACTIONAL_LOG2;
4430f883b22SFabrice Gasnier 
4440f883b22SFabrice Gasnier 	default:
4450f883b22SFabrice Gasnier 		return -EINVAL;
4460f883b22SFabrice Gasnier 	}
4470f883b22SFabrice Gasnier }
4480f883b22SFabrice Gasnier 
4490f883b22SFabrice Gasnier static irqreturn_t stm32_adc_isr(int irq, void *data)
4500f883b22SFabrice Gasnier {
4510f883b22SFabrice Gasnier 	struct stm32_adc *adc = data;
452*da9b9485SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
4530f883b22SFabrice Gasnier 	u32 status = stm32_adc_readl(adc, STM32F4_ADC_SR);
4540f883b22SFabrice Gasnier 
4550f883b22SFabrice Gasnier 	if (status & STM32F4_EOC) {
456*da9b9485SFabrice Gasnier 		/* Reading DR also clears EOC status flag */
457*da9b9485SFabrice Gasnier 		adc->buffer[adc->bufi] = stm32_adc_readw(adc, STM32F4_ADC_DR);
458*da9b9485SFabrice Gasnier 		if (iio_buffer_enabled(indio_dev)) {
459*da9b9485SFabrice Gasnier 			adc->bufi++;
460*da9b9485SFabrice Gasnier 			if (adc->bufi >= adc->num_conv) {
461*da9b9485SFabrice Gasnier 				stm32_adc_conv_irq_disable(adc);
462*da9b9485SFabrice Gasnier 				iio_trigger_poll(indio_dev->trig);
463*da9b9485SFabrice Gasnier 			}
464*da9b9485SFabrice Gasnier 		} else {
4650f883b22SFabrice Gasnier 			complete(&adc->completion);
466*da9b9485SFabrice Gasnier 		}
4670f883b22SFabrice Gasnier 		return IRQ_HANDLED;
4680f883b22SFabrice Gasnier 	}
4690f883b22SFabrice Gasnier 
4700f883b22SFabrice Gasnier 	return IRQ_NONE;
4710f883b22SFabrice Gasnier }
4720f883b22SFabrice Gasnier 
473*da9b9485SFabrice Gasnier /**
474*da9b9485SFabrice Gasnier  * stm32_adc_validate_trigger() - validate trigger for stm32 adc
475*da9b9485SFabrice Gasnier  * @indio_dev: IIO device
476*da9b9485SFabrice Gasnier  * @trig: new trigger
477*da9b9485SFabrice Gasnier  *
478*da9b9485SFabrice Gasnier  * Returns: 0 if trig matches one of the triggers registered by stm32 adc
479*da9b9485SFabrice Gasnier  * driver, -EINVAL otherwise.
480*da9b9485SFabrice Gasnier  */
481*da9b9485SFabrice Gasnier static int stm32_adc_validate_trigger(struct iio_dev *indio_dev,
482*da9b9485SFabrice Gasnier 				      struct iio_trigger *trig)
483*da9b9485SFabrice Gasnier {
484*da9b9485SFabrice Gasnier 	return stm32_adc_get_trig_extsel(trig) < 0 ? -EINVAL : 0;
485*da9b9485SFabrice Gasnier }
486*da9b9485SFabrice Gasnier 
487*da9b9485SFabrice Gasnier static int stm32_adc_update_scan_mode(struct iio_dev *indio_dev,
488*da9b9485SFabrice Gasnier 				      const unsigned long *scan_mask)
489*da9b9485SFabrice Gasnier {
490*da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
491*da9b9485SFabrice Gasnier 	int ret;
492*da9b9485SFabrice Gasnier 
493*da9b9485SFabrice Gasnier 	adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength);
494*da9b9485SFabrice Gasnier 
495*da9b9485SFabrice Gasnier 	ret = stm32_adc_conf_scan_seq(indio_dev, scan_mask);
496*da9b9485SFabrice Gasnier 	if (ret)
497*da9b9485SFabrice Gasnier 		return ret;
498*da9b9485SFabrice Gasnier 
499*da9b9485SFabrice Gasnier 	return 0;
500*da9b9485SFabrice Gasnier }
501*da9b9485SFabrice Gasnier 
5020f883b22SFabrice Gasnier static int stm32_adc_of_xlate(struct iio_dev *indio_dev,
5030f883b22SFabrice Gasnier 			      const struct of_phandle_args *iiospec)
5040f883b22SFabrice Gasnier {
5050f883b22SFabrice Gasnier 	int i;
5060f883b22SFabrice Gasnier 
5070f883b22SFabrice Gasnier 	for (i = 0; i < indio_dev->num_channels; i++)
5080f883b22SFabrice Gasnier 		if (indio_dev->channels[i].channel == iiospec->args[0])
5090f883b22SFabrice Gasnier 			return i;
5100f883b22SFabrice Gasnier 
5110f883b22SFabrice Gasnier 	return -EINVAL;
5120f883b22SFabrice Gasnier }
5130f883b22SFabrice Gasnier 
5140f883b22SFabrice Gasnier /**
5150f883b22SFabrice Gasnier  * stm32_adc_debugfs_reg_access - read or write register value
5160f883b22SFabrice Gasnier  *
5170f883b22SFabrice Gasnier  * To read a value from an ADC register:
5180f883b22SFabrice Gasnier  *   echo [ADC reg offset] > direct_reg_access
5190f883b22SFabrice Gasnier  *   cat direct_reg_access
5200f883b22SFabrice Gasnier  *
5210f883b22SFabrice Gasnier  * To write a value in a ADC register:
5220f883b22SFabrice Gasnier  *   echo [ADC_reg_offset] [value] > direct_reg_access
5230f883b22SFabrice Gasnier  */
5240f883b22SFabrice Gasnier static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev,
5250f883b22SFabrice Gasnier 					unsigned reg, unsigned writeval,
5260f883b22SFabrice Gasnier 					unsigned *readval)
5270f883b22SFabrice Gasnier {
5280f883b22SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
5290f883b22SFabrice Gasnier 
5300f883b22SFabrice Gasnier 	if (!readval)
5310f883b22SFabrice Gasnier 		stm32_adc_writel(adc, reg, writeval);
5320f883b22SFabrice Gasnier 	else
5330f883b22SFabrice Gasnier 		*readval = stm32_adc_readl(adc, reg);
5340f883b22SFabrice Gasnier 
5350f883b22SFabrice Gasnier 	return 0;
5360f883b22SFabrice Gasnier }
5370f883b22SFabrice Gasnier 
5380f883b22SFabrice Gasnier static const struct iio_info stm32_adc_iio_info = {
5390f883b22SFabrice Gasnier 	.read_raw = stm32_adc_read_raw,
540*da9b9485SFabrice Gasnier 	.validate_trigger = stm32_adc_validate_trigger,
541*da9b9485SFabrice Gasnier 	.update_scan_mode = stm32_adc_update_scan_mode,
5420f883b22SFabrice Gasnier 	.debugfs_reg_access = stm32_adc_debugfs_reg_access,
5430f883b22SFabrice Gasnier 	.of_xlate = stm32_adc_of_xlate,
5440f883b22SFabrice Gasnier 	.driver_module = THIS_MODULE,
5450f883b22SFabrice Gasnier };
5460f883b22SFabrice Gasnier 
547*da9b9485SFabrice Gasnier static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev)
548*da9b9485SFabrice Gasnier {
549*da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
550*da9b9485SFabrice Gasnier 	int ret;
551*da9b9485SFabrice Gasnier 
552*da9b9485SFabrice Gasnier 	ret = stm32_adc_set_trig(indio_dev, indio_dev->trig);
553*da9b9485SFabrice Gasnier 	if (ret) {
554*da9b9485SFabrice Gasnier 		dev_err(&indio_dev->dev, "Can't set trigger\n");
555*da9b9485SFabrice Gasnier 		return ret;
556*da9b9485SFabrice Gasnier 	}
557*da9b9485SFabrice Gasnier 
558*da9b9485SFabrice Gasnier 	ret = iio_triggered_buffer_postenable(indio_dev);
559*da9b9485SFabrice Gasnier 	if (ret < 0)
560*da9b9485SFabrice Gasnier 		goto err_clr_trig;
561*da9b9485SFabrice Gasnier 
562*da9b9485SFabrice Gasnier 	/* Reset adc buffer index */
563*da9b9485SFabrice Gasnier 	adc->bufi = 0;
564*da9b9485SFabrice Gasnier 
565*da9b9485SFabrice Gasnier 	stm32_adc_conv_irq_enable(adc);
566*da9b9485SFabrice Gasnier 	stm32_adc_start_conv(adc);
567*da9b9485SFabrice Gasnier 
568*da9b9485SFabrice Gasnier 	return 0;
569*da9b9485SFabrice Gasnier 
570*da9b9485SFabrice Gasnier err_clr_trig:
571*da9b9485SFabrice Gasnier 	stm32_adc_set_trig(indio_dev, NULL);
572*da9b9485SFabrice Gasnier 
573*da9b9485SFabrice Gasnier 	return ret;
574*da9b9485SFabrice Gasnier }
575*da9b9485SFabrice Gasnier 
576*da9b9485SFabrice Gasnier static int stm32_adc_buffer_predisable(struct iio_dev *indio_dev)
577*da9b9485SFabrice Gasnier {
578*da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
579*da9b9485SFabrice Gasnier 	int ret;
580*da9b9485SFabrice Gasnier 
581*da9b9485SFabrice Gasnier 	stm32_adc_stop_conv(adc);
582*da9b9485SFabrice Gasnier 	stm32_adc_conv_irq_disable(adc);
583*da9b9485SFabrice Gasnier 
584*da9b9485SFabrice Gasnier 	ret = iio_triggered_buffer_predisable(indio_dev);
585*da9b9485SFabrice Gasnier 	if (ret < 0)
586*da9b9485SFabrice Gasnier 		dev_err(&indio_dev->dev, "predisable failed\n");
587*da9b9485SFabrice Gasnier 
588*da9b9485SFabrice Gasnier 	if (stm32_adc_set_trig(indio_dev, NULL))
589*da9b9485SFabrice Gasnier 		dev_err(&indio_dev->dev, "Can't clear trigger\n");
590*da9b9485SFabrice Gasnier 
591*da9b9485SFabrice Gasnier 	return ret;
592*da9b9485SFabrice Gasnier }
593*da9b9485SFabrice Gasnier 
594*da9b9485SFabrice Gasnier static const struct iio_buffer_setup_ops stm32_adc_buffer_setup_ops = {
595*da9b9485SFabrice Gasnier 	.postenable = &stm32_adc_buffer_postenable,
596*da9b9485SFabrice Gasnier 	.predisable = &stm32_adc_buffer_predisable,
597*da9b9485SFabrice Gasnier };
598*da9b9485SFabrice Gasnier 
599*da9b9485SFabrice Gasnier static irqreturn_t stm32_adc_trigger_handler(int irq, void *p)
600*da9b9485SFabrice Gasnier {
601*da9b9485SFabrice Gasnier 	struct iio_poll_func *pf = p;
602*da9b9485SFabrice Gasnier 	struct iio_dev *indio_dev = pf->indio_dev;
603*da9b9485SFabrice Gasnier 	struct stm32_adc *adc = iio_priv(indio_dev);
604*da9b9485SFabrice Gasnier 
605*da9b9485SFabrice Gasnier 	dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi);
606*da9b9485SFabrice Gasnier 
607*da9b9485SFabrice Gasnier 	/* reset buffer index */
608*da9b9485SFabrice Gasnier 	adc->bufi = 0;
609*da9b9485SFabrice Gasnier 	iio_push_to_buffers_with_timestamp(indio_dev, adc->buffer,
610*da9b9485SFabrice Gasnier 					   pf->timestamp);
611*da9b9485SFabrice Gasnier 
612*da9b9485SFabrice Gasnier 	iio_trigger_notify_done(indio_dev->trig);
613*da9b9485SFabrice Gasnier 
614*da9b9485SFabrice Gasnier 	/* re-enable eoc irq */
615*da9b9485SFabrice Gasnier 	stm32_adc_conv_irq_enable(adc);
616*da9b9485SFabrice Gasnier 
617*da9b9485SFabrice Gasnier 	return IRQ_HANDLED;
618*da9b9485SFabrice Gasnier }
619*da9b9485SFabrice Gasnier 
6200f883b22SFabrice Gasnier static void stm32_adc_chan_init_one(struct iio_dev *indio_dev,
6210f883b22SFabrice Gasnier 				    struct iio_chan_spec *chan,
6220f883b22SFabrice Gasnier 				    const struct stm32_adc_chan_spec *channel,
6230f883b22SFabrice Gasnier 				    int scan_index)
6240f883b22SFabrice Gasnier {
6250f883b22SFabrice Gasnier 	chan->type = channel->type;
6260f883b22SFabrice Gasnier 	chan->channel = channel->channel;
6270f883b22SFabrice Gasnier 	chan->datasheet_name = channel->name;
6280f883b22SFabrice Gasnier 	chan->scan_index = scan_index;
6290f883b22SFabrice Gasnier 	chan->indexed = 1;
6300f883b22SFabrice Gasnier 	chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
6310f883b22SFabrice Gasnier 	chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
6320f883b22SFabrice Gasnier 	chan->scan_type.sign = 'u';
6330f883b22SFabrice Gasnier 	chan->scan_type.realbits = 12;
6340f883b22SFabrice Gasnier 	chan->scan_type.storagebits = 16;
6350f883b22SFabrice Gasnier }
6360f883b22SFabrice Gasnier 
6370f883b22SFabrice Gasnier static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
6380f883b22SFabrice Gasnier {
6390f883b22SFabrice Gasnier 	struct device_node *node = indio_dev->dev.of_node;
6400f883b22SFabrice Gasnier 	struct property *prop;
6410f883b22SFabrice Gasnier 	const __be32 *cur;
6420f883b22SFabrice Gasnier 	struct iio_chan_spec *channels;
6430f883b22SFabrice Gasnier 	int scan_index = 0, num_channels;
6440f883b22SFabrice Gasnier 	u32 val;
6450f883b22SFabrice Gasnier 
6460f883b22SFabrice Gasnier 	num_channels = of_property_count_u32_elems(node, "st,adc-channels");
6470f883b22SFabrice Gasnier 	if (num_channels < 0 ||
6480f883b22SFabrice Gasnier 	    num_channels >= ARRAY_SIZE(stm32f4_adc123_channels)) {
6490f883b22SFabrice Gasnier 		dev_err(&indio_dev->dev, "Bad st,adc-channels?\n");
6500f883b22SFabrice Gasnier 		return num_channels < 0 ? num_channels : -EINVAL;
6510f883b22SFabrice Gasnier 	}
6520f883b22SFabrice Gasnier 
6530f883b22SFabrice Gasnier 	channels = devm_kcalloc(&indio_dev->dev, num_channels,
6540f883b22SFabrice Gasnier 				sizeof(struct iio_chan_spec), GFP_KERNEL);
6550f883b22SFabrice Gasnier 	if (!channels)
6560f883b22SFabrice Gasnier 		return -ENOMEM;
6570f883b22SFabrice Gasnier 
6580f883b22SFabrice Gasnier 	of_property_for_each_u32(node, "st,adc-channels", prop, cur, val) {
6590f883b22SFabrice Gasnier 		if (val >= ARRAY_SIZE(stm32f4_adc123_channels)) {
6600f883b22SFabrice Gasnier 			dev_err(&indio_dev->dev, "Invalid channel %d\n", val);
6610f883b22SFabrice Gasnier 			return -EINVAL;
6620f883b22SFabrice Gasnier 		}
6630f883b22SFabrice Gasnier 		stm32_adc_chan_init_one(indio_dev, &channels[scan_index],
6640f883b22SFabrice Gasnier 					&stm32f4_adc123_channels[val],
6650f883b22SFabrice Gasnier 					scan_index);
6660f883b22SFabrice Gasnier 		scan_index++;
6670f883b22SFabrice Gasnier 	}
6680f883b22SFabrice Gasnier 
6690f883b22SFabrice Gasnier 	indio_dev->num_channels = scan_index;
6700f883b22SFabrice Gasnier 	indio_dev->channels = channels;
6710f883b22SFabrice Gasnier 
6720f883b22SFabrice Gasnier 	return 0;
6730f883b22SFabrice Gasnier }
6740f883b22SFabrice Gasnier 
6750f883b22SFabrice Gasnier static int stm32_adc_probe(struct platform_device *pdev)
6760f883b22SFabrice Gasnier {
6770f883b22SFabrice Gasnier 	struct iio_dev *indio_dev;
6780f883b22SFabrice Gasnier 	struct stm32_adc *adc;
6790f883b22SFabrice Gasnier 	int ret;
6800f883b22SFabrice Gasnier 
6810f883b22SFabrice Gasnier 	if (!pdev->dev.of_node)
6820f883b22SFabrice Gasnier 		return -ENODEV;
6830f883b22SFabrice Gasnier 
6840f883b22SFabrice Gasnier 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
6850f883b22SFabrice Gasnier 	if (!indio_dev)
6860f883b22SFabrice Gasnier 		return -ENOMEM;
6870f883b22SFabrice Gasnier 
6880f883b22SFabrice Gasnier 	adc = iio_priv(indio_dev);
6890f883b22SFabrice Gasnier 	adc->common = dev_get_drvdata(pdev->dev.parent);
6900f883b22SFabrice Gasnier 	spin_lock_init(&adc->lock);
6910f883b22SFabrice Gasnier 	init_completion(&adc->completion);
6920f883b22SFabrice Gasnier 
6930f883b22SFabrice Gasnier 	indio_dev->name = dev_name(&pdev->dev);
6940f883b22SFabrice Gasnier 	indio_dev->dev.parent = &pdev->dev;
6950f883b22SFabrice Gasnier 	indio_dev->dev.of_node = pdev->dev.of_node;
6960f883b22SFabrice Gasnier 	indio_dev->info = &stm32_adc_iio_info;
6970f883b22SFabrice Gasnier 	indio_dev->modes = INDIO_DIRECT_MODE;
6980f883b22SFabrice Gasnier 
6990f883b22SFabrice Gasnier 	platform_set_drvdata(pdev, adc);
7000f883b22SFabrice Gasnier 
7010f883b22SFabrice Gasnier 	ret = of_property_read_u32(pdev->dev.of_node, "reg", &adc->offset);
7020f883b22SFabrice Gasnier 	if (ret != 0) {
7030f883b22SFabrice Gasnier 		dev_err(&pdev->dev, "missing reg property\n");
7040f883b22SFabrice Gasnier 		return -EINVAL;
7050f883b22SFabrice Gasnier 	}
7060f883b22SFabrice Gasnier 
7070f883b22SFabrice Gasnier 	adc->irq = platform_get_irq(pdev, 0);
7080f883b22SFabrice Gasnier 	if (adc->irq < 0) {
7090f883b22SFabrice Gasnier 		dev_err(&pdev->dev, "failed to get irq\n");
7100f883b22SFabrice Gasnier 		return adc->irq;
7110f883b22SFabrice Gasnier 	}
7120f883b22SFabrice Gasnier 
7130f883b22SFabrice Gasnier 	ret = devm_request_irq(&pdev->dev, adc->irq, stm32_adc_isr,
7140f883b22SFabrice Gasnier 			       0, pdev->name, adc);
7150f883b22SFabrice Gasnier 	if (ret) {
7160f883b22SFabrice Gasnier 		dev_err(&pdev->dev, "failed to request IRQ\n");
7170f883b22SFabrice Gasnier 		return ret;
7180f883b22SFabrice Gasnier 	}
7190f883b22SFabrice Gasnier 
7200f883b22SFabrice Gasnier 	adc->clk = devm_clk_get(&pdev->dev, NULL);
7210f883b22SFabrice Gasnier 	if (IS_ERR(adc->clk)) {
7220f883b22SFabrice Gasnier 		dev_err(&pdev->dev, "Can't get clock\n");
7230f883b22SFabrice Gasnier 		return PTR_ERR(adc->clk);
7240f883b22SFabrice Gasnier 	}
7250f883b22SFabrice Gasnier 
7260f883b22SFabrice Gasnier 	ret = clk_prepare_enable(adc->clk);
7270f883b22SFabrice Gasnier 	if (ret < 0) {
7280f883b22SFabrice Gasnier 		dev_err(&pdev->dev, "clk enable failed\n");
7290f883b22SFabrice Gasnier 		return ret;
7300f883b22SFabrice Gasnier 	}
7310f883b22SFabrice Gasnier 
7320f883b22SFabrice Gasnier 	ret = stm32_adc_chan_of_init(indio_dev);
7330f883b22SFabrice Gasnier 	if (ret < 0)
7340f883b22SFabrice Gasnier 		goto err_clk_disable;
7350f883b22SFabrice Gasnier 
736*da9b9485SFabrice Gasnier 	ret = iio_triggered_buffer_setup(indio_dev,
737*da9b9485SFabrice Gasnier 					 &iio_pollfunc_store_time,
738*da9b9485SFabrice Gasnier 					 &stm32_adc_trigger_handler,
739*da9b9485SFabrice Gasnier 					 &stm32_adc_buffer_setup_ops);
7400f883b22SFabrice Gasnier 	if (ret) {
741*da9b9485SFabrice Gasnier 		dev_err(&pdev->dev, "buffer setup failed\n");
7420f883b22SFabrice Gasnier 		goto err_clk_disable;
7430f883b22SFabrice Gasnier 	}
7440f883b22SFabrice Gasnier 
745*da9b9485SFabrice Gasnier 	ret = iio_device_register(indio_dev);
746*da9b9485SFabrice Gasnier 	if (ret) {
747*da9b9485SFabrice Gasnier 		dev_err(&pdev->dev, "iio dev register failed\n");
748*da9b9485SFabrice Gasnier 		goto err_buffer_cleanup;
749*da9b9485SFabrice Gasnier 	}
750*da9b9485SFabrice Gasnier 
7510f883b22SFabrice Gasnier 	return 0;
7520f883b22SFabrice Gasnier 
753*da9b9485SFabrice Gasnier err_buffer_cleanup:
754*da9b9485SFabrice Gasnier 	iio_triggered_buffer_cleanup(indio_dev);
755*da9b9485SFabrice Gasnier 
7560f883b22SFabrice Gasnier err_clk_disable:
7570f883b22SFabrice Gasnier 	clk_disable_unprepare(adc->clk);
7580f883b22SFabrice Gasnier 
7590f883b22SFabrice Gasnier 	return ret;
7600f883b22SFabrice Gasnier }
7610f883b22SFabrice Gasnier 
7620f883b22SFabrice Gasnier static int stm32_adc_remove(struct platform_device *pdev)
7630f883b22SFabrice Gasnier {
7640f883b22SFabrice Gasnier 	struct stm32_adc *adc = platform_get_drvdata(pdev);
7650f883b22SFabrice Gasnier 	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
7660f883b22SFabrice Gasnier 
7670f883b22SFabrice Gasnier 	iio_device_unregister(indio_dev);
768*da9b9485SFabrice Gasnier 	iio_triggered_buffer_cleanup(indio_dev);
7690f883b22SFabrice Gasnier 	clk_disable_unprepare(adc->clk);
7700f883b22SFabrice Gasnier 
7710f883b22SFabrice Gasnier 	return 0;
7720f883b22SFabrice Gasnier }
7730f883b22SFabrice Gasnier 
7740f883b22SFabrice Gasnier static const struct of_device_id stm32_adc_of_match[] = {
7750f883b22SFabrice Gasnier 	{ .compatible = "st,stm32f4-adc" },
7760f883b22SFabrice Gasnier 	{},
7770f883b22SFabrice Gasnier };
7780f883b22SFabrice Gasnier MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
7790f883b22SFabrice Gasnier 
7800f883b22SFabrice Gasnier static struct platform_driver stm32_adc_driver = {
7810f883b22SFabrice Gasnier 	.probe = stm32_adc_probe,
7820f883b22SFabrice Gasnier 	.remove = stm32_adc_remove,
7830f883b22SFabrice Gasnier 	.driver = {
7840f883b22SFabrice Gasnier 		.name = "stm32-adc",
7850f883b22SFabrice Gasnier 		.of_match_table = stm32_adc_of_match,
7860f883b22SFabrice Gasnier 	},
7870f883b22SFabrice Gasnier };
7880f883b22SFabrice Gasnier module_platform_driver(stm32_adc_driver);
7890f883b22SFabrice Gasnier 
7900f883b22SFabrice Gasnier MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
7910f883b22SFabrice Gasnier MODULE_DESCRIPTION("STMicroelectronics STM32 ADC IIO driver");
7920f883b22SFabrice Gasnier MODULE_LICENSE("GPL v2");
7930f883b22SFabrice Gasnier MODULE_ALIAS("platform:stm32-adc");
794