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>
25 #include <linux/pm_runtime.h>
27 #include <linux/of_device.h>
29 #include "stm32-adc-core.h"
31 /* Number of linear calibration shadow registers / LINCALRDYW control bits */
32 #define STM32H7_LINCALFACT_NUM 6
34 /* BOOST bit must be set on STM32H7 when ADC clock is above 20MHz */
35 #define STM32H7_BOOST_CLKRATE 20000000UL
37 #define STM32_ADC_CH_MAX 20 /* max number of channels */
38 #define STM32_ADC_CH_SZ 10 /* max channel name size */
39 #define STM32_ADC_MAX_SQ 16 /* SQ1..SQ16 */
40 #define STM32_ADC_MAX_SMP 7 /* SMPx range is [0..7] */
41 #define STM32_ADC_TIMEOUT_US 100000
42 #define STM32_ADC_TIMEOUT (msecs_to_jiffies(STM32_ADC_TIMEOUT_US / 1000))
43 #define STM32_ADC_HW_STOP_DELAY_MS 100
45 #define STM32_DMA_BUFFER_SIZE PAGE_SIZE
47 /* External trigger enable */
48 enum stm32_adc_exten
{
50 STM32_EXTEN_HWTRIG_RISING_EDGE
,
51 STM32_EXTEN_HWTRIG_FALLING_EDGE
,
52 STM32_EXTEN_HWTRIG_BOTH_EDGES
,
55 /* extsel - trigger mux selection value */
56 enum stm32_adc_extsel
{
81 * struct stm32_adc_trig_info - ADC trigger info
82 * @name: name of the trigger, corresponding to its source
83 * @extsel: trigger selection
85 struct stm32_adc_trig_info
{
87 enum stm32_adc_extsel extsel
;
91 * struct stm32_adc_calib - optional adc calibration data
92 * @calfact_s: Calibration offset for single ended channels
93 * @calfact_d: Calibration offset in differential
94 * @lincalfact: Linearity calibration factor
95 * @calibrated: Indicates calibration status
97 struct stm32_adc_calib
{
100 u32 lincalfact
[STM32H7_LINCALFACT_NUM
];
105 * struct stm32_adc_regs - stm32 ADC misc registers & bitfield desc
106 * @reg: register offset
107 * @mask: bitfield mask
110 struct stm32_adc_regs
{
117 * struct stm32_adc_regspec - stm32 registers definition
118 * @dr: data register offset
119 * @ier_eoc: interrupt enable register & eocie bitfield
120 * @ier_ovr: interrupt enable register & overrun bitfield
121 * @isr_eoc: interrupt status register & eoc bitfield
122 * @isr_ovr: interrupt status register & overrun bitfield
123 * @sqr: reference to sequence registers array
124 * @exten: trigger control register & bitfield
125 * @extsel: trigger selection register & bitfield
126 * @res: resolution selection register & bitfield
127 * @smpr: smpr1 & smpr2 registers offset array
128 * @smp_bits: smpr1 & smpr2 index and bitfields
130 struct stm32_adc_regspec
{
132 const struct stm32_adc_regs ier_eoc
;
133 const struct stm32_adc_regs ier_ovr
;
134 const struct stm32_adc_regs isr_eoc
;
135 const struct stm32_adc_regs isr_ovr
;
136 const struct stm32_adc_regs
*sqr
;
137 const struct stm32_adc_regs exten
;
138 const struct stm32_adc_regs extsel
;
139 const struct stm32_adc_regs res
;
141 const struct stm32_adc_regs
*smp_bits
;
147 * struct stm32_adc_cfg - stm32 compatible configuration data
148 * @regs: registers descriptions
149 * @adc_info: per instance input channels definitions
150 * @trigs: external trigger sources
151 * @clk_required: clock is required
152 * @has_vregready: vregready status flag presence
153 * @prepare: optional prepare routine (power-up, enable)
154 * @start_conv: routine to start conversions
155 * @stop_conv: routine to stop conversions
156 * @unprepare: optional unprepare routine (disable, power-down)
157 * @smp_cycles: programmable sampling time (ADC clock cycles)
159 struct stm32_adc_cfg
{
160 const struct stm32_adc_regspec
*regs
;
161 const struct stm32_adc_info
*adc_info
;
162 struct stm32_adc_trig_info
*trigs
;
165 int (*prepare
)(struct stm32_adc
*);
166 void (*start_conv
)(struct stm32_adc
*, bool dma
);
167 void (*stop_conv
)(struct stm32_adc
*);
168 void (*unprepare
)(struct stm32_adc
*);
169 const unsigned int *smp_cycles
;
173 * struct stm32_adc - private data of each ADC IIO instance
174 * @common: reference to ADC block common data
175 * @offset: ADC instance register offset in ADC block
176 * @cfg: compatible configuration data
177 * @completion: end of single conversion completion
178 * @buffer: data buffer
179 * @clk: clock for this adc instance
180 * @irq: interrupt for this adc instance
182 * @bufi: data buffer index
183 * @num_conv: expected number of scan conversions
184 * @res: data resolution (e.g. RES bitfield value)
185 * @trigger_polarity: external trigger polarity (e.g. exten)
186 * @dma_chan: dma channel
187 * @rx_buf: dma rx buffer cpu address
188 * @rx_dma_buf: dma rx buffer bus address
189 * @rx_buf_sz: dma rx buffer size
190 * @difsel: bitmask to set single-ended/differential channel
191 * @pcsel: bitmask to preselect channels on some devices
192 * @smpr_val: sampling time settings (e.g. smpr1 / smpr2)
193 * @cal: optional calibration data on some devices
194 * @chan_name: channel name array
197 struct stm32_adc_common
*common
;
199 const struct stm32_adc_cfg
*cfg
;
200 struct completion completion
;
201 u16 buffer
[STM32_ADC_MAX_SQ
];
204 spinlock_t lock
; /* interrupt lock */
206 unsigned int num_conv
;
208 u32 trigger_polarity
;
209 struct dma_chan
*dma_chan
;
211 dma_addr_t rx_dma_buf
;
212 unsigned int rx_buf_sz
;
216 struct stm32_adc_calib cal
;
217 char chan_name
[STM32_ADC_CH_MAX
][STM32_ADC_CH_SZ
];
220 struct stm32_adc_diff_channel
{
226 * struct stm32_adc_info - stm32 ADC, per instance config data
227 * @max_channels: Number of channels
228 * @resolutions: available resolutions
229 * @num_res: number of available resolutions
231 struct stm32_adc_info
{
233 const unsigned int *resolutions
;
234 const unsigned int num_res
;
237 static const unsigned int stm32f4_adc_resolutions
[] = {
238 /* sorted values so the index matches RES[1:0] in STM32F4_ADC_CR1 */
242 /* stm32f4 can have up to 16 channels */
243 static const struct stm32_adc_info stm32f4_adc_info
= {
245 .resolutions
= stm32f4_adc_resolutions
,
246 .num_res
= ARRAY_SIZE(stm32f4_adc_resolutions
),
249 static const unsigned int stm32h7_adc_resolutions
[] = {
250 /* sorted values so the index matches RES[2:0] in STM32H7_ADC_CFGR */
254 /* stm32h7 can have up to 20 channels */
255 static const struct stm32_adc_info stm32h7_adc_info
= {
256 .max_channels
= STM32_ADC_CH_MAX
,
257 .resolutions
= stm32h7_adc_resolutions
,
258 .num_res
= ARRAY_SIZE(stm32h7_adc_resolutions
),
262 * stm32f4_sq - describe regular sequence registers
263 * - L: sequence len (register & bit field)
264 * - SQ1..SQ16: sequence entries (register & bit field)
266 static const struct stm32_adc_regs stm32f4_sq
[STM32_ADC_MAX_SQ
+ 1] = {
267 /* L: len bit field description to be kept as first element */
268 { STM32F4_ADC_SQR1
, GENMASK(23, 20), 20 },
269 /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
270 { STM32F4_ADC_SQR3
, GENMASK(4, 0), 0 },
271 { STM32F4_ADC_SQR3
, GENMASK(9, 5), 5 },
272 { STM32F4_ADC_SQR3
, GENMASK(14, 10), 10 },
273 { STM32F4_ADC_SQR3
, GENMASK(19, 15), 15 },
274 { STM32F4_ADC_SQR3
, GENMASK(24, 20), 20 },
275 { STM32F4_ADC_SQR3
, GENMASK(29, 25), 25 },
276 { STM32F4_ADC_SQR2
, GENMASK(4, 0), 0 },
277 { STM32F4_ADC_SQR2
, GENMASK(9, 5), 5 },
278 { STM32F4_ADC_SQR2
, GENMASK(14, 10), 10 },
279 { STM32F4_ADC_SQR2
, GENMASK(19, 15), 15 },
280 { STM32F4_ADC_SQR2
, GENMASK(24, 20), 20 },
281 { STM32F4_ADC_SQR2
, GENMASK(29, 25), 25 },
282 { STM32F4_ADC_SQR1
, GENMASK(4, 0), 0 },
283 { STM32F4_ADC_SQR1
, GENMASK(9, 5), 5 },
284 { STM32F4_ADC_SQR1
, GENMASK(14, 10), 10 },
285 { STM32F4_ADC_SQR1
, GENMASK(19, 15), 15 },
288 /* STM32F4 external trigger sources for all instances */
289 static struct stm32_adc_trig_info stm32f4_adc_trigs
[] = {
290 { TIM1_CH1
, STM32_EXT0
},
291 { TIM1_CH2
, STM32_EXT1
},
292 { TIM1_CH3
, STM32_EXT2
},
293 { TIM2_CH2
, STM32_EXT3
},
294 { TIM2_CH3
, STM32_EXT4
},
295 { TIM2_CH4
, STM32_EXT5
},
296 { TIM2_TRGO
, STM32_EXT6
},
297 { TIM3_CH1
, STM32_EXT7
},
298 { TIM3_TRGO
, STM32_EXT8
},
299 { TIM4_CH4
, STM32_EXT9
},
300 { TIM5_CH1
, STM32_EXT10
},
301 { TIM5_CH2
, STM32_EXT11
},
302 { TIM5_CH3
, STM32_EXT12
},
303 { TIM8_CH1
, STM32_EXT13
},
304 { TIM8_TRGO
, STM32_EXT14
},
309 * stm32f4_smp_bits[] - describe sampling time register index & bit fields
310 * Sorted so it can be indexed by channel number.
312 static const struct stm32_adc_regs stm32f4_smp_bits
[] = {
313 /* STM32F4_ADC_SMPR2: smpr[] index, mask, shift for SMP0 to SMP9 */
314 { 1, GENMASK(2, 0), 0 },
315 { 1, GENMASK(5, 3), 3 },
316 { 1, GENMASK(8, 6), 6 },
317 { 1, GENMASK(11, 9), 9 },
318 { 1, GENMASK(14, 12), 12 },
319 { 1, GENMASK(17, 15), 15 },
320 { 1, GENMASK(20, 18), 18 },
321 { 1, GENMASK(23, 21), 21 },
322 { 1, GENMASK(26, 24), 24 },
323 { 1, GENMASK(29, 27), 27 },
324 /* STM32F4_ADC_SMPR1, smpr[] index, mask, shift for SMP10 to SMP18 */
325 { 0, GENMASK(2, 0), 0 },
326 { 0, GENMASK(5, 3), 3 },
327 { 0, GENMASK(8, 6), 6 },
328 { 0, GENMASK(11, 9), 9 },
329 { 0, GENMASK(14, 12), 12 },
330 { 0, GENMASK(17, 15), 15 },
331 { 0, GENMASK(20, 18), 18 },
332 { 0, GENMASK(23, 21), 21 },
333 { 0, GENMASK(26, 24), 24 },
336 /* STM32F4 programmable sampling time (ADC clock cycles) */
337 static const unsigned int stm32f4_adc_smp_cycles
[STM32_ADC_MAX_SMP
+ 1] = {
338 3, 15, 28, 56, 84, 112, 144, 480,
341 static const struct stm32_adc_regspec stm32f4_adc_regspec
= {
342 .dr
= STM32F4_ADC_DR
,
343 .ier_eoc
= { STM32F4_ADC_CR1
, STM32F4_EOCIE
},
344 .ier_ovr
= { STM32F4_ADC_CR1
, STM32F4_OVRIE
},
345 .isr_eoc
= { STM32F4_ADC_SR
, STM32F4_EOC
},
346 .isr_ovr
= { STM32F4_ADC_SR
, STM32F4_OVR
},
348 .exten
= { STM32F4_ADC_CR2
, STM32F4_EXTEN_MASK
, STM32F4_EXTEN_SHIFT
},
349 .extsel
= { STM32F4_ADC_CR2
, STM32F4_EXTSEL_MASK
,
350 STM32F4_EXTSEL_SHIFT
},
351 .res
= { STM32F4_ADC_CR1
, STM32F4_RES_MASK
, STM32F4_RES_SHIFT
},
352 .smpr
= { STM32F4_ADC_SMPR1
, STM32F4_ADC_SMPR2
},
353 .smp_bits
= stm32f4_smp_bits
,
356 static const struct stm32_adc_regs stm32h7_sq
[STM32_ADC_MAX_SQ
+ 1] = {
357 /* L: len bit field description to be kept as first element */
358 { STM32H7_ADC_SQR1
, GENMASK(3, 0), 0 },
359 /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
360 { STM32H7_ADC_SQR1
, GENMASK(10, 6), 6 },
361 { STM32H7_ADC_SQR1
, GENMASK(16, 12), 12 },
362 { STM32H7_ADC_SQR1
, GENMASK(22, 18), 18 },
363 { STM32H7_ADC_SQR1
, GENMASK(28, 24), 24 },
364 { STM32H7_ADC_SQR2
, GENMASK(4, 0), 0 },
365 { STM32H7_ADC_SQR2
, GENMASK(10, 6), 6 },
366 { STM32H7_ADC_SQR2
, GENMASK(16, 12), 12 },
367 { STM32H7_ADC_SQR2
, GENMASK(22, 18), 18 },
368 { STM32H7_ADC_SQR2
, GENMASK(28, 24), 24 },
369 { STM32H7_ADC_SQR3
, GENMASK(4, 0), 0 },
370 { STM32H7_ADC_SQR3
, GENMASK(10, 6), 6 },
371 { STM32H7_ADC_SQR3
, GENMASK(16, 12), 12 },
372 { STM32H7_ADC_SQR3
, GENMASK(22, 18), 18 },
373 { STM32H7_ADC_SQR3
, GENMASK(28, 24), 24 },
374 { STM32H7_ADC_SQR4
, GENMASK(4, 0), 0 },
375 { STM32H7_ADC_SQR4
, GENMASK(10, 6), 6 },
378 /* STM32H7 external trigger sources for all instances */
379 static struct stm32_adc_trig_info stm32h7_adc_trigs
[] = {
380 { TIM1_CH1
, STM32_EXT0
},
381 { TIM1_CH2
, STM32_EXT1
},
382 { TIM1_CH3
, STM32_EXT2
},
383 { TIM2_CH2
, STM32_EXT3
},
384 { TIM3_TRGO
, STM32_EXT4
},
385 { TIM4_CH4
, STM32_EXT5
},
386 { TIM8_TRGO
, STM32_EXT7
},
387 { TIM8_TRGO2
, STM32_EXT8
},
388 { TIM1_TRGO
, STM32_EXT9
},
389 { TIM1_TRGO2
, STM32_EXT10
},
390 { TIM2_TRGO
, STM32_EXT11
},
391 { TIM4_TRGO
, STM32_EXT12
},
392 { TIM6_TRGO
, STM32_EXT13
},
393 { TIM15_TRGO
, STM32_EXT14
},
394 { TIM3_CH4
, STM32_EXT15
},
395 { LPTIM1_OUT
, STM32_EXT18
},
396 { LPTIM2_OUT
, STM32_EXT19
},
397 { LPTIM3_OUT
, STM32_EXT20
},
402 * stm32h7_smp_bits - describe sampling time register index & bit fields
403 * Sorted so it can be indexed by channel number.
405 static const struct stm32_adc_regs stm32h7_smp_bits
[] = {
406 /* STM32H7_ADC_SMPR1, smpr[] index, mask, shift for SMP0 to SMP9 */
407 { 0, GENMASK(2, 0), 0 },
408 { 0, GENMASK(5, 3), 3 },
409 { 0, GENMASK(8, 6), 6 },
410 { 0, GENMASK(11, 9), 9 },
411 { 0, GENMASK(14, 12), 12 },
412 { 0, GENMASK(17, 15), 15 },
413 { 0, GENMASK(20, 18), 18 },
414 { 0, GENMASK(23, 21), 21 },
415 { 0, GENMASK(26, 24), 24 },
416 { 0, GENMASK(29, 27), 27 },
417 /* STM32H7_ADC_SMPR2, smpr[] index, mask, shift for SMP10 to SMP19 */
418 { 1, GENMASK(2, 0), 0 },
419 { 1, GENMASK(5, 3), 3 },
420 { 1, GENMASK(8, 6), 6 },
421 { 1, GENMASK(11, 9), 9 },
422 { 1, GENMASK(14, 12), 12 },
423 { 1, GENMASK(17, 15), 15 },
424 { 1, GENMASK(20, 18), 18 },
425 { 1, GENMASK(23, 21), 21 },
426 { 1, GENMASK(26, 24), 24 },
427 { 1, GENMASK(29, 27), 27 },
430 /* STM32H7 programmable sampling time (ADC clock cycles, rounded down) */
431 static const unsigned int stm32h7_adc_smp_cycles
[STM32_ADC_MAX_SMP
+ 1] = {
432 1, 2, 8, 16, 32, 64, 387, 810,
435 static const struct stm32_adc_regspec stm32h7_adc_regspec
= {
436 .dr
= STM32H7_ADC_DR
,
437 .ier_eoc
= { STM32H7_ADC_IER
, STM32H7_EOCIE
},
438 .ier_ovr
= { STM32H7_ADC_IER
, STM32H7_OVRIE
},
439 .isr_eoc
= { STM32H7_ADC_ISR
, STM32H7_EOC
},
440 .isr_ovr
= { STM32H7_ADC_ISR
, STM32H7_OVR
},
442 .exten
= { STM32H7_ADC_CFGR
, STM32H7_EXTEN_MASK
, STM32H7_EXTEN_SHIFT
},
443 .extsel
= { STM32H7_ADC_CFGR
, STM32H7_EXTSEL_MASK
,
444 STM32H7_EXTSEL_SHIFT
},
445 .res
= { STM32H7_ADC_CFGR
, STM32H7_RES_MASK
, STM32H7_RES_SHIFT
},
446 .smpr
= { STM32H7_ADC_SMPR1
, STM32H7_ADC_SMPR2
},
447 .smp_bits
= stm32h7_smp_bits
,
451 * STM32 ADC registers access routines
452 * @adc: stm32 adc instance
453 * @reg: reg offset in adc instance
455 * Note: All instances share same base, with 0x0, 0x100 or 0x200 offset resp.
456 * for adc1, adc2 and adc3.
458 static u32
stm32_adc_readl(struct stm32_adc
*adc
, u32 reg
)
460 return readl_relaxed(adc
->common
->base
+ adc
->offset
+ reg
);
463 #define stm32_adc_readl_addr(addr) stm32_adc_readl(adc, addr)
465 #define stm32_adc_readl_poll_timeout(reg, val, cond, sleep_us, timeout_us) \
466 readx_poll_timeout(stm32_adc_readl_addr, reg, val, \
467 cond, sleep_us, timeout_us)
469 static u16
stm32_adc_readw(struct stm32_adc
*adc
, u32 reg
)
471 return readw_relaxed(adc
->common
->base
+ adc
->offset
+ reg
);
474 static void stm32_adc_writel(struct stm32_adc
*adc
, u32 reg
, u32 val
)
476 writel_relaxed(val
, adc
->common
->base
+ adc
->offset
+ reg
);
479 static void stm32_adc_set_bits(struct stm32_adc
*adc
, u32 reg
, u32 bits
)
483 spin_lock_irqsave(&adc
->lock
, flags
);
484 stm32_adc_writel(adc
, reg
, stm32_adc_readl(adc
, reg
) | bits
);
485 spin_unlock_irqrestore(&adc
->lock
, flags
);
488 static void stm32_adc_clr_bits(struct stm32_adc
*adc
, u32 reg
, u32 bits
)
492 spin_lock_irqsave(&adc
->lock
, flags
);
493 stm32_adc_writel(adc
, reg
, stm32_adc_readl(adc
, reg
) & ~bits
);
494 spin_unlock_irqrestore(&adc
->lock
, flags
);
498 * stm32_adc_conv_irq_enable() - Enable end of conversion interrupt
499 * @adc: stm32 adc instance
501 static void stm32_adc_conv_irq_enable(struct stm32_adc
*adc
)
503 stm32_adc_set_bits(adc
, adc
->cfg
->regs
->ier_eoc
.reg
,
504 adc
->cfg
->regs
->ier_eoc
.mask
);
508 * stm32_adc_conv_irq_disable() - Disable end of conversion interrupt
509 * @adc: stm32 adc instance
511 static void stm32_adc_conv_irq_disable(struct stm32_adc
*adc
)
513 stm32_adc_clr_bits(adc
, adc
->cfg
->regs
->ier_eoc
.reg
,
514 adc
->cfg
->regs
->ier_eoc
.mask
);
517 static void stm32_adc_ovr_irq_enable(struct stm32_adc
*adc
)
519 stm32_adc_set_bits(adc
, adc
->cfg
->regs
->ier_ovr
.reg
,
520 adc
->cfg
->regs
->ier_ovr
.mask
);
523 static void stm32_adc_ovr_irq_disable(struct stm32_adc
*adc
)
525 stm32_adc_clr_bits(adc
, adc
->cfg
->regs
->ier_ovr
.reg
,
526 adc
->cfg
->regs
->ier_ovr
.mask
);
529 static void stm32_adc_set_res(struct stm32_adc
*adc
)
531 const struct stm32_adc_regs
*res
= &adc
->cfg
->regs
->res
;
534 val
= stm32_adc_readl(adc
, res
->reg
);
535 val
= (val
& ~res
->mask
) | (adc
->res
<< res
->shift
);
536 stm32_adc_writel(adc
, res
->reg
, val
);
539 static int stm32_adc_hw_stop(struct device
*dev
)
541 struct stm32_adc
*adc
= dev_get_drvdata(dev
);
543 if (adc
->cfg
->unprepare
)
544 adc
->cfg
->unprepare(adc
);
547 clk_disable_unprepare(adc
->clk
);
552 static int stm32_adc_hw_start(struct device
*dev
)
554 struct stm32_adc
*adc
= dev_get_drvdata(dev
);
558 ret
= clk_prepare_enable(adc
->clk
);
563 stm32_adc_set_res(adc
);
565 if (adc
->cfg
->prepare
) {
566 ret
= adc
->cfg
->prepare(adc
);
575 clk_disable_unprepare(adc
->clk
);
581 * stm32f4_adc_start_conv() - Start conversions for regular channels.
582 * @adc: stm32 adc instance
583 * @dma: use dma to transfer conversion result
585 * Start conversions for regular channels.
586 * Also take care of normal or DMA mode. Circular DMA may be used for regular
587 * conversions, in IIO buffer modes. Otherwise, use ADC interrupt with direct
588 * DR read instead (e.g. read_raw, or triggered buffer mode without DMA).
590 static void stm32f4_adc_start_conv(struct stm32_adc
*adc
, bool dma
)
592 stm32_adc_set_bits(adc
, STM32F4_ADC_CR1
, STM32F4_SCAN
);
595 stm32_adc_set_bits(adc
, STM32F4_ADC_CR2
,
596 STM32F4_DMA
| STM32F4_DDS
);
598 stm32_adc_set_bits(adc
, STM32F4_ADC_CR2
, STM32F4_EOCS
| STM32F4_ADON
);
600 /* Wait for Power-up time (tSTAB from datasheet) */
603 /* Software start ? (e.g. trigger detection disabled ?) */
604 if (!(stm32_adc_readl(adc
, STM32F4_ADC_CR2
) & STM32F4_EXTEN_MASK
))
605 stm32_adc_set_bits(adc
, STM32F4_ADC_CR2
, STM32F4_SWSTART
);
608 static void stm32f4_adc_stop_conv(struct stm32_adc
*adc
)
610 stm32_adc_clr_bits(adc
, STM32F4_ADC_CR2
, STM32F4_EXTEN_MASK
);
611 stm32_adc_clr_bits(adc
, STM32F4_ADC_SR
, STM32F4_STRT
);
613 stm32_adc_clr_bits(adc
, STM32F4_ADC_CR1
, STM32F4_SCAN
);
614 stm32_adc_clr_bits(adc
, STM32F4_ADC_CR2
,
615 STM32F4_ADON
| STM32F4_DMA
| STM32F4_DDS
);
618 static void stm32h7_adc_start_conv(struct stm32_adc
*adc
, bool dma
)
620 enum stm32h7_adc_dmngt dmngt
;
625 dmngt
= STM32H7_DMNGT_DMA_CIRC
;
627 dmngt
= STM32H7_DMNGT_DR_ONLY
;
629 spin_lock_irqsave(&adc
->lock
, flags
);
630 val
= stm32_adc_readl(adc
, STM32H7_ADC_CFGR
);
631 val
= (val
& ~STM32H7_DMNGT_MASK
) | (dmngt
<< STM32H7_DMNGT_SHIFT
);
632 stm32_adc_writel(adc
, STM32H7_ADC_CFGR
, val
);
633 spin_unlock_irqrestore(&adc
->lock
, flags
);
635 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADSTART
);
638 static void stm32h7_adc_stop_conv(struct stm32_adc
*adc
)
640 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
644 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADSTP
);
646 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
647 !(val
& (STM32H7_ADSTART
)),
648 100, STM32_ADC_TIMEOUT_US
);
650 dev_warn(&indio_dev
->dev
, "stop failed\n");
652 stm32_adc_clr_bits(adc
, STM32H7_ADC_CFGR
, STM32H7_DMNGT_MASK
);
655 static int stm32h7_adc_exit_pwr_down(struct stm32_adc
*adc
)
657 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
661 /* Exit deep power down, then enable ADC voltage regulator */
662 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, STM32H7_DEEPPWD
);
663 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADVREGEN
);
665 if (adc
->common
->rate
> STM32H7_BOOST_CLKRATE
)
666 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_BOOST
);
668 /* Wait for startup time */
669 if (!adc
->cfg
->has_vregready
) {
670 usleep_range(10, 20);
674 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR
, val
,
675 val
& STM32MP1_VREGREADY
, 100,
676 STM32_ADC_TIMEOUT_US
);
678 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_DEEPPWD
);
679 dev_err(&indio_dev
->dev
, "Failed to exit power down\n");
685 static void stm32h7_adc_enter_pwr_down(struct stm32_adc
*adc
)
687 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, STM32H7_BOOST
);
689 /* Setting DEEPPWD disables ADC vreg and clears ADVREGEN */
690 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_DEEPPWD
);
693 static int stm32h7_adc_enable(struct stm32_adc
*adc
)
695 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
699 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADEN
);
701 /* Poll for ADRDY to be set (after adc startup time) */
702 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR
, val
,
704 100, STM32_ADC_TIMEOUT_US
);
706 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADDIS
);
707 dev_err(&indio_dev
->dev
, "Failed to enable ADC\n");
709 /* Clear ADRDY by writing one */
710 stm32_adc_set_bits(adc
, STM32H7_ADC_ISR
, STM32H7_ADRDY
);
716 static void stm32h7_adc_disable(struct stm32_adc
*adc
)
718 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
722 /* Disable ADC and wait until it's effectively disabled */
723 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADDIS
);
724 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
725 !(val
& STM32H7_ADEN
), 100,
726 STM32_ADC_TIMEOUT_US
);
728 dev_warn(&indio_dev
->dev
, "Failed to disable\n");
732 * stm32h7_adc_read_selfcalib() - read calibration shadow regs, save result
733 * @adc: stm32 adc instance
734 * Note: Must be called once ADC is enabled, so LINCALRDYW[1..6] are writable
736 static int stm32h7_adc_read_selfcalib(struct stm32_adc
*adc
)
738 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
740 u32 lincalrdyw_mask
, val
;
742 /* Read linearity calibration */
743 lincalrdyw_mask
= STM32H7_LINCALRDYW6
;
744 for (i
= STM32H7_LINCALFACT_NUM
- 1; i
>= 0; i
--) {
745 /* Clear STM32H7_LINCALRDYW[6..1]: transfer calib to CALFACT2 */
746 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, lincalrdyw_mask
);
748 /* Poll: wait calib data to be ready in CALFACT2 register */
749 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
750 !(val
& lincalrdyw_mask
),
751 100, STM32_ADC_TIMEOUT_US
);
753 dev_err(&indio_dev
->dev
, "Failed to read calfact\n");
757 val
= stm32_adc_readl(adc
, STM32H7_ADC_CALFACT2
);
758 adc
->cal
.lincalfact
[i
] = (val
& STM32H7_LINCALFACT_MASK
);
759 adc
->cal
.lincalfact
[i
] >>= STM32H7_LINCALFACT_SHIFT
;
761 lincalrdyw_mask
>>= 1;
764 /* Read offset calibration */
765 val
= stm32_adc_readl(adc
, STM32H7_ADC_CALFACT
);
766 adc
->cal
.calfact_s
= (val
& STM32H7_CALFACT_S_MASK
);
767 adc
->cal
.calfact_s
>>= STM32H7_CALFACT_S_SHIFT
;
768 adc
->cal
.calfact_d
= (val
& STM32H7_CALFACT_D_MASK
);
769 adc
->cal
.calfact_d
>>= STM32H7_CALFACT_D_SHIFT
;
770 adc
->cal
.calibrated
= true;
776 * stm32h7_adc_restore_selfcalib() - Restore saved self-calibration result
777 * @adc: stm32 adc instance
778 * Note: ADC must be enabled, with no on-going conversions.
780 static int stm32h7_adc_restore_selfcalib(struct stm32_adc
*adc
)
782 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
784 u32 lincalrdyw_mask
, val
;
786 val
= (adc
->cal
.calfact_s
<< STM32H7_CALFACT_S_SHIFT
) |
787 (adc
->cal
.calfact_d
<< STM32H7_CALFACT_D_SHIFT
);
788 stm32_adc_writel(adc
, STM32H7_ADC_CALFACT
, val
);
790 lincalrdyw_mask
= STM32H7_LINCALRDYW6
;
791 for (i
= STM32H7_LINCALFACT_NUM
- 1; i
>= 0; i
--) {
793 * Write saved calibration data to shadow registers:
794 * Write CALFACT2, and set LINCALRDYW[6..1] bit to trigger
795 * data write. Then poll to wait for complete transfer.
797 val
= adc
->cal
.lincalfact
[i
] << STM32H7_LINCALFACT_SHIFT
;
798 stm32_adc_writel(adc
, STM32H7_ADC_CALFACT2
, val
);
799 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, lincalrdyw_mask
);
800 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
801 val
& lincalrdyw_mask
,
802 100, STM32_ADC_TIMEOUT_US
);
804 dev_err(&indio_dev
->dev
, "Failed to write calfact\n");
809 * Read back calibration data, has two effects:
810 * - It ensures bits LINCALRDYW[6..1] are kept cleared
811 * for next time calibration needs to be restored.
812 * - BTW, bit clear triggers a read, then check data has been
815 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, lincalrdyw_mask
);
816 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
817 !(val
& lincalrdyw_mask
),
818 100, STM32_ADC_TIMEOUT_US
);
820 dev_err(&indio_dev
->dev
, "Failed to read calfact\n");
823 val
= stm32_adc_readl(adc
, STM32H7_ADC_CALFACT2
);
824 if (val
!= adc
->cal
.lincalfact
[i
] << STM32H7_LINCALFACT_SHIFT
) {
825 dev_err(&indio_dev
->dev
, "calfact not consistent\n");
829 lincalrdyw_mask
>>= 1;
836 * Fixed timeout value for ADC calibration.
838 * - low clock frequency
839 * - maximum prescalers
840 * Calibration requires:
841 * - 131,072 ADC clock cycle for the linear calibration
842 * - 20 ADC clock cycle for the offset calibration
844 * Set to 100ms for now
846 #define STM32H7_ADC_CALIB_TIMEOUT_US 100000
849 * stm32h7_adc_selfcalib() - Procedure to calibrate ADC
850 * @adc: stm32 adc instance
851 * Note: Must be called once ADC is out of power down.
853 static int stm32h7_adc_selfcalib(struct stm32_adc
*adc
)
855 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
859 if (adc
->cal
.calibrated
)
863 * Select calibration mode:
864 * - Offset calibration for single ended inputs
865 * - No linearity calibration (do it later, before reading it)
867 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADCALDIF
);
868 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADCALLIN
);
870 /* Start calibration, then wait for completion */
871 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADCAL
);
872 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
873 !(val
& STM32H7_ADCAL
), 100,
874 STM32H7_ADC_CALIB_TIMEOUT_US
);
876 dev_err(&indio_dev
->dev
, "calibration failed\n");
881 * Select calibration mode, then start calibration:
882 * - Offset calibration for differential input
883 * - Linearity calibration (needs to be done only once for single/diff)
884 * will run simultaneously with offset calibration.
886 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
,
887 STM32H7_ADCALDIF
| STM32H7_ADCALLIN
);
888 stm32_adc_set_bits(adc
, STM32H7_ADC_CR
, STM32H7_ADCAL
);
889 ret
= stm32_adc_readl_poll_timeout(STM32H7_ADC_CR
, val
,
890 !(val
& STM32H7_ADCAL
), 100,
891 STM32H7_ADC_CALIB_TIMEOUT_US
);
893 dev_err(&indio_dev
->dev
, "calibration failed\n");
898 stm32_adc_clr_bits(adc
, STM32H7_ADC_CR
,
899 STM32H7_ADCALDIF
| STM32H7_ADCALLIN
);
905 * stm32h7_adc_prepare() - Leave power down mode to enable ADC.
906 * @adc: stm32 adc instance
907 * Leave power down mode.
908 * Configure channels as single ended or differential before enabling ADC.
910 * Restore calibration data.
911 * Pre-select channels that may be used in PCSEL (required by input MUX / IO):
912 * - Only one input is selected for single ended (e.g. 'vinp')
913 * - Two inputs are selected for differential channels (e.g. 'vinp' & 'vinn')
915 static int stm32h7_adc_prepare(struct stm32_adc
*adc
)
919 ret
= stm32h7_adc_exit_pwr_down(adc
);
923 ret
= stm32h7_adc_selfcalib(adc
);
928 stm32_adc_writel(adc
, STM32H7_ADC_DIFSEL
, adc
->difsel
);
930 ret
= stm32h7_adc_enable(adc
);
934 /* Either restore or read calibration result for future reference */
936 ret
= stm32h7_adc_restore_selfcalib(adc
);
938 ret
= stm32h7_adc_read_selfcalib(adc
);
942 stm32_adc_writel(adc
, STM32H7_ADC_PCSEL
, adc
->pcsel
);
947 stm32h7_adc_disable(adc
);
949 stm32h7_adc_enter_pwr_down(adc
);
954 static void stm32h7_adc_unprepare(struct stm32_adc
*adc
)
956 stm32h7_adc_disable(adc
);
957 stm32h7_adc_enter_pwr_down(adc
);
961 * stm32_adc_conf_scan_seq() - Build regular channels scan sequence
962 * @indio_dev: IIO device
963 * @scan_mask: channels to be converted
965 * Conversion sequence :
966 * Apply sampling time settings for all channels.
967 * Configure ADC scan sequence based on selected channels in scan_mask.
968 * Add channels to SQR registers, from scan_mask LSB to MSB, then
969 * program sequence len.
971 static int stm32_adc_conf_scan_seq(struct iio_dev
*indio_dev
,
972 const unsigned long *scan_mask
)
974 struct stm32_adc
*adc
= iio_priv(indio_dev
);
975 const struct stm32_adc_regs
*sqr
= adc
->cfg
->regs
->sqr
;
976 const struct iio_chan_spec
*chan
;
980 /* Apply sampling time settings */
981 stm32_adc_writel(adc
, adc
->cfg
->regs
->smpr
[0], adc
->smpr_val
[0]);
982 stm32_adc_writel(adc
, adc
->cfg
->regs
->smpr
[1], adc
->smpr_val
[1]);
984 for_each_set_bit(bit
, scan_mask
, indio_dev
->masklength
) {
985 chan
= indio_dev
->channels
+ bit
;
987 * Assign one channel per SQ entry in regular
988 * sequence, starting with SQ1.
991 if (i
> STM32_ADC_MAX_SQ
)
994 dev_dbg(&indio_dev
->dev
, "%s chan %d to SQ%d\n",
995 __func__
, chan
->channel
, i
);
997 val
= stm32_adc_readl(adc
, sqr
[i
].reg
);
999 val
|= chan
->channel
<< sqr
[i
].shift
;
1000 stm32_adc_writel(adc
, sqr
[i
].reg
, val
);
1007 val
= stm32_adc_readl(adc
, sqr
[0].reg
);
1008 val
&= ~sqr
[0].mask
;
1009 val
|= ((i
- 1) << sqr
[0].shift
);
1010 stm32_adc_writel(adc
, sqr
[0].reg
, val
);
1016 * stm32_adc_get_trig_extsel() - Get external trigger selection
1017 * @indio_dev: IIO device structure
1020 * Returns trigger extsel value, if trig matches, -EINVAL otherwise.
1022 static int stm32_adc_get_trig_extsel(struct iio_dev
*indio_dev
,
1023 struct iio_trigger
*trig
)
1025 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1028 /* lookup triggers registered by stm32 timer trigger driver */
1029 for (i
= 0; adc
->cfg
->trigs
[i
].name
; i
++) {
1031 * Checking both stm32 timer trigger type and trig name
1032 * should be safe against arbitrary trigger names.
1034 if ((is_stm32_timer_trigger(trig
) ||
1035 is_stm32_lptim_trigger(trig
)) &&
1036 !strcmp(adc
->cfg
->trigs
[i
].name
, trig
->name
)) {
1037 return adc
->cfg
->trigs
[i
].extsel
;
1045 * stm32_adc_set_trig() - Set a regular trigger
1046 * @indio_dev: IIO device
1047 * @trig: IIO trigger
1049 * Set trigger source/polarity (e.g. SW, or HW with polarity) :
1050 * - if HW trigger disabled (e.g. trig == NULL, conversion launched by sw)
1051 * - if HW trigger enabled, set source & polarity
1053 static int stm32_adc_set_trig(struct iio_dev
*indio_dev
,
1054 struct iio_trigger
*trig
)
1056 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1057 u32 val
, extsel
= 0, exten
= STM32_EXTEN_SWTRIG
;
1058 unsigned long flags
;
1062 ret
= stm32_adc_get_trig_extsel(indio_dev
, trig
);
1066 /* set trigger source and polarity (default to rising edge) */
1068 exten
= adc
->trigger_polarity
+ STM32_EXTEN_HWTRIG_RISING_EDGE
;
1071 spin_lock_irqsave(&adc
->lock
, flags
);
1072 val
= stm32_adc_readl(adc
, adc
->cfg
->regs
->exten
.reg
);
1073 val
&= ~(adc
->cfg
->regs
->exten
.mask
| adc
->cfg
->regs
->extsel
.mask
);
1074 val
|= exten
<< adc
->cfg
->regs
->exten
.shift
;
1075 val
|= extsel
<< adc
->cfg
->regs
->extsel
.shift
;
1076 stm32_adc_writel(adc
, adc
->cfg
->regs
->exten
.reg
, val
);
1077 spin_unlock_irqrestore(&adc
->lock
, flags
);
1082 static int stm32_adc_set_trig_pol(struct iio_dev
*indio_dev
,
1083 const struct iio_chan_spec
*chan
,
1086 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1088 adc
->trigger_polarity
= type
;
1093 static int stm32_adc_get_trig_pol(struct iio_dev
*indio_dev
,
1094 const struct iio_chan_spec
*chan
)
1096 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1098 return adc
->trigger_polarity
;
1101 static const char * const stm32_trig_pol_items
[] = {
1102 "rising-edge", "falling-edge", "both-edges",
1105 static const struct iio_enum stm32_adc_trig_pol
= {
1106 .items
= stm32_trig_pol_items
,
1107 .num_items
= ARRAY_SIZE(stm32_trig_pol_items
),
1108 .get
= stm32_adc_get_trig_pol
,
1109 .set
= stm32_adc_set_trig_pol
,
1113 * stm32_adc_single_conv() - Performs a single conversion
1114 * @indio_dev: IIO device
1115 * @chan: IIO channel
1116 * @res: conversion result
1118 * The function performs a single conversion on a given channel:
1119 * - Apply sampling time settings
1120 * - Program sequencer with one channel (e.g. in SQ1 with len = 1)
1122 * - Start conversion, then wait for interrupt completion.
1124 static int stm32_adc_single_conv(struct iio_dev
*indio_dev
,
1125 const struct iio_chan_spec
*chan
,
1128 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1129 struct device
*dev
= indio_dev
->dev
.parent
;
1130 const struct stm32_adc_regspec
*regs
= adc
->cfg
->regs
;
1135 reinit_completion(&adc
->completion
);
1139 ret
= pm_runtime_get_sync(dev
);
1141 pm_runtime_put_noidle(dev
);
1145 /* Apply sampling time settings */
1146 stm32_adc_writel(adc
, regs
->smpr
[0], adc
->smpr_val
[0]);
1147 stm32_adc_writel(adc
, regs
->smpr
[1], adc
->smpr_val
[1]);
1149 /* Program chan number in regular sequence (SQ1) */
1150 val
= stm32_adc_readl(adc
, regs
->sqr
[1].reg
);
1151 val
&= ~regs
->sqr
[1].mask
;
1152 val
|= chan
->channel
<< regs
->sqr
[1].shift
;
1153 stm32_adc_writel(adc
, regs
->sqr
[1].reg
, val
);
1155 /* Set regular sequence len (0 for 1 conversion) */
1156 stm32_adc_clr_bits(adc
, regs
->sqr
[0].reg
, regs
->sqr
[0].mask
);
1158 /* Trigger detection disabled (conversion can be launched in SW) */
1159 stm32_adc_clr_bits(adc
, regs
->exten
.reg
, regs
->exten
.mask
);
1161 stm32_adc_conv_irq_enable(adc
);
1163 adc
->cfg
->start_conv(adc
, false);
1165 timeout
= wait_for_completion_interruptible_timeout(
1166 &adc
->completion
, STM32_ADC_TIMEOUT
);
1169 } else if (timeout
< 0) {
1172 *res
= adc
->buffer
[0];
1176 adc
->cfg
->stop_conv(adc
);
1178 stm32_adc_conv_irq_disable(adc
);
1180 pm_runtime_mark_last_busy(dev
);
1181 pm_runtime_put_autosuspend(dev
);
1186 static int stm32_adc_read_raw(struct iio_dev
*indio_dev
,
1187 struct iio_chan_spec
const *chan
,
1188 int *val
, int *val2
, long mask
)
1190 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1194 case IIO_CHAN_INFO_RAW
:
1195 ret
= iio_device_claim_direct_mode(indio_dev
);
1198 if (chan
->type
== IIO_VOLTAGE
)
1199 ret
= stm32_adc_single_conv(indio_dev
, chan
, val
);
1202 iio_device_release_direct_mode(indio_dev
);
1205 case IIO_CHAN_INFO_SCALE
:
1206 if (chan
->differential
) {
1207 *val
= adc
->common
->vref_mv
* 2;
1208 *val2
= chan
->scan_type
.realbits
;
1210 *val
= adc
->common
->vref_mv
;
1211 *val2
= chan
->scan_type
.realbits
;
1213 return IIO_VAL_FRACTIONAL_LOG2
;
1215 case IIO_CHAN_INFO_OFFSET
:
1216 if (chan
->differential
)
1217 /* ADC_full_scale / 2 */
1218 *val
= -((1 << chan
->scan_type
.realbits
) / 2);
1228 static irqreturn_t
stm32_adc_threaded_isr(int irq
, void *data
)
1230 struct stm32_adc
*adc
= data
;
1231 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1232 const struct stm32_adc_regspec
*regs
= adc
->cfg
->regs
;
1233 u32 status
= stm32_adc_readl(adc
, regs
->isr_eoc
.reg
);
1235 if (status
& regs
->isr_ovr
.mask
)
1236 dev_err(&indio_dev
->dev
, "Overrun, stopping: restart needed\n");
1241 static irqreturn_t
stm32_adc_isr(int irq
, void *data
)
1243 struct stm32_adc
*adc
= data
;
1244 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1245 const struct stm32_adc_regspec
*regs
= adc
->cfg
->regs
;
1246 u32 status
= stm32_adc_readl(adc
, regs
->isr_eoc
.reg
);
1248 if (status
& regs
->isr_ovr
.mask
) {
1250 * Overrun occurred on regular conversions: data for wrong
1251 * channel may be read. Unconditionally disable interrupts
1252 * to stop processing data and print error message.
1253 * Restarting the capture can be done by disabling, then
1254 * re-enabling it (e.g. write 0, then 1 to buffer/enable).
1256 stm32_adc_ovr_irq_disable(adc
);
1257 stm32_adc_conv_irq_disable(adc
);
1258 return IRQ_WAKE_THREAD
;
1261 if (status
& regs
->isr_eoc
.mask
) {
1262 /* Reading DR also clears EOC status flag */
1263 adc
->buffer
[adc
->bufi
] = stm32_adc_readw(adc
, regs
->dr
);
1264 if (iio_buffer_enabled(indio_dev
)) {
1266 if (adc
->bufi
>= adc
->num_conv
) {
1267 stm32_adc_conv_irq_disable(adc
);
1268 iio_trigger_poll(indio_dev
->trig
);
1271 complete(&adc
->completion
);
1280 * stm32_adc_validate_trigger() - validate trigger for stm32 adc
1281 * @indio_dev: IIO device
1282 * @trig: new trigger
1284 * Returns: 0 if trig matches one of the triggers registered by stm32 adc
1285 * driver, -EINVAL otherwise.
1287 static int stm32_adc_validate_trigger(struct iio_dev
*indio_dev
,
1288 struct iio_trigger
*trig
)
1290 return stm32_adc_get_trig_extsel(indio_dev
, trig
) < 0 ? -EINVAL
: 0;
1293 static int stm32_adc_set_watermark(struct iio_dev
*indio_dev
, unsigned int val
)
1295 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1296 unsigned int watermark
= STM32_DMA_BUFFER_SIZE
/ 2;
1297 unsigned int rx_buf_sz
= STM32_DMA_BUFFER_SIZE
;
1300 * dma cyclic transfers are used, buffer is split into two periods.
1302 * - always one buffer (period) dma is working on
1303 * - one buffer (period) driver can push with iio_trigger_poll().
1305 watermark
= min(watermark
, val
* (unsigned)(sizeof(u16
)));
1306 adc
->rx_buf_sz
= min(rx_buf_sz
, watermark
* 2 * adc
->num_conv
);
1311 static int stm32_adc_update_scan_mode(struct iio_dev
*indio_dev
,
1312 const unsigned long *scan_mask
)
1314 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1315 struct device
*dev
= indio_dev
->dev
.parent
;
1318 ret
= pm_runtime_get_sync(dev
);
1320 pm_runtime_put_noidle(dev
);
1324 adc
->num_conv
= bitmap_weight(scan_mask
, indio_dev
->masklength
);
1326 ret
= stm32_adc_conf_scan_seq(indio_dev
, scan_mask
);
1327 pm_runtime_mark_last_busy(dev
);
1328 pm_runtime_put_autosuspend(dev
);
1333 static int stm32_adc_of_xlate(struct iio_dev
*indio_dev
,
1334 const struct of_phandle_args
*iiospec
)
1338 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
1339 if (indio_dev
->channels
[i
].channel
== iiospec
->args
[0])
1346 * stm32_adc_debugfs_reg_access - read or write register value
1347 * @indio_dev: IIO device structure
1348 * @reg: register offset
1349 * @writeval: value to write
1350 * @readval: value to read
1352 * To read a value from an ADC register:
1353 * echo [ADC reg offset] > direct_reg_access
1354 * cat direct_reg_access
1356 * To write a value in a ADC register:
1357 * echo [ADC_reg_offset] [value] > direct_reg_access
1359 static int stm32_adc_debugfs_reg_access(struct iio_dev
*indio_dev
,
1360 unsigned reg
, unsigned writeval
,
1363 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1364 struct device
*dev
= indio_dev
->dev
.parent
;
1367 ret
= pm_runtime_get_sync(dev
);
1369 pm_runtime_put_noidle(dev
);
1374 stm32_adc_writel(adc
, reg
, writeval
);
1376 *readval
= stm32_adc_readl(adc
, reg
);
1378 pm_runtime_mark_last_busy(dev
);
1379 pm_runtime_put_autosuspend(dev
);
1384 static const struct iio_info stm32_adc_iio_info
= {
1385 .read_raw
= stm32_adc_read_raw
,
1386 .validate_trigger
= stm32_adc_validate_trigger
,
1387 .hwfifo_set_watermark
= stm32_adc_set_watermark
,
1388 .update_scan_mode
= stm32_adc_update_scan_mode
,
1389 .debugfs_reg_access
= stm32_adc_debugfs_reg_access
,
1390 .of_xlate
= stm32_adc_of_xlate
,
1393 static unsigned int stm32_adc_dma_residue(struct stm32_adc
*adc
)
1395 struct dma_tx_state state
;
1396 enum dma_status status
;
1398 status
= dmaengine_tx_status(adc
->dma_chan
,
1399 adc
->dma_chan
->cookie
,
1401 if (status
== DMA_IN_PROGRESS
) {
1402 /* Residue is size in bytes from end of buffer */
1403 unsigned int i
= adc
->rx_buf_sz
- state
.residue
;
1406 /* Return available bytes */
1408 size
= i
- adc
->bufi
;
1410 size
= adc
->rx_buf_sz
+ i
- adc
->bufi
;
1418 static void stm32_adc_dma_buffer_done(void *data
)
1420 struct iio_dev
*indio_dev
= data
;
1422 iio_trigger_poll_chained(indio_dev
->trig
);
1425 static int stm32_adc_dma_start(struct iio_dev
*indio_dev
)
1427 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1428 struct dma_async_tx_descriptor
*desc
;
1429 dma_cookie_t cookie
;
1435 dev_dbg(&indio_dev
->dev
, "%s size=%d watermark=%d\n", __func__
,
1436 adc
->rx_buf_sz
, adc
->rx_buf_sz
/ 2);
1438 /* Prepare a DMA cyclic transaction */
1439 desc
= dmaengine_prep_dma_cyclic(adc
->dma_chan
,
1441 adc
->rx_buf_sz
, adc
->rx_buf_sz
/ 2,
1443 DMA_PREP_INTERRUPT
);
1447 desc
->callback
= stm32_adc_dma_buffer_done
;
1448 desc
->callback_param
= indio_dev
;
1450 cookie
= dmaengine_submit(desc
);
1451 ret
= dma_submit_error(cookie
);
1453 dmaengine_terminate_sync(adc
->dma_chan
);
1457 /* Issue pending DMA requests */
1458 dma_async_issue_pending(adc
->dma_chan
);
1463 static int __stm32_adc_buffer_postenable(struct iio_dev
*indio_dev
)
1465 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1466 struct device
*dev
= indio_dev
->dev
.parent
;
1469 ret
= pm_runtime_get_sync(dev
);
1471 pm_runtime_put_noidle(dev
);
1475 ret
= stm32_adc_set_trig(indio_dev
, indio_dev
->trig
);
1477 dev_err(&indio_dev
->dev
, "Can't set trigger\n");
1481 ret
= stm32_adc_dma_start(indio_dev
);
1483 dev_err(&indio_dev
->dev
, "Can't start dma\n");
1487 /* Reset adc buffer index */
1490 stm32_adc_ovr_irq_enable(adc
);
1493 stm32_adc_conv_irq_enable(adc
);
1495 adc
->cfg
->start_conv(adc
, !!adc
->dma_chan
);
1500 stm32_adc_set_trig(indio_dev
, NULL
);
1502 pm_runtime_mark_last_busy(dev
);
1503 pm_runtime_put_autosuspend(dev
);
1508 static int stm32_adc_buffer_postenable(struct iio_dev
*indio_dev
)
1512 ret
= iio_triggered_buffer_postenable(indio_dev
);
1516 ret
= __stm32_adc_buffer_postenable(indio_dev
);
1518 iio_triggered_buffer_predisable(indio_dev
);
1523 static void __stm32_adc_buffer_predisable(struct iio_dev
*indio_dev
)
1525 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1526 struct device
*dev
= indio_dev
->dev
.parent
;
1528 adc
->cfg
->stop_conv(adc
);
1530 stm32_adc_conv_irq_disable(adc
);
1532 stm32_adc_ovr_irq_disable(adc
);
1535 dmaengine_terminate_sync(adc
->dma_chan
);
1537 if (stm32_adc_set_trig(indio_dev
, NULL
))
1538 dev_err(&indio_dev
->dev
, "Can't clear trigger\n");
1540 pm_runtime_mark_last_busy(dev
);
1541 pm_runtime_put_autosuspend(dev
);
1544 static int stm32_adc_buffer_predisable(struct iio_dev
*indio_dev
)
1548 __stm32_adc_buffer_predisable(indio_dev
);
1550 ret
= iio_triggered_buffer_predisable(indio_dev
);
1552 dev_err(&indio_dev
->dev
, "predisable failed\n");
1557 static const struct iio_buffer_setup_ops stm32_adc_buffer_setup_ops
= {
1558 .postenable
= &stm32_adc_buffer_postenable
,
1559 .predisable
= &stm32_adc_buffer_predisable
,
1562 static irqreturn_t
stm32_adc_trigger_handler(int irq
, void *p
)
1564 struct iio_poll_func
*pf
= p
;
1565 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1566 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1568 dev_dbg(&indio_dev
->dev
, "%s bufi=%d\n", __func__
, adc
->bufi
);
1570 if (!adc
->dma_chan
) {
1571 /* reset buffer index */
1573 iio_push_to_buffers_with_timestamp(indio_dev
, adc
->buffer
,
1576 int residue
= stm32_adc_dma_residue(adc
);
1578 while (residue
>= indio_dev
->scan_bytes
) {
1579 u16
*buffer
= (u16
*)&adc
->rx_buf
[adc
->bufi
];
1581 iio_push_to_buffers_with_timestamp(indio_dev
, buffer
,
1583 residue
-= indio_dev
->scan_bytes
;
1584 adc
->bufi
+= indio_dev
->scan_bytes
;
1585 if (adc
->bufi
>= adc
->rx_buf_sz
)
1590 iio_trigger_notify_done(indio_dev
->trig
);
1592 /* re-enable eoc irq */
1594 stm32_adc_conv_irq_enable(adc
);
1599 static const struct iio_chan_spec_ext_info stm32_adc_ext_info
[] = {
1600 IIO_ENUM("trigger_polarity", IIO_SHARED_BY_ALL
, &stm32_adc_trig_pol
),
1602 .name
= "trigger_polarity_available",
1603 .shared
= IIO_SHARED_BY_ALL
,
1604 .read
= iio_enum_available_read
,
1605 .private = (uintptr_t)&stm32_adc_trig_pol
,
1610 static int stm32_adc_of_get_resolution(struct iio_dev
*indio_dev
)
1612 struct device_node
*node
= indio_dev
->dev
.of_node
;
1613 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1617 if (of_property_read_u32(node
, "assigned-resolution-bits", &res
))
1618 res
= adc
->cfg
->adc_info
->resolutions
[0];
1620 for (i
= 0; i
< adc
->cfg
->adc_info
->num_res
; i
++)
1621 if (res
== adc
->cfg
->adc_info
->resolutions
[i
])
1623 if (i
>= adc
->cfg
->adc_info
->num_res
) {
1624 dev_err(&indio_dev
->dev
, "Bad resolution: %u bits\n", res
);
1628 dev_dbg(&indio_dev
->dev
, "Using %u bits resolution\n", res
);
1634 static void stm32_adc_smpr_init(struct stm32_adc
*adc
, int channel
, u32 smp_ns
)
1636 const struct stm32_adc_regs
*smpr
= &adc
->cfg
->regs
->smp_bits
[channel
];
1637 u32 period_ns
, shift
= smpr
->shift
, mask
= smpr
->mask
;
1638 unsigned int smp
, r
= smpr
->reg
;
1640 /* Determine sampling time (ADC clock cycles) */
1641 period_ns
= NSEC_PER_SEC
/ adc
->common
->rate
;
1642 for (smp
= 0; smp
<= STM32_ADC_MAX_SMP
; smp
++)
1643 if ((period_ns
* adc
->cfg
->smp_cycles
[smp
]) >= smp_ns
)
1645 if (smp
> STM32_ADC_MAX_SMP
)
1646 smp
= STM32_ADC_MAX_SMP
;
1648 /* pre-build sampling time registers (e.g. smpr1, smpr2) */
1649 adc
->smpr_val
[r
] = (adc
->smpr_val
[r
] & ~mask
) | (smp
<< shift
);
1652 static void stm32_adc_chan_init_one(struct iio_dev
*indio_dev
,
1653 struct iio_chan_spec
*chan
, u32 vinp
,
1654 u32 vinn
, int scan_index
, bool differential
)
1656 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1657 char *name
= adc
->chan_name
[vinp
];
1659 chan
->type
= IIO_VOLTAGE
;
1660 chan
->channel
= vinp
;
1662 chan
->differential
= 1;
1663 chan
->channel2
= vinn
;
1664 snprintf(name
, STM32_ADC_CH_SZ
, "in%d-in%d", vinp
, vinn
);
1666 snprintf(name
, STM32_ADC_CH_SZ
, "in%d", vinp
);
1668 chan
->datasheet_name
= name
;
1669 chan
->scan_index
= scan_index
;
1671 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
1672 chan
->info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
) |
1673 BIT(IIO_CHAN_INFO_OFFSET
);
1674 chan
->scan_type
.sign
= 'u';
1675 chan
->scan_type
.realbits
= adc
->cfg
->adc_info
->resolutions
[adc
->res
];
1676 chan
->scan_type
.storagebits
= 16;
1677 chan
->ext_info
= stm32_adc_ext_info
;
1679 /* pre-build selected channels mask */
1680 adc
->pcsel
|= BIT(chan
->channel
);
1682 /* pre-build diff channels mask */
1683 adc
->difsel
|= BIT(chan
->channel
);
1684 /* Also add negative input to pre-selected channels */
1685 adc
->pcsel
|= BIT(chan
->channel2
);
1689 static int stm32_adc_chan_of_init(struct iio_dev
*indio_dev
)
1691 struct device_node
*node
= indio_dev
->dev
.of_node
;
1692 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1693 const struct stm32_adc_info
*adc_info
= adc
->cfg
->adc_info
;
1694 struct stm32_adc_diff_channel diff
[STM32_ADC_CH_MAX
];
1695 struct property
*prop
;
1697 struct iio_chan_spec
*channels
;
1698 int scan_index
= 0, num_channels
= 0, num_diff
= 0, ret
, i
;
1701 ret
= of_property_count_u32_elems(node
, "st,adc-channels");
1702 if (ret
> adc_info
->max_channels
) {
1703 dev_err(&indio_dev
->dev
, "Bad st,adc-channels?\n");
1705 } else if (ret
> 0) {
1706 num_channels
+= ret
;
1709 ret
= of_property_count_elems_of_size(node
, "st,adc-diff-channels",
1711 if (ret
> adc_info
->max_channels
) {
1712 dev_err(&indio_dev
->dev
, "Bad st,adc-diff-channels?\n");
1714 } else if (ret
> 0) {
1715 int size
= ret
* sizeof(*diff
) / sizeof(u32
);
1718 num_channels
+= ret
;
1719 ret
= of_property_read_u32_array(node
, "st,adc-diff-channels",
1725 if (!num_channels
) {
1726 dev_err(&indio_dev
->dev
, "No channels configured\n");
1730 /* Optional sample time is provided either for each, or all channels */
1731 ret
= of_property_count_u32_elems(node
, "st,min-sample-time-nsecs");
1732 if (ret
> 1 && ret
!= num_channels
) {
1733 dev_err(&indio_dev
->dev
, "Invalid st,min-sample-time-nsecs\n");
1737 channels
= devm_kcalloc(&indio_dev
->dev
, num_channels
,
1738 sizeof(struct iio_chan_spec
), GFP_KERNEL
);
1742 of_property_for_each_u32(node
, "st,adc-channels", prop
, cur
, val
) {
1743 if (val
>= adc_info
->max_channels
) {
1744 dev_err(&indio_dev
->dev
, "Invalid channel %d\n", val
);
1748 /* Channel can't be configured both as single-ended & diff */
1749 for (i
= 0; i
< num_diff
; i
++) {
1750 if (val
== diff
[i
].vinp
) {
1751 dev_err(&indio_dev
->dev
,
1752 "channel %d miss-configured\n", val
);
1756 stm32_adc_chan_init_one(indio_dev
, &channels
[scan_index
], val
,
1757 0, scan_index
, false);
1761 for (i
= 0; i
< num_diff
; i
++) {
1762 if (diff
[i
].vinp
>= adc_info
->max_channels
||
1763 diff
[i
].vinn
>= adc_info
->max_channels
) {
1764 dev_err(&indio_dev
->dev
, "Invalid channel in%d-in%d\n",
1765 diff
[i
].vinp
, diff
[i
].vinn
);
1768 stm32_adc_chan_init_one(indio_dev
, &channels
[scan_index
],
1769 diff
[i
].vinp
, diff
[i
].vinn
, scan_index
,
1774 for (i
= 0; i
< scan_index
; i
++) {
1776 * Using of_property_read_u32_index(), smp value will only be
1777 * modified if valid u32 value can be decoded. This allows to
1778 * get either no value, 1 shared value for all indexes, or one
1779 * value per channel.
1781 of_property_read_u32_index(node
, "st,min-sample-time-nsecs",
1783 /* Prepare sampling time settings */
1784 stm32_adc_smpr_init(adc
, channels
[i
].channel
, smp
);
1787 indio_dev
->num_channels
= scan_index
;
1788 indio_dev
->channels
= channels
;
1793 static int stm32_adc_dma_request(struct iio_dev
*indio_dev
)
1795 struct stm32_adc
*adc
= iio_priv(indio_dev
);
1796 struct dma_slave_config config
;
1799 adc
->dma_chan
= dma_request_chan(&indio_dev
->dev
, "rx");
1800 if (IS_ERR(adc
->dma_chan
)) {
1801 ret
= PTR_ERR(adc
->dma_chan
);
1802 if (ret
!= -ENODEV
) {
1803 if (ret
!= -EPROBE_DEFER
)
1804 dev_err(&indio_dev
->dev
,
1805 "DMA channel request failed with %d\n",
1810 /* DMA is optional: fall back to IRQ mode */
1811 adc
->dma_chan
= NULL
;
1815 adc
->rx_buf
= dma_alloc_coherent(adc
->dma_chan
->device
->dev
,
1816 STM32_DMA_BUFFER_SIZE
,
1817 &adc
->rx_dma_buf
, GFP_KERNEL
);
1823 /* Configure DMA channel to read data register */
1824 memset(&config
, 0, sizeof(config
));
1825 config
.src_addr
= (dma_addr_t
)adc
->common
->phys_base
;
1826 config
.src_addr
+= adc
->offset
+ adc
->cfg
->regs
->dr
;
1827 config
.src_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
1829 ret
= dmaengine_slave_config(adc
->dma_chan
, &config
);
1836 dma_free_coherent(adc
->dma_chan
->device
->dev
, STM32_DMA_BUFFER_SIZE
,
1837 adc
->rx_buf
, adc
->rx_dma_buf
);
1839 dma_release_channel(adc
->dma_chan
);
1844 static int stm32_adc_probe(struct platform_device
*pdev
)
1846 struct iio_dev
*indio_dev
;
1847 struct device
*dev
= &pdev
->dev
;
1848 struct stm32_adc
*adc
;
1851 if (!pdev
->dev
.of_node
)
1854 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*adc
));
1858 adc
= iio_priv(indio_dev
);
1859 adc
->common
= dev_get_drvdata(pdev
->dev
.parent
);
1860 spin_lock_init(&adc
->lock
);
1861 init_completion(&adc
->completion
);
1862 adc
->cfg
= (const struct stm32_adc_cfg
*)
1863 of_match_device(dev
->driver
->of_match_table
, dev
)->data
;
1865 indio_dev
->name
= dev_name(&pdev
->dev
);
1866 indio_dev
->dev
.parent
= &pdev
->dev
;
1867 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
1868 indio_dev
->info
= &stm32_adc_iio_info
;
1869 indio_dev
->modes
= INDIO_DIRECT_MODE
| INDIO_HARDWARE_TRIGGERED
;
1871 platform_set_drvdata(pdev
, adc
);
1873 ret
= of_property_read_u32(pdev
->dev
.of_node
, "reg", &adc
->offset
);
1875 dev_err(&pdev
->dev
, "missing reg property\n");
1879 adc
->irq
= platform_get_irq(pdev
, 0);
1883 ret
= devm_request_threaded_irq(&pdev
->dev
, adc
->irq
, stm32_adc_isr
,
1884 stm32_adc_threaded_isr
,
1885 0, pdev
->name
, adc
);
1887 dev_err(&pdev
->dev
, "failed to request IRQ\n");
1891 adc
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
1892 if (IS_ERR(adc
->clk
)) {
1893 ret
= PTR_ERR(adc
->clk
);
1894 if (ret
== -ENOENT
&& !adc
->cfg
->clk_required
) {
1897 dev_err(&pdev
->dev
, "Can't get clock\n");
1902 ret
= stm32_adc_of_get_resolution(indio_dev
);
1906 ret
= stm32_adc_chan_of_init(indio_dev
);
1910 ret
= stm32_adc_dma_request(indio_dev
);
1914 ret
= iio_triggered_buffer_setup(indio_dev
,
1915 &iio_pollfunc_store_time
,
1916 &stm32_adc_trigger_handler
,
1917 &stm32_adc_buffer_setup_ops
);
1919 dev_err(&pdev
->dev
, "buffer setup failed\n");
1920 goto err_dma_disable
;
1923 /* Get stm32-adc-core PM online */
1924 pm_runtime_get_noresume(dev
);
1925 pm_runtime_set_active(dev
);
1926 pm_runtime_set_autosuspend_delay(dev
, STM32_ADC_HW_STOP_DELAY_MS
);
1927 pm_runtime_use_autosuspend(dev
);
1928 pm_runtime_enable(dev
);
1930 ret
= stm32_adc_hw_start(dev
);
1932 goto err_buffer_cleanup
;
1934 ret
= iio_device_register(indio_dev
);
1936 dev_err(&pdev
->dev
, "iio dev register failed\n");
1940 pm_runtime_mark_last_busy(dev
);
1941 pm_runtime_put_autosuspend(dev
);
1946 stm32_adc_hw_stop(dev
);
1949 pm_runtime_disable(dev
);
1950 pm_runtime_set_suspended(dev
);
1951 pm_runtime_put_noidle(dev
);
1952 iio_triggered_buffer_cleanup(indio_dev
);
1955 if (adc
->dma_chan
) {
1956 dma_free_coherent(adc
->dma_chan
->device
->dev
,
1957 STM32_DMA_BUFFER_SIZE
,
1958 adc
->rx_buf
, adc
->rx_dma_buf
);
1959 dma_release_channel(adc
->dma_chan
);
1965 static int stm32_adc_remove(struct platform_device
*pdev
)
1967 struct stm32_adc
*adc
= platform_get_drvdata(pdev
);
1968 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1970 pm_runtime_get_sync(&pdev
->dev
);
1971 iio_device_unregister(indio_dev
);
1972 stm32_adc_hw_stop(&pdev
->dev
);
1973 pm_runtime_disable(&pdev
->dev
);
1974 pm_runtime_set_suspended(&pdev
->dev
);
1975 pm_runtime_put_noidle(&pdev
->dev
);
1976 iio_triggered_buffer_cleanup(indio_dev
);
1977 if (adc
->dma_chan
) {
1978 dma_free_coherent(adc
->dma_chan
->device
->dev
,
1979 STM32_DMA_BUFFER_SIZE
,
1980 adc
->rx_buf
, adc
->rx_dma_buf
);
1981 dma_release_channel(adc
->dma_chan
);
1987 #if defined(CONFIG_PM_SLEEP)
1988 static int stm32_adc_suspend(struct device
*dev
)
1990 struct stm32_adc
*adc
= dev_get_drvdata(dev
);
1991 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1993 if (iio_buffer_enabled(indio_dev
))
1994 __stm32_adc_buffer_predisable(indio_dev
);
1996 return pm_runtime_force_suspend(dev
);
1999 static int stm32_adc_resume(struct device
*dev
)
2001 struct stm32_adc
*adc
= dev_get_drvdata(dev
);
2002 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
2005 ret
= pm_runtime_force_resume(dev
);
2009 if (!iio_buffer_enabled(indio_dev
))
2012 ret
= stm32_adc_update_scan_mode(indio_dev
,
2013 indio_dev
->active_scan_mask
);
2017 return __stm32_adc_buffer_postenable(indio_dev
);
2021 #if defined(CONFIG_PM)
2022 static int stm32_adc_runtime_suspend(struct device
*dev
)
2024 return stm32_adc_hw_stop(dev
);
2027 static int stm32_adc_runtime_resume(struct device
*dev
)
2029 return stm32_adc_hw_start(dev
);
2033 static const struct dev_pm_ops stm32_adc_pm_ops
= {
2034 SET_SYSTEM_SLEEP_PM_OPS(stm32_adc_suspend
, stm32_adc_resume
)
2035 SET_RUNTIME_PM_OPS(stm32_adc_runtime_suspend
, stm32_adc_runtime_resume
,
2039 static const struct stm32_adc_cfg stm32f4_adc_cfg
= {
2040 .regs
= &stm32f4_adc_regspec
,
2041 .adc_info
= &stm32f4_adc_info
,
2042 .trigs
= stm32f4_adc_trigs
,
2043 .clk_required
= true,
2044 .start_conv
= stm32f4_adc_start_conv
,
2045 .stop_conv
= stm32f4_adc_stop_conv
,
2046 .smp_cycles
= stm32f4_adc_smp_cycles
,
2049 static const struct stm32_adc_cfg stm32h7_adc_cfg
= {
2050 .regs
= &stm32h7_adc_regspec
,
2051 .adc_info
= &stm32h7_adc_info
,
2052 .trigs
= stm32h7_adc_trigs
,
2053 .start_conv
= stm32h7_adc_start_conv
,
2054 .stop_conv
= stm32h7_adc_stop_conv
,
2055 .prepare
= stm32h7_adc_prepare
,
2056 .unprepare
= stm32h7_adc_unprepare
,
2057 .smp_cycles
= stm32h7_adc_smp_cycles
,
2060 static const struct stm32_adc_cfg stm32mp1_adc_cfg
= {
2061 .regs
= &stm32h7_adc_regspec
,
2062 .adc_info
= &stm32h7_adc_info
,
2063 .trigs
= stm32h7_adc_trigs
,
2064 .has_vregready
= true,
2065 .start_conv
= stm32h7_adc_start_conv
,
2066 .stop_conv
= stm32h7_adc_stop_conv
,
2067 .prepare
= stm32h7_adc_prepare
,
2068 .unprepare
= stm32h7_adc_unprepare
,
2069 .smp_cycles
= stm32h7_adc_smp_cycles
,
2072 static const struct of_device_id stm32_adc_of_match
[] = {
2073 { .compatible
= "st,stm32f4-adc", .data
= (void *)&stm32f4_adc_cfg
},
2074 { .compatible
= "st,stm32h7-adc", .data
= (void *)&stm32h7_adc_cfg
},
2075 { .compatible
= "st,stm32mp1-adc", .data
= (void *)&stm32mp1_adc_cfg
},
2078 MODULE_DEVICE_TABLE(of
, stm32_adc_of_match
);
2080 static struct platform_driver stm32_adc_driver
= {
2081 .probe
= stm32_adc_probe
,
2082 .remove
= stm32_adc_remove
,
2084 .name
= "stm32-adc",
2085 .of_match_table
= stm32_adc_of_match
,
2086 .pm
= &stm32_adc_pm_ops
,
2089 module_platform_driver(stm32_adc_driver
);
2091 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
2092 MODULE_DESCRIPTION("STMicroelectronics STM32 ADC IIO driver");
2093 MODULE_LICENSE("GPL v2");
2094 MODULE_ALIAS("platform:stm32-adc");