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 /* Number of linear calibration shadow registers / LINCALRDYW control bits */
31 #define STM32H7_LINCALFACT_NUM 6
33 /* BOOST bit must be set on STM32H7 when ADC clock is above 20MHz */
34 #define STM32H7_BOOST_CLKRATE 20000000UL
36 #define STM32_ADC_CH_MAX 20 /* max number of channels */
37 #define STM32_ADC_CH_SZ 10 /* max channel name size */
38 #define STM32_ADC_MAX_SQ 16 /* SQ1..SQ16 */
39 #define STM32_ADC_MAX_SMP 7 /* SMPx range is [0..7] */
40 #define STM32_ADC_TIMEOUT_US 100000
41 #define STM32_ADC_TIMEOUT (msecs_to_jiffies(STM32_ADC_TIMEOUT_US / 1000))
43 #define STM32_DMA_BUFFER_SIZE PAGE_SIZE
45 /* External trigger enable */
46 enum stm32_adc_exten
{
48 STM32_EXTEN_HWTRIG_RISING_EDGE
,
49 STM32_EXTEN_HWTRIG_FALLING_EDGE
,
50 STM32_EXTEN_HWTRIG_BOTH_EDGES
,
53 /* extsel - trigger mux selection value */
54 enum stm32_adc_extsel
{
79 * struct stm32_adc_trig_info - ADC trigger info
80 * @name: name of the trigger, corresponding to its source
81 * @extsel: trigger selection
83 struct stm32_adc_trig_info
{
85 enum stm32_adc_extsel extsel
;
89 * struct stm32_adc_calib - optional adc calibration data
90 * @calfact_s: Calibration offset for single ended channels
91 * @calfact_d: Calibration offset in differential
92 * @lincalfact: Linearity calibration factor
94 struct stm32_adc_calib
{
97 u32 lincalfact
[STM32H7_LINCALFACT_NUM
];
101 * stm32_adc_regs - stm32 ADC misc registers & bitfield desc
102 * @reg: register offset
103 * @mask: bitfield mask
106 struct stm32_adc_regs
{
113 * stm32_adc_regspec - stm32 registers definition, compatible dependent data
114 * @dr: data register offset
115 * @ier_eoc: interrupt enable register & eocie bitfield
116 * @isr_eoc: interrupt status register & eoc bitfield
117 * @sqr: reference to sequence registers array
118 * @exten: trigger control register & bitfield
119 * @extsel: trigger selection register & bitfield
120 * @res: resolution selection register & bitfield
121 * @smpr: smpr1 & smpr2 registers offset array
122 * @smp_bits: smpr1 & smpr2 index and bitfields
124 struct stm32_adc_regspec
{
126 const struct stm32_adc_regs ier_eoc
;
127 const struct stm32_adc_regs isr_eoc
;
128 const struct stm32_adc_regs
*sqr
;
129 const struct stm32_adc_regs exten
;
130 const struct stm32_adc_regs extsel
;
131 const struct stm32_adc_regs res
;
133 const struct stm32_adc_regs
*smp_bits
;
139 * stm32_adc_cfg - stm32 compatible configuration data
140 * @regs: registers descriptions
141 * @adc_info: per instance input channels definitions
142 * @trigs: external trigger sources
143 * @clk_required: clock is required
144 * @has_vregready: vregready status flag presence
145 * @selfcalib: optional routine for self-calibration
146 * @prepare: optional prepare routine (power-up, enable)
147 * @start_conv: routine to start conversions
148 * @stop_conv: routine to stop conversions
149 * @unprepare: optional unprepare routine (disable, power-down)
150 * @smp_cycles: programmable sampling time (ADC clock cycles)
152 struct stm32_adc_cfg
{
153 const struct stm32_adc_regspec
*regs
;
154 const struct stm32_adc_info
*adc_info
;
155 struct stm32_adc_trig_info
*trigs
;
158 int (*selfcalib
)(struct stm32_adc
*);
159 int (*prepare
)(struct stm32_adc
*);
160 void (*start_conv
)(struct stm32_adc
*, bool dma
);
161 void (*stop_conv
)(struct stm32_adc
*);
162 void (*unprepare
)(struct stm32_adc
*);
163 const unsigned int *smp_cycles
;
167 * struct stm32_adc - private data of each ADC IIO instance
168 * @common: reference to ADC block common data
169 * @offset: ADC instance register offset in ADC block
170 * @cfg: compatible configuration data
171 * @completion: end of single conversion completion
172 * @buffer: data buffer
173 * @clk: clock for this adc instance
174 * @irq: interrupt for this adc instance
176 * @bufi: data buffer index
177 * @num_conv: expected number of scan conversions
178 * @res: data resolution (e.g. RES bitfield value)
179 * @trigger_polarity: external trigger polarity (e.g. exten)
180 * @dma_chan: dma channel
181 * @rx_buf: dma rx buffer cpu address
182 * @rx_dma_buf: dma rx buffer bus address
183 * @rx_buf_sz: dma rx buffer size
184 * @difsel bitmask to set single-ended/differential channel
185 * @pcsel bitmask to preselect channels on some devices
186 * @smpr_val: sampling time settings (e.g. smpr1 / smpr2)
187 * @cal: optional calibration data on some devices
188 * @chan_name: channel name array
191 struct stm32_adc_common
*common
;
193 const struct stm32_adc_cfg
*cfg
;
194 struct completion completion
;
195 u16 buffer
[STM32_ADC_MAX_SQ
];
198 spinlock_t lock
; /* interrupt lock */
200 unsigned int num_conv
;
202 u32 trigger_polarity
;
203 struct dma_chan
*dma_chan
;
205 dma_addr_t rx_dma_buf
;
206 unsigned int rx_buf_sz
;
210 struct stm32_adc_calib cal
;
211 char chan_name
[STM32_ADC_CH_MAX
][STM32_ADC_CH_SZ
];
214 struct stm32_adc_diff_channel
{
220 * struct stm32_adc_info - stm32 ADC, per instance config data
221 * @max_channels: Number of channels
222 * @resolutions: available resolutions
223 * @num_res: number of available resolutions
225 struct stm32_adc_info
{
227 const unsigned int *resolutions
;
228 const unsigned int num_res
;
231 static const unsigned int stm32f4_adc_resolutions
[] = {
232 /* sorted values so the index matches RES[1:0] in STM32F4_ADC_CR1 */
236 /* stm32f4 can have up to 16 channels */
237 static const struct stm32_adc_info stm32f4_adc_info
= {
239 .resolutions
= stm32f4_adc_resolutions
,
240 .num_res
= ARRAY_SIZE(stm32f4_adc_resolutions
),
243 static const unsigned int stm32h7_adc_resolutions
[] = {
244 /* sorted values so the index matches RES[2:0] in STM32H7_ADC_CFGR */
248 /* stm32h7 can have up to 20 channels */
249 static const struct stm32_adc_info stm32h7_adc_info
= {
250 .max_channels
= STM32_ADC_CH_MAX
,
251 .resolutions
= stm32h7_adc_resolutions
,
252 .num_res
= ARRAY_SIZE(stm32h7_adc_resolutions
),
256 * stm32f4_sq - describe regular sequence registers
257 * - L: sequence len (register & bit field)
258 * - SQ1..SQ16: sequence entries (register & bit field)
260 static const struct stm32_adc_regs stm32f4_sq
[STM32_ADC_MAX_SQ
+ 1] = {
261 /* L: len bit field description to be kept as first element */
262 { STM32F4_ADC_SQR1
, GENMASK(23, 20), 20 },
263 /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
264 { STM32F4_ADC_SQR3
, GENMASK(4, 0), 0 },
265 { STM32F4_ADC_SQR3
, GENMASK(9, 5), 5 },
266 { STM32F4_ADC_SQR3
, GENMASK(14, 10), 10 },
267 { STM32F4_ADC_SQR3
, GENMASK(19, 15), 15 },
268 { STM32F4_ADC_SQR3
, GENMASK(24, 20), 20 },
269 { STM32F4_ADC_SQR3
, GENMASK(29, 25), 25 },
270 { STM32F4_ADC_SQR2
, GENMASK(4, 0), 0 },
271 { STM32F4_ADC_SQR2
, GENMASK(9, 5), 5 },
272 { STM32F4_ADC_SQR2
, GENMASK(14, 10), 10 },
273 { STM32F4_ADC_SQR2
, GENMASK(19, 15), 15 },
274 { STM32F4_ADC_SQR2
, GENMASK(24, 20), 20 },
275 { STM32F4_ADC_SQR2
, GENMASK(29, 25), 25 },
276 { STM32F4_ADC_SQR1
, GENMASK(4, 0), 0 },
277 { STM32F4_ADC_SQR1
, GENMASK(9, 5), 5 },
278 { STM32F4_ADC_SQR1
, GENMASK(14, 10), 10 },
279 { STM32F4_ADC_SQR1
, GENMASK(19, 15), 15 },
282 /* STM32F4 external trigger sources for all instances */
283 static struct stm32_adc_trig_info stm32f4_adc_trigs
[] = {
284 { TIM1_CH1
, STM32_EXT0
},
285 { TIM1_CH2
, STM32_EXT1
},
286 { TIM1_CH3
, STM32_EXT2
},
287 { TIM2_CH2
, STM32_EXT3
},
288 { TIM2_CH3
, STM32_EXT4
},
289 { TIM2_CH4
, STM32_EXT5
},
290 { TIM2_TRGO
, STM32_EXT6
},
291 { TIM3_CH1
, STM32_EXT7
},
292 { TIM3_TRGO
, STM32_EXT8
},
293 { TIM4_CH4
, STM32_EXT9
},
294 { TIM5_CH1
, STM32_EXT10
},
295 { TIM5_CH2
, STM32_EXT11
},
296 { TIM5_CH3
, STM32_EXT12
},
297 { TIM8_CH1
, STM32_EXT13
},
298 { TIM8_TRGO
, STM32_EXT14
},
303 * stm32f4_smp_bits[] - describe sampling time register index & bit fields
304 * Sorted so it can be indexed by channel number.
306 static const struct stm32_adc_regs stm32f4_smp_bits
[] = {
307 /* STM32F4_ADC_SMPR2: smpr[] index, mask, shift for SMP0 to SMP9 */
308 { 1, GENMASK(2, 0), 0 },
309 { 1, GENMASK(5, 3), 3 },
310 { 1, GENMASK(8, 6), 6 },
311 { 1, GENMASK(11, 9), 9 },
312 { 1, GENMASK(14, 12), 12 },
313 { 1, GENMASK(17, 15), 15 },
314 { 1, GENMASK(20, 18), 18 },
315 { 1, GENMASK(23, 21), 21 },
316 { 1, GENMASK(26, 24), 24 },
317 { 1, GENMASK(29, 27), 27 },
318 /* STM32F4_ADC_SMPR1, smpr[] index, mask, shift for SMP10 to SMP18 */
319 { 0, GENMASK(2, 0), 0 },
320 { 0, GENMASK(5, 3), 3 },
321 { 0, GENMASK(8, 6), 6 },
322 { 0, GENMASK(11, 9), 9 },
323 { 0, GENMASK(14, 12), 12 },
324 { 0, GENMASK(17, 15), 15 },
325 { 0, GENMASK(20, 18), 18 },
326 { 0, GENMASK(23, 21), 21 },
327 { 0, GENMASK(26, 24), 24 },
330 /* STM32F4 programmable sampling time (ADC clock cycles) */
331 static const unsigned int stm32f4_adc_smp_cycles
[STM32_ADC_MAX_SMP
+ 1] = {
332 3, 15, 28, 56, 84, 112, 144, 480,
335 static const struct stm32_adc_regspec stm32f4_adc_regspec
= {
336 .dr
= STM32F4_ADC_DR
,
337 .ier_eoc
= { STM32F4_ADC_CR1
, STM32F4_EOCIE
},
338 .isr_eoc
= { STM32F4_ADC_SR
, STM32F4_EOC
},
340 .exten
= { STM32F4_ADC_CR2
, STM32F4_EXTEN_MASK
, STM32F4_EXTEN_SHIFT
},
341 .extsel
= { STM32F4_ADC_CR2
, STM32F4_EXTSEL_MASK
,
342 STM32F4_EXTSEL_SHIFT
},
343 .res
= { STM32F4_ADC_CR1
, STM32F4_RES_MASK
, STM32F4_RES_SHIFT
},
344 .smpr
= { STM32F4_ADC_SMPR1
, STM32F4_ADC_SMPR2
},
345 .smp_bits
= stm32f4_smp_bits
,
348 static const struct stm32_adc_regs stm32h7_sq
[STM32_ADC_MAX_SQ
+ 1] = {
349 /* L: len bit field description to be kept as first element */
350 { STM32H7_ADC_SQR1
, GENMASK(3, 0), 0 },
351 /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
352 { STM32H7_ADC_SQR1
, GENMASK(10, 6), 6 },
353 { STM32H7_ADC_SQR1
, GENMASK(16, 12), 12 },
354 { STM32H7_ADC_SQR1
, GENMASK(22, 18), 18 },
355 { STM32H7_ADC_SQR1
, GENMASK(28, 24), 24 },
356 { STM32H7_ADC_SQR2
, GENMASK(4, 0), 0 },
357 { STM32H7_ADC_SQR2
, GENMASK(10, 6), 6 },
358 { STM32H7_ADC_SQR2
, GENMASK(16, 12), 12 },
359 { STM32H7_ADC_SQR2
, GENMASK(22, 18), 18 },
360 { STM32H7_ADC_SQR2
, GENMASK(28, 24), 24 },
361 { STM32H7_ADC_SQR3
, GENMASK(4, 0), 0 },
362 { STM32H7_ADC_SQR3
, GENMASK(10, 6), 6 },
363 { STM32H7_ADC_SQR3
, GENMASK(16, 12), 12 },
364 { STM32H7_ADC_SQR3
, GENMASK(22, 18), 18 },
365 { STM32H7_ADC_SQR3
, GENMASK(28, 24), 24 },
366 { STM32H7_ADC_SQR4
, GENMASK(4, 0), 0 },
367 { STM32H7_ADC_SQR4
, GENMASK(10, 6), 6 },
370 /* STM32H7 external trigger sources for all instances */
371 static struct stm32_adc_trig_info stm32h7_adc_trigs
[] = {
372 { TIM1_CH1
, STM32_EXT0
},
373 { TIM1_CH2
, STM32_EXT1
},
374 { TIM1_CH3
, STM32_EXT2
},
375 { TIM2_CH2
, STM32_EXT3
},
376 { TIM3_TRGO
, STM32_EXT4
},
377 { TIM4_CH4
, STM32_EXT5
},
378 { TIM8_TRGO
, STM32_EXT7
},
379 { TIM8_TRGO2
, STM32_EXT8
},
380 { TIM1_TRGO
, STM32_EXT9
},
381 { TIM1_TRGO2
, STM32_EXT10
},
382 { TIM2_TRGO
, STM32_EXT11
},
383 { TIM4_TRGO
, STM32_EXT12
},
384 { TIM6_TRGO
, STM32_EXT13
},
385 { TIM15_TRGO
, STM32_EXT14
},
386 { TIM3_CH4
, STM32_EXT15
},
387 { LPTIM1_OUT
, STM32_EXT18
},
388 { LPTIM2_OUT
, STM32_EXT19
},
389 { LPTIM3_OUT
, STM32_EXT20
},
394 * stm32h7_smp_bits - describe sampling time register index & bit fields
395 * Sorted so it can be indexed by channel number.
397 static const struct stm32_adc_regs stm32h7_smp_bits
[] = {
398 /* STM32H7_ADC_SMPR1, smpr[] index, mask, shift for SMP0 to SMP9 */
399 { 0, GENMASK(2, 0), 0 },
400 { 0, GENMASK(5, 3), 3 },
401 { 0, GENMASK(8, 6), 6 },
402 { 0, GENMASK(11, 9), 9 },
403 { 0, GENMASK(14, 12), 12 },
404 { 0, GENMASK(17, 15), 15 },
405 { 0, GENMASK(20, 18), 18 },
406 { 0, GENMASK(23, 21), 21 },
407 { 0, GENMASK(26, 24), 24 },
408 { 0, GENMASK(29, 27), 27 },
409 /* STM32H7_ADC_SMPR2, smpr[] index, mask, shift for SMP10 to SMP19 */
410 { 1, GENMASK(2, 0), 0 },
411 { 1, GENMASK(5, 3), 3 },
412 { 1, GENMASK(8, 6), 6 },
413 { 1, GENMASK(11, 9), 9 },
414 { 1, GENMASK(14, 12), 12 },
415 { 1, GENMASK(17, 15), 15 },
416 { 1, GENMASK(20, 18), 18 },
417 { 1, GENMASK(23, 21), 21 },
418 { 1, GENMASK(26, 24), 24 },
419 { 1, GENMASK(29, 27), 27 },
422 /* STM32H7 programmable sampling time (ADC clock cycles, rounded down) */
423 static const unsigned int stm32h7_adc_smp_cycles
[STM32_ADC_MAX_SMP
+ 1] = {
424 1, 2, 8, 16, 32, 64, 387, 810,
427 static const struct stm32_adc_regspec stm32h7_adc_regspec
= {
428 .dr
= STM32H7_ADC_DR
,
429 .ier_eoc
= { STM32H7_ADC_IER
, STM32H7_EOCIE
},
430 .isr_eoc
= { STM32H7_ADC_ISR
, STM32H7_EOC
},
432 .exten
= { STM32H7_ADC_CFGR
, STM32H7_EXTEN_MASK
, STM32H7_EXTEN_SHIFT
},
433 .extsel
= { STM32H7_ADC_CFGR
, STM32H7_EXTSEL_MASK
,
434 STM32H7_EXTSEL_SHIFT
},
435 .res
= { STM32H7_ADC_CFGR
, STM32H7_RES_MASK
, STM32H7_RES_SHIFT
},
436 .smpr
= { STM32H7_ADC_SMPR1
, STM32H7_ADC_SMPR2
},
437 .smp_bits
= stm32h7_smp_bits
,
441 * STM32 ADC registers access routines
442 * @adc: stm32 adc instance
443 * @reg: reg offset in adc instance
445 * Note: All instances share same base, with 0x0, 0x100 or 0x200 offset resp.
446 * for adc1, adc2 and adc3.
448 static u32
stm32_adc_readl(struct stm32_adc
*adc
, u32 reg
)
450 return readl_relaxed(adc
->common
->base
+ adc
->offset
+ reg
);
453 #define stm32_adc_readl_addr(addr) stm32_adc_readl(adc, addr)
455 #define stm32_adc_readl_poll_timeout(reg, val, cond, sleep_us, timeout_us) \
456 readx_poll_timeout(stm32_adc_readl_addr, reg, val, \
457 cond, sleep_us, timeout_us)
459 static u16
stm32_adc_readw(struct stm32_adc
*adc
, u32 reg
)
461 return readw_relaxed(adc
->common
->base
+ adc
->offset
+ reg
);
464 static void stm32_adc_writel(struct stm32_adc
*adc
, u32 reg
, u32 val
)
466 writel_relaxed(val
, adc
->common
->base
+ adc
->offset
+ reg
);
469 static void stm32_adc_set_bits(struct stm32_adc
*adc
, u32 reg
, u32 bits
)
473 spin_lock_irqsave(&adc
->lock
, flags
);
474 stm32_adc_writel(adc
, reg
, stm32_adc_readl(adc
, reg
) | bits
);
475 spin_unlock_irqrestore(&adc
->lock
, flags
);
478 static void stm32_adc_clr_bits(struct stm32_adc
*adc
, u32 reg
, u32 bits
)
482 spin_lock_irqsave(&adc
->lock
, flags
);
483 stm32_adc_writel(adc
, reg
, stm32_adc_readl(adc
, reg
) & ~bits
);
484 spin_unlock_irqrestore(&adc
->lock
, flags
);
488 * stm32_adc_conv_irq_enable() - Enable end of conversion interrupt
489 * @adc: stm32 adc instance
491 static void stm32_adc_conv_irq_enable(struct stm32_adc
*adc
)
493 stm32_adc_set_bits(adc
, adc
->cfg
->regs
->ier_eoc
.reg
,
494 adc
->cfg
->regs
->ier_eoc
.mask
);
498 * stm32_adc_conv_irq_disable() - Disable end of conversion interrupt
499 * @adc: stm32 adc instance
501 static void stm32_adc_conv_irq_disable(struct stm32_adc
*adc
)
503 stm32_adc_clr_bits(adc
, adc
->cfg
->regs
->ier_eoc
.reg
,
504 adc
->cfg
->regs
->ier_eoc
.mask
);
507 static void stm32_adc_set_res(struct stm32_adc
*adc
)
509 const struct stm32_adc_regs
*res
= &adc
->cfg
->regs
->res
;
512 val
= stm32_adc_readl(adc
, res
->reg
);
513 val
= (val
& ~res
->mask
) | (adc
->res
<< res
->shift
);
514 stm32_adc_writel(adc
, res
->reg
, val
);
518 * stm32f4_adc_start_conv() - Start conversions for regular channels.
519 * @adc: stm32 adc instance
520 * @dma: use dma to transfer conversion result
522 * Start conversions for regular channels.
523 * Also take care of normal or DMA mode. Circular DMA may be used for regular
524 * conversions, in IIO buffer modes. Otherwise, use ADC interrupt with direct
525 * DR read instead (e.g. read_raw, or triggered buffer mode without DMA).
527 static void stm32f4_adc_start_conv(struct stm32_adc
*adc
, bool dma
)
529 stm32_adc_set_bits(adc
, STM32F4_ADC_CR1
, STM32F4_SCAN
);
532 stm32_adc_set_bits(adc
, STM32F4_ADC_CR2
,
533 STM32F4_DMA
| STM32F4_DDS
);
535 stm32_adc_set_bits(adc
, STM32F4_ADC_CR2
, STM32F4_EOCS
| STM32F4_ADON
);
537 /* Wait for Power-up time (tSTAB from datasheet) */
540 /* Software start ? (e.g. trigger detection disabled ?) */
541 if (!(stm32_adc_readl(adc
, STM32F4_ADC_CR2
) & STM32F4_EXTEN_MASK
))
542 stm32_adc_set_bits(adc
, STM32F4_ADC_CR2
, STM32F4_SWSTART
);
545 static void stm32f4_adc_stop_conv(struct stm32_adc
*adc
)
547 stm32_adc_clr_bits(adc
, STM32F4_ADC_CR2
, STM32F4_EXTEN_MASK
);
548 stm32_adc_clr_bits(adc
, STM32F4_ADC_SR
, STM32F4_STRT
);
550 stm32_adc_clr_bits(adc
, STM32F4_ADC_CR1
, STM32F4_SCAN
);
551 stm32_adc_clr_bits(adc
, STM32F4_ADC_CR2
,
552 STM32F4_ADON
| STM32F4_DMA
| STM32F4_DDS
);
555 static void stm32h7_adc_start_conv(struct stm32_adc
*adc
, bool dma
)
557 enum stm32h7_adc_dmngt dmngt
;
562 dmngt
= STM32H7_DMNGT_DMA_CIRC
;
564 dmngt
= STM32H7_DMNGT_DR_ONLY
;
566 spin_lock_irqsave(&adc
->lock
, flags
);
567 val
= stm32_adc_readl(adc
, STM32H7_ADC_CFGR
);
568 val
= (val
& ~STM32H7_DMNGT_MASK
) | (dmngt
<< STM32H7_DMNGT_SHIFT
);
569 stm32_adc_writel(adc
, STM32H7_ADC_CFGR
, val
);
570 spin_unlock_irqrestore(&adc
->lock
, flags
);
572 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADSTART
);
575 static void stm32h7_adc_stop_conv(struct stm32_adc
*adc
)
577 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
581 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADSTP
);
583 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
584 !(val
& (STM32H7_ADSTART
)),
585 100, STM32_ADC_TIMEOUT_US
);
587 dev_warn(&indio_dev
->dev
, "stop failed\n");
589 stm32_adc_clr_bits(adc
, STM32H7_ADC_CFGR
, STM32H7_DMNGT_MASK
);
592 static int stm32h7_adc_exit_pwr_down(struct stm32_adc
*adc
)
594 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
598 /* Exit deep power down, then enable ADC voltage regulator */
599 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, STM32H7_DEEPPWD
);
600 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADVREGEN
);
602 if (adc
->common
->rate
> STM32H7_BOOST_CLKRATE
)
603 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_BOOST
);
605 /* Wait for startup time */
606 if (!adc
->cfg
->has_vregready
) {
607 usleep_range(10, 20);
611 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR
, val
,
612 val
& STM32MP1_VREGREADY
, 100,
613 STM32_ADC_TIMEOUT_US
);
615 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_DEEPPWD
);
616 dev_err(&indio_dev
->dev
, "Failed to exit power down\n");
622 static void stm32h7_adc_enter_pwr_down(struct stm32_adc
*adc
)
624 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, STM32H7_BOOST
);
626 /* Setting DEEPPWD disables ADC vreg and clears ADVREGEN */
627 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_DEEPPWD
);
630 static int stm32h7_adc_enable(struct stm32_adc
*adc
)
632 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
636 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADEN
);
638 /* Poll for ADRDY to be set (after adc startup time) */
639 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR
, val
,
641 100, STM32_ADC_TIMEOUT_US
);
643 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADDIS
);
644 dev_err(&indio_dev
->dev
, "Failed to enable ADC\n");
646 /* Clear ADRDY by writing one */
647 stm32_adc_set_bits(adc
, STM32H7_ADC_ISR
, STM32H7_ADRDY
);
653 static void stm32h7_adc_disable(struct stm32_adc
*adc
)
655 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
659 /* Disable ADC and wait until it's effectively disabled */
660 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADDIS
);
661 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
662 !(val
& STM32H7_ADEN
), 100,
663 STM32_ADC_TIMEOUT_US
);
665 dev_warn(&indio_dev
->dev
, "Failed to disable\n");
669 * stm32h7_adc_read_selfcalib() - read calibration shadow regs, save result
670 * @adc: stm32 adc instance
672 static int stm32h7_adc_read_selfcalib(struct stm32_adc
*adc
)
674 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
676 u32 lincalrdyw_mask
, val
;
678 /* Enable adc so LINCALRDYW1..6 bits are writable */
679 ret
= stm32h7_adc_enable(adc
);
683 /* Read linearity calibration */
684 lincalrdyw_mask
= STM32H7_LINCALRDYW6
;
685 for (i
= STM32H7_LINCALFACT_NUM
- 1; i
>= 0; i
--) {
686 /* Clear STM32H7_LINCALRDYW[6..1]: transfer calib to CALFACT2 */
687 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, lincalrdyw_mask
);
689 /* Poll: wait calib data to be ready in CALFACT2 register */
690 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
691 !(val
& lincalrdyw_mask
),
692 100, STM32_ADC_TIMEOUT_US
);
694 dev_err(&indio_dev
->dev
, "Failed to read calfact\n");
698 val
= stm32_adc_readl(adc
, STM32H7_ADC_CALFACT2
);
699 adc
->cal
.lincalfact
[i
] = (val
& STM32H7_LINCALFACT_MASK
);
700 adc
->cal
.lincalfact
[i
] >>= STM32H7_LINCALFACT_SHIFT
;
702 lincalrdyw_mask
>>= 1;
705 /* Read offset calibration */
706 val
= stm32_adc_readl(adc
, STM32H7_ADC_CALFACT
);
707 adc
->cal
.calfact_s
= (val
& STM32H7_CALFACT_S_MASK
);
708 adc
->cal
.calfact_s
>>= STM32H7_CALFACT_S_SHIFT
;
709 adc
->cal
.calfact_d
= (val
& STM32H7_CALFACT_D_MASK
);
710 adc
->cal
.calfact_d
>>= STM32H7_CALFACT_D_SHIFT
;
713 stm32h7_adc_disable(adc
);
719 * stm32h7_adc_restore_selfcalib() - Restore saved self-calibration result
720 * @adc: stm32 adc instance
721 * Note: ADC must be enabled, with no on-going conversions.
723 static int stm32h7_adc_restore_selfcalib(struct stm32_adc
*adc
)
725 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
727 u32 lincalrdyw_mask
, val
;
729 val
= (adc
->cal
.calfact_s
<< STM32H7_CALFACT_S_SHIFT
) |
730 (adc
->cal
.calfact_d
<< STM32H7_CALFACT_D_SHIFT
);
731 stm32_adc_writel(adc
, STM32H7_ADC_CALFACT
, val
);
733 lincalrdyw_mask
= STM32H7_LINCALRDYW6
;
734 for (i
= STM32H7_LINCALFACT_NUM
- 1; i
>= 0; i
--) {
736 * Write saved calibration data to shadow registers:
737 * Write CALFACT2, and set LINCALRDYW[6..1] bit to trigger
738 * data write. Then poll to wait for complete transfer.
740 val
= adc
->cal
.lincalfact
[i
] << STM32H7_LINCALFACT_SHIFT
;
741 stm32_adc_writel(adc
, STM32H7_ADC_CALFACT2
, val
);
742 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, lincalrdyw_mask
);
743 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
744 val
& lincalrdyw_mask
,
745 100, STM32_ADC_TIMEOUT_US
);
747 dev_err(&indio_dev
->dev
, "Failed to write calfact\n");
752 * Read back calibration data, has two effects:
753 * - It ensures bits LINCALRDYW[6..1] are kept cleared
754 * for next time calibration needs to be restored.
755 * - BTW, bit clear triggers a read, then check data has been
758 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, lincalrdyw_mask
);
759 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
760 !(val
& lincalrdyw_mask
),
761 100, STM32_ADC_TIMEOUT_US
);
763 dev_err(&indio_dev
->dev
, "Failed to read calfact\n");
766 val
= stm32_adc_readl(adc
, STM32H7_ADC_CALFACT2
);
767 if (val
!= adc
->cal
.lincalfact
[i
] << STM32H7_LINCALFACT_SHIFT
) {
768 dev_err(&indio_dev
->dev
, "calfact not consistent\n");
772 lincalrdyw_mask
>>= 1;
779 * Fixed timeout value for ADC calibration.
781 * - low clock frequency
782 * - maximum prescalers
783 * Calibration requires:
784 * - 131,072 ADC clock cycle for the linear calibration
785 * - 20 ADC clock cycle for the offset calibration
787 * Set to 100ms for now
789 #define STM32H7_ADC_CALIB_TIMEOUT_US 100000
792 * stm32h7_adc_selfcalib() - Procedure to calibrate ADC (from power down)
793 * @adc: stm32 adc instance
794 * Exit from power down, calibrate ADC, then return to power down.
796 static int stm32h7_adc_selfcalib(struct stm32_adc
*adc
)
798 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
802 ret
= stm32h7_adc_exit_pwr_down(adc
);
807 * Select calibration mode:
808 * - Offset calibration for single ended inputs
809 * - No linearity calibration (do it later, before reading it)
811 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADCALDIF
);
812 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADCALLIN
);
814 /* Start calibration, then wait for completion */
815 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADCAL
);
816 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
817 !(val
& STM32H7_ADCAL
), 100,
818 STM32H7_ADC_CALIB_TIMEOUT_US
);
820 dev_err(&indio_dev
->dev
, "calibration failed\n");
825 * Select calibration mode, then start calibration:
826 * - Offset calibration for differential input
827 * - Linearity calibration (needs to be done only once for single/diff)
828 * will run simultaneously with offset calibration.
830 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
,
831 STM32H7_ADCALDIF
| STM32H7_ADCALLIN
);
832 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADCAL
);
833 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
834 !(val
& STM32H7_ADCAL
), 100,
835 STM32H7_ADC_CALIB_TIMEOUT_US
);
837 dev_err(&indio_dev
->dev
, "calibration failed\n");
841 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
,
842 STM32H7_ADCALDIF
| STM32H7_ADCALLIN
);
844 /* Read calibration result for future reference */
845 ret
= stm32h7_adc_read_selfcalib(adc
);
848 stm32h7_adc_enter_pwr_down(adc
);
854 * stm32h7_adc_prepare() - Leave power down mode to enable ADC.
855 * @adc: stm32 adc instance
856 * Leave power down mode.
857 * Configure channels as single ended or differential before enabling ADC.
859 * Restore calibration data.
860 * Pre-select channels that may be used in PCSEL (required by input MUX / IO):
861 * - Only one input is selected for single ended (e.g. 'vinp')
862 * - Two inputs are selected for differential channels (e.g. 'vinp' & 'vinn')
864 static int stm32h7_adc_prepare(struct stm32_adc
*adc
)
868 ret
= stm32h7_adc_exit_pwr_down(adc
);
872 stm32_adc_writel(adc
, STM32H7_ADC_DIFSEL
, adc
->difsel
);
874 ret
= stm32h7_adc_enable(adc
);
878 ret
= stm32h7_adc_restore_selfcalib(adc
);
882 stm32_adc_writel(adc
, STM32H7_ADC_PCSEL
, adc
->pcsel
);
887 stm32h7_adc_disable(adc
);
889 stm32h7_adc_enter_pwr_down(adc
);
894 static void stm32h7_adc_unprepare(struct stm32_adc
*adc
)
896 stm32h7_adc_disable(adc
);
897 stm32h7_adc_enter_pwr_down(adc
);
901 * stm32_adc_conf_scan_seq() - Build regular channels scan sequence
902 * @indio_dev: IIO device
903 * @scan_mask: channels to be converted
905 * Conversion sequence :
906 * Apply sampling time settings for all channels.
907 * Configure ADC scan sequence based on selected channels in scan_mask.
908 * Add channels to SQR registers, from scan_mask LSB to MSB, then
909 * program sequence len.
911 static int stm32_adc_conf_scan_seq(struct iio_dev
*indio_dev
,
912 const unsigned long *scan_mask
)
914 struct stm32_adc
*adc
= iio_priv(indio_dev
);
915 const struct stm32_adc_regs
*sqr
= adc
->cfg
->regs
->sqr
;
916 const struct iio_chan_spec
*chan
;
920 /* Apply sampling time settings */
921 stm32_adc_writel(adc
, adc
->cfg
->regs
->smpr
[0], adc
->smpr_val
[0]);
922 stm32_adc_writel(adc
, adc
->cfg
->regs
->smpr
[1], adc
->smpr_val
[1]);
924 for_each_set_bit(bit
, scan_mask
, indio_dev
->masklength
) {
925 chan
= indio_dev
->channels
+ bit
;
927 * Assign one channel per SQ entry in regular
928 * sequence, starting with SQ1.
931 if (i
> STM32_ADC_MAX_SQ
)
934 dev_dbg(&indio_dev
->dev
, "%s chan %d to SQ%d\n",
935 __func__
, chan
->channel
, i
);
937 val
= stm32_adc_readl(adc
, sqr
[i
].reg
);
939 val
|= chan
->channel
<< sqr
[i
].shift
;
940 stm32_adc_writel(adc
, sqr
[i
].reg
, val
);
947 val
= stm32_adc_readl(adc
, sqr
[0].reg
);
949 val
|= ((i
- 1) << sqr
[0].shift
);
950 stm32_adc_writel(adc
, sqr
[0].reg
, val
);
956 * stm32_adc_get_trig_extsel() - Get external trigger selection
959 * Returns trigger extsel value, if trig matches, -EINVAL otherwise.
961 static int stm32_adc_get_trig_extsel(struct iio_dev
*indio_dev
,
962 struct iio_trigger
*trig
)
964 struct stm32_adc
*adc
= iio_priv(indio_dev
);
967 /* lookup triggers registered by stm32 timer trigger driver */
968 for (i
= 0; adc
->cfg
->trigs
[i
].name
; i
++) {
970 * Checking both stm32 timer trigger type and trig name
971 * should be safe against arbitrary trigger names.
973 if ((is_stm32_timer_trigger(trig
) ||
974 is_stm32_lptim_trigger(trig
)) &&
975 !strcmp(adc
->cfg
->trigs
[i
].name
, trig
->name
)) {
976 return adc
->cfg
->trigs
[i
].extsel
;
984 * stm32_adc_set_trig() - Set a regular trigger
985 * @indio_dev: IIO device
988 * Set trigger source/polarity (e.g. SW, or HW with polarity) :
989 * - if HW trigger disabled (e.g. trig == NULL, conversion launched by sw)
990 * - if HW trigger enabled, set source & polarity
992 static int stm32_adc_set_trig(struct iio_dev
*indio_dev
,
993 struct iio_trigger
*trig
)
995 struct stm32_adc
*adc
= iio_priv(indio_dev
);
996 u32 val
, extsel
= 0, exten
= STM32_EXTEN_SWTRIG
;
1001 ret
= stm32_adc_get_trig_extsel(indio_dev
, trig
);
1005 /* set trigger source and polarity (default to rising edge) */
1007 exten
= adc
->trigger_polarity
+ STM32_EXTEN_HWTRIG_RISING_EDGE
;
1010 spin_lock_irqsave(&adc
->lock
, flags
);
1011 val
= stm32_adc_readl(adc
, adc
->cfg
->regs
->exten
.reg
);
1012 val
&= ~(adc
->cfg
->regs
->exten
.mask
| adc
->cfg
->regs
->extsel
.mask
);
1013 val
|= exten
<< adc
->cfg
->regs
->exten
.shift
;
1014 val
|= extsel
<< adc
->cfg
->regs
->extsel
.shift
;
1015 stm32_adc_writel(adc
, adc
->cfg
->regs
->exten
.reg
, val
);
1016 spin_unlock_irqrestore(&adc
->lock
, flags
);
1021 static int stm32_adc_set_trig_pol(struct iio_dev
*indio_dev
,
1022 const struct iio_chan_spec
*chan
,
1025 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1027 adc
->trigger_polarity
= type
;
1032 static int stm32_adc_get_trig_pol(struct iio_dev
*indio_dev
,
1033 const struct iio_chan_spec
*chan
)
1035 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1037 return adc
->trigger_polarity
;
1040 static const char * const stm32_trig_pol_items
[] = {
1041 "rising-edge", "falling-edge", "both-edges",
1044 static const struct iio_enum stm32_adc_trig_pol
= {
1045 .items
= stm32_trig_pol_items
,
1046 .num_items
= ARRAY_SIZE(stm32_trig_pol_items
),
1047 .get
= stm32_adc_get_trig_pol
,
1048 .set
= stm32_adc_set_trig_pol
,
1052 * stm32_adc_single_conv() - Performs a single conversion
1053 * @indio_dev: IIO device
1054 * @chan: IIO channel
1055 * @res: conversion result
1057 * The function performs a single conversion on a given channel:
1058 * - Apply sampling time settings
1059 * - Program sequencer with one channel (e.g. in SQ1 with len = 1)
1061 * - Start conversion, then wait for interrupt completion.
1063 static int stm32_adc_single_conv(struct iio_dev
*indio_dev
,
1064 const struct iio_chan_spec
*chan
,
1067 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1068 const struct stm32_adc_regspec
*regs
= adc
->cfg
->regs
;
1073 reinit_completion(&adc
->completion
);
1077 if (adc
->cfg
->prepare
) {
1078 ret
= adc
->cfg
->prepare(adc
);
1083 /* Apply sampling time settings */
1084 stm32_adc_writel(adc
, regs
->smpr
[0], adc
->smpr_val
[0]);
1085 stm32_adc_writel(adc
, regs
->smpr
[1], adc
->smpr_val
[1]);
1087 /* Program chan number in regular sequence (SQ1) */
1088 val
= stm32_adc_readl(adc
, regs
->sqr
[1].reg
);
1089 val
&= ~regs
->sqr
[1].mask
;
1090 val
|= chan
->channel
<< regs
->sqr
[1].shift
;
1091 stm32_adc_writel(adc
, regs
->sqr
[1].reg
, val
);
1093 /* Set regular sequence len (0 for 1 conversion) */
1094 stm32_adc_clr_bits(adc
, regs
->sqr
[0].reg
, regs
->sqr
[0].mask
);
1096 /* Trigger detection disabled (conversion can be launched in SW) */
1097 stm32_adc_clr_bits(adc
, regs
->exten
.reg
, regs
->exten
.mask
);
1099 stm32_adc_conv_irq_enable(adc
);
1101 adc
->cfg
->start_conv(adc
, false);
1103 timeout
= wait_for_completion_interruptible_timeout(
1104 &adc
->completion
, STM32_ADC_TIMEOUT
);
1107 } else if (timeout
< 0) {
1110 *res
= adc
->buffer
[0];
1114 adc
->cfg
->stop_conv(adc
);
1116 stm32_adc_conv_irq_disable(adc
);
1118 if (adc
->cfg
->unprepare
)
1119 adc
->cfg
->unprepare(adc
);
1124 static int stm32_adc_read_raw(struct iio_dev
*indio_dev
,
1125 struct iio_chan_spec
const *chan
,
1126 int *val
, int *val2
, long mask
)
1128 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1132 case IIO_CHAN_INFO_RAW
:
1133 ret
= iio_device_claim_direct_mode(indio_dev
);
1136 if (chan
->type
== IIO_VOLTAGE
)
1137 ret
= stm32_adc_single_conv(indio_dev
, chan
, val
);
1140 iio_device_release_direct_mode(indio_dev
);
1143 case IIO_CHAN_INFO_SCALE
:
1144 if (chan
->differential
) {
1145 *val
= adc
->common
->vref_mv
* 2;
1146 *val2
= chan
->scan_type
.realbits
;
1148 *val
= adc
->common
->vref_mv
;
1149 *val2
= chan
->scan_type
.realbits
;
1151 return IIO_VAL_FRACTIONAL_LOG2
;
1153 case IIO_CHAN_INFO_OFFSET
:
1154 if (chan
->differential
)
1155 /* ADC_full_scale / 2 */
1156 *val
= -((1 << chan
->scan_type
.realbits
) / 2);
1166 static irqreturn_t
stm32_adc_isr(int irq
, void *data
)
1168 struct stm32_adc
*adc
= data
;
1169 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1170 const struct stm32_adc_regspec
*regs
= adc
->cfg
->regs
;
1171 u32 status
= stm32_adc_readl(adc
, regs
->isr_eoc
.reg
);
1173 if (status
& regs
->isr_eoc
.mask
) {
1174 /* Reading DR also clears EOC status flag */
1175 adc
->buffer
[adc
->bufi
] = stm32_adc_readw(adc
, regs
->dr
);
1176 if (iio_buffer_enabled(indio_dev
)) {
1178 if (adc
->bufi
>= adc
->num_conv
) {
1179 stm32_adc_conv_irq_disable(adc
);
1180 iio_trigger_poll(indio_dev
->trig
);
1183 complete(&adc
->completion
);
1192 * stm32_adc_validate_trigger() - validate trigger for stm32 adc
1193 * @indio_dev: IIO device
1194 * @trig: new trigger
1196 * Returns: 0 if trig matches one of the triggers registered by stm32 adc
1197 * driver, -EINVAL otherwise.
1199 static int stm32_adc_validate_trigger(struct iio_dev
*indio_dev
,
1200 struct iio_trigger
*trig
)
1202 return stm32_adc_get_trig_extsel(indio_dev
, trig
) < 0 ? -EINVAL
: 0;
1205 static int stm32_adc_set_watermark(struct iio_dev
*indio_dev
, unsigned int val
)
1207 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1208 unsigned int watermark
= STM32_DMA_BUFFER_SIZE
/ 2;
1209 unsigned int rx_buf_sz
= STM32_DMA_BUFFER_SIZE
;
1212 * dma cyclic transfers are used, buffer is split into two periods.
1214 * - always one buffer (period) dma is working on
1215 * - one buffer (period) driver can push with iio_trigger_poll().
1217 watermark
= min(watermark
, val
* (unsigned)(sizeof(u16
)));
1218 adc
->rx_buf_sz
= min(rx_buf_sz
, watermark
* 2 * adc
->num_conv
);
1223 static int stm32_adc_update_scan_mode(struct iio_dev
*indio_dev
,
1224 const unsigned long *scan_mask
)
1226 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1229 adc
->num_conv
= bitmap_weight(scan_mask
, indio_dev
->masklength
);
1231 ret
= stm32_adc_conf_scan_seq(indio_dev
, scan_mask
);
1238 static int stm32_adc_of_xlate(struct iio_dev
*indio_dev
,
1239 const struct of_phandle_args
*iiospec
)
1243 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
1244 if (indio_dev
->channels
[i
].channel
== iiospec
->args
[0])
1251 * stm32_adc_debugfs_reg_access - read or write register value
1253 * To read a value from an ADC register:
1254 * echo [ADC reg offset] > direct_reg_access
1255 * cat direct_reg_access
1257 * To write a value in a ADC register:
1258 * echo [ADC_reg_offset] [value] > direct_reg_access
1260 static int stm32_adc_debugfs_reg_access(struct iio_dev
*indio_dev
,
1261 unsigned reg
, unsigned writeval
,
1264 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1267 stm32_adc_writel(adc
, reg
, writeval
);
1269 *readval
= stm32_adc_readl(adc
, reg
);
1274 static const struct iio_info stm32_adc_iio_info
= {
1275 .read_raw
= stm32_adc_read_raw
,
1276 .validate_trigger
= stm32_adc_validate_trigger
,
1277 .hwfifo_set_watermark
= stm32_adc_set_watermark
,
1278 .update_scan_mode
= stm32_adc_update_scan_mode
,
1279 .debugfs_reg_access
= stm32_adc_debugfs_reg_access
,
1280 .of_xlate
= stm32_adc_of_xlate
,
1283 static unsigned int stm32_adc_dma_residue(struct stm32_adc
*adc
)
1285 struct dma_tx_state state
;
1286 enum dma_status status
;
1288 status
= dmaengine_tx_status(adc
->dma_chan
,
1289 adc
->dma_chan
->cookie
,
1291 if (status
== DMA_IN_PROGRESS
) {
1292 /* Residue is size in bytes from end of buffer */
1293 unsigned int i
= adc
->rx_buf_sz
- state
.residue
;
1296 /* Return available bytes */
1298 size
= i
- adc
->bufi
;
1300 size
= adc
->rx_buf_sz
+ i
- adc
->bufi
;
1308 static void stm32_adc_dma_buffer_done(void *data
)
1310 struct iio_dev
*indio_dev
= data
;
1311 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1312 int residue
= stm32_adc_dma_residue(adc
);
1315 * In DMA mode the trigger services of IIO are not used
1316 * (e.g. no call to iio_trigger_poll).
1317 * Calling irq handler associated to the hardware trigger is not
1318 * relevant as the conversions have already been done. Data
1319 * transfers are performed directly in DMA callback instead.
1320 * This implementation avoids to call trigger irq handler that
1321 * may sleep, in an atomic context (DMA irq handler context).
1323 dev_dbg(&indio_dev
->dev
, "%s bufi=%d\n", __func__
, adc
->bufi
);
1325 while (residue
>= indio_dev
->scan_bytes
) {
1326 u16
*buffer
= (u16
*)&adc
->rx_buf
[adc
->bufi
];
1328 iio_push_to_buffers(indio_dev
, buffer
);
1330 residue
-= indio_dev
->scan_bytes
;
1331 adc
->bufi
+= indio_dev
->scan_bytes
;
1332 if (adc
->bufi
>= adc
->rx_buf_sz
)
1337 static int stm32_adc_dma_start(struct iio_dev
*indio_dev
)
1339 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1340 struct dma_async_tx_descriptor
*desc
;
1341 dma_cookie_t cookie
;
1347 dev_dbg(&indio_dev
->dev
, "%s size=%d watermark=%d\n", __func__
,
1348 adc
->rx_buf_sz
, adc
->rx_buf_sz
/ 2);
1350 /* Prepare a DMA cyclic transaction */
1351 desc
= dmaengine_prep_dma_cyclic(adc
->dma_chan
,
1353 adc
->rx_buf_sz
, adc
->rx_buf_sz
/ 2,
1355 DMA_PREP_INTERRUPT
);
1359 desc
->callback
= stm32_adc_dma_buffer_done
;
1360 desc
->callback_param
= indio_dev
;
1362 cookie
= dmaengine_submit(desc
);
1363 ret
= dma_submit_error(cookie
);
1365 dmaengine_terminate_sync(adc
->dma_chan
);
1369 /* Issue pending DMA requests */
1370 dma_async_issue_pending(adc
->dma_chan
);
1375 static int stm32_adc_buffer_postenable(struct iio_dev
*indio_dev
)
1377 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1380 if (adc
->cfg
->prepare
) {
1381 ret
= adc
->cfg
->prepare(adc
);
1386 ret
= stm32_adc_set_trig(indio_dev
, indio_dev
->trig
);
1388 dev_err(&indio_dev
->dev
, "Can't set trigger\n");
1392 ret
= stm32_adc_dma_start(indio_dev
);
1394 dev_err(&indio_dev
->dev
, "Can't start dma\n");
1398 ret
= iio_triggered_buffer_postenable(indio_dev
);
1402 /* Reset adc buffer index */
1406 stm32_adc_conv_irq_enable(adc
);
1408 adc
->cfg
->start_conv(adc
, !!adc
->dma_chan
);
1414 dmaengine_terminate_all(adc
->dma_chan
);
1416 stm32_adc_set_trig(indio_dev
, NULL
);
1418 if (adc
->cfg
->unprepare
)
1419 adc
->cfg
->unprepare(adc
);
1424 static int stm32_adc_buffer_predisable(struct iio_dev
*indio_dev
)
1426 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1429 adc
->cfg
->stop_conv(adc
);
1431 stm32_adc_conv_irq_disable(adc
);
1433 ret
= iio_triggered_buffer_predisable(indio_dev
);
1435 dev_err(&indio_dev
->dev
, "predisable failed\n");
1438 dmaengine_terminate_sync(adc
->dma_chan
);
1440 if (stm32_adc_set_trig(indio_dev
, NULL
))
1441 dev_err(&indio_dev
->dev
, "Can't clear trigger\n");
1443 if (adc
->cfg
->unprepare
)
1444 adc
->cfg
->unprepare(adc
);
1449 static const struct iio_buffer_setup_ops stm32_adc_buffer_setup_ops
= {
1450 .postenable
= &stm32_adc_buffer_postenable
,
1451 .predisable
= &stm32_adc_buffer_predisable
,
1454 static irqreturn_t
stm32_adc_trigger_handler(int irq
, void *p
)
1456 struct iio_poll_func
*pf
= p
;
1457 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1458 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1460 dev_dbg(&indio_dev
->dev
, "%s bufi=%d\n", __func__
, adc
->bufi
);
1462 if (!adc
->dma_chan
) {
1463 /* reset buffer index */
1465 iio_push_to_buffers_with_timestamp(indio_dev
, adc
->buffer
,
1468 int residue
= stm32_adc_dma_residue(adc
);
1470 while (residue
>= indio_dev
->scan_bytes
) {
1471 u16
*buffer
= (u16
*)&adc
->rx_buf
[adc
->bufi
];
1473 iio_push_to_buffers_with_timestamp(indio_dev
, buffer
,
1475 residue
-= indio_dev
->scan_bytes
;
1476 adc
->bufi
+= indio_dev
->scan_bytes
;
1477 if (adc
->bufi
>= adc
->rx_buf_sz
)
1482 iio_trigger_notify_done(indio_dev
->trig
);
1484 /* re-enable eoc irq */
1486 stm32_adc_conv_irq_enable(adc
);
1491 static const struct iio_chan_spec_ext_info stm32_adc_ext_info
[] = {
1492 IIO_ENUM("trigger_polarity", IIO_SHARED_BY_ALL
, &stm32_adc_trig_pol
),
1494 .name
= "trigger_polarity_available",
1495 .shared
= IIO_SHARED_BY_ALL
,
1496 .read
= iio_enum_available_read
,
1497 .private = (uintptr_t)&stm32_adc_trig_pol
,
1502 static int stm32_adc_of_get_resolution(struct iio_dev
*indio_dev
)
1504 struct device_node
*node
= indio_dev
->dev
.of_node
;
1505 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1509 if (of_property_read_u32(node
, "assigned-resolution-bits", &res
))
1510 res
= adc
->cfg
->adc_info
->resolutions
[0];
1512 for (i
= 0; i
< adc
->cfg
->adc_info
->num_res
; i
++)
1513 if (res
== adc
->cfg
->adc_info
->resolutions
[i
])
1515 if (i
>= adc
->cfg
->adc_info
->num_res
) {
1516 dev_err(&indio_dev
->dev
, "Bad resolution: %u bits\n", res
);
1520 dev_dbg(&indio_dev
->dev
, "Using %u bits resolution\n", res
);
1526 static void stm32_adc_smpr_init(struct stm32_adc
*adc
, int channel
, u32 smp_ns
)
1528 const struct stm32_adc_regs
*smpr
= &adc
->cfg
->regs
->smp_bits
[channel
];
1529 u32 period_ns
, shift
= smpr
->shift
, mask
= smpr
->mask
;
1530 unsigned int smp
, r
= smpr
->reg
;
1532 /* Determine sampling time (ADC clock cycles) */
1533 period_ns
= NSEC_PER_SEC
/ adc
->common
->rate
;
1534 for (smp
= 0; smp
<= STM32_ADC_MAX_SMP
; smp
++)
1535 if ((period_ns
* adc
->cfg
->smp_cycles
[smp
]) >= smp_ns
)
1537 if (smp
> STM32_ADC_MAX_SMP
)
1538 smp
= STM32_ADC_MAX_SMP
;
1540 /* pre-build sampling time registers (e.g. smpr1, smpr2) */
1541 adc
->smpr_val
[r
] = (adc
->smpr_val
[r
] & ~mask
) | (smp
<< shift
);
1544 static void stm32_adc_chan_init_one(struct iio_dev
*indio_dev
,
1545 struct iio_chan_spec
*chan
, u32 vinp
,
1546 u32 vinn
, int scan_index
, bool differential
)
1548 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1549 char *name
= adc
->chan_name
[vinp
];
1551 chan
->type
= IIO_VOLTAGE
;
1552 chan
->channel
= vinp
;
1554 chan
->differential
= 1;
1555 chan
->channel2
= vinn
;
1556 snprintf(name
, STM32_ADC_CH_SZ
, "in%d-in%d", vinp
, vinn
);
1558 snprintf(name
, STM32_ADC_CH_SZ
, "in%d", vinp
);
1560 chan
->datasheet_name
= name
;
1561 chan
->scan_index
= scan_index
;
1563 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
1564 chan
->info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
) |
1565 BIT(IIO_CHAN_INFO_OFFSET
);
1566 chan
->scan_type
.sign
= 'u';
1567 chan
->scan_type
.realbits
= adc
->cfg
->adc_info
->resolutions
[adc
->res
];
1568 chan
->scan_type
.storagebits
= 16;
1569 chan
->ext_info
= stm32_adc_ext_info
;
1571 /* pre-build selected channels mask */
1572 adc
->pcsel
|= BIT(chan
->channel
);
1574 /* pre-build diff channels mask */
1575 adc
->difsel
|= BIT(chan
->channel
);
1576 /* Also add negative input to pre-selected channels */
1577 adc
->pcsel
|= BIT(chan
->channel2
);
1581 static int stm32_adc_chan_of_init(struct iio_dev
*indio_dev
)
1583 struct device_node
*node
= indio_dev
->dev
.of_node
;
1584 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1585 const struct stm32_adc_info
*adc_info
= adc
->cfg
->adc_info
;
1586 struct stm32_adc_diff_channel diff
[STM32_ADC_CH_MAX
];
1587 struct property
*prop
;
1589 struct iio_chan_spec
*channels
;
1590 int scan_index
= 0, num_channels
= 0, num_diff
= 0, ret
, i
;
1593 ret
= of_property_count_u32_elems(node
, "st,adc-channels");
1594 if (ret
> adc_info
->max_channels
) {
1595 dev_err(&indio_dev
->dev
, "Bad st,adc-channels?\n");
1597 } else if (ret
> 0) {
1598 num_channels
+= ret
;
1601 ret
= of_property_count_elems_of_size(node
, "st,adc-diff-channels",
1603 if (ret
> adc_info
->max_channels
) {
1604 dev_err(&indio_dev
->dev
, "Bad st,adc-diff-channels?\n");
1606 } else if (ret
> 0) {
1607 int size
= ret
* sizeof(*diff
) / sizeof(u32
);
1610 num_channels
+= ret
;
1611 ret
= of_property_read_u32_array(node
, "st,adc-diff-channels",
1617 if (!num_channels
) {
1618 dev_err(&indio_dev
->dev
, "No channels configured\n");
1622 /* Optional sample time is provided either for each, or all channels */
1623 ret
= of_property_count_u32_elems(node
, "st,min-sample-time-nsecs");
1624 if (ret
> 1 && ret
!= num_channels
) {
1625 dev_err(&indio_dev
->dev
, "Invalid st,min-sample-time-nsecs\n");
1629 channels
= devm_kcalloc(&indio_dev
->dev
, num_channels
,
1630 sizeof(struct iio_chan_spec
), GFP_KERNEL
);
1634 of_property_for_each_u32(node
, "st,adc-channels", prop
, cur
, val
) {
1635 if (val
>= adc_info
->max_channels
) {
1636 dev_err(&indio_dev
->dev
, "Invalid channel %d\n", val
);
1640 /* Channel can't be configured both as single-ended & diff */
1641 for (i
= 0; i
< num_diff
; i
++) {
1642 if (val
== diff
[i
].vinp
) {
1643 dev_err(&indio_dev
->dev
,
1644 "channel %d miss-configured\n", val
);
1648 stm32_adc_chan_init_one(indio_dev
, &channels
[scan_index
], val
,
1649 0, scan_index
, false);
1653 for (i
= 0; i
< num_diff
; i
++) {
1654 if (diff
[i
].vinp
>= adc_info
->max_channels
||
1655 diff
[i
].vinn
>= adc_info
->max_channels
) {
1656 dev_err(&indio_dev
->dev
, "Invalid channel in%d-in%d\n",
1657 diff
[i
].vinp
, diff
[i
].vinn
);
1660 stm32_adc_chan_init_one(indio_dev
, &channels
[scan_index
],
1661 diff
[i
].vinp
, diff
[i
].vinn
, scan_index
,
1666 for (i
= 0; i
< scan_index
; i
++) {
1668 * Using of_property_read_u32_index(), smp value will only be
1669 * modified if valid u32 value can be decoded. This allows to
1670 * get either no value, 1 shared value for all indexes, or one
1671 * value per channel.
1673 of_property_read_u32_index(node
, "st,min-sample-time-nsecs",
1675 /* Prepare sampling time settings */
1676 stm32_adc_smpr_init(adc
, channels
[i
].channel
, smp
);
1679 indio_dev
->num_channels
= scan_index
;
1680 indio_dev
->channels
= channels
;
1685 static int stm32_adc_dma_request(struct device
*dev
, struct iio_dev
*indio_dev
)
1687 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1688 struct dma_slave_config config
;
1691 adc
->dma_chan
= dma_request_chan(dev
, "rx");
1692 if (IS_ERR(adc
->dma_chan
)) {
1693 ret
= PTR_ERR(adc
->dma_chan
);
1694 if (ret
!= -ENODEV
) {
1695 if (ret
!= -EPROBE_DEFER
)
1697 "DMA channel request failed with %d\n",
1702 /* DMA is optional: fall back to IRQ mode */
1703 adc
->dma_chan
= NULL
;
1707 adc
->rx_buf
= dma_alloc_coherent(adc
->dma_chan
->device
->dev
,
1708 STM32_DMA_BUFFER_SIZE
,
1709 &adc
->rx_dma_buf
, GFP_KERNEL
);
1715 /* Configure DMA channel to read data register */
1716 memset(&config
, 0, sizeof(config
));
1717 config
.src_addr
= (dma_addr_t
)adc
->common
->phys_base
;
1718 config
.src_addr
+= adc
->offset
+ adc
->cfg
->regs
->dr
;
1719 config
.src_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
1721 ret
= dmaengine_slave_config(adc
->dma_chan
, &config
);
1728 dma_free_coherent(adc
->dma_chan
->device
->dev
, STM32_DMA_BUFFER_SIZE
,
1729 adc
->rx_buf
, adc
->rx_dma_buf
);
1731 dma_release_channel(adc
->dma_chan
);
1736 static int stm32_adc_probe(struct platform_device
*pdev
)
1738 struct iio_dev
*indio_dev
;
1739 struct device
*dev
= &pdev
->dev
;
1740 irqreturn_t (*handler
)(int irq
, void *p
) = NULL
;
1741 struct stm32_adc
*adc
;
1744 if (!pdev
->dev
.of_node
)
1747 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*adc
));
1751 adc
= iio_priv(indio_dev
);
1752 adc
->common
= dev_get_drvdata(pdev
->dev
.parent
);
1753 spin_lock_init(&adc
->lock
);
1754 init_completion(&adc
->completion
);
1755 adc
->cfg
= (const struct stm32_adc_cfg
*)
1756 of_match_device(dev
->driver
->of_match_table
, dev
)->data
;
1758 indio_dev
->name
= dev_name(&pdev
->dev
);
1759 indio_dev
->dev
.parent
= &pdev
->dev
;
1760 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
1761 indio_dev
->info
= &stm32_adc_iio_info
;
1762 indio_dev
->modes
= INDIO_DIRECT_MODE
| INDIO_HARDWARE_TRIGGERED
;
1764 platform_set_drvdata(pdev
, adc
);
1766 ret
= of_property_read_u32(pdev
->dev
.of_node
, "reg", &adc
->offset
);
1768 dev_err(&pdev
->dev
, "missing reg property\n");
1772 adc
->irq
= platform_get_irq(pdev
, 0);
1774 dev_err(&pdev
->dev
, "failed to get irq\n");
1778 ret
= devm_request_irq(&pdev
->dev
, adc
->irq
, stm32_adc_isr
,
1779 0, pdev
->name
, adc
);
1781 dev_err(&pdev
->dev
, "failed to request IRQ\n");
1785 adc
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
1786 if (IS_ERR(adc
->clk
)) {
1787 ret
= PTR_ERR(adc
->clk
);
1788 if (ret
== -ENOENT
&& !adc
->cfg
->clk_required
) {
1791 dev_err(&pdev
->dev
, "Can't get clock\n");
1797 ret
= clk_prepare_enable(adc
->clk
);
1799 dev_err(&pdev
->dev
, "clk enable failed\n");
1804 ret
= stm32_adc_of_get_resolution(indio_dev
);
1806 goto err_clk_disable
;
1807 stm32_adc_set_res(adc
);
1809 if (adc
->cfg
->selfcalib
) {
1810 ret
= adc
->cfg
->selfcalib(adc
);
1812 goto err_clk_disable
;
1815 ret
= stm32_adc_chan_of_init(indio_dev
);
1817 goto err_clk_disable
;
1819 ret
= stm32_adc_dma_request(dev
, indio_dev
);
1821 goto err_clk_disable
;
1824 handler
= &stm32_adc_trigger_handler
;
1826 ret
= iio_triggered_buffer_setup(indio_dev
,
1827 &iio_pollfunc_store_time
, handler
,
1828 &stm32_adc_buffer_setup_ops
);
1830 dev_err(&pdev
->dev
, "buffer setup failed\n");
1831 goto err_dma_disable
;
1834 ret
= iio_device_register(indio_dev
);
1836 dev_err(&pdev
->dev
, "iio dev register failed\n");
1837 goto err_buffer_cleanup
;
1843 iio_triggered_buffer_cleanup(indio_dev
);
1846 if (adc
->dma_chan
) {
1847 dma_free_coherent(adc
->dma_chan
->device
->dev
,
1848 STM32_DMA_BUFFER_SIZE
,
1849 adc
->rx_buf
, adc
->rx_dma_buf
);
1850 dma_release_channel(adc
->dma_chan
);
1854 clk_disable_unprepare(adc
->clk
);
1859 static int stm32_adc_remove(struct platform_device
*pdev
)
1861 struct stm32_adc
*adc
= platform_get_drvdata(pdev
);
1862 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1864 iio_device_unregister(indio_dev
);
1865 iio_triggered_buffer_cleanup(indio_dev
);
1866 if (adc
->dma_chan
) {
1867 dma_free_coherent(adc
->dma_chan
->device
->dev
,
1868 STM32_DMA_BUFFER_SIZE
,
1869 adc
->rx_buf
, adc
->rx_dma_buf
);
1870 dma_release_channel(adc
->dma_chan
);
1873 clk_disable_unprepare(adc
->clk
);
1878 static const struct stm32_adc_cfg stm32f4_adc_cfg
= {
1879 .regs
= &stm32f4_adc_regspec
,
1880 .adc_info
= &stm32f4_adc_info
,
1881 .trigs
= stm32f4_adc_trigs
,
1882 .clk_required
= true,
1883 .start_conv
= stm32f4_adc_start_conv
,
1884 .stop_conv
= stm32f4_adc_stop_conv
,
1885 .smp_cycles
= stm32f4_adc_smp_cycles
,
1888 static const struct stm32_adc_cfg stm32h7_adc_cfg
= {
1889 .regs
= &stm32h7_adc_regspec
,
1890 .adc_info
= &stm32h7_adc_info
,
1891 .trigs
= stm32h7_adc_trigs
,
1892 .selfcalib
= stm32h7_adc_selfcalib
,
1893 .start_conv
= stm32h7_adc_start_conv
,
1894 .stop_conv
= stm32h7_adc_stop_conv
,
1895 .prepare
= stm32h7_adc_prepare
,
1896 .unprepare
= stm32h7_adc_unprepare
,
1897 .smp_cycles
= stm32h7_adc_smp_cycles
,
1900 static const struct stm32_adc_cfg stm32mp1_adc_cfg
= {
1901 .regs
= &stm32h7_adc_regspec
,
1902 .adc_info
= &stm32h7_adc_info
,
1903 .trigs
= stm32h7_adc_trigs
,
1904 .has_vregready
= true,
1905 .selfcalib
= stm32h7_adc_selfcalib
,
1906 .start_conv
= stm32h7_adc_start_conv
,
1907 .stop_conv
= stm32h7_adc_stop_conv
,
1908 .prepare
= stm32h7_adc_prepare
,
1909 .unprepare
= stm32h7_adc_unprepare
,
1910 .smp_cycles
= stm32h7_adc_smp_cycles
,
1913 static const struct of_device_id stm32_adc_of_match
[] = {
1914 { .compatible
= "st,stm32f4-adc", .data
= (void *)&stm32f4_adc_cfg
},
1915 { .compatible
= "st,stm32h7-adc", .data
= (void *)&stm32h7_adc_cfg
},
1916 { .compatible
= "st,stm32mp1-adc", .data
= (void *)&stm32mp1_adc_cfg
},
1919 MODULE_DEVICE_TABLE(of
, stm32_adc_of_match
);
1921 static struct platform_driver stm32_adc_driver
= {
1922 .probe
= stm32_adc_probe
,
1923 .remove
= stm32_adc_remove
,
1925 .name
= "stm32-adc",
1926 .of_match_table
= stm32_adc_of_match
,
1929 module_platform_driver(stm32_adc_driver
);
1931 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
1932 MODULE_DESCRIPTION("STMicroelectronics STM32 ADC IIO driver");
1933 MODULE_LICENSE("GPL v2");
1934 MODULE_ALIAS("platform:stm32-adc");