2 * This file is part of STM32 ADC driver
4 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
5 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/clk.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/dmaengine.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/timer/stm32-lptim-trigger.h>
29 #include <linux/iio/timer/stm32-timer-trigger.h>
30 #include <linux/iio/trigger.h>
31 #include <linux/iio/trigger_consumer.h>
32 #include <linux/iio/triggered_buffer.h>
33 #include <linux/interrupt.h>
35 #include <linux/iopoll.h>
36 #include <linux/module.h>
37 #include <linux/platform_device.h>
39 #include <linux/of_device.h>
41 #include "stm32-adc-core.h"
43 /* STM32F4 - Registers for each ADC instance */
44 #define STM32F4_ADC_SR 0x00
45 #define STM32F4_ADC_CR1 0x04
46 #define STM32F4_ADC_CR2 0x08
47 #define STM32F4_ADC_SMPR1 0x0C
48 #define STM32F4_ADC_SMPR2 0x10
49 #define STM32F4_ADC_HTR 0x24
50 #define STM32F4_ADC_LTR 0x28
51 #define STM32F4_ADC_SQR1 0x2C
52 #define STM32F4_ADC_SQR2 0x30
53 #define STM32F4_ADC_SQR3 0x34
54 #define STM32F4_ADC_JSQR 0x38
55 #define STM32F4_ADC_JDR1 0x3C
56 #define STM32F4_ADC_JDR2 0x40
57 #define STM32F4_ADC_JDR3 0x44
58 #define STM32F4_ADC_JDR4 0x48
59 #define STM32F4_ADC_DR 0x4C
61 /* STM32F4_ADC_SR - bit fields */
62 #define STM32F4_STRT BIT(4)
63 #define STM32F4_EOC BIT(1)
65 /* STM32F4_ADC_CR1 - bit fields */
66 #define STM32F4_RES_SHIFT 24
67 #define STM32F4_RES_MASK GENMASK(25, 24)
68 #define STM32F4_SCAN BIT(8)
69 #define STM32F4_EOCIE BIT(5)
71 /* STM32F4_ADC_CR2 - bit fields */
72 #define STM32F4_SWSTART BIT(30)
73 #define STM32F4_EXTEN_SHIFT 28
74 #define STM32F4_EXTEN_MASK GENMASK(29, 28)
75 #define STM32F4_EXTSEL_SHIFT 24
76 #define STM32F4_EXTSEL_MASK GENMASK(27, 24)
77 #define STM32F4_EOCS BIT(10)
78 #define STM32F4_DDS BIT(9)
79 #define STM32F4_DMA BIT(8)
80 #define STM32F4_ADON BIT(0)
82 /* STM32H7 - Registers for each ADC instance */
83 #define STM32H7_ADC_ISR 0x00
84 #define STM32H7_ADC_IER 0x04
85 #define STM32H7_ADC_CR 0x08
86 #define STM32H7_ADC_CFGR 0x0C
87 #define STM32H7_ADC_SMPR1 0x14
88 #define STM32H7_ADC_SMPR2 0x18
89 #define STM32H7_ADC_PCSEL 0x1C
90 #define STM32H7_ADC_SQR1 0x30
91 #define STM32H7_ADC_SQR2 0x34
92 #define STM32H7_ADC_SQR3 0x38
93 #define STM32H7_ADC_SQR4 0x3C
94 #define STM32H7_ADC_DR 0x40
95 #define STM32H7_ADC_DIFSEL 0xC0
96 #define STM32H7_ADC_CALFACT 0xC4
97 #define STM32H7_ADC_CALFACT2 0xC8
99 /* STM32H7_ADC_ISR - bit fields */
100 #define STM32H7_EOC BIT(2)
101 #define STM32H7_ADRDY BIT(0)
103 /* STM32H7_ADC_IER - bit fields */
104 #define STM32H7_EOCIE STM32H7_EOC
106 /* STM32H7_ADC_CR - bit fields */
107 #define STM32H7_ADCAL BIT(31)
108 #define STM32H7_ADCALDIF BIT(30)
109 #define STM32H7_DEEPPWD BIT(29)
110 #define STM32H7_ADVREGEN BIT(28)
111 #define STM32H7_LINCALRDYW6 BIT(27)
112 #define STM32H7_LINCALRDYW5 BIT(26)
113 #define STM32H7_LINCALRDYW4 BIT(25)
114 #define STM32H7_LINCALRDYW3 BIT(24)
115 #define STM32H7_LINCALRDYW2 BIT(23)
116 #define STM32H7_LINCALRDYW1 BIT(22)
117 #define STM32H7_ADCALLIN BIT(16)
118 #define STM32H7_BOOST BIT(8)
119 #define STM32H7_ADSTP BIT(4)
120 #define STM32H7_ADSTART BIT(2)
121 #define STM32H7_ADDIS BIT(1)
122 #define STM32H7_ADEN BIT(0)
124 /* STM32H7_ADC_CFGR bit fields */
125 #define STM32H7_EXTEN_SHIFT 10
126 #define STM32H7_EXTEN_MASK GENMASK(11, 10)
127 #define STM32H7_EXTSEL_SHIFT 5
128 #define STM32H7_EXTSEL_MASK GENMASK(9, 5)
129 #define STM32H7_RES_SHIFT 2
130 #define STM32H7_RES_MASK GENMASK(4, 2)
131 #define STM32H7_DMNGT_SHIFT 0
132 #define STM32H7_DMNGT_MASK GENMASK(1, 0)
134 enum stm32h7_adc_dmngt {
135 STM32H7_DMNGT_DR_ONLY, /* Regular data in DR only */
136 STM32H7_DMNGT_DMA_ONESHOT, /* DMA one shot mode */
137 STM32H7_DMNGT_DFSDM, /* DFSDM mode */
138 STM32H7_DMNGT_DMA_CIRC, /* DMA circular mode */
141 /* STM32H7_ADC_CALFACT - bit fields */
142 #define STM32H7_CALFACT_D_SHIFT 16
143 #define STM32H7_CALFACT_D_MASK GENMASK(26, 16)
144 #define STM32H7_CALFACT_S_SHIFT 0
145 #define STM32H7_CALFACT_S_MASK GENMASK(10, 0)
147 /* STM32H7_ADC_CALFACT2 - bit fields */
148 #define STM32H7_LINCALFACT_SHIFT 0
149 #define STM32H7_LINCALFACT_MASK GENMASK(29, 0)
151 /* Number of linear calibration shadow registers / LINCALRDYW control bits */
152 #define STM32H7_LINCALFACT_NUM 6
154 /* BOOST bit must be set on STM32H7 when ADC clock is above 20MHz */
155 #define STM32H7_BOOST_CLKRATE 20000000UL
157 #define STM32_ADC_CH_MAX 20 /* max number of channels */
158 #define STM32_ADC_CH_SZ 10 /* max channel name size */
159 #define STM32_ADC_MAX_SQ 16 /* SQ1..SQ16 */
160 #define STM32_ADC_MAX_SMP 7 /* SMPx range is [0..7] */
161 #define STM32_ADC_TIMEOUT_US 100000
162 #define STM32_ADC_TIMEOUT (msecs_to_jiffies(STM32_ADC_TIMEOUT_US / 1000))
164 #define STM32_DMA_BUFFER_SIZE PAGE_SIZE
166 /* External trigger enable */
167 enum stm32_adc_exten {
169 STM32_EXTEN_HWTRIG_RISING_EDGE,
170 STM32_EXTEN_HWTRIG_FALLING_EDGE,
171 STM32_EXTEN_HWTRIG_BOTH_EDGES,
174 /* extsel - trigger mux selection value */
175 enum stm32_adc_extsel {
200 * struct stm32_adc_trig_info - ADC trigger info
201 * @name: name of the trigger, corresponding to its source
202 * @extsel: trigger selection
204 struct stm32_adc_trig_info {
206 enum stm32_adc_extsel extsel;
210 * struct stm32_adc_calib - optional adc calibration data
211 * @calfact_s: Calibration offset for single ended channels
212 * @calfact_d: Calibration offset in differential
213 * @lincalfact: Linearity calibration factor
215 struct stm32_adc_calib {
218 u32 lincalfact[STM32H7_LINCALFACT_NUM];
222 * stm32_adc_regs - stm32 ADC misc registers & bitfield desc
223 * @reg: register offset
224 * @mask: bitfield mask
227 struct stm32_adc_regs {
234 * stm32_adc_regspec - stm32 registers definition, compatible dependent data
235 * @dr: data register offset
236 * @ier_eoc: interrupt enable register & eocie bitfield
237 * @isr_eoc: interrupt status register & eoc bitfield
238 * @sqr: reference to sequence registers array
239 * @exten: trigger control register & bitfield
240 * @extsel: trigger selection register & bitfield
241 * @res: resolution selection register & bitfield
242 * @smpr: smpr1 & smpr2 registers offset array
243 * @smp_bits: smpr1 & smpr2 index and bitfields
245 struct stm32_adc_regspec {
247 const struct stm32_adc_regs ier_eoc;
248 const struct stm32_adc_regs isr_eoc;
249 const struct stm32_adc_regs *sqr;
250 const struct stm32_adc_regs exten;
251 const struct stm32_adc_regs extsel;
252 const struct stm32_adc_regs res;
254 const struct stm32_adc_regs *smp_bits;
260 * stm32_adc_cfg - stm32 compatible configuration data
261 * @regs: registers descriptions
262 * @adc_info: per instance input channels definitions
263 * @trigs: external trigger sources
264 * @clk_required: clock is required
265 * @selfcalib: optional routine for self-calibration
266 * @prepare: optional prepare routine (power-up, enable)
267 * @start_conv: routine to start conversions
268 * @stop_conv: routine to stop conversions
269 * @unprepare: optional unprepare routine (disable, power-down)
270 * @smp_cycles: programmable sampling time (ADC clock cycles)
272 struct stm32_adc_cfg {
273 const struct stm32_adc_regspec *regs;
274 const struct stm32_adc_info *adc_info;
275 struct stm32_adc_trig_info *trigs;
277 int (*selfcalib)(struct stm32_adc *);
278 int (*prepare)(struct stm32_adc *);
279 void (*start_conv)(struct stm32_adc *, bool dma);
280 void (*stop_conv)(struct stm32_adc *);
281 void (*unprepare)(struct stm32_adc *);
282 const unsigned int *smp_cycles;
286 * struct stm32_adc - private data of each ADC IIO instance
287 * @common: reference to ADC block common data
288 * @offset: ADC instance register offset in ADC block
289 * @cfg: compatible configuration data
290 * @completion: end of single conversion completion
291 * @buffer: data buffer
292 * @clk: clock for this adc instance
293 * @irq: interrupt for this adc instance
295 * @bufi: data buffer index
296 * @num_conv: expected number of scan conversions
297 * @res: data resolution (e.g. RES bitfield value)
298 * @trigger_polarity: external trigger polarity (e.g. exten)
299 * @dma_chan: dma channel
300 * @rx_buf: dma rx buffer cpu address
301 * @rx_dma_buf: dma rx buffer bus address
302 * @rx_buf_sz: dma rx buffer size
303 * @difsel bitmask to set single-ended/differential channel
304 * @pcsel bitmask to preselect channels on some devices
305 * @smpr_val: sampling time settings (e.g. smpr1 / smpr2)
306 * @cal: optional calibration data on some devices
307 * @chan_name: channel name array
310 struct stm32_adc_common *common;
312 const struct stm32_adc_cfg *cfg;
313 struct completion completion;
314 u16 buffer[STM32_ADC_MAX_SQ];
317 spinlock_t lock; /* interrupt lock */
319 unsigned int num_conv;
321 u32 trigger_polarity;
322 struct dma_chan *dma_chan;
324 dma_addr_t rx_dma_buf;
325 unsigned int rx_buf_sz;
329 struct stm32_adc_calib cal;
330 char chan_name[STM32_ADC_CH_MAX][STM32_ADC_CH_SZ];
333 struct stm32_adc_diff_channel {
339 * struct stm32_adc_info - stm32 ADC, per instance config data
340 * @max_channels: Number of channels
341 * @resolutions: available resolutions
342 * @num_res: number of available resolutions
344 struct stm32_adc_info {
346 const unsigned int *resolutions;
347 const unsigned int num_res;
350 static const unsigned int stm32f4_adc_resolutions[] = {
351 /* sorted values so the index matches RES[1:0] in STM32F4_ADC_CR1 */
355 /* stm32f4 can have up to 16 channels */
356 static const struct stm32_adc_info stm32f4_adc_info = {
358 .resolutions = stm32f4_adc_resolutions,
359 .num_res = ARRAY_SIZE(stm32f4_adc_resolutions),
362 static const unsigned int stm32h7_adc_resolutions[] = {
363 /* sorted values so the index matches RES[2:0] in STM32H7_ADC_CFGR */
367 /* stm32h7 can have up to 20 channels */
368 static const struct stm32_adc_info stm32h7_adc_info = {
369 .max_channels = STM32_ADC_CH_MAX,
370 .resolutions = stm32h7_adc_resolutions,
371 .num_res = ARRAY_SIZE(stm32h7_adc_resolutions),
375 * stm32f4_sq - describe regular sequence registers
376 * - L: sequence len (register & bit field)
377 * - SQ1..SQ16: sequence entries (register & bit field)
379 static const struct stm32_adc_regs stm32f4_sq[STM32_ADC_MAX_SQ + 1] = {
380 /* L: len bit field description to be kept as first element */
381 { STM32F4_ADC_SQR1, GENMASK(23, 20), 20 },
382 /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
383 { STM32F4_ADC_SQR3, GENMASK(4, 0), 0 },
384 { STM32F4_ADC_SQR3, GENMASK(9, 5), 5 },
385 { STM32F4_ADC_SQR3, GENMASK(14, 10), 10 },
386 { STM32F4_ADC_SQR3, GENMASK(19, 15), 15 },
387 { STM32F4_ADC_SQR3, GENMASK(24, 20), 20 },
388 { STM32F4_ADC_SQR3, GENMASK(29, 25), 25 },
389 { STM32F4_ADC_SQR2, GENMASK(4, 0), 0 },
390 { STM32F4_ADC_SQR2, GENMASK(9, 5), 5 },
391 { STM32F4_ADC_SQR2, GENMASK(14, 10), 10 },
392 { STM32F4_ADC_SQR2, GENMASK(19, 15), 15 },
393 { STM32F4_ADC_SQR2, GENMASK(24, 20), 20 },
394 { STM32F4_ADC_SQR2, GENMASK(29, 25), 25 },
395 { STM32F4_ADC_SQR1, GENMASK(4, 0), 0 },
396 { STM32F4_ADC_SQR1, GENMASK(9, 5), 5 },
397 { STM32F4_ADC_SQR1, GENMASK(14, 10), 10 },
398 { STM32F4_ADC_SQR1, GENMASK(19, 15), 15 },
401 /* STM32F4 external trigger sources for all instances */
402 static struct stm32_adc_trig_info stm32f4_adc_trigs[] = {
403 { TIM1_CH1, STM32_EXT0 },
404 { TIM1_CH2, STM32_EXT1 },
405 { TIM1_CH3, STM32_EXT2 },
406 { TIM2_CH2, STM32_EXT3 },
407 { TIM2_CH3, STM32_EXT4 },
408 { TIM2_CH4, STM32_EXT5 },
409 { TIM2_TRGO, STM32_EXT6 },
410 { TIM3_CH1, STM32_EXT7 },
411 { TIM3_TRGO, STM32_EXT8 },
412 { TIM4_CH4, STM32_EXT9 },
413 { TIM5_CH1, STM32_EXT10 },
414 { TIM5_CH2, STM32_EXT11 },
415 { TIM5_CH3, STM32_EXT12 },
416 { TIM8_CH1, STM32_EXT13 },
417 { TIM8_TRGO, STM32_EXT14 },
422 * stm32f4_smp_bits[] - describe sampling time register index & bit fields
423 * Sorted so it can be indexed by channel number.
425 static const struct stm32_adc_regs stm32f4_smp_bits[] = {
426 /* STM32F4_ADC_SMPR2: smpr[] index, mask, shift for SMP0 to SMP9 */
427 { 1, GENMASK(2, 0), 0 },
428 { 1, GENMASK(5, 3), 3 },
429 { 1, GENMASK(8, 6), 6 },
430 { 1, GENMASK(11, 9), 9 },
431 { 1, GENMASK(14, 12), 12 },
432 { 1, GENMASK(17, 15), 15 },
433 { 1, GENMASK(20, 18), 18 },
434 { 1, GENMASK(23, 21), 21 },
435 { 1, GENMASK(26, 24), 24 },
436 { 1, GENMASK(29, 27), 27 },
437 /* STM32F4_ADC_SMPR1, smpr[] index, mask, shift for SMP10 to SMP18 */
438 { 0, GENMASK(2, 0), 0 },
439 { 0, GENMASK(5, 3), 3 },
440 { 0, GENMASK(8, 6), 6 },
441 { 0, GENMASK(11, 9), 9 },
442 { 0, GENMASK(14, 12), 12 },
443 { 0, GENMASK(17, 15), 15 },
444 { 0, GENMASK(20, 18), 18 },
445 { 0, GENMASK(23, 21), 21 },
446 { 0, GENMASK(26, 24), 24 },
449 /* STM32F4 programmable sampling time (ADC clock cycles) */
450 static const unsigned int stm32f4_adc_smp_cycles[STM32_ADC_MAX_SMP + 1] = {
451 3, 15, 28, 56, 84, 112, 144, 480,
454 static const struct stm32_adc_regspec stm32f4_adc_regspec = {
455 .dr = STM32F4_ADC_DR,
456 .ier_eoc = { STM32F4_ADC_CR1, STM32F4_EOCIE },
457 .isr_eoc = { STM32F4_ADC_SR, STM32F4_EOC },
459 .exten = { STM32F4_ADC_CR2, STM32F4_EXTEN_MASK, STM32F4_EXTEN_SHIFT },
460 .extsel = { STM32F4_ADC_CR2, STM32F4_EXTSEL_MASK,
461 STM32F4_EXTSEL_SHIFT },
462 .res = { STM32F4_ADC_CR1, STM32F4_RES_MASK, STM32F4_RES_SHIFT },
463 .smpr = { STM32F4_ADC_SMPR1, STM32F4_ADC_SMPR2 },
464 .smp_bits = stm32f4_smp_bits,
467 static const struct stm32_adc_regs stm32h7_sq[STM32_ADC_MAX_SQ + 1] = {
468 /* L: len bit field description to be kept as first element */
469 { STM32H7_ADC_SQR1, GENMASK(3, 0), 0 },
470 /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
471 { STM32H7_ADC_SQR1, GENMASK(10, 6), 6 },
472 { STM32H7_ADC_SQR1, GENMASK(16, 12), 12 },
473 { STM32H7_ADC_SQR1, GENMASK(22, 18), 18 },
474 { STM32H7_ADC_SQR1, GENMASK(28, 24), 24 },
475 { STM32H7_ADC_SQR2, GENMASK(4, 0), 0 },
476 { STM32H7_ADC_SQR2, GENMASK(10, 6), 6 },
477 { STM32H7_ADC_SQR2, GENMASK(16, 12), 12 },
478 { STM32H7_ADC_SQR2, GENMASK(22, 18), 18 },
479 { STM32H7_ADC_SQR2, GENMASK(28, 24), 24 },
480 { STM32H7_ADC_SQR3, GENMASK(4, 0), 0 },
481 { STM32H7_ADC_SQR3, GENMASK(10, 6), 6 },
482 { STM32H7_ADC_SQR3, GENMASK(16, 12), 12 },
483 { STM32H7_ADC_SQR3, GENMASK(22, 18), 18 },
484 { STM32H7_ADC_SQR3, GENMASK(28, 24), 24 },
485 { STM32H7_ADC_SQR4, GENMASK(4, 0), 0 },
486 { STM32H7_ADC_SQR4, GENMASK(10, 6), 6 },
489 /* STM32H7 external trigger sources for all instances */
490 static struct stm32_adc_trig_info stm32h7_adc_trigs[] = {
491 { TIM1_CH1, STM32_EXT0 },
492 { TIM1_CH2, STM32_EXT1 },
493 { TIM1_CH3, STM32_EXT2 },
494 { TIM2_CH2, STM32_EXT3 },
495 { TIM3_TRGO, STM32_EXT4 },
496 { TIM4_CH4, STM32_EXT5 },
497 { TIM8_TRGO, STM32_EXT7 },
498 { TIM8_TRGO2, STM32_EXT8 },
499 { TIM1_TRGO, STM32_EXT9 },
500 { TIM1_TRGO2, STM32_EXT10 },
501 { TIM2_TRGO, STM32_EXT11 },
502 { TIM4_TRGO, STM32_EXT12 },
503 { TIM6_TRGO, STM32_EXT13 },
504 { TIM15_TRGO, STM32_EXT14 },
505 { TIM3_CH4, STM32_EXT15 },
506 { LPTIM1_OUT, STM32_EXT18 },
507 { LPTIM2_OUT, STM32_EXT19 },
508 { LPTIM3_OUT, STM32_EXT20 },
513 * stm32h7_smp_bits - describe sampling time register index & bit fields
514 * Sorted so it can be indexed by channel number.
516 static const struct stm32_adc_regs stm32h7_smp_bits[] = {
517 /* STM32H7_ADC_SMPR1, smpr[] index, mask, shift for SMP0 to SMP9 */
518 { 0, GENMASK(2, 0), 0 },
519 { 0, GENMASK(5, 3), 3 },
520 { 0, GENMASK(8, 6), 6 },
521 { 0, GENMASK(11, 9), 9 },
522 { 0, GENMASK(14, 12), 12 },
523 { 0, GENMASK(17, 15), 15 },
524 { 0, GENMASK(20, 18), 18 },
525 { 0, GENMASK(23, 21), 21 },
526 { 0, GENMASK(26, 24), 24 },
527 { 0, GENMASK(29, 27), 27 },
528 /* STM32H7_ADC_SMPR2, smpr[] index, mask, shift for SMP10 to SMP19 */
529 { 1, GENMASK(2, 0), 0 },
530 { 1, GENMASK(5, 3), 3 },
531 { 1, GENMASK(8, 6), 6 },
532 { 1, GENMASK(11, 9), 9 },
533 { 1, GENMASK(14, 12), 12 },
534 { 1, GENMASK(17, 15), 15 },
535 { 1, GENMASK(20, 18), 18 },
536 { 1, GENMASK(23, 21), 21 },
537 { 1, GENMASK(26, 24), 24 },
538 { 1, GENMASK(29, 27), 27 },
541 /* STM32H7 programmable sampling time (ADC clock cycles, rounded down) */
542 static const unsigned int stm32h7_adc_smp_cycles[STM32_ADC_MAX_SMP + 1] = {
543 1, 2, 8, 16, 32, 64, 387, 810,
546 static const struct stm32_adc_regspec stm32h7_adc_regspec = {
547 .dr = STM32H7_ADC_DR,
548 .ier_eoc = { STM32H7_ADC_IER, STM32H7_EOCIE },
549 .isr_eoc = { STM32H7_ADC_ISR, STM32H7_EOC },
551 .exten = { STM32H7_ADC_CFGR, STM32H7_EXTEN_MASK, STM32H7_EXTEN_SHIFT },
552 .extsel = { STM32H7_ADC_CFGR, STM32H7_EXTSEL_MASK,
553 STM32H7_EXTSEL_SHIFT },
554 .res = { STM32H7_ADC_CFGR, STM32H7_RES_MASK, STM32H7_RES_SHIFT },
555 .smpr = { STM32H7_ADC_SMPR1, STM32H7_ADC_SMPR2 },
556 .smp_bits = stm32h7_smp_bits,
560 * STM32 ADC registers access routines
561 * @adc: stm32 adc instance
562 * @reg: reg offset in adc instance
564 * Note: All instances share same base, with 0x0, 0x100 or 0x200 offset resp.
565 * for adc1, adc2 and adc3.
567 static u32 stm32_adc_readl(struct stm32_adc *adc, u32 reg)
569 return readl_relaxed(adc->common->base + adc->offset + reg);
572 #define stm32_adc_readl_addr(addr) stm32_adc_readl(adc, addr)
574 #define stm32_adc_readl_poll_timeout(reg, val, cond, sleep_us, timeout_us) \
575 readx_poll_timeout(stm32_adc_readl_addr, reg, val, \
576 cond, sleep_us, timeout_us)
578 static u16 stm32_adc_readw(struct stm32_adc *adc, u32 reg)
580 return readw_relaxed(adc->common->base + adc->offset + reg);
583 static void stm32_adc_writel(struct stm32_adc *adc, u32 reg, u32 val)
585 writel_relaxed(val, adc->common->base + adc->offset + reg);
588 static void stm32_adc_set_bits(struct stm32_adc *adc, u32 reg, u32 bits)
592 spin_lock_irqsave(&adc->lock, flags);
593 stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) | bits);
594 spin_unlock_irqrestore(&adc->lock, flags);
597 static void stm32_adc_clr_bits(struct stm32_adc *adc, u32 reg, u32 bits)
601 spin_lock_irqsave(&adc->lock, flags);
602 stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) & ~bits);
603 spin_unlock_irqrestore(&adc->lock, flags);
607 * stm32_adc_conv_irq_enable() - Enable end of conversion interrupt
608 * @adc: stm32 adc instance
610 static void stm32_adc_conv_irq_enable(struct stm32_adc *adc)
612 stm32_adc_set_bits(adc, adc->cfg->regs->ier_eoc.reg,
613 adc->cfg->regs->ier_eoc.mask);
617 * stm32_adc_conv_irq_disable() - Disable end of conversion interrupt
618 * @adc: stm32 adc instance
620 static void stm32_adc_conv_irq_disable(struct stm32_adc *adc)
622 stm32_adc_clr_bits(adc, adc->cfg->regs->ier_eoc.reg,
623 adc->cfg->regs->ier_eoc.mask);
626 static void stm32_adc_set_res(struct stm32_adc *adc)
628 const struct stm32_adc_regs *res = &adc->cfg->regs->res;
631 val = stm32_adc_readl(adc, res->reg);
632 val = (val & ~res->mask) | (adc->res << res->shift);
633 stm32_adc_writel(adc, res->reg, val);
637 * stm32f4_adc_start_conv() - Start conversions for regular channels.
638 * @adc: stm32 adc instance
639 * @dma: use dma to transfer conversion result
641 * Start conversions for regular channels.
642 * Also take care of normal or DMA mode. Circular DMA may be used for regular
643 * conversions, in IIO buffer modes. Otherwise, use ADC interrupt with direct
644 * DR read instead (e.g. read_raw, or triggered buffer mode without DMA).
646 static void stm32f4_adc_start_conv(struct stm32_adc *adc, bool dma)
648 stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN);
651 stm32_adc_set_bits(adc, STM32F4_ADC_CR2,
652 STM32F4_DMA | STM32F4_DDS);
654 stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_EOCS | STM32F4_ADON);
656 /* Wait for Power-up time (tSTAB from datasheet) */
659 /* Software start ? (e.g. trigger detection disabled ?) */
660 if (!(stm32_adc_readl(adc, STM32F4_ADC_CR2) & STM32F4_EXTEN_MASK))
661 stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_SWSTART);
664 static void stm32f4_adc_stop_conv(struct stm32_adc *adc)
666 stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK);
667 stm32_adc_clr_bits(adc, STM32F4_ADC_SR, STM32F4_STRT);
669 stm32_adc_clr_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN);
670 stm32_adc_clr_bits(adc, STM32F4_ADC_CR2,
671 STM32F4_ADON | STM32F4_DMA | STM32F4_DDS);
674 static void stm32h7_adc_start_conv(struct stm32_adc *adc, bool dma)
676 enum stm32h7_adc_dmngt dmngt;
681 dmngt = STM32H7_DMNGT_DMA_CIRC;
683 dmngt = STM32H7_DMNGT_DR_ONLY;
685 spin_lock_irqsave(&adc->lock, flags);
686 val = stm32_adc_readl(adc, STM32H7_ADC_CFGR);
687 val = (val & ~STM32H7_DMNGT_MASK) | (dmngt << STM32H7_DMNGT_SHIFT);
688 stm32_adc_writel(adc, STM32H7_ADC_CFGR, val);
689 spin_unlock_irqrestore(&adc->lock, flags);
691 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTART);
694 static void stm32h7_adc_stop_conv(struct stm32_adc *adc)
696 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
700 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTP);
702 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
703 !(val & (STM32H7_ADSTART)),
704 100, STM32_ADC_TIMEOUT_US);
706 dev_warn(&indio_dev->dev, "stop failed\n");
708 stm32_adc_clr_bits(adc, STM32H7_ADC_CFGR, STM32H7_DMNGT_MASK);
711 static void stm32h7_adc_exit_pwr_down(struct stm32_adc *adc)
713 /* Exit deep power down, then enable ADC voltage regulator */
714 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD);
715 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADVREGEN);
717 if (adc->common->rate > STM32H7_BOOST_CLKRATE)
718 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_BOOST);
720 /* Wait for startup time */
721 usleep_range(10, 20);
724 static void stm32h7_adc_enter_pwr_down(struct stm32_adc *adc)
726 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_BOOST);
728 /* Setting DEEPPWD disables ADC vreg and clears ADVREGEN */
729 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD);
732 static int stm32h7_adc_enable(struct stm32_adc *adc)
734 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
738 /* Clear ADRDY by writing one, then enable ADC */
739 stm32_adc_set_bits(adc, STM32H7_ADC_ISR, STM32H7_ADRDY);
740 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADEN);
742 /* Poll for ADRDY to be set (after adc startup time) */
743 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR, val,
745 100, STM32_ADC_TIMEOUT_US);
747 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADEN);
748 dev_err(&indio_dev->dev, "Failed to enable ADC\n");
754 static void stm32h7_adc_disable(struct stm32_adc *adc)
756 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
760 /* Disable ADC and wait until it's effectively disabled */
761 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADDIS);
762 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
763 !(val & STM32H7_ADEN), 100,
764 STM32_ADC_TIMEOUT_US);
766 dev_warn(&indio_dev->dev, "Failed to disable\n");
770 * stm32h7_adc_read_selfcalib() - read calibration shadow regs, save result
771 * @adc: stm32 adc instance
773 static int stm32h7_adc_read_selfcalib(struct stm32_adc *adc)
775 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
777 u32 lincalrdyw_mask, val;
779 /* Enable adc so LINCALRDYW1..6 bits are writable */
780 ret = stm32h7_adc_enable(adc);
784 /* Read linearity calibration */
785 lincalrdyw_mask = STM32H7_LINCALRDYW6;
786 for (i = STM32H7_LINCALFACT_NUM - 1; i >= 0; i--) {
787 /* Clear STM32H7_LINCALRDYW[6..1]: transfer calib to CALFACT2 */
788 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask);
790 /* Poll: wait calib data to be ready in CALFACT2 register */
791 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
792 !(val & lincalrdyw_mask),
793 100, STM32_ADC_TIMEOUT_US);
795 dev_err(&indio_dev->dev, "Failed to read calfact\n");
799 val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2);
800 adc->cal.lincalfact[i] = (val & STM32H7_LINCALFACT_MASK);
801 adc->cal.lincalfact[i] >>= STM32H7_LINCALFACT_SHIFT;
803 lincalrdyw_mask >>= 1;
806 /* Read offset calibration */
807 val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT);
808 adc->cal.calfact_s = (val & STM32H7_CALFACT_S_MASK);
809 adc->cal.calfact_s >>= STM32H7_CALFACT_S_SHIFT;
810 adc->cal.calfact_d = (val & STM32H7_CALFACT_D_MASK);
811 adc->cal.calfact_d >>= STM32H7_CALFACT_D_SHIFT;
814 stm32h7_adc_disable(adc);
820 * stm32h7_adc_restore_selfcalib() - Restore saved self-calibration result
821 * @adc: stm32 adc instance
822 * Note: ADC must be enabled, with no on-going conversions.
824 static int stm32h7_adc_restore_selfcalib(struct stm32_adc *adc)
826 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
828 u32 lincalrdyw_mask, val;
830 val = (adc->cal.calfact_s << STM32H7_CALFACT_S_SHIFT) |
831 (adc->cal.calfact_d << STM32H7_CALFACT_D_SHIFT);
832 stm32_adc_writel(adc, STM32H7_ADC_CALFACT, val);
834 lincalrdyw_mask = STM32H7_LINCALRDYW6;
835 for (i = STM32H7_LINCALFACT_NUM - 1; i >= 0; i--) {
837 * Write saved calibration data to shadow registers:
838 * Write CALFACT2, and set LINCALRDYW[6..1] bit to trigger
839 * data write. Then poll to wait for complete transfer.
841 val = adc->cal.lincalfact[i] << STM32H7_LINCALFACT_SHIFT;
842 stm32_adc_writel(adc, STM32H7_ADC_CALFACT2, val);
843 stm32_adc_set_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask);
844 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
845 val & lincalrdyw_mask,
846 100, STM32_ADC_TIMEOUT_US);
848 dev_err(&indio_dev->dev, "Failed to write calfact\n");
853 * Read back calibration data, has two effects:
854 * - It ensures bits LINCALRDYW[6..1] are kept cleared
855 * for next time calibration needs to be restored.
856 * - BTW, bit clear triggers a read, then check data has been
859 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask);
860 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
861 !(val & lincalrdyw_mask),
862 100, STM32_ADC_TIMEOUT_US);
864 dev_err(&indio_dev->dev, "Failed to read calfact\n");
867 val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2);
868 if (val != adc->cal.lincalfact[i] << STM32H7_LINCALFACT_SHIFT) {
869 dev_err(&indio_dev->dev, "calfact not consistent\n");
873 lincalrdyw_mask >>= 1;
880 * Fixed timeout value for ADC calibration.
882 * - low clock frequency
883 * - maximum prescalers
884 * Calibration requires:
885 * - 131,072 ADC clock cycle for the linear calibration
886 * - 20 ADC clock cycle for the offset calibration
888 * Set to 100ms for now
890 #define STM32H7_ADC_CALIB_TIMEOUT_US 100000
893 * stm32h7_adc_selfcalib() - Procedure to calibrate ADC (from power down)
894 * @adc: stm32 adc instance
895 * Exit from power down, calibrate ADC, then return to power down.
897 static int stm32h7_adc_selfcalib(struct stm32_adc *adc)
899 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
903 stm32h7_adc_exit_pwr_down(adc);
906 * Select calibration mode:
907 * - Offset calibration for single ended inputs
908 * - No linearity calibration (do it later, before reading it)
910 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADCALDIF);
911 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADCALLIN);
913 /* Start calibration, then wait for completion */
914 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADCAL);
915 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
916 !(val & STM32H7_ADCAL), 100,
917 STM32H7_ADC_CALIB_TIMEOUT_US);
919 dev_err(&indio_dev->dev, "calibration failed\n");
924 * Select calibration mode, then start calibration:
925 * - Offset calibration for differential input
926 * - Linearity calibration (needs to be done only once for single/diff)
927 * will run simultaneously with offset calibration.
929 stm32_adc_set_bits(adc, STM32H7_ADC_CR,
930 STM32H7_ADCALDIF | STM32H7_ADCALLIN);
931 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADCAL);
932 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
933 !(val & STM32H7_ADCAL), 100,
934 STM32H7_ADC_CALIB_TIMEOUT_US);
936 dev_err(&indio_dev->dev, "calibration failed\n");
940 stm32_adc_clr_bits(adc, STM32H7_ADC_CR,
941 STM32H7_ADCALDIF | STM32H7_ADCALLIN);
943 /* Read calibration result for future reference */
944 ret = stm32h7_adc_read_selfcalib(adc);
947 stm32h7_adc_enter_pwr_down(adc);
953 * stm32h7_adc_prepare() - Leave power down mode to enable ADC.
954 * @adc: stm32 adc instance
955 * Leave power down mode.
956 * Configure channels as single ended or differential before enabling ADC.
958 * Restore calibration data.
959 * Pre-select channels that may be used in PCSEL (required by input MUX / IO):
960 * - Only one input is selected for single ended (e.g. 'vinp')
961 * - Two inputs are selected for differential channels (e.g. 'vinp' & 'vinn')
963 static int stm32h7_adc_prepare(struct stm32_adc *adc)
967 stm32h7_adc_exit_pwr_down(adc);
968 stm32_adc_writel(adc, STM32H7_ADC_DIFSEL, adc->difsel);
970 ret = stm32h7_adc_enable(adc);
974 ret = stm32h7_adc_restore_selfcalib(adc);
978 stm32_adc_writel(adc, STM32H7_ADC_PCSEL, adc->pcsel);
983 stm32h7_adc_disable(adc);
985 stm32h7_adc_enter_pwr_down(adc);
990 static void stm32h7_adc_unprepare(struct stm32_adc *adc)
992 stm32h7_adc_disable(adc);
993 stm32h7_adc_enter_pwr_down(adc);
997 * stm32_adc_conf_scan_seq() - Build regular channels scan sequence
998 * @indio_dev: IIO device
999 * @scan_mask: channels to be converted
1001 * Conversion sequence :
1002 * Apply sampling time settings for all channels.
1003 * Configure ADC scan sequence based on selected channels in scan_mask.
1004 * Add channels to SQR registers, from scan_mask LSB to MSB, then
1005 * program sequence len.
1007 static int stm32_adc_conf_scan_seq(struct iio_dev *indio_dev,
1008 const unsigned long *scan_mask)
1010 struct stm32_adc *adc = iio_priv(indio_dev);
1011 const struct stm32_adc_regs *sqr = adc->cfg->regs->sqr;
1012 const struct iio_chan_spec *chan;
1016 /* Apply sampling time settings */
1017 stm32_adc_writel(adc, adc->cfg->regs->smpr[0], adc->smpr_val[0]);
1018 stm32_adc_writel(adc, adc->cfg->regs->smpr[1], adc->smpr_val[1]);
1020 for_each_set_bit(bit, scan_mask, indio_dev->masklength) {
1021 chan = indio_dev->channels + bit;
1023 * Assign one channel per SQ entry in regular
1024 * sequence, starting with SQ1.
1027 if (i > STM32_ADC_MAX_SQ)
1030 dev_dbg(&indio_dev->dev, "%s chan %d to SQ%d\n",
1031 __func__, chan->channel, i);
1033 val = stm32_adc_readl(adc, sqr[i].reg);
1034 val &= ~sqr[i].mask;
1035 val |= chan->channel << sqr[i].shift;
1036 stm32_adc_writel(adc, sqr[i].reg, val);
1043 val = stm32_adc_readl(adc, sqr[0].reg);
1044 val &= ~sqr[0].mask;
1045 val |= ((i - 1) << sqr[0].shift);
1046 stm32_adc_writel(adc, sqr[0].reg, val);
1052 * stm32_adc_get_trig_extsel() - Get external trigger selection
1055 * Returns trigger extsel value, if trig matches, -EINVAL otherwise.
1057 static int stm32_adc_get_trig_extsel(struct iio_dev *indio_dev,
1058 struct iio_trigger *trig)
1060 struct stm32_adc *adc = iio_priv(indio_dev);
1063 /* lookup triggers registered by stm32 timer trigger driver */
1064 for (i = 0; adc->cfg->trigs[i].name; i++) {
1066 * Checking both stm32 timer trigger type and trig name
1067 * should be safe against arbitrary trigger names.
1069 if ((is_stm32_timer_trigger(trig) ||
1070 is_stm32_lptim_trigger(trig)) &&
1071 !strcmp(adc->cfg->trigs[i].name, trig->name)) {
1072 return adc->cfg->trigs[i].extsel;
1080 * stm32_adc_set_trig() - Set a regular trigger
1081 * @indio_dev: IIO device
1082 * @trig: IIO trigger
1084 * Set trigger source/polarity (e.g. SW, or HW with polarity) :
1085 * - if HW trigger disabled (e.g. trig == NULL, conversion launched by sw)
1086 * - if HW trigger enabled, set source & polarity
1088 static int stm32_adc_set_trig(struct iio_dev *indio_dev,
1089 struct iio_trigger *trig)
1091 struct stm32_adc *adc = iio_priv(indio_dev);
1092 u32 val, extsel = 0, exten = STM32_EXTEN_SWTRIG;
1093 unsigned long flags;
1097 ret = stm32_adc_get_trig_extsel(indio_dev, trig);
1101 /* set trigger source and polarity (default to rising edge) */
1103 exten = adc->trigger_polarity + STM32_EXTEN_HWTRIG_RISING_EDGE;
1106 spin_lock_irqsave(&adc->lock, flags);
1107 val = stm32_adc_readl(adc, adc->cfg->regs->exten.reg);
1108 val &= ~(adc->cfg->regs->exten.mask | adc->cfg->regs->extsel.mask);
1109 val |= exten << adc->cfg->regs->exten.shift;
1110 val |= extsel << adc->cfg->regs->extsel.shift;
1111 stm32_adc_writel(adc, adc->cfg->regs->exten.reg, val);
1112 spin_unlock_irqrestore(&adc->lock, flags);
1117 static int stm32_adc_set_trig_pol(struct iio_dev *indio_dev,
1118 const struct iio_chan_spec *chan,
1121 struct stm32_adc *adc = iio_priv(indio_dev);
1123 adc->trigger_polarity = type;
1128 static int stm32_adc_get_trig_pol(struct iio_dev *indio_dev,
1129 const struct iio_chan_spec *chan)
1131 struct stm32_adc *adc = iio_priv(indio_dev);
1133 return adc->trigger_polarity;
1136 static const char * const stm32_trig_pol_items[] = {
1137 "rising-edge", "falling-edge", "both-edges",
1140 static const struct iio_enum stm32_adc_trig_pol = {
1141 .items = stm32_trig_pol_items,
1142 .num_items = ARRAY_SIZE(stm32_trig_pol_items),
1143 .get = stm32_adc_get_trig_pol,
1144 .set = stm32_adc_set_trig_pol,
1148 * stm32_adc_single_conv() - Performs a single conversion
1149 * @indio_dev: IIO device
1150 * @chan: IIO channel
1151 * @res: conversion result
1153 * The function performs a single conversion on a given channel:
1154 * - Apply sampling time settings
1155 * - Program sequencer with one channel (e.g. in SQ1 with len = 1)
1157 * - Start conversion, then wait for interrupt completion.
1159 static int stm32_adc_single_conv(struct iio_dev *indio_dev,
1160 const struct iio_chan_spec *chan,
1163 struct stm32_adc *adc = iio_priv(indio_dev);
1164 const struct stm32_adc_regspec *regs = adc->cfg->regs;
1169 reinit_completion(&adc->completion);
1173 if (adc->cfg->prepare) {
1174 ret = adc->cfg->prepare(adc);
1179 /* Apply sampling time settings */
1180 stm32_adc_writel(adc, regs->smpr[0], adc->smpr_val[0]);
1181 stm32_adc_writel(adc, regs->smpr[1], adc->smpr_val[1]);
1183 /* Program chan number in regular sequence (SQ1) */
1184 val = stm32_adc_readl(adc, regs->sqr[1].reg);
1185 val &= ~regs->sqr[1].mask;
1186 val |= chan->channel << regs->sqr[1].shift;
1187 stm32_adc_writel(adc, regs->sqr[1].reg, val);
1189 /* Set regular sequence len (0 for 1 conversion) */
1190 stm32_adc_clr_bits(adc, regs->sqr[0].reg, regs->sqr[0].mask);
1192 /* Trigger detection disabled (conversion can be launched in SW) */
1193 stm32_adc_clr_bits(adc, regs->exten.reg, regs->exten.mask);
1195 stm32_adc_conv_irq_enable(adc);
1197 adc->cfg->start_conv(adc, false);
1199 timeout = wait_for_completion_interruptible_timeout(
1200 &adc->completion, STM32_ADC_TIMEOUT);
1203 } else if (timeout < 0) {
1206 *res = adc->buffer[0];
1210 adc->cfg->stop_conv(adc);
1212 stm32_adc_conv_irq_disable(adc);
1214 if (adc->cfg->unprepare)
1215 adc->cfg->unprepare(adc);
1220 static int stm32_adc_read_raw(struct iio_dev *indio_dev,
1221 struct iio_chan_spec const *chan,
1222 int *val, int *val2, long mask)
1224 struct stm32_adc *adc = iio_priv(indio_dev);
1228 case IIO_CHAN_INFO_RAW:
1229 ret = iio_device_claim_direct_mode(indio_dev);
1232 if (chan->type == IIO_VOLTAGE)
1233 ret = stm32_adc_single_conv(indio_dev, chan, val);
1236 iio_device_release_direct_mode(indio_dev);
1239 case IIO_CHAN_INFO_SCALE:
1240 if (chan->differential) {
1241 *val = adc->common->vref_mv * 2;
1242 *val2 = chan->scan_type.realbits;
1244 *val = adc->common->vref_mv;
1245 *val2 = chan->scan_type.realbits;
1247 return IIO_VAL_FRACTIONAL_LOG2;
1249 case IIO_CHAN_INFO_OFFSET:
1250 if (chan->differential)
1251 /* ADC_full_scale / 2 */
1252 *val = -((1 << chan->scan_type.realbits) / 2);
1262 static irqreturn_t stm32_adc_isr(int irq, void *data)
1264 struct stm32_adc *adc = data;
1265 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
1266 const struct stm32_adc_regspec *regs = adc->cfg->regs;
1267 u32 status = stm32_adc_readl(adc, regs->isr_eoc.reg);
1269 if (status & regs->isr_eoc.mask) {
1270 /* Reading DR also clears EOC status flag */
1271 adc->buffer[adc->bufi] = stm32_adc_readw(adc, regs->dr);
1272 if (iio_buffer_enabled(indio_dev)) {
1274 if (adc->bufi >= adc->num_conv) {
1275 stm32_adc_conv_irq_disable(adc);
1276 iio_trigger_poll(indio_dev->trig);
1279 complete(&adc->completion);
1288 * stm32_adc_validate_trigger() - validate trigger for stm32 adc
1289 * @indio_dev: IIO device
1290 * @trig: new trigger
1292 * Returns: 0 if trig matches one of the triggers registered by stm32 adc
1293 * driver, -EINVAL otherwise.
1295 static int stm32_adc_validate_trigger(struct iio_dev *indio_dev,
1296 struct iio_trigger *trig)
1298 return stm32_adc_get_trig_extsel(indio_dev, trig) < 0 ? -EINVAL : 0;
1301 static int stm32_adc_set_watermark(struct iio_dev *indio_dev, unsigned int val)
1303 struct stm32_adc *adc = iio_priv(indio_dev);
1304 unsigned int watermark = STM32_DMA_BUFFER_SIZE / 2;
1307 * dma cyclic transfers are used, buffer is split into two periods.
1309 * - always one buffer (period) dma is working on
1310 * - one buffer (period) driver can push with iio_trigger_poll().
1312 watermark = min(watermark, val * (unsigned)(sizeof(u16)));
1313 adc->rx_buf_sz = watermark * 2;
1318 static int stm32_adc_update_scan_mode(struct iio_dev *indio_dev,
1319 const unsigned long *scan_mask)
1321 struct stm32_adc *adc = iio_priv(indio_dev);
1324 adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength);
1326 ret = stm32_adc_conf_scan_seq(indio_dev, scan_mask);
1333 static int stm32_adc_of_xlate(struct iio_dev *indio_dev,
1334 const struct of_phandle_args *iiospec)
1338 for (i = 0; i < indio_dev->num_channels; i++)
1339 if (indio_dev->channels[i].channel == iiospec->args[0])
1346 * stm32_adc_debugfs_reg_access - read or write register value
1348 * To read a value from an ADC register:
1349 * echo [ADC reg offset] > direct_reg_access
1350 * cat direct_reg_access
1352 * To write a value in a ADC register:
1353 * echo [ADC_reg_offset] [value] > direct_reg_access
1355 static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev,
1356 unsigned reg, unsigned writeval,
1359 struct stm32_adc *adc = iio_priv(indio_dev);
1362 stm32_adc_writel(adc, reg, writeval);
1364 *readval = stm32_adc_readl(adc, reg);
1369 static const struct iio_info stm32_adc_iio_info = {
1370 .read_raw = stm32_adc_read_raw,
1371 .validate_trigger = stm32_adc_validate_trigger,
1372 .hwfifo_set_watermark = stm32_adc_set_watermark,
1373 .update_scan_mode = stm32_adc_update_scan_mode,
1374 .debugfs_reg_access = stm32_adc_debugfs_reg_access,
1375 .of_xlate = stm32_adc_of_xlate,
1378 static unsigned int stm32_adc_dma_residue(struct stm32_adc *adc)
1380 struct dma_tx_state state;
1381 enum dma_status status;
1383 status = dmaengine_tx_status(adc->dma_chan,
1384 adc->dma_chan->cookie,
1386 if (status == DMA_IN_PROGRESS) {
1387 /* Residue is size in bytes from end of buffer */
1388 unsigned int i = adc->rx_buf_sz - state.residue;
1391 /* Return available bytes */
1393 size = i - adc->bufi;
1395 size = adc->rx_buf_sz + i - adc->bufi;
1403 static void stm32_adc_dma_buffer_done(void *data)
1405 struct iio_dev *indio_dev = data;
1407 iio_trigger_poll_chained(indio_dev->trig);
1410 static int stm32_adc_dma_start(struct iio_dev *indio_dev)
1412 struct stm32_adc *adc = iio_priv(indio_dev);
1413 struct dma_async_tx_descriptor *desc;
1414 dma_cookie_t cookie;
1420 dev_dbg(&indio_dev->dev, "%s size=%d watermark=%d\n", __func__,
1421 adc->rx_buf_sz, adc->rx_buf_sz / 2);
1423 /* Prepare a DMA cyclic transaction */
1424 desc = dmaengine_prep_dma_cyclic(adc->dma_chan,
1426 adc->rx_buf_sz, adc->rx_buf_sz / 2,
1428 DMA_PREP_INTERRUPT);
1432 desc->callback = stm32_adc_dma_buffer_done;
1433 desc->callback_param = indio_dev;
1435 cookie = dmaengine_submit(desc);
1436 ret = dma_submit_error(cookie);
1438 dmaengine_terminate_all(adc->dma_chan);
1442 /* Issue pending DMA requests */
1443 dma_async_issue_pending(adc->dma_chan);
1448 static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev)
1450 struct stm32_adc *adc = iio_priv(indio_dev);
1453 if (adc->cfg->prepare) {
1454 ret = adc->cfg->prepare(adc);
1459 ret = stm32_adc_set_trig(indio_dev, indio_dev->trig);
1461 dev_err(&indio_dev->dev, "Can't set trigger\n");
1465 ret = stm32_adc_dma_start(indio_dev);
1467 dev_err(&indio_dev->dev, "Can't start dma\n");
1471 ret = iio_triggered_buffer_postenable(indio_dev);
1475 /* Reset adc buffer index */
1479 stm32_adc_conv_irq_enable(adc);
1481 adc->cfg->start_conv(adc, !!adc->dma_chan);
1487 dmaengine_terminate_all(adc->dma_chan);
1489 stm32_adc_set_trig(indio_dev, NULL);
1491 if (adc->cfg->unprepare)
1492 adc->cfg->unprepare(adc);
1497 static int stm32_adc_buffer_predisable(struct iio_dev *indio_dev)
1499 struct stm32_adc *adc = iio_priv(indio_dev);
1502 adc->cfg->stop_conv(adc);
1504 stm32_adc_conv_irq_disable(adc);
1506 ret = iio_triggered_buffer_predisable(indio_dev);
1508 dev_err(&indio_dev->dev, "predisable failed\n");
1511 dmaengine_terminate_all(adc->dma_chan);
1513 if (stm32_adc_set_trig(indio_dev, NULL))
1514 dev_err(&indio_dev->dev, "Can't clear trigger\n");
1516 if (adc->cfg->unprepare)
1517 adc->cfg->unprepare(adc);
1522 static const struct iio_buffer_setup_ops stm32_adc_buffer_setup_ops = {
1523 .postenable = &stm32_adc_buffer_postenable,
1524 .predisable = &stm32_adc_buffer_predisable,
1527 static irqreturn_t stm32_adc_trigger_handler(int irq, void *p)
1529 struct iio_poll_func *pf = p;
1530 struct iio_dev *indio_dev = pf->indio_dev;
1531 struct stm32_adc *adc = iio_priv(indio_dev);
1533 dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi);
1535 if (!adc->dma_chan) {
1536 /* reset buffer index */
1538 iio_push_to_buffers_with_timestamp(indio_dev, adc->buffer,
1541 int residue = stm32_adc_dma_residue(adc);
1543 while (residue >= indio_dev->scan_bytes) {
1544 u16 *buffer = (u16 *)&adc->rx_buf[adc->bufi];
1546 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
1548 residue -= indio_dev->scan_bytes;
1549 adc->bufi += indio_dev->scan_bytes;
1550 if (adc->bufi >= adc->rx_buf_sz)
1555 iio_trigger_notify_done(indio_dev->trig);
1557 /* re-enable eoc irq */
1559 stm32_adc_conv_irq_enable(adc);
1564 static const struct iio_chan_spec_ext_info stm32_adc_ext_info[] = {
1565 IIO_ENUM("trigger_polarity", IIO_SHARED_BY_ALL, &stm32_adc_trig_pol),
1567 .name = "trigger_polarity_available",
1568 .shared = IIO_SHARED_BY_ALL,
1569 .read = iio_enum_available_read,
1570 .private = (uintptr_t)&stm32_adc_trig_pol,
1575 static int stm32_adc_of_get_resolution(struct iio_dev *indio_dev)
1577 struct device_node *node = indio_dev->dev.of_node;
1578 struct stm32_adc *adc = iio_priv(indio_dev);
1582 if (of_property_read_u32(node, "assigned-resolution-bits", &res))
1583 res = adc->cfg->adc_info->resolutions[0];
1585 for (i = 0; i < adc->cfg->adc_info->num_res; i++)
1586 if (res == adc->cfg->adc_info->resolutions[i])
1588 if (i >= adc->cfg->adc_info->num_res) {
1589 dev_err(&indio_dev->dev, "Bad resolution: %u bits\n", res);
1593 dev_dbg(&indio_dev->dev, "Using %u bits resolution\n", res);
1599 static void stm32_adc_smpr_init(struct stm32_adc *adc, int channel, u32 smp_ns)
1601 const struct stm32_adc_regs *smpr = &adc->cfg->regs->smp_bits[channel];
1602 u32 period_ns, shift = smpr->shift, mask = smpr->mask;
1603 unsigned int smp, r = smpr->reg;
1605 /* Determine sampling time (ADC clock cycles) */
1606 period_ns = NSEC_PER_SEC / adc->common->rate;
1607 for (smp = 0; smp <= STM32_ADC_MAX_SMP; smp++)
1608 if ((period_ns * adc->cfg->smp_cycles[smp]) >= smp_ns)
1610 if (smp > STM32_ADC_MAX_SMP)
1611 smp = STM32_ADC_MAX_SMP;
1613 /* pre-build sampling time registers (e.g. smpr1, smpr2) */
1614 adc->smpr_val[r] = (adc->smpr_val[r] & ~mask) | (smp << shift);
1617 static void stm32_adc_chan_init_one(struct iio_dev *indio_dev,
1618 struct iio_chan_spec *chan, u32 vinp,
1619 u32 vinn, int scan_index, bool differential)
1621 struct stm32_adc *adc = iio_priv(indio_dev);
1622 char *name = adc->chan_name[vinp];
1624 chan->type = IIO_VOLTAGE;
1625 chan->channel = vinp;
1627 chan->differential = 1;
1628 chan->channel2 = vinn;
1629 snprintf(name, STM32_ADC_CH_SZ, "in%d-in%d", vinp, vinn);
1631 snprintf(name, STM32_ADC_CH_SZ, "in%d", vinp);
1633 chan->datasheet_name = name;
1634 chan->scan_index = scan_index;
1636 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
1637 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
1638 BIT(IIO_CHAN_INFO_OFFSET);
1639 chan->scan_type.sign = 'u';
1640 chan->scan_type.realbits = adc->cfg->adc_info->resolutions[adc->res];
1641 chan->scan_type.storagebits = 16;
1642 chan->ext_info = stm32_adc_ext_info;
1644 /* pre-build selected channels mask */
1645 adc->pcsel |= BIT(chan->channel);
1647 /* pre-build diff channels mask */
1648 adc->difsel |= BIT(chan->channel);
1649 /* Also add negative input to pre-selected channels */
1650 adc->pcsel |= BIT(chan->channel2);
1654 static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
1656 struct device_node *node = indio_dev->dev.of_node;
1657 struct stm32_adc *adc = iio_priv(indio_dev);
1658 const struct stm32_adc_info *adc_info = adc->cfg->adc_info;
1659 struct stm32_adc_diff_channel diff[STM32_ADC_CH_MAX];
1660 struct property *prop;
1662 struct iio_chan_spec *channels;
1663 int scan_index = 0, num_channels = 0, num_diff = 0, ret, i;
1666 ret = of_property_count_u32_elems(node, "st,adc-channels");
1667 if (ret > adc_info->max_channels) {
1668 dev_err(&indio_dev->dev, "Bad st,adc-channels?\n");
1670 } else if (ret > 0) {
1671 num_channels += ret;
1674 ret = of_property_count_elems_of_size(node, "st,adc-diff-channels",
1676 if (ret > adc_info->max_channels) {
1677 dev_err(&indio_dev->dev, "Bad st,adc-diff-channels?\n");
1679 } else if (ret > 0) {
1680 int size = ret * sizeof(*diff) / sizeof(u32);
1683 num_channels += ret;
1684 ret = of_property_read_u32_array(node, "st,adc-diff-channels",
1690 if (!num_channels) {
1691 dev_err(&indio_dev->dev, "No channels configured\n");
1695 /* Optional sample time is provided either for each, or all channels */
1696 ret = of_property_count_u32_elems(node, "st,min-sample-time-nsecs");
1697 if (ret > 1 && ret != num_channels) {
1698 dev_err(&indio_dev->dev, "Invalid st,min-sample-time-nsecs\n");
1702 channels = devm_kcalloc(&indio_dev->dev, num_channels,
1703 sizeof(struct iio_chan_spec), GFP_KERNEL);
1707 of_property_for_each_u32(node, "st,adc-channels", prop, cur, val) {
1708 if (val >= adc_info->max_channels) {
1709 dev_err(&indio_dev->dev, "Invalid channel %d\n", val);
1713 /* Channel can't be configured both as single-ended & diff */
1714 for (i = 0; i < num_diff; i++) {
1715 if (val == diff[i].vinp) {
1716 dev_err(&indio_dev->dev,
1717 "channel %d miss-configured\n", val);
1721 stm32_adc_chan_init_one(indio_dev, &channels[scan_index], val,
1722 0, scan_index, false);
1726 for (i = 0; i < num_diff; i++) {
1727 if (diff[i].vinp >= adc_info->max_channels ||
1728 diff[i].vinn >= adc_info->max_channels) {
1729 dev_err(&indio_dev->dev, "Invalid channel in%d-in%d\n",
1730 diff[i].vinp, diff[i].vinn);
1733 stm32_adc_chan_init_one(indio_dev, &channels[scan_index],
1734 diff[i].vinp, diff[i].vinn, scan_index,
1739 for (i = 0; i < scan_index; i++) {
1741 * Using of_property_read_u32_index(), smp value will only be
1742 * modified if valid u32 value can be decoded. This allows to
1743 * get either no value, 1 shared value for all indexes, or one
1744 * value per channel.
1746 of_property_read_u32_index(node, "st,min-sample-time-nsecs",
1748 /* Prepare sampling time settings */
1749 stm32_adc_smpr_init(adc, channels[i].channel, smp);
1752 indio_dev->num_channels = scan_index;
1753 indio_dev->channels = channels;
1758 static int stm32_adc_dma_request(struct iio_dev *indio_dev)
1760 struct stm32_adc *adc = iio_priv(indio_dev);
1761 struct dma_slave_config config;
1764 adc->dma_chan = dma_request_slave_channel(&indio_dev->dev, "rx");
1768 adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev,
1769 STM32_DMA_BUFFER_SIZE,
1770 &adc->rx_dma_buf, GFP_KERNEL);
1776 /* Configure DMA channel to read data register */
1777 memset(&config, 0, sizeof(config));
1778 config.src_addr = (dma_addr_t)adc->common->phys_base;
1779 config.src_addr += adc->offset + adc->cfg->regs->dr;
1780 config.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1782 ret = dmaengine_slave_config(adc->dma_chan, &config);
1789 dma_free_coherent(adc->dma_chan->device->dev, STM32_DMA_BUFFER_SIZE,
1790 adc->rx_buf, adc->rx_dma_buf);
1792 dma_release_channel(adc->dma_chan);
1797 static int stm32_adc_probe(struct platform_device *pdev)
1799 struct iio_dev *indio_dev;
1800 struct device *dev = &pdev->dev;
1801 struct stm32_adc *adc;
1804 if (!pdev->dev.of_node)
1807 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
1811 adc = iio_priv(indio_dev);
1812 adc->common = dev_get_drvdata(pdev->dev.parent);
1813 spin_lock_init(&adc->lock);
1814 init_completion(&adc->completion);
1815 adc->cfg = (const struct stm32_adc_cfg *)
1816 of_match_device(dev->driver->of_match_table, dev)->data;
1818 indio_dev->name = dev_name(&pdev->dev);
1819 indio_dev->dev.parent = &pdev->dev;
1820 indio_dev->dev.of_node = pdev->dev.of_node;
1821 indio_dev->info = &stm32_adc_iio_info;
1822 indio_dev->modes = INDIO_DIRECT_MODE | INDIO_HARDWARE_TRIGGERED;
1824 platform_set_drvdata(pdev, adc);
1826 ret = of_property_read_u32(pdev->dev.of_node, "reg", &adc->offset);
1828 dev_err(&pdev->dev, "missing reg property\n");
1832 adc->irq = platform_get_irq(pdev, 0);
1834 dev_err(&pdev->dev, "failed to get irq\n");
1838 ret = devm_request_irq(&pdev->dev, adc->irq, stm32_adc_isr,
1839 0, pdev->name, adc);
1841 dev_err(&pdev->dev, "failed to request IRQ\n");
1845 adc->clk = devm_clk_get(&pdev->dev, NULL);
1846 if (IS_ERR(adc->clk)) {
1847 ret = PTR_ERR(adc->clk);
1848 if (ret == -ENOENT && !adc->cfg->clk_required) {
1851 dev_err(&pdev->dev, "Can't get clock\n");
1857 ret = clk_prepare_enable(adc->clk);
1859 dev_err(&pdev->dev, "clk enable failed\n");
1864 ret = stm32_adc_of_get_resolution(indio_dev);
1866 goto err_clk_disable;
1867 stm32_adc_set_res(adc);
1869 if (adc->cfg->selfcalib) {
1870 ret = adc->cfg->selfcalib(adc);
1872 goto err_clk_disable;
1875 ret = stm32_adc_chan_of_init(indio_dev);
1877 goto err_clk_disable;
1879 ret = stm32_adc_dma_request(indio_dev);
1881 goto err_clk_disable;
1883 ret = iio_triggered_buffer_setup(indio_dev,
1884 &iio_pollfunc_store_time,
1885 &stm32_adc_trigger_handler,
1886 &stm32_adc_buffer_setup_ops);
1888 dev_err(&pdev->dev, "buffer setup failed\n");
1889 goto err_dma_disable;
1892 ret = iio_device_register(indio_dev);
1894 dev_err(&pdev->dev, "iio dev register failed\n");
1895 goto err_buffer_cleanup;
1901 iio_triggered_buffer_cleanup(indio_dev);
1904 if (adc->dma_chan) {
1905 dma_free_coherent(adc->dma_chan->device->dev,
1906 STM32_DMA_BUFFER_SIZE,
1907 adc->rx_buf, adc->rx_dma_buf);
1908 dma_release_channel(adc->dma_chan);
1912 clk_disable_unprepare(adc->clk);
1917 static int stm32_adc_remove(struct platform_device *pdev)
1919 struct stm32_adc *adc = platform_get_drvdata(pdev);
1920 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
1922 iio_device_unregister(indio_dev);
1923 iio_triggered_buffer_cleanup(indio_dev);
1924 if (adc->dma_chan) {
1925 dma_free_coherent(adc->dma_chan->device->dev,
1926 STM32_DMA_BUFFER_SIZE,
1927 adc->rx_buf, adc->rx_dma_buf);
1928 dma_release_channel(adc->dma_chan);
1931 clk_disable_unprepare(adc->clk);
1936 static const struct stm32_adc_cfg stm32f4_adc_cfg = {
1937 .regs = &stm32f4_adc_regspec,
1938 .adc_info = &stm32f4_adc_info,
1939 .trigs = stm32f4_adc_trigs,
1940 .clk_required = true,
1941 .start_conv = stm32f4_adc_start_conv,
1942 .stop_conv = stm32f4_adc_stop_conv,
1943 .smp_cycles = stm32f4_adc_smp_cycles,
1946 static const struct stm32_adc_cfg stm32h7_adc_cfg = {
1947 .regs = &stm32h7_adc_regspec,
1948 .adc_info = &stm32h7_adc_info,
1949 .trigs = stm32h7_adc_trigs,
1950 .selfcalib = stm32h7_adc_selfcalib,
1951 .start_conv = stm32h7_adc_start_conv,
1952 .stop_conv = stm32h7_adc_stop_conv,
1953 .prepare = stm32h7_adc_prepare,
1954 .unprepare = stm32h7_adc_unprepare,
1955 .smp_cycles = stm32h7_adc_smp_cycles,
1958 static const struct of_device_id stm32_adc_of_match[] = {
1959 { .compatible = "st,stm32f4-adc", .data = (void *)&stm32f4_adc_cfg },
1960 { .compatible = "st,stm32h7-adc", .data = (void *)&stm32h7_adc_cfg },
1963 MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
1965 static struct platform_driver stm32_adc_driver = {
1966 .probe = stm32_adc_probe,
1967 .remove = stm32_adc_remove,
1969 .name = "stm32-adc",
1970 .of_match_table = stm32_adc_of_match,
1973 module_platform_driver(stm32_adc_driver);
1975 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
1976 MODULE_DESCRIPTION("STMicroelectronics STM32 ADC IIO driver");
1977 MODULE_LICENSE("GPL v2");
1978 MODULE_ALIAS("platform:stm32-adc");