1 // SPDX-License-Identifier: GPL-2.0
3 * This file is part of STM32 ADC driver
5 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
6 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/dmaengine.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/buffer.h>
15 #include <linux/iio/timer/stm32-lptim-trigger.h>
16 #include <linux/iio/timer/stm32-timer-trigger.h>
17 #include <linux/iio/trigger.h>
18 #include <linux/iio/trigger_consumer.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/interrupt.h>
22 #include <linux/iopoll.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
26 #include <linux/of_device.h>
28 #include "stm32-adc-core.h"
30 /* STM32F4 - Registers for each ADC instance */
31 #define STM32F4_ADC_SR 0x00
32 #define STM32F4_ADC_CR1 0x04
33 #define STM32F4_ADC_CR2 0x08
34 #define STM32F4_ADC_SMPR1 0x0C
35 #define STM32F4_ADC_SMPR2 0x10
36 #define STM32F4_ADC_HTR 0x24
37 #define STM32F4_ADC_LTR 0x28
38 #define STM32F4_ADC_SQR1 0x2C
39 #define STM32F4_ADC_SQR2 0x30
40 #define STM32F4_ADC_SQR3 0x34
41 #define STM32F4_ADC_JSQR 0x38
42 #define STM32F4_ADC_JDR1 0x3C
43 #define STM32F4_ADC_JDR2 0x40
44 #define STM32F4_ADC_JDR3 0x44
45 #define STM32F4_ADC_JDR4 0x48
46 #define STM32F4_ADC_DR 0x4C
48 /* STM32F4_ADC_SR - bit fields */
49 #define STM32F4_STRT BIT(4)
50 #define STM32F4_EOC BIT(1)
52 /* STM32F4_ADC_CR1 - bit fields */
53 #define STM32F4_RES_SHIFT 24
54 #define STM32F4_RES_MASK GENMASK(25, 24)
55 #define STM32F4_SCAN BIT(8)
56 #define STM32F4_EOCIE BIT(5)
58 /* STM32F4_ADC_CR2 - bit fields */
59 #define STM32F4_SWSTART BIT(30)
60 #define STM32F4_EXTEN_SHIFT 28
61 #define STM32F4_EXTEN_MASK GENMASK(29, 28)
62 #define STM32F4_EXTSEL_SHIFT 24
63 #define STM32F4_EXTSEL_MASK GENMASK(27, 24)
64 #define STM32F4_EOCS BIT(10)
65 #define STM32F4_DDS BIT(9)
66 #define STM32F4_DMA BIT(8)
67 #define STM32F4_ADON BIT(0)
69 /* STM32H7 - Registers for each ADC instance */
70 #define STM32H7_ADC_ISR 0x00
71 #define STM32H7_ADC_IER 0x04
72 #define STM32H7_ADC_CR 0x08
73 #define STM32H7_ADC_CFGR 0x0C
74 #define STM32H7_ADC_SMPR1 0x14
75 #define STM32H7_ADC_SMPR2 0x18
76 #define STM32H7_ADC_PCSEL 0x1C
77 #define STM32H7_ADC_SQR1 0x30
78 #define STM32H7_ADC_SQR2 0x34
79 #define STM32H7_ADC_SQR3 0x38
80 #define STM32H7_ADC_SQR4 0x3C
81 #define STM32H7_ADC_DR 0x40
82 #define STM32H7_ADC_DIFSEL 0xC0
83 #define STM32H7_ADC_CALFACT 0xC4
84 #define STM32H7_ADC_CALFACT2 0xC8
86 /* STM32H7_ADC_ISR - bit fields */
87 #define STM32MP1_VREGREADY BIT(12)
88 #define STM32H7_EOC BIT(2)
89 #define STM32H7_ADRDY BIT(0)
91 /* STM32H7_ADC_IER - bit fields */
92 #define STM32H7_EOCIE STM32H7_EOC
94 /* STM32H7_ADC_CR - bit fields */
95 #define STM32H7_ADCAL BIT(31)
96 #define STM32H7_ADCALDIF BIT(30)
97 #define STM32H7_DEEPPWD BIT(29)
98 #define STM32H7_ADVREGEN BIT(28)
99 #define STM32H7_LINCALRDYW6 BIT(27)
100 #define STM32H7_LINCALRDYW5 BIT(26)
101 #define STM32H7_LINCALRDYW4 BIT(25)
102 #define STM32H7_LINCALRDYW3 BIT(24)
103 #define STM32H7_LINCALRDYW2 BIT(23)
104 #define STM32H7_LINCALRDYW1 BIT(22)
105 #define STM32H7_ADCALLIN BIT(16)
106 #define STM32H7_BOOST BIT(8)
107 #define STM32H7_ADSTP BIT(4)
108 #define STM32H7_ADSTART BIT(2)
109 #define STM32H7_ADDIS BIT(1)
110 #define STM32H7_ADEN BIT(0)
112 /* STM32H7_ADC_CFGR bit fields */
113 #define STM32H7_EXTEN_SHIFT 10
114 #define STM32H7_EXTEN_MASK GENMASK(11, 10)
115 #define STM32H7_EXTSEL_SHIFT 5
116 #define STM32H7_EXTSEL_MASK GENMASK(9, 5)
117 #define STM32H7_RES_SHIFT 2
118 #define STM32H7_RES_MASK GENMASK(4, 2)
119 #define STM32H7_DMNGT_SHIFT 0
120 #define STM32H7_DMNGT_MASK GENMASK(1, 0)
122 enum stm32h7_adc_dmngt
{
123 STM32H7_DMNGT_DR_ONLY
, /* Regular data in DR only */
124 STM32H7_DMNGT_DMA_ONESHOT
, /* DMA one shot mode */
125 STM32H7_DMNGT_DFSDM
, /* DFSDM mode */
126 STM32H7_DMNGT_DMA_CIRC
, /* DMA circular mode */
129 /* STM32H7_ADC_CALFACT - bit fields */
130 #define STM32H7_CALFACT_D_SHIFT 16
131 #define STM32H7_CALFACT_D_MASK GENMASK(26, 16)
132 #define STM32H7_CALFACT_S_SHIFT 0
133 #define STM32H7_CALFACT_S_MASK GENMASK(10, 0)
135 /* STM32H7_ADC_CALFACT2 - bit fields */
136 #define STM32H7_LINCALFACT_SHIFT 0
137 #define STM32H7_LINCALFACT_MASK GENMASK(29, 0)
139 /* Number of linear calibration shadow registers / LINCALRDYW control bits */
140 #define STM32H7_LINCALFACT_NUM 6
142 /* BOOST bit must be set on STM32H7 when ADC clock is above 20MHz */
143 #define STM32H7_BOOST_CLKRATE 20000000UL
145 #define STM32_ADC_CH_MAX 20 /* max number of channels */
146 #define STM32_ADC_CH_SZ 10 /* max channel name size */
147 #define STM32_ADC_MAX_SQ 16 /* SQ1..SQ16 */
148 #define STM32_ADC_MAX_SMP 7 /* SMPx range is [0..7] */
149 #define STM32_ADC_TIMEOUT_US 100000
150 #define STM32_ADC_TIMEOUT (msecs_to_jiffies(STM32_ADC_TIMEOUT_US / 1000))
152 #define STM32_DMA_BUFFER_SIZE PAGE_SIZE
154 /* External trigger enable */
155 enum stm32_adc_exten
{
157 STM32_EXTEN_HWTRIG_RISING_EDGE
,
158 STM32_EXTEN_HWTRIG_FALLING_EDGE
,
159 STM32_EXTEN_HWTRIG_BOTH_EDGES
,
162 /* extsel - trigger mux selection value */
163 enum stm32_adc_extsel
{
188 * struct stm32_adc_trig_info - ADC trigger info
189 * @name: name of the trigger, corresponding to its source
190 * @extsel: trigger selection
192 struct stm32_adc_trig_info
{
194 enum stm32_adc_extsel extsel
;
198 * struct stm32_adc_calib - optional adc calibration data
199 * @calfact_s: Calibration offset for single ended channels
200 * @calfact_d: Calibration offset in differential
201 * @lincalfact: Linearity calibration factor
203 struct stm32_adc_calib
{
206 u32 lincalfact
[STM32H7_LINCALFACT_NUM
];
210 * stm32_adc_regs - stm32 ADC misc registers & bitfield desc
211 * @reg: register offset
212 * @mask: bitfield mask
215 struct stm32_adc_regs
{
222 * stm32_adc_regspec - stm32 registers definition, compatible dependent data
223 * @dr: data register offset
224 * @ier_eoc: interrupt enable register & eocie bitfield
225 * @isr_eoc: interrupt status register & eoc bitfield
226 * @sqr: reference to sequence registers array
227 * @exten: trigger control register & bitfield
228 * @extsel: trigger selection register & bitfield
229 * @res: resolution selection register & bitfield
230 * @smpr: smpr1 & smpr2 registers offset array
231 * @smp_bits: smpr1 & smpr2 index and bitfields
233 struct stm32_adc_regspec
{
235 const struct stm32_adc_regs ier_eoc
;
236 const struct stm32_adc_regs isr_eoc
;
237 const struct stm32_adc_regs
*sqr
;
238 const struct stm32_adc_regs exten
;
239 const struct stm32_adc_regs extsel
;
240 const struct stm32_adc_regs res
;
242 const struct stm32_adc_regs
*smp_bits
;
248 * stm32_adc_cfg - stm32 compatible configuration data
249 * @regs: registers descriptions
250 * @adc_info: per instance input channels definitions
251 * @trigs: external trigger sources
252 * @clk_required: clock is required
253 * @has_vregready: vregready status flag presence
254 * @selfcalib: optional routine for self-calibration
255 * @prepare: optional prepare routine (power-up, enable)
256 * @start_conv: routine to start conversions
257 * @stop_conv: routine to stop conversions
258 * @unprepare: optional unprepare routine (disable, power-down)
259 * @smp_cycles: programmable sampling time (ADC clock cycles)
261 struct stm32_adc_cfg
{
262 const struct stm32_adc_regspec
*regs
;
263 const struct stm32_adc_info
*adc_info
;
264 struct stm32_adc_trig_info
*trigs
;
267 int (*selfcalib
)(struct stm32_adc
*);
268 int (*prepare
)(struct stm32_adc
*);
269 void (*start_conv
)(struct stm32_adc
*, bool dma
);
270 void (*stop_conv
)(struct stm32_adc
*);
271 void (*unprepare
)(struct stm32_adc
*);
272 const unsigned int *smp_cycles
;
276 * struct stm32_adc - private data of each ADC IIO instance
277 * @common: reference to ADC block common data
278 * @offset: ADC instance register offset in ADC block
279 * @cfg: compatible configuration data
280 * @completion: end of single conversion completion
281 * @buffer: data buffer
282 * @clk: clock for this adc instance
283 * @irq: interrupt for this adc instance
285 * @bufi: data buffer index
286 * @num_conv: expected number of scan conversions
287 * @res: data resolution (e.g. RES bitfield value)
288 * @trigger_polarity: external trigger polarity (e.g. exten)
289 * @dma_chan: dma channel
290 * @rx_buf: dma rx buffer cpu address
291 * @rx_dma_buf: dma rx buffer bus address
292 * @rx_buf_sz: dma rx buffer size
293 * @difsel bitmask to set single-ended/differential channel
294 * @pcsel bitmask to preselect channels on some devices
295 * @smpr_val: sampling time settings (e.g. smpr1 / smpr2)
296 * @cal: optional calibration data on some devices
297 * @chan_name: channel name array
300 struct stm32_adc_common
*common
;
302 const struct stm32_adc_cfg
*cfg
;
303 struct completion completion
;
304 u16 buffer
[STM32_ADC_MAX_SQ
];
307 spinlock_t lock
; /* interrupt lock */
309 unsigned int num_conv
;
311 u32 trigger_polarity
;
312 struct dma_chan
*dma_chan
;
314 dma_addr_t rx_dma_buf
;
315 unsigned int rx_buf_sz
;
319 struct stm32_adc_calib cal
;
320 char chan_name
[STM32_ADC_CH_MAX
][STM32_ADC_CH_SZ
];
323 struct stm32_adc_diff_channel
{
329 * struct stm32_adc_info - stm32 ADC, per instance config data
330 * @max_channels: Number of channels
331 * @resolutions: available resolutions
332 * @num_res: number of available resolutions
334 struct stm32_adc_info
{
336 const unsigned int *resolutions
;
337 const unsigned int num_res
;
340 static const unsigned int stm32f4_adc_resolutions
[] = {
341 /* sorted values so the index matches RES[1:0] in STM32F4_ADC_CR1 */
345 /* stm32f4 can have up to 16 channels */
346 static const struct stm32_adc_info stm32f4_adc_info
= {
348 .resolutions
= stm32f4_adc_resolutions
,
349 .num_res
= ARRAY_SIZE(stm32f4_adc_resolutions
),
352 static const unsigned int stm32h7_adc_resolutions
[] = {
353 /* sorted values so the index matches RES[2:0] in STM32H7_ADC_CFGR */
357 /* stm32h7 can have up to 20 channels */
358 static const struct stm32_adc_info stm32h7_adc_info
= {
359 .max_channels
= STM32_ADC_CH_MAX
,
360 .resolutions
= stm32h7_adc_resolutions
,
361 .num_res
= ARRAY_SIZE(stm32h7_adc_resolutions
),
365 * stm32f4_sq - describe regular sequence registers
366 * - L: sequence len (register & bit field)
367 * - SQ1..SQ16: sequence entries (register & bit field)
369 static const struct stm32_adc_regs stm32f4_sq
[STM32_ADC_MAX_SQ
+ 1] = {
370 /* L: len bit field description to be kept as first element */
371 { STM32F4_ADC_SQR1
, GENMASK(23, 20), 20 },
372 /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
373 { STM32F4_ADC_SQR3
, GENMASK(4, 0), 0 },
374 { STM32F4_ADC_SQR3
, GENMASK(9, 5), 5 },
375 { STM32F4_ADC_SQR3
, GENMASK(14, 10), 10 },
376 { STM32F4_ADC_SQR3
, GENMASK(19, 15), 15 },
377 { STM32F4_ADC_SQR3
, GENMASK(24, 20), 20 },
378 { STM32F4_ADC_SQR3
, GENMASK(29, 25), 25 },
379 { STM32F4_ADC_SQR2
, GENMASK(4, 0), 0 },
380 { STM32F4_ADC_SQR2
, GENMASK(9, 5), 5 },
381 { STM32F4_ADC_SQR2
, GENMASK(14, 10), 10 },
382 { STM32F4_ADC_SQR2
, GENMASK(19, 15), 15 },
383 { STM32F4_ADC_SQR2
, GENMASK(24, 20), 20 },
384 { STM32F4_ADC_SQR2
, GENMASK(29, 25), 25 },
385 { STM32F4_ADC_SQR1
, GENMASK(4, 0), 0 },
386 { STM32F4_ADC_SQR1
, GENMASK(9, 5), 5 },
387 { STM32F4_ADC_SQR1
, GENMASK(14, 10), 10 },
388 { STM32F4_ADC_SQR1
, GENMASK(19, 15), 15 },
391 /* STM32F4 external trigger sources for all instances */
392 static struct stm32_adc_trig_info stm32f4_adc_trigs
[] = {
393 { TIM1_CH1
, STM32_EXT0
},
394 { TIM1_CH2
, STM32_EXT1
},
395 { TIM1_CH3
, STM32_EXT2
},
396 { TIM2_CH2
, STM32_EXT3
},
397 { TIM2_CH3
, STM32_EXT4
},
398 { TIM2_CH4
, STM32_EXT5
},
399 { TIM2_TRGO
, STM32_EXT6
},
400 { TIM3_CH1
, STM32_EXT7
},
401 { TIM3_TRGO
, STM32_EXT8
},
402 { TIM4_CH4
, STM32_EXT9
},
403 { TIM5_CH1
, STM32_EXT10
},
404 { TIM5_CH2
, STM32_EXT11
},
405 { TIM5_CH3
, STM32_EXT12
},
406 { TIM8_CH1
, STM32_EXT13
},
407 { TIM8_TRGO
, STM32_EXT14
},
412 * stm32f4_smp_bits[] - describe sampling time register index & bit fields
413 * Sorted so it can be indexed by channel number.
415 static const struct stm32_adc_regs stm32f4_smp_bits
[] = {
416 /* STM32F4_ADC_SMPR2: smpr[] index, mask, shift for SMP0 to SMP9 */
417 { 1, GENMASK(2, 0), 0 },
418 { 1, GENMASK(5, 3), 3 },
419 { 1, GENMASK(8, 6), 6 },
420 { 1, GENMASK(11, 9), 9 },
421 { 1, GENMASK(14, 12), 12 },
422 { 1, GENMASK(17, 15), 15 },
423 { 1, GENMASK(20, 18), 18 },
424 { 1, GENMASK(23, 21), 21 },
425 { 1, GENMASK(26, 24), 24 },
426 { 1, GENMASK(29, 27), 27 },
427 /* STM32F4_ADC_SMPR1, smpr[] index, mask, shift for SMP10 to SMP18 */
428 { 0, GENMASK(2, 0), 0 },
429 { 0, GENMASK(5, 3), 3 },
430 { 0, GENMASK(8, 6), 6 },
431 { 0, GENMASK(11, 9), 9 },
432 { 0, GENMASK(14, 12), 12 },
433 { 0, GENMASK(17, 15), 15 },
434 { 0, GENMASK(20, 18), 18 },
435 { 0, GENMASK(23, 21), 21 },
436 { 0, GENMASK(26, 24), 24 },
439 /* STM32F4 programmable sampling time (ADC clock cycles) */
440 static const unsigned int stm32f4_adc_smp_cycles
[STM32_ADC_MAX_SMP
+ 1] = {
441 3, 15, 28, 56, 84, 112, 144, 480,
444 static const struct stm32_adc_regspec stm32f4_adc_regspec
= {
445 .dr
= STM32F4_ADC_DR
,
446 .ier_eoc
= { STM32F4_ADC_CR1
, STM32F4_EOCIE
},
447 .isr_eoc
= { STM32F4_ADC_SR
, STM32F4_EOC
},
449 .exten
= { STM32F4_ADC_CR2
, STM32F4_EXTEN_MASK
, STM32F4_EXTEN_SHIFT
},
450 .extsel
= { STM32F4_ADC_CR2
, STM32F4_EXTSEL_MASK
,
451 STM32F4_EXTSEL_SHIFT
},
452 .res
= { STM32F4_ADC_CR1
, STM32F4_RES_MASK
, STM32F4_RES_SHIFT
},
453 .smpr
= { STM32F4_ADC_SMPR1
, STM32F4_ADC_SMPR2
},
454 .smp_bits
= stm32f4_smp_bits
,
457 static const struct stm32_adc_regs stm32h7_sq
[STM32_ADC_MAX_SQ
+ 1] = {
458 /* L: len bit field description to be kept as first element */
459 { STM32H7_ADC_SQR1
, GENMASK(3, 0), 0 },
460 /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
461 { STM32H7_ADC_SQR1
, GENMASK(10, 6), 6 },
462 { STM32H7_ADC_SQR1
, GENMASK(16, 12), 12 },
463 { STM32H7_ADC_SQR1
, GENMASK(22, 18), 18 },
464 { STM32H7_ADC_SQR1
, GENMASK(28, 24), 24 },
465 { STM32H7_ADC_SQR2
, GENMASK(4, 0), 0 },
466 { STM32H7_ADC_SQR2
, GENMASK(10, 6), 6 },
467 { STM32H7_ADC_SQR2
, GENMASK(16, 12), 12 },
468 { STM32H7_ADC_SQR2
, GENMASK(22, 18), 18 },
469 { STM32H7_ADC_SQR2
, GENMASK(28, 24), 24 },
470 { STM32H7_ADC_SQR3
, GENMASK(4, 0), 0 },
471 { STM32H7_ADC_SQR3
, GENMASK(10, 6), 6 },
472 { STM32H7_ADC_SQR3
, GENMASK(16, 12), 12 },
473 { STM32H7_ADC_SQR3
, GENMASK(22, 18), 18 },
474 { STM32H7_ADC_SQR3
, GENMASK(28, 24), 24 },
475 { STM32H7_ADC_SQR4
, GENMASK(4, 0), 0 },
476 { STM32H7_ADC_SQR4
, GENMASK(10, 6), 6 },
479 /* STM32H7 external trigger sources for all instances */
480 static struct stm32_adc_trig_info stm32h7_adc_trigs
[] = {
481 { TIM1_CH1
, STM32_EXT0
},
482 { TIM1_CH2
, STM32_EXT1
},
483 { TIM1_CH3
, STM32_EXT2
},
484 { TIM2_CH2
, STM32_EXT3
},
485 { TIM3_TRGO
, STM32_EXT4
},
486 { TIM4_CH4
, STM32_EXT5
},
487 { TIM8_TRGO
, STM32_EXT7
},
488 { TIM8_TRGO2
, STM32_EXT8
},
489 { TIM1_TRGO
, STM32_EXT9
},
490 { TIM1_TRGO2
, STM32_EXT10
},
491 { TIM2_TRGO
, STM32_EXT11
},
492 { TIM4_TRGO
, STM32_EXT12
},
493 { TIM6_TRGO
, STM32_EXT13
},
494 { TIM15_TRGO
, STM32_EXT14
},
495 { TIM3_CH4
, STM32_EXT15
},
496 { LPTIM1_OUT
, STM32_EXT18
},
497 { LPTIM2_OUT
, STM32_EXT19
},
498 { LPTIM3_OUT
, STM32_EXT20
},
503 * stm32h7_smp_bits - describe sampling time register index & bit fields
504 * Sorted so it can be indexed by channel number.
506 static const struct stm32_adc_regs stm32h7_smp_bits
[] = {
507 /* STM32H7_ADC_SMPR1, smpr[] index, mask, shift for SMP0 to SMP9 */
508 { 0, GENMASK(2, 0), 0 },
509 { 0, GENMASK(5, 3), 3 },
510 { 0, GENMASK(8, 6), 6 },
511 { 0, GENMASK(11, 9), 9 },
512 { 0, GENMASK(14, 12), 12 },
513 { 0, GENMASK(17, 15), 15 },
514 { 0, GENMASK(20, 18), 18 },
515 { 0, GENMASK(23, 21), 21 },
516 { 0, GENMASK(26, 24), 24 },
517 { 0, GENMASK(29, 27), 27 },
518 /* STM32H7_ADC_SMPR2, smpr[] index, mask, shift for SMP10 to SMP19 */
519 { 1, GENMASK(2, 0), 0 },
520 { 1, GENMASK(5, 3), 3 },
521 { 1, GENMASK(8, 6), 6 },
522 { 1, GENMASK(11, 9), 9 },
523 { 1, GENMASK(14, 12), 12 },
524 { 1, GENMASK(17, 15), 15 },
525 { 1, GENMASK(20, 18), 18 },
526 { 1, GENMASK(23, 21), 21 },
527 { 1, GENMASK(26, 24), 24 },
528 { 1, GENMASK(29, 27), 27 },
531 /* STM32H7 programmable sampling time (ADC clock cycles, rounded down) */
532 static const unsigned int stm32h7_adc_smp_cycles
[STM32_ADC_MAX_SMP
+ 1] = {
533 1, 2, 8, 16, 32, 64, 387, 810,
536 static const struct stm32_adc_regspec stm32h7_adc_regspec
= {
537 .dr
= STM32H7_ADC_DR
,
538 .ier_eoc
= { STM32H7_ADC_IER
, STM32H7_EOCIE
},
539 .isr_eoc
= { STM32H7_ADC_ISR
, STM32H7_EOC
},
541 .exten
= { STM32H7_ADC_CFGR
, STM32H7_EXTEN_MASK
, STM32H7_EXTEN_SHIFT
},
542 .extsel
= { STM32H7_ADC_CFGR
, STM32H7_EXTSEL_MASK
,
543 STM32H7_EXTSEL_SHIFT
},
544 .res
= { STM32H7_ADC_CFGR
, STM32H7_RES_MASK
, STM32H7_RES_SHIFT
},
545 .smpr
= { STM32H7_ADC_SMPR1
, STM32H7_ADC_SMPR2
},
546 .smp_bits
= stm32h7_smp_bits
,
550 * STM32 ADC registers access routines
551 * @adc: stm32 adc instance
552 * @reg: reg offset in adc instance
554 * Note: All instances share same base, with 0x0, 0x100 or 0x200 offset resp.
555 * for adc1, adc2 and adc3.
557 static u32
stm32_adc_readl(struct stm32_adc
*adc
, u32 reg
)
559 return readl_relaxed(adc
->common
->base
+ adc
->offset
+ reg
);
562 #define stm32_adc_readl_addr(addr) stm32_adc_readl(adc, addr)
564 #define stm32_adc_readl_poll_timeout(reg, val, cond, sleep_us, timeout_us) \
565 readx_poll_timeout(stm32_adc_readl_addr, reg, val, \
566 cond, sleep_us, timeout_us)
568 static u16
stm32_adc_readw(struct stm32_adc
*adc
, u32 reg
)
570 return readw_relaxed(adc
->common
->base
+ adc
->offset
+ reg
);
573 static void stm32_adc_writel(struct stm32_adc
*adc
, u32 reg
, u32 val
)
575 writel_relaxed(val
, adc
->common
->base
+ adc
->offset
+ reg
);
578 static void stm32_adc_set_bits(struct stm32_adc
*adc
, u32 reg
, u32 bits
)
582 spin_lock_irqsave(&adc
->lock
, flags
);
583 stm32_adc_writel(adc
, reg
, stm32_adc_readl(adc
, reg
) | bits
);
584 spin_unlock_irqrestore(&adc
->lock
, flags
);
587 static void stm32_adc_clr_bits(struct stm32_adc
*adc
, u32 reg
, u32 bits
)
591 spin_lock_irqsave(&adc
->lock
, flags
);
592 stm32_adc_writel(adc
, reg
, stm32_adc_readl(adc
, reg
) & ~bits
);
593 spin_unlock_irqrestore(&adc
->lock
, flags
);
597 * stm32_adc_conv_irq_enable() - Enable end of conversion interrupt
598 * @adc: stm32 adc instance
600 static void stm32_adc_conv_irq_enable(struct stm32_adc
*adc
)
602 stm32_adc_set_bits(adc
, adc
->cfg
->regs
->ier_eoc
.reg
,
603 adc
->cfg
->regs
->ier_eoc
.mask
);
607 * stm32_adc_conv_irq_disable() - Disable end of conversion interrupt
608 * @adc: stm32 adc instance
610 static void stm32_adc_conv_irq_disable(struct stm32_adc
*adc
)
612 stm32_adc_clr_bits(adc
, adc
->cfg
->regs
->ier_eoc
.reg
,
613 adc
->cfg
->regs
->ier_eoc
.mask
);
616 static void stm32_adc_set_res(struct stm32_adc
*adc
)
618 const struct stm32_adc_regs
*res
= &adc
->cfg
->regs
->res
;
621 val
= stm32_adc_readl(adc
, res
->reg
);
622 val
= (val
& ~res
->mask
) | (adc
->res
<< res
->shift
);
623 stm32_adc_writel(adc
, res
->reg
, val
);
627 * stm32f4_adc_start_conv() - Start conversions for regular channels.
628 * @adc: stm32 adc instance
629 * @dma: use dma to transfer conversion result
631 * Start conversions for regular channels.
632 * Also take care of normal or DMA mode. Circular DMA may be used for regular
633 * conversions, in IIO buffer modes. Otherwise, use ADC interrupt with direct
634 * DR read instead (e.g. read_raw, or triggered buffer mode without DMA).
636 static void stm32f4_adc_start_conv(struct stm32_adc
*adc
, bool dma
)
638 stm32_adc_set_bits(adc
, STM32F4_ADC_CR1
, STM32F4_SCAN
);
641 stm32_adc_set_bits(adc
, STM32F4_ADC_CR2
,
642 STM32F4_DMA
| STM32F4_DDS
);
644 stm32_adc_set_bits(adc
, STM32F4_ADC_CR2
, STM32F4_EOCS
| STM32F4_ADON
);
646 /* Wait for Power-up time (tSTAB from datasheet) */
649 /* Software start ? (e.g. trigger detection disabled ?) */
650 if (!(stm32_adc_readl(adc
, STM32F4_ADC_CR2
) & STM32F4_EXTEN_MASK
))
651 stm32_adc_set_bits(adc
, STM32F4_ADC_CR2
, STM32F4_SWSTART
);
654 static void stm32f4_adc_stop_conv(struct stm32_adc
*adc
)
656 stm32_adc_clr_bits(adc
, STM32F4_ADC_CR2
, STM32F4_EXTEN_MASK
);
657 stm32_adc_clr_bits(adc
, STM32F4_ADC_SR
, STM32F4_STRT
);
659 stm32_adc_clr_bits(adc
, STM32F4_ADC_CR1
, STM32F4_SCAN
);
660 stm32_adc_clr_bits(adc
, STM32F4_ADC_CR2
,
661 STM32F4_ADON
| STM32F4_DMA
| STM32F4_DDS
);
664 static void stm32h7_adc_start_conv(struct stm32_adc
*adc
, bool dma
)
666 enum stm32h7_adc_dmngt dmngt
;
671 dmngt
= STM32H7_DMNGT_DMA_CIRC
;
673 dmngt
= STM32H7_DMNGT_DR_ONLY
;
675 spin_lock_irqsave(&adc
->lock
, flags
);
676 val
= stm32_adc_readl(adc
, STM32H7_ADC_CFGR
);
677 val
= (val
& ~STM32H7_DMNGT_MASK
) | (dmngt
<< STM32H7_DMNGT_SHIFT
);
678 stm32_adc_writel(adc
, STM32H7_ADC_CFGR
, val
);
679 spin_unlock_irqrestore(&adc
->lock
, flags
);
681 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADSTART
);
684 static void stm32h7_adc_stop_conv(struct stm32_adc
*adc
)
686 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
690 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADSTP
);
692 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
693 !(val
& (STM32H7_ADSTART
)),
694 100, STM32_ADC_TIMEOUT_US
);
696 dev_warn(&indio_dev
->dev
, "stop failed\n");
698 stm32_adc_clr_bits(adc
, STM32H7_ADC_CFGR
, STM32H7_DMNGT_MASK
);
701 static int stm32h7_adc_exit_pwr_down(struct stm32_adc
*adc
)
703 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
707 /* Exit deep power down, then enable ADC voltage regulator */
708 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, STM32H7_DEEPPWD
);
709 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADVREGEN
);
711 if (adc
->common
->rate
> STM32H7_BOOST_CLKRATE
)
712 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_BOOST
);
714 /* Wait for startup time */
715 if (!adc
->cfg
->has_vregready
) {
716 usleep_range(10, 20);
720 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR
, val
,
721 val
& STM32MP1_VREGREADY
, 100,
722 STM32_ADC_TIMEOUT_US
);
724 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_DEEPPWD
);
725 dev_err(&indio_dev
->dev
, "Failed to exit power down\n");
731 static void stm32h7_adc_enter_pwr_down(struct stm32_adc
*adc
)
733 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, STM32H7_BOOST
);
735 /* Setting DEEPPWD disables ADC vreg and clears ADVREGEN */
736 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_DEEPPWD
);
739 static int stm32h7_adc_enable(struct stm32_adc
*adc
)
741 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
745 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADEN
);
747 /* Poll for ADRDY to be set (after adc startup time) */
748 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR
, val
,
750 100, STM32_ADC_TIMEOUT_US
);
752 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADDIS
);
753 dev_err(&indio_dev
->dev
, "Failed to enable ADC\n");
755 /* Clear ADRDY by writing one */
756 stm32_adc_set_bits(adc
, STM32H7_ADC_ISR
, STM32H7_ADRDY
);
762 static void stm32h7_adc_disable(struct stm32_adc
*adc
)
764 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
768 /* Disable ADC and wait until it's effectively disabled */
769 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADDIS
);
770 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
771 !(val
& STM32H7_ADEN
), 100,
772 STM32_ADC_TIMEOUT_US
);
774 dev_warn(&indio_dev
->dev
, "Failed to disable\n");
778 * stm32h7_adc_read_selfcalib() - read calibration shadow regs, save result
779 * @adc: stm32 adc instance
781 static int stm32h7_adc_read_selfcalib(struct stm32_adc
*adc
)
783 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
785 u32 lincalrdyw_mask
, val
;
787 /* Enable adc so LINCALRDYW1..6 bits are writable */
788 ret
= stm32h7_adc_enable(adc
);
792 /* Read linearity calibration */
793 lincalrdyw_mask
= STM32H7_LINCALRDYW6
;
794 for (i
= STM32H7_LINCALFACT_NUM
- 1; i
>= 0; i
--) {
795 /* Clear STM32H7_LINCALRDYW[6..1]: transfer calib to CALFACT2 */
796 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, lincalrdyw_mask
);
798 /* Poll: wait calib data to be ready in CALFACT2 register */
799 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
800 !(val
& lincalrdyw_mask
),
801 100, STM32_ADC_TIMEOUT_US
);
803 dev_err(&indio_dev
->dev
, "Failed to read calfact\n");
807 val
= stm32_adc_readl(adc
, STM32H7_ADC_CALFACT2
);
808 adc
->cal
.lincalfact
[i
] = (val
& STM32H7_LINCALFACT_MASK
);
809 adc
->cal
.lincalfact
[i
] >>= STM32H7_LINCALFACT_SHIFT
;
811 lincalrdyw_mask
>>= 1;
814 /* Read offset calibration */
815 val
= stm32_adc_readl(adc
, STM32H7_ADC_CALFACT
);
816 adc
->cal
.calfact_s
= (val
& STM32H7_CALFACT_S_MASK
);
817 adc
->cal
.calfact_s
>>= STM32H7_CALFACT_S_SHIFT
;
818 adc
->cal
.calfact_d
= (val
& STM32H7_CALFACT_D_MASK
);
819 adc
->cal
.calfact_d
>>= STM32H7_CALFACT_D_SHIFT
;
822 stm32h7_adc_disable(adc
);
828 * stm32h7_adc_restore_selfcalib() - Restore saved self-calibration result
829 * @adc: stm32 adc instance
830 * Note: ADC must be enabled, with no on-going conversions.
832 static int stm32h7_adc_restore_selfcalib(struct stm32_adc
*adc
)
834 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
836 u32 lincalrdyw_mask
, val
;
838 val
= (adc
->cal
.calfact_s
<< STM32H7_CALFACT_S_SHIFT
) |
839 (adc
->cal
.calfact_d
<< STM32H7_CALFACT_D_SHIFT
);
840 stm32_adc_writel(adc
, STM32H7_ADC_CALFACT
, val
);
842 lincalrdyw_mask
= STM32H7_LINCALRDYW6
;
843 for (i
= STM32H7_LINCALFACT_NUM
- 1; i
>= 0; i
--) {
845 * Write saved calibration data to shadow registers:
846 * Write CALFACT2, and set LINCALRDYW[6..1] bit to trigger
847 * data write. Then poll to wait for complete transfer.
849 val
= adc
->cal
.lincalfact
[i
] << STM32H7_LINCALFACT_SHIFT
;
850 stm32_adc_writel(adc
, STM32H7_ADC_CALFACT2
, val
);
851 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, lincalrdyw_mask
);
852 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
853 val
& lincalrdyw_mask
,
854 100, STM32_ADC_TIMEOUT_US
);
856 dev_err(&indio_dev
->dev
, "Failed to write calfact\n");
861 * Read back calibration data, has two effects:
862 * - It ensures bits LINCALRDYW[6..1] are kept cleared
863 * for next time calibration needs to be restored.
864 * - BTW, bit clear triggers a read, then check data has been
867 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, lincalrdyw_mask
);
868 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
869 !(val
& lincalrdyw_mask
),
870 100, STM32_ADC_TIMEOUT_US
);
872 dev_err(&indio_dev
->dev
, "Failed to read calfact\n");
875 val
= stm32_adc_readl(adc
, STM32H7_ADC_CALFACT2
);
876 if (val
!= adc
->cal
.lincalfact
[i
] << STM32H7_LINCALFACT_SHIFT
) {
877 dev_err(&indio_dev
->dev
, "calfact not consistent\n");
881 lincalrdyw_mask
>>= 1;
888 * Fixed timeout value for ADC calibration.
890 * - low clock frequency
891 * - maximum prescalers
892 * Calibration requires:
893 * - 131,072 ADC clock cycle for the linear calibration
894 * - 20 ADC clock cycle for the offset calibration
896 * Set to 100ms for now
898 #define STM32H7_ADC_CALIB_TIMEOUT_US 100000
901 * stm32h7_adc_selfcalib() - Procedure to calibrate ADC (from power down)
902 * @adc: stm32 adc instance
903 * Exit from power down, calibrate ADC, then return to power down.
905 static int stm32h7_adc_selfcalib(struct stm32_adc
*adc
)
907 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
911 ret
= stm32h7_adc_exit_pwr_down(adc
);
916 * Select calibration mode:
917 * - Offset calibration for single ended inputs
918 * - No linearity calibration (do it later, before reading it)
920 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADCALDIF
);
921 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADCALLIN
);
923 /* Start calibration, then wait for completion */
924 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADCAL
);
925 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
926 !(val
& STM32H7_ADCAL
), 100,
927 STM32H7_ADC_CALIB_TIMEOUT_US
);
929 dev_err(&indio_dev
->dev
, "calibration failed\n");
934 * Select calibration mode, then start calibration:
935 * - Offset calibration for differential input
936 * - Linearity calibration (needs to be done only once for single/diff)
937 * will run simultaneously with offset calibration.
939 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
,
940 STM32H7_ADCALDIF
| STM32H7_ADCALLIN
);
941 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADCAL
);
942 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
943 !(val
& STM32H7_ADCAL
), 100,
944 STM32H7_ADC_CALIB_TIMEOUT_US
);
946 dev_err(&indio_dev
->dev
, "calibration failed\n");
950 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
,
951 STM32H7_ADCALDIF
| STM32H7_ADCALLIN
);
953 /* Read calibration result for future reference */
954 ret
= stm32h7_adc_read_selfcalib(adc
);
957 stm32h7_adc_enter_pwr_down(adc
);
963 * stm32h7_adc_prepare() - Leave power down mode to enable ADC.
964 * @adc: stm32 adc instance
965 * Leave power down mode.
966 * Configure channels as single ended or differential before enabling ADC.
968 * Restore calibration data.
969 * Pre-select channels that may be used in PCSEL (required by input MUX / IO):
970 * - Only one input is selected for single ended (e.g. 'vinp')
971 * - Two inputs are selected for differential channels (e.g. 'vinp' & 'vinn')
973 static int stm32h7_adc_prepare(struct stm32_adc
*adc
)
977 ret
= stm32h7_adc_exit_pwr_down(adc
);
981 stm32_adc_writel(adc
, STM32H7_ADC_DIFSEL
, adc
->difsel
);
983 ret
= stm32h7_adc_enable(adc
);
987 ret
= stm32h7_adc_restore_selfcalib(adc
);
991 stm32_adc_writel(adc
, STM32H7_ADC_PCSEL
, adc
->pcsel
);
996 stm32h7_adc_disable(adc
);
998 stm32h7_adc_enter_pwr_down(adc
);
1003 static void stm32h7_adc_unprepare(struct stm32_adc
*adc
)
1005 stm32h7_adc_disable(adc
);
1006 stm32h7_adc_enter_pwr_down(adc
);
1010 * stm32_adc_conf_scan_seq() - Build regular channels scan sequence
1011 * @indio_dev: IIO device
1012 * @scan_mask: channels to be converted
1014 * Conversion sequence :
1015 * Apply sampling time settings for all channels.
1016 * Configure ADC scan sequence based on selected channels in scan_mask.
1017 * Add channels to SQR registers, from scan_mask LSB to MSB, then
1018 * program sequence len.
1020 static int stm32_adc_conf_scan_seq(struct iio_dev
*indio_dev
,
1021 const unsigned long *scan_mask
)
1023 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1024 const struct stm32_adc_regs
*sqr
= adc
->cfg
->regs
->sqr
;
1025 const struct iio_chan_spec
*chan
;
1029 /* Apply sampling time settings */
1030 stm32_adc_writel(adc
, adc
->cfg
->regs
->smpr
[0], adc
->smpr_val
[0]);
1031 stm32_adc_writel(adc
, adc
->cfg
->regs
->smpr
[1], adc
->smpr_val
[1]);
1033 for_each_set_bit(bit
, scan_mask
, indio_dev
->masklength
) {
1034 chan
= indio_dev
->channels
+ bit
;
1036 * Assign one channel per SQ entry in regular
1037 * sequence, starting with SQ1.
1040 if (i
> STM32_ADC_MAX_SQ
)
1043 dev_dbg(&indio_dev
->dev
, "%s chan %d to SQ%d\n",
1044 __func__
, chan
->channel
, i
);
1046 val
= stm32_adc_readl(adc
, sqr
[i
].reg
);
1047 val
&= ~sqr
[i
].mask
;
1048 val
|= chan
->channel
<< sqr
[i
].shift
;
1049 stm32_adc_writel(adc
, sqr
[i
].reg
, val
);
1056 val
= stm32_adc_readl(adc
, sqr
[0].reg
);
1057 val
&= ~sqr
[0].mask
;
1058 val
|= ((i
- 1) << sqr
[0].shift
);
1059 stm32_adc_writel(adc
, sqr
[0].reg
, val
);
1065 * stm32_adc_get_trig_extsel() - Get external trigger selection
1068 * Returns trigger extsel value, if trig matches, -EINVAL otherwise.
1070 static int stm32_adc_get_trig_extsel(struct iio_dev
*indio_dev
,
1071 struct iio_trigger
*trig
)
1073 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1076 /* lookup triggers registered by stm32 timer trigger driver */
1077 for (i
= 0; adc
->cfg
->trigs
[i
].name
; i
++) {
1079 * Checking both stm32 timer trigger type and trig name
1080 * should be safe against arbitrary trigger names.
1082 if ((is_stm32_timer_trigger(trig
) ||
1083 is_stm32_lptim_trigger(trig
)) &&
1084 !strcmp(adc
->cfg
->trigs
[i
].name
, trig
->name
)) {
1085 return adc
->cfg
->trigs
[i
].extsel
;
1093 * stm32_adc_set_trig() - Set a regular trigger
1094 * @indio_dev: IIO device
1095 * @trig: IIO trigger
1097 * Set trigger source/polarity (e.g. SW, or HW with polarity) :
1098 * - if HW trigger disabled (e.g. trig == NULL, conversion launched by sw)
1099 * - if HW trigger enabled, set source & polarity
1101 static int stm32_adc_set_trig(struct iio_dev
*indio_dev
,
1102 struct iio_trigger
*trig
)
1104 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1105 u32 val
, extsel
= 0, exten
= STM32_EXTEN_SWTRIG
;
1106 unsigned long flags
;
1110 ret
= stm32_adc_get_trig_extsel(indio_dev
, trig
);
1114 /* set trigger source and polarity (default to rising edge) */
1116 exten
= adc
->trigger_polarity
+ STM32_EXTEN_HWTRIG_RISING_EDGE
;
1119 spin_lock_irqsave(&adc
->lock
, flags
);
1120 val
= stm32_adc_readl(adc
, adc
->cfg
->regs
->exten
.reg
);
1121 val
&= ~(adc
->cfg
->regs
->exten
.mask
| adc
->cfg
->regs
->extsel
.mask
);
1122 val
|= exten
<< adc
->cfg
->regs
->exten
.shift
;
1123 val
|= extsel
<< adc
->cfg
->regs
->extsel
.shift
;
1124 stm32_adc_writel(adc
, adc
->cfg
->regs
->exten
.reg
, val
);
1125 spin_unlock_irqrestore(&adc
->lock
, flags
);
1130 static int stm32_adc_set_trig_pol(struct iio_dev
*indio_dev
,
1131 const struct iio_chan_spec
*chan
,
1134 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1136 adc
->trigger_polarity
= type
;
1141 static int stm32_adc_get_trig_pol(struct iio_dev
*indio_dev
,
1142 const struct iio_chan_spec
*chan
)
1144 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1146 return adc
->trigger_polarity
;
1149 static const char * const stm32_trig_pol_items
[] = {
1150 "rising-edge", "falling-edge", "both-edges",
1153 static const struct iio_enum stm32_adc_trig_pol
= {
1154 .items
= stm32_trig_pol_items
,
1155 .num_items
= ARRAY_SIZE(stm32_trig_pol_items
),
1156 .get
= stm32_adc_get_trig_pol
,
1157 .set
= stm32_adc_set_trig_pol
,
1161 * stm32_adc_single_conv() - Performs a single conversion
1162 * @indio_dev: IIO device
1163 * @chan: IIO channel
1164 * @res: conversion result
1166 * The function performs a single conversion on a given channel:
1167 * - Apply sampling time settings
1168 * - Program sequencer with one channel (e.g. in SQ1 with len = 1)
1170 * - Start conversion, then wait for interrupt completion.
1172 static int stm32_adc_single_conv(struct iio_dev
*indio_dev
,
1173 const struct iio_chan_spec
*chan
,
1176 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1177 const struct stm32_adc_regspec
*regs
= adc
->cfg
->regs
;
1182 reinit_completion(&adc
->completion
);
1186 if (adc
->cfg
->prepare
) {
1187 ret
= adc
->cfg
->prepare(adc
);
1192 /* Apply sampling time settings */
1193 stm32_adc_writel(adc
, regs
->smpr
[0], adc
->smpr_val
[0]);
1194 stm32_adc_writel(adc
, regs
->smpr
[1], adc
->smpr_val
[1]);
1196 /* Program chan number in regular sequence (SQ1) */
1197 val
= stm32_adc_readl(adc
, regs
->sqr
[1].reg
);
1198 val
&= ~regs
->sqr
[1].mask
;
1199 val
|= chan
->channel
<< regs
->sqr
[1].shift
;
1200 stm32_adc_writel(adc
, regs
->sqr
[1].reg
, val
);
1202 /* Set regular sequence len (0 for 1 conversion) */
1203 stm32_adc_clr_bits(adc
, regs
->sqr
[0].reg
, regs
->sqr
[0].mask
);
1205 /* Trigger detection disabled (conversion can be launched in SW) */
1206 stm32_adc_clr_bits(adc
, regs
->exten
.reg
, regs
->exten
.mask
);
1208 stm32_adc_conv_irq_enable(adc
);
1210 adc
->cfg
->start_conv(adc
, false);
1212 timeout
= wait_for_completion_interruptible_timeout(
1213 &adc
->completion
, STM32_ADC_TIMEOUT
);
1216 } else if (timeout
< 0) {
1219 *res
= adc
->buffer
[0];
1223 adc
->cfg
->stop_conv(adc
);
1225 stm32_adc_conv_irq_disable(adc
);
1227 if (adc
->cfg
->unprepare
)
1228 adc
->cfg
->unprepare(adc
);
1233 static int stm32_adc_read_raw(struct iio_dev
*indio_dev
,
1234 struct iio_chan_spec
const *chan
,
1235 int *val
, int *val2
, long mask
)
1237 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1241 case IIO_CHAN_INFO_RAW
:
1242 ret
= iio_device_claim_direct_mode(indio_dev
);
1245 if (chan
->type
== IIO_VOLTAGE
)
1246 ret
= stm32_adc_single_conv(indio_dev
, chan
, val
);
1249 iio_device_release_direct_mode(indio_dev
);
1252 case IIO_CHAN_INFO_SCALE
:
1253 if (chan
->differential
) {
1254 *val
= adc
->common
->vref_mv
* 2;
1255 *val2
= chan
->scan_type
.realbits
;
1257 *val
= adc
->common
->vref_mv
;
1258 *val2
= chan
->scan_type
.realbits
;
1260 return IIO_VAL_FRACTIONAL_LOG2
;
1262 case IIO_CHAN_INFO_OFFSET
:
1263 if (chan
->differential
)
1264 /* ADC_full_scale / 2 */
1265 *val
= -((1 << chan
->scan_type
.realbits
) / 2);
1275 static irqreturn_t
stm32_adc_isr(int irq
, void *data
)
1277 struct stm32_adc
*adc
= data
;
1278 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1279 const struct stm32_adc_regspec
*regs
= adc
->cfg
->regs
;
1280 u32 status
= stm32_adc_readl(adc
, regs
->isr_eoc
.reg
);
1282 if (status
& regs
->isr_eoc
.mask
) {
1283 /* Reading DR also clears EOC status flag */
1284 adc
->buffer
[adc
->bufi
] = stm32_adc_readw(adc
, regs
->dr
);
1285 if (iio_buffer_enabled(indio_dev
)) {
1287 if (adc
->bufi
>= adc
->num_conv
) {
1288 stm32_adc_conv_irq_disable(adc
);
1289 iio_trigger_poll(indio_dev
->trig
);
1292 complete(&adc
->completion
);
1301 * stm32_adc_validate_trigger() - validate trigger for stm32 adc
1302 * @indio_dev: IIO device
1303 * @trig: new trigger
1305 * Returns: 0 if trig matches one of the triggers registered by stm32 adc
1306 * driver, -EINVAL otherwise.
1308 static int stm32_adc_validate_trigger(struct iio_dev
*indio_dev
,
1309 struct iio_trigger
*trig
)
1311 return stm32_adc_get_trig_extsel(indio_dev
, trig
) < 0 ? -EINVAL
: 0;
1314 static int stm32_adc_set_watermark(struct iio_dev
*indio_dev
, unsigned int val
)
1316 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1317 unsigned int watermark
= STM32_DMA_BUFFER_SIZE
/ 2;
1318 unsigned int rx_buf_sz
= STM32_DMA_BUFFER_SIZE
;
1321 * dma cyclic transfers are used, buffer is split into two periods.
1323 * - always one buffer (period) dma is working on
1324 * - one buffer (period) driver can push with iio_trigger_poll().
1326 watermark
= min(watermark
, val
* (unsigned)(sizeof(u16
)));
1327 adc
->rx_buf_sz
= min(rx_buf_sz
, watermark
* 2 * adc
->num_conv
);
1332 static int stm32_adc_update_scan_mode(struct iio_dev
*indio_dev
,
1333 const unsigned long *scan_mask
)
1335 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1338 adc
->num_conv
= bitmap_weight(scan_mask
, indio_dev
->masklength
);
1340 ret
= stm32_adc_conf_scan_seq(indio_dev
, scan_mask
);
1347 static int stm32_adc_of_xlate(struct iio_dev
*indio_dev
,
1348 const struct of_phandle_args
*iiospec
)
1352 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
1353 if (indio_dev
->channels
[i
].channel
== iiospec
->args
[0])
1360 * stm32_adc_debugfs_reg_access - read or write register value
1362 * To read a value from an ADC register:
1363 * echo [ADC reg offset] > direct_reg_access
1364 * cat direct_reg_access
1366 * To write a value in a ADC register:
1367 * echo [ADC_reg_offset] [value] > direct_reg_access
1369 static int stm32_adc_debugfs_reg_access(struct iio_dev
*indio_dev
,
1370 unsigned reg
, unsigned writeval
,
1373 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1376 stm32_adc_writel(adc
, reg
, writeval
);
1378 *readval
= stm32_adc_readl(adc
, reg
);
1383 static const struct iio_info stm32_adc_iio_info
= {
1384 .read_raw
= stm32_adc_read_raw
,
1385 .validate_trigger
= stm32_adc_validate_trigger
,
1386 .hwfifo_set_watermark
= stm32_adc_set_watermark
,
1387 .update_scan_mode
= stm32_adc_update_scan_mode
,
1388 .debugfs_reg_access
= stm32_adc_debugfs_reg_access
,
1389 .of_xlate
= stm32_adc_of_xlate
,
1392 static unsigned int stm32_adc_dma_residue(struct stm32_adc
*adc
)
1394 struct dma_tx_state state
;
1395 enum dma_status status
;
1397 status
= dmaengine_tx_status(adc
->dma_chan
,
1398 adc
->dma_chan
->cookie
,
1400 if (status
== DMA_IN_PROGRESS
) {
1401 /* Residue is size in bytes from end of buffer */
1402 unsigned int i
= adc
->rx_buf_sz
- state
.residue
;
1405 /* Return available bytes */
1407 size
= i
- adc
->bufi
;
1409 size
= adc
->rx_buf_sz
+ i
- adc
->bufi
;
1417 static void stm32_adc_dma_buffer_done(void *data
)
1419 struct iio_dev
*indio_dev
= data
;
1421 iio_trigger_poll_chained(indio_dev
->trig
);
1424 static int stm32_adc_dma_start(struct iio_dev
*indio_dev
)
1426 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1427 struct dma_async_tx_descriptor
*desc
;
1428 dma_cookie_t cookie
;
1434 dev_dbg(&indio_dev
->dev
, "%s size=%d watermark=%d\n", __func__
,
1435 adc
->rx_buf_sz
, adc
->rx_buf_sz
/ 2);
1437 /* Prepare a DMA cyclic transaction */
1438 desc
= dmaengine_prep_dma_cyclic(adc
->dma_chan
,
1440 adc
->rx_buf_sz
, adc
->rx_buf_sz
/ 2,
1442 DMA_PREP_INTERRUPT
);
1446 desc
->callback
= stm32_adc_dma_buffer_done
;
1447 desc
->callback_param
= indio_dev
;
1449 cookie
= dmaengine_submit(desc
);
1450 ret
= dma_submit_error(cookie
);
1452 dmaengine_terminate_all(adc
->dma_chan
);
1456 /* Issue pending DMA requests */
1457 dma_async_issue_pending(adc
->dma_chan
);
1462 static int stm32_adc_buffer_postenable(struct iio_dev
*indio_dev
)
1464 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1467 if (adc
->cfg
->prepare
) {
1468 ret
= adc
->cfg
->prepare(adc
);
1473 ret
= stm32_adc_set_trig(indio_dev
, indio_dev
->trig
);
1475 dev_err(&indio_dev
->dev
, "Can't set trigger\n");
1479 ret
= stm32_adc_dma_start(indio_dev
);
1481 dev_err(&indio_dev
->dev
, "Can't start dma\n");
1485 ret
= iio_triggered_buffer_postenable(indio_dev
);
1489 /* Reset adc buffer index */
1493 stm32_adc_conv_irq_enable(adc
);
1495 adc
->cfg
->start_conv(adc
, !!adc
->dma_chan
);
1501 dmaengine_terminate_all(adc
->dma_chan
);
1503 stm32_adc_set_trig(indio_dev
, NULL
);
1505 if (adc
->cfg
->unprepare
)
1506 adc
->cfg
->unprepare(adc
);
1511 static int stm32_adc_buffer_predisable(struct iio_dev
*indio_dev
)
1513 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1516 adc
->cfg
->stop_conv(adc
);
1518 stm32_adc_conv_irq_disable(adc
);
1520 ret
= iio_triggered_buffer_predisable(indio_dev
);
1522 dev_err(&indio_dev
->dev
, "predisable failed\n");
1525 dmaengine_terminate_all(adc
->dma_chan
);
1527 if (stm32_adc_set_trig(indio_dev
, NULL
))
1528 dev_err(&indio_dev
->dev
, "Can't clear trigger\n");
1530 if (adc
->cfg
->unprepare
)
1531 adc
->cfg
->unprepare(adc
);
1536 static const struct iio_buffer_setup_ops stm32_adc_buffer_setup_ops
= {
1537 .postenable
= &stm32_adc_buffer_postenable
,
1538 .predisable
= &stm32_adc_buffer_predisable
,
1541 static irqreturn_t
stm32_adc_trigger_handler(int irq
, void *p
)
1543 struct iio_poll_func
*pf
= p
;
1544 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1545 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1547 dev_dbg(&indio_dev
->dev
, "%s bufi=%d\n", __func__
, adc
->bufi
);
1549 if (!adc
->dma_chan
) {
1550 /* reset buffer index */
1552 iio_push_to_buffers_with_timestamp(indio_dev
, adc
->buffer
,
1555 int residue
= stm32_adc_dma_residue(adc
);
1557 while (residue
>= indio_dev
->scan_bytes
) {
1558 u16
*buffer
= (u16
*)&adc
->rx_buf
[adc
->bufi
];
1560 iio_push_to_buffers_with_timestamp(indio_dev
, buffer
,
1562 residue
-= indio_dev
->scan_bytes
;
1563 adc
->bufi
+= indio_dev
->scan_bytes
;
1564 if (adc
->bufi
>= adc
->rx_buf_sz
)
1569 iio_trigger_notify_done(indio_dev
->trig
);
1571 /* re-enable eoc irq */
1573 stm32_adc_conv_irq_enable(adc
);
1578 static const struct iio_chan_spec_ext_info stm32_adc_ext_info
[] = {
1579 IIO_ENUM("trigger_polarity", IIO_SHARED_BY_ALL
, &stm32_adc_trig_pol
),
1581 .name
= "trigger_polarity_available",
1582 .shared
= IIO_SHARED_BY_ALL
,
1583 .read
= iio_enum_available_read
,
1584 .private = (uintptr_t)&stm32_adc_trig_pol
,
1589 static int stm32_adc_of_get_resolution(struct iio_dev
*indio_dev
)
1591 struct device_node
*node
= indio_dev
->dev
.of_node
;
1592 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1596 if (of_property_read_u32(node
, "assigned-resolution-bits", &res
))
1597 res
= adc
->cfg
->adc_info
->resolutions
[0];
1599 for (i
= 0; i
< adc
->cfg
->adc_info
->num_res
; i
++)
1600 if (res
== adc
->cfg
->adc_info
->resolutions
[i
])
1602 if (i
>= adc
->cfg
->adc_info
->num_res
) {
1603 dev_err(&indio_dev
->dev
, "Bad resolution: %u bits\n", res
);
1607 dev_dbg(&indio_dev
->dev
, "Using %u bits resolution\n", res
);
1613 static void stm32_adc_smpr_init(struct stm32_adc
*adc
, int channel
, u32 smp_ns
)
1615 const struct stm32_adc_regs
*smpr
= &adc
->cfg
->regs
->smp_bits
[channel
];
1616 u32 period_ns
, shift
= smpr
->shift
, mask
= smpr
->mask
;
1617 unsigned int smp
, r
= smpr
->reg
;
1619 /* Determine sampling time (ADC clock cycles) */
1620 period_ns
= NSEC_PER_SEC
/ adc
->common
->rate
;
1621 for (smp
= 0; smp
<= STM32_ADC_MAX_SMP
; smp
++)
1622 if ((period_ns
* adc
->cfg
->smp_cycles
[smp
]) >= smp_ns
)
1624 if (smp
> STM32_ADC_MAX_SMP
)
1625 smp
= STM32_ADC_MAX_SMP
;
1627 /* pre-build sampling time registers (e.g. smpr1, smpr2) */
1628 adc
->smpr_val
[r
] = (adc
->smpr_val
[r
] & ~mask
) | (smp
<< shift
);
1631 static void stm32_adc_chan_init_one(struct iio_dev
*indio_dev
,
1632 struct iio_chan_spec
*chan
, u32 vinp
,
1633 u32 vinn
, int scan_index
, bool differential
)
1635 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1636 char *name
= adc
->chan_name
[vinp
];
1638 chan
->type
= IIO_VOLTAGE
;
1639 chan
->channel
= vinp
;
1641 chan
->differential
= 1;
1642 chan
->channel2
= vinn
;
1643 snprintf(name
, STM32_ADC_CH_SZ
, "in%d-in%d", vinp
, vinn
);
1645 snprintf(name
, STM32_ADC_CH_SZ
, "in%d", vinp
);
1647 chan
->datasheet_name
= name
;
1648 chan
->scan_index
= scan_index
;
1650 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
1651 chan
->info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
) |
1652 BIT(IIO_CHAN_INFO_OFFSET
);
1653 chan
->scan_type
.sign
= 'u';
1654 chan
->scan_type
.realbits
= adc
->cfg
->adc_info
->resolutions
[adc
->res
];
1655 chan
->scan_type
.storagebits
= 16;
1656 chan
->ext_info
= stm32_adc_ext_info
;
1658 /* pre-build selected channels mask */
1659 adc
->pcsel
|= BIT(chan
->channel
);
1661 /* pre-build diff channels mask */
1662 adc
->difsel
|= BIT(chan
->channel
);
1663 /* Also add negative input to pre-selected channels */
1664 adc
->pcsel
|= BIT(chan
->channel2
);
1668 static int stm32_adc_chan_of_init(struct iio_dev
*indio_dev
)
1670 struct device_node
*node
= indio_dev
->dev
.of_node
;
1671 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1672 const struct stm32_adc_info
*adc_info
= adc
->cfg
->adc_info
;
1673 struct stm32_adc_diff_channel diff
[STM32_ADC_CH_MAX
];
1674 struct property
*prop
;
1676 struct iio_chan_spec
*channels
;
1677 int scan_index
= 0, num_channels
= 0, num_diff
= 0, ret
, i
;
1680 ret
= of_property_count_u32_elems(node
, "st,adc-channels");
1681 if (ret
> adc_info
->max_channels
) {
1682 dev_err(&indio_dev
->dev
, "Bad st,adc-channels?\n");
1684 } else if (ret
> 0) {
1685 num_channels
+= ret
;
1688 ret
= of_property_count_elems_of_size(node
, "st,adc-diff-channels",
1690 if (ret
> adc_info
->max_channels
) {
1691 dev_err(&indio_dev
->dev
, "Bad st,adc-diff-channels?\n");
1693 } else if (ret
> 0) {
1694 int size
= ret
* sizeof(*diff
) / sizeof(u32
);
1697 num_channels
+= ret
;
1698 ret
= of_property_read_u32_array(node
, "st,adc-diff-channels",
1704 if (!num_channels
) {
1705 dev_err(&indio_dev
->dev
, "No channels configured\n");
1709 /* Optional sample time is provided either for each, or all channels */
1710 ret
= of_property_count_u32_elems(node
, "st,min-sample-time-nsecs");
1711 if (ret
> 1 && ret
!= num_channels
) {
1712 dev_err(&indio_dev
->dev
, "Invalid st,min-sample-time-nsecs\n");
1716 channels
= devm_kcalloc(&indio_dev
->dev
, num_channels
,
1717 sizeof(struct iio_chan_spec
), GFP_KERNEL
);
1721 of_property_for_each_u32(node
, "st,adc-channels", prop
, cur
, val
) {
1722 if (val
>= adc_info
->max_channels
) {
1723 dev_err(&indio_dev
->dev
, "Invalid channel %d\n", val
);
1727 /* Channel can't be configured both as single-ended & diff */
1728 for (i
= 0; i
< num_diff
; i
++) {
1729 if (val
== diff
[i
].vinp
) {
1730 dev_err(&indio_dev
->dev
,
1731 "channel %d miss-configured\n", val
);
1735 stm32_adc_chan_init_one(indio_dev
, &channels
[scan_index
], val
,
1736 0, scan_index
, false);
1740 for (i
= 0; i
< num_diff
; i
++) {
1741 if (diff
[i
].vinp
>= adc_info
->max_channels
||
1742 diff
[i
].vinn
>= adc_info
->max_channels
) {
1743 dev_err(&indio_dev
->dev
, "Invalid channel in%d-in%d\n",
1744 diff
[i
].vinp
, diff
[i
].vinn
);
1747 stm32_adc_chan_init_one(indio_dev
, &channels
[scan_index
],
1748 diff
[i
].vinp
, diff
[i
].vinn
, scan_index
,
1753 for (i
= 0; i
< scan_index
; i
++) {
1755 * Using of_property_read_u32_index(), smp value will only be
1756 * modified if valid u32 value can be decoded. This allows to
1757 * get either no value, 1 shared value for all indexes, or one
1758 * value per channel.
1760 of_property_read_u32_index(node
, "st,min-sample-time-nsecs",
1762 /* Prepare sampling time settings */
1763 stm32_adc_smpr_init(adc
, channels
[i
].channel
, smp
);
1766 indio_dev
->num_channels
= scan_index
;
1767 indio_dev
->channels
= channels
;
1772 static int stm32_adc_dma_request(struct iio_dev
*indio_dev
)
1774 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1775 struct dma_slave_config config
;
1778 adc
->dma_chan
= dma_request_slave_channel(&indio_dev
->dev
, "rx");
1782 adc
->rx_buf
= dma_alloc_coherent(adc
->dma_chan
->device
->dev
,
1783 STM32_DMA_BUFFER_SIZE
,
1784 &adc
->rx_dma_buf
, GFP_KERNEL
);
1790 /* Configure DMA channel to read data register */
1791 memset(&config
, 0, sizeof(config
));
1792 config
.src_addr
= (dma_addr_t
)adc
->common
->phys_base
;
1793 config
.src_addr
+= adc
->offset
+ adc
->cfg
->regs
->dr
;
1794 config
.src_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
1796 ret
= dmaengine_slave_config(adc
->dma_chan
, &config
);
1803 dma_free_coherent(adc
->dma_chan
->device
->dev
, STM32_DMA_BUFFER_SIZE
,
1804 adc
->rx_buf
, adc
->rx_dma_buf
);
1806 dma_release_channel(adc
->dma_chan
);
1811 static int stm32_adc_probe(struct platform_device
*pdev
)
1813 struct iio_dev
*indio_dev
;
1814 struct device
*dev
= &pdev
->dev
;
1815 struct stm32_adc
*adc
;
1818 if (!pdev
->dev
.of_node
)
1821 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*adc
));
1825 adc
= iio_priv(indio_dev
);
1826 adc
->common
= dev_get_drvdata(pdev
->dev
.parent
);
1827 spin_lock_init(&adc
->lock
);
1828 init_completion(&adc
->completion
);
1829 adc
->cfg
= (const struct stm32_adc_cfg
*)
1830 of_match_device(dev
->driver
->of_match_table
, dev
)->data
;
1832 indio_dev
->name
= dev_name(&pdev
->dev
);
1833 indio_dev
->dev
.parent
= &pdev
->dev
;
1834 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
1835 indio_dev
->info
= &stm32_adc_iio_info
;
1836 indio_dev
->modes
= INDIO_DIRECT_MODE
| INDIO_HARDWARE_TRIGGERED
;
1838 platform_set_drvdata(pdev
, adc
);
1840 ret
= of_property_read_u32(pdev
->dev
.of_node
, "reg", &adc
->offset
);
1842 dev_err(&pdev
->dev
, "missing reg property\n");
1846 adc
->irq
= platform_get_irq(pdev
, 0);
1848 dev_err(&pdev
->dev
, "failed to get irq\n");
1852 ret
= devm_request_irq(&pdev
->dev
, adc
->irq
, stm32_adc_isr
,
1853 0, pdev
->name
, adc
);
1855 dev_err(&pdev
->dev
, "failed to request IRQ\n");
1859 adc
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
1860 if (IS_ERR(adc
->clk
)) {
1861 ret
= PTR_ERR(adc
->clk
);
1862 if (ret
== -ENOENT
&& !adc
->cfg
->clk_required
) {
1865 dev_err(&pdev
->dev
, "Can't get clock\n");
1871 ret
= clk_prepare_enable(adc
->clk
);
1873 dev_err(&pdev
->dev
, "clk enable failed\n");
1878 ret
= stm32_adc_of_get_resolution(indio_dev
);
1880 goto err_clk_disable
;
1881 stm32_adc_set_res(adc
);
1883 if (adc
->cfg
->selfcalib
) {
1884 ret
= adc
->cfg
->selfcalib(adc
);
1886 goto err_clk_disable
;
1889 ret
= stm32_adc_chan_of_init(indio_dev
);
1891 goto err_clk_disable
;
1893 ret
= stm32_adc_dma_request(indio_dev
);
1895 goto err_clk_disable
;
1897 ret
= iio_triggered_buffer_setup(indio_dev
,
1898 &iio_pollfunc_store_time
,
1899 &stm32_adc_trigger_handler
,
1900 &stm32_adc_buffer_setup_ops
);
1902 dev_err(&pdev
->dev
, "buffer setup failed\n");
1903 goto err_dma_disable
;
1906 ret
= iio_device_register(indio_dev
);
1908 dev_err(&pdev
->dev
, "iio dev register failed\n");
1909 goto err_buffer_cleanup
;
1915 iio_triggered_buffer_cleanup(indio_dev
);
1918 if (adc
->dma_chan
) {
1919 dma_free_coherent(adc
->dma_chan
->device
->dev
,
1920 STM32_DMA_BUFFER_SIZE
,
1921 adc
->rx_buf
, adc
->rx_dma_buf
);
1922 dma_release_channel(adc
->dma_chan
);
1926 clk_disable_unprepare(adc
->clk
);
1931 static int stm32_adc_remove(struct platform_device
*pdev
)
1933 struct stm32_adc
*adc
= platform_get_drvdata(pdev
);
1934 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1936 iio_device_unregister(indio_dev
);
1937 iio_triggered_buffer_cleanup(indio_dev
);
1938 if (adc
->dma_chan
) {
1939 dma_free_coherent(adc
->dma_chan
->device
->dev
,
1940 STM32_DMA_BUFFER_SIZE
,
1941 adc
->rx_buf
, adc
->rx_dma_buf
);
1942 dma_release_channel(adc
->dma_chan
);
1945 clk_disable_unprepare(adc
->clk
);
1950 static const struct stm32_adc_cfg stm32f4_adc_cfg
= {
1951 .regs
= &stm32f4_adc_regspec
,
1952 .adc_info
= &stm32f4_adc_info
,
1953 .trigs
= stm32f4_adc_trigs
,
1954 .clk_required
= true,
1955 .start_conv
= stm32f4_adc_start_conv
,
1956 .stop_conv
= stm32f4_adc_stop_conv
,
1957 .smp_cycles
= stm32f4_adc_smp_cycles
,
1960 static const struct stm32_adc_cfg stm32h7_adc_cfg
= {
1961 .regs
= &stm32h7_adc_regspec
,
1962 .adc_info
= &stm32h7_adc_info
,
1963 .trigs
= stm32h7_adc_trigs
,
1964 .selfcalib
= stm32h7_adc_selfcalib
,
1965 .start_conv
= stm32h7_adc_start_conv
,
1966 .stop_conv
= stm32h7_adc_stop_conv
,
1967 .prepare
= stm32h7_adc_prepare
,
1968 .unprepare
= stm32h7_adc_unprepare
,
1969 .smp_cycles
= stm32h7_adc_smp_cycles
,
1972 static const struct stm32_adc_cfg stm32mp1_adc_cfg
= {
1973 .regs
= &stm32h7_adc_regspec
,
1974 .adc_info
= &stm32h7_adc_info
,
1975 .trigs
= stm32h7_adc_trigs
,
1976 .has_vregready
= true,
1977 .selfcalib
= stm32h7_adc_selfcalib
,
1978 .start_conv
= stm32h7_adc_start_conv
,
1979 .stop_conv
= stm32h7_adc_stop_conv
,
1980 .prepare
= stm32h7_adc_prepare
,
1981 .unprepare
= stm32h7_adc_unprepare
,
1982 .smp_cycles
= stm32h7_adc_smp_cycles
,
1985 static const struct of_device_id stm32_adc_of_match
[] = {
1986 { .compatible
= "st,stm32f4-adc", .data
= (void *)&stm32f4_adc_cfg
},
1987 { .compatible
= "st,stm32h7-adc", .data
= (void *)&stm32h7_adc_cfg
},
1988 { .compatible
= "st,stm32mp1-adc", .data
= (void *)&stm32mp1_adc_cfg
},
1991 MODULE_DEVICE_TABLE(of
, stm32_adc_of_match
);
1993 static struct platform_driver stm32_adc_driver
= {
1994 .probe
= stm32_adc_probe
,
1995 .remove
= stm32_adc_remove
,
1997 .name
= "stm32-adc",
1998 .of_match_table
= stm32_adc_of_match
,
2001 module_platform_driver(stm32_adc_driver
);
2003 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
2004 MODULE_DESCRIPTION("STMicroelectronics STM32 ADC IIO driver");
2005 MODULE_LICENSE("GPL v2");
2006 MODULE_ALIAS("platform:stm32-adc");