blob: 9939dee01743382d227c9360ad3b2453a1bc7755 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * This file is part of STM32 ADC driver
4 *
5 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
6 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
7 */
8
9#include <linux/clk.h>
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>
21#include <linux/io.h>
22#include <linux/iopoll.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
David Brazdil0f672f62019-12-10 10:32:29 +000025#include <linux/pm_runtime.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026#include <linux/of.h>
27#include <linux/of_device.h>
28
29#include "stm32-adc-core.h"
30
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031/* Number of linear calibration shadow registers / LINCALRDYW control bits */
32#define STM32H7_LINCALFACT_NUM 6
33
34/* BOOST bit must be set on STM32H7 when ADC clock is above 20MHz */
35#define STM32H7_BOOST_CLKRATE 20000000UL
36
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))
David Brazdil0f672f62019-12-10 10:32:29 +000043#define STM32_ADC_HW_STOP_DELAY_MS 100
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044
45#define STM32_DMA_BUFFER_SIZE PAGE_SIZE
46
47/* External trigger enable */
48enum stm32_adc_exten {
49 STM32_EXTEN_SWTRIG,
50 STM32_EXTEN_HWTRIG_RISING_EDGE,
51 STM32_EXTEN_HWTRIG_FALLING_EDGE,
52 STM32_EXTEN_HWTRIG_BOTH_EDGES,
53};
54
55/* extsel - trigger mux selection value */
56enum stm32_adc_extsel {
57 STM32_EXT0,
58 STM32_EXT1,
59 STM32_EXT2,
60 STM32_EXT3,
61 STM32_EXT4,
62 STM32_EXT5,
63 STM32_EXT6,
64 STM32_EXT7,
65 STM32_EXT8,
66 STM32_EXT9,
67 STM32_EXT10,
68 STM32_EXT11,
69 STM32_EXT12,
70 STM32_EXT13,
71 STM32_EXT14,
72 STM32_EXT15,
73 STM32_EXT16,
74 STM32_EXT17,
75 STM32_EXT18,
76 STM32_EXT19,
77 STM32_EXT20,
78};
79
80/**
81 * struct stm32_adc_trig_info - ADC trigger info
82 * @name: name of the trigger, corresponding to its source
83 * @extsel: trigger selection
84 */
85struct stm32_adc_trig_info {
86 const char *name;
87 enum stm32_adc_extsel extsel;
88};
89
90/**
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
David Brazdil0f672f62019-12-10 10:32:29 +000095 * @calibrated: Indicates calibration status
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000096 */
97struct stm32_adc_calib {
98 u32 calfact_s;
99 u32 calfact_d;
100 u32 lincalfact[STM32H7_LINCALFACT_NUM];
David Brazdil0f672f62019-12-10 10:32:29 +0000101 bool calibrated;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000102};
103
104/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200105 * struct stm32_adc_regs - stm32 ADC misc registers & bitfield desc
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000106 * @reg: register offset
107 * @mask: bitfield mask
108 * @shift: left shift
109 */
110struct stm32_adc_regs {
111 int reg;
112 int mask;
113 int shift;
114};
115
116/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200117 * struct stm32_adc_regspec - stm32 registers definition
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000118 * @dr: data register offset
119 * @ier_eoc: interrupt enable register & eocie bitfield
Olivier Deprez157378f2022-04-04 15:47:50 +0200120 * @ier_ovr: interrupt enable register & overrun bitfield
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121 * @isr_eoc: interrupt status register & eoc bitfield
Olivier Deprez157378f2022-04-04 15:47:50 +0200122 * @isr_ovr: interrupt status register & overrun bitfield
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000123 * @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
129 */
130struct stm32_adc_regspec {
131 const u32 dr;
132 const struct stm32_adc_regs ier_eoc;
Olivier Deprez157378f2022-04-04 15:47:50 +0200133 const struct stm32_adc_regs ier_ovr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000134 const struct stm32_adc_regs isr_eoc;
Olivier Deprez157378f2022-04-04 15:47:50 +0200135 const struct stm32_adc_regs isr_ovr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136 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;
140 const u32 smpr[2];
141 const struct stm32_adc_regs *smp_bits;
142};
143
144struct stm32_adc;
145
146/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200147 * struct stm32_adc_cfg - stm32 compatible configuration data
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 * @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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153 * @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)
Olivier Deprez157378f2022-04-04 15:47:50 +0200157 * @irq_clear: routine to clear irqs
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000158 * @smp_cycles: programmable sampling time (ADC clock cycles)
159 */
160struct stm32_adc_cfg {
161 const struct stm32_adc_regspec *regs;
162 const struct stm32_adc_info *adc_info;
163 struct stm32_adc_trig_info *trigs;
164 bool clk_required;
165 bool has_vregready;
Olivier Deprez157378f2022-04-04 15:47:50 +0200166 int (*prepare)(struct iio_dev *);
167 void (*start_conv)(struct iio_dev *, bool dma);
168 void (*stop_conv)(struct iio_dev *);
169 void (*unprepare)(struct iio_dev *);
170 void (*irq_clear)(struct iio_dev *indio_dev, u32 msk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000171 const unsigned int *smp_cycles;
172};
173
174/**
175 * struct stm32_adc - private data of each ADC IIO instance
176 * @common: reference to ADC block common data
177 * @offset: ADC instance register offset in ADC block
178 * @cfg: compatible configuration data
179 * @completion: end of single conversion completion
180 * @buffer: data buffer
181 * @clk: clock for this adc instance
182 * @irq: interrupt for this adc instance
183 * @lock: spinlock
184 * @bufi: data buffer index
185 * @num_conv: expected number of scan conversions
186 * @res: data resolution (e.g. RES bitfield value)
187 * @trigger_polarity: external trigger polarity (e.g. exten)
188 * @dma_chan: dma channel
189 * @rx_buf: dma rx buffer cpu address
190 * @rx_dma_buf: dma rx buffer bus address
191 * @rx_buf_sz: dma rx buffer size
Olivier Deprez157378f2022-04-04 15:47:50 +0200192 * @difsel: bitmask to set single-ended/differential channel
193 * @pcsel: bitmask to preselect channels on some devices
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000194 * @smpr_val: sampling time settings (e.g. smpr1 / smpr2)
195 * @cal: optional calibration data on some devices
196 * @chan_name: channel name array
197 */
198struct stm32_adc {
199 struct stm32_adc_common *common;
200 u32 offset;
201 const struct stm32_adc_cfg *cfg;
202 struct completion completion;
203 u16 buffer[STM32_ADC_MAX_SQ];
204 struct clk *clk;
205 int irq;
206 spinlock_t lock; /* interrupt lock */
207 unsigned int bufi;
208 unsigned int num_conv;
209 u32 res;
210 u32 trigger_polarity;
211 struct dma_chan *dma_chan;
212 u8 *rx_buf;
213 dma_addr_t rx_dma_buf;
214 unsigned int rx_buf_sz;
215 u32 difsel;
216 u32 pcsel;
217 u32 smpr_val[2];
218 struct stm32_adc_calib cal;
219 char chan_name[STM32_ADC_CH_MAX][STM32_ADC_CH_SZ];
220};
221
222struct stm32_adc_diff_channel {
223 u32 vinp;
224 u32 vinn;
225};
226
227/**
228 * struct stm32_adc_info - stm32 ADC, per instance config data
229 * @max_channels: Number of channels
230 * @resolutions: available resolutions
231 * @num_res: number of available resolutions
232 */
233struct stm32_adc_info {
234 int max_channels;
235 const unsigned int *resolutions;
236 const unsigned int num_res;
237};
238
239static const unsigned int stm32f4_adc_resolutions[] = {
240 /* sorted values so the index matches RES[1:0] in STM32F4_ADC_CR1 */
241 12, 10, 8, 6,
242};
243
244/* stm32f4 can have up to 16 channels */
245static const struct stm32_adc_info stm32f4_adc_info = {
246 .max_channels = 16,
247 .resolutions = stm32f4_adc_resolutions,
248 .num_res = ARRAY_SIZE(stm32f4_adc_resolutions),
249};
250
251static const unsigned int stm32h7_adc_resolutions[] = {
252 /* sorted values so the index matches RES[2:0] in STM32H7_ADC_CFGR */
253 16, 14, 12, 10, 8,
254};
255
256/* stm32h7 can have up to 20 channels */
257static const struct stm32_adc_info stm32h7_adc_info = {
258 .max_channels = STM32_ADC_CH_MAX,
259 .resolutions = stm32h7_adc_resolutions,
260 .num_res = ARRAY_SIZE(stm32h7_adc_resolutions),
261};
262
Olivier Deprez157378f2022-04-04 15:47:50 +0200263/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000264 * stm32f4_sq - describe regular sequence registers
265 * - L: sequence len (register & bit field)
266 * - SQ1..SQ16: sequence entries (register & bit field)
267 */
268static const struct stm32_adc_regs stm32f4_sq[STM32_ADC_MAX_SQ + 1] = {
269 /* L: len bit field description to be kept as first element */
270 { STM32F4_ADC_SQR1, GENMASK(23, 20), 20 },
271 /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
272 { STM32F4_ADC_SQR3, GENMASK(4, 0), 0 },
273 { STM32F4_ADC_SQR3, GENMASK(9, 5), 5 },
274 { STM32F4_ADC_SQR3, GENMASK(14, 10), 10 },
275 { STM32F4_ADC_SQR3, GENMASK(19, 15), 15 },
276 { STM32F4_ADC_SQR3, GENMASK(24, 20), 20 },
277 { STM32F4_ADC_SQR3, GENMASK(29, 25), 25 },
278 { STM32F4_ADC_SQR2, GENMASK(4, 0), 0 },
279 { STM32F4_ADC_SQR2, GENMASK(9, 5), 5 },
280 { STM32F4_ADC_SQR2, GENMASK(14, 10), 10 },
281 { STM32F4_ADC_SQR2, GENMASK(19, 15), 15 },
282 { STM32F4_ADC_SQR2, GENMASK(24, 20), 20 },
283 { STM32F4_ADC_SQR2, GENMASK(29, 25), 25 },
284 { STM32F4_ADC_SQR1, GENMASK(4, 0), 0 },
285 { STM32F4_ADC_SQR1, GENMASK(9, 5), 5 },
286 { STM32F4_ADC_SQR1, GENMASK(14, 10), 10 },
287 { STM32F4_ADC_SQR1, GENMASK(19, 15), 15 },
288};
289
290/* STM32F4 external trigger sources for all instances */
291static struct stm32_adc_trig_info stm32f4_adc_trigs[] = {
292 { TIM1_CH1, STM32_EXT0 },
293 { TIM1_CH2, STM32_EXT1 },
294 { TIM1_CH3, STM32_EXT2 },
295 { TIM2_CH2, STM32_EXT3 },
296 { TIM2_CH3, STM32_EXT4 },
297 { TIM2_CH4, STM32_EXT5 },
298 { TIM2_TRGO, STM32_EXT6 },
299 { TIM3_CH1, STM32_EXT7 },
300 { TIM3_TRGO, STM32_EXT8 },
301 { TIM4_CH4, STM32_EXT9 },
302 { TIM5_CH1, STM32_EXT10 },
303 { TIM5_CH2, STM32_EXT11 },
304 { TIM5_CH3, STM32_EXT12 },
305 { TIM8_CH1, STM32_EXT13 },
306 { TIM8_TRGO, STM32_EXT14 },
307 {}, /* sentinel */
308};
309
Olivier Deprez157378f2022-04-04 15:47:50 +0200310/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000311 * stm32f4_smp_bits[] - describe sampling time register index & bit fields
312 * Sorted so it can be indexed by channel number.
313 */
314static const struct stm32_adc_regs stm32f4_smp_bits[] = {
315 /* STM32F4_ADC_SMPR2: smpr[] index, mask, shift for SMP0 to SMP9 */
316 { 1, GENMASK(2, 0), 0 },
317 { 1, GENMASK(5, 3), 3 },
318 { 1, GENMASK(8, 6), 6 },
319 { 1, GENMASK(11, 9), 9 },
320 { 1, GENMASK(14, 12), 12 },
321 { 1, GENMASK(17, 15), 15 },
322 { 1, GENMASK(20, 18), 18 },
323 { 1, GENMASK(23, 21), 21 },
324 { 1, GENMASK(26, 24), 24 },
325 { 1, GENMASK(29, 27), 27 },
326 /* STM32F4_ADC_SMPR1, smpr[] index, mask, shift for SMP10 to SMP18 */
327 { 0, GENMASK(2, 0), 0 },
328 { 0, GENMASK(5, 3), 3 },
329 { 0, GENMASK(8, 6), 6 },
330 { 0, GENMASK(11, 9), 9 },
331 { 0, GENMASK(14, 12), 12 },
332 { 0, GENMASK(17, 15), 15 },
333 { 0, GENMASK(20, 18), 18 },
334 { 0, GENMASK(23, 21), 21 },
335 { 0, GENMASK(26, 24), 24 },
336};
337
338/* STM32F4 programmable sampling time (ADC clock cycles) */
339static const unsigned int stm32f4_adc_smp_cycles[STM32_ADC_MAX_SMP + 1] = {
340 3, 15, 28, 56, 84, 112, 144, 480,
341};
342
343static const struct stm32_adc_regspec stm32f4_adc_regspec = {
344 .dr = STM32F4_ADC_DR,
345 .ier_eoc = { STM32F4_ADC_CR1, STM32F4_EOCIE },
Olivier Deprez157378f2022-04-04 15:47:50 +0200346 .ier_ovr = { STM32F4_ADC_CR1, STM32F4_OVRIE },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000347 .isr_eoc = { STM32F4_ADC_SR, STM32F4_EOC },
Olivier Deprez157378f2022-04-04 15:47:50 +0200348 .isr_ovr = { STM32F4_ADC_SR, STM32F4_OVR },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349 .sqr = stm32f4_sq,
350 .exten = { STM32F4_ADC_CR2, STM32F4_EXTEN_MASK, STM32F4_EXTEN_SHIFT },
351 .extsel = { STM32F4_ADC_CR2, STM32F4_EXTSEL_MASK,
352 STM32F4_EXTSEL_SHIFT },
353 .res = { STM32F4_ADC_CR1, STM32F4_RES_MASK, STM32F4_RES_SHIFT },
354 .smpr = { STM32F4_ADC_SMPR1, STM32F4_ADC_SMPR2 },
355 .smp_bits = stm32f4_smp_bits,
356};
357
358static const struct stm32_adc_regs stm32h7_sq[STM32_ADC_MAX_SQ + 1] = {
359 /* L: len bit field description to be kept as first element */
360 { STM32H7_ADC_SQR1, GENMASK(3, 0), 0 },
361 /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */
362 { STM32H7_ADC_SQR1, GENMASK(10, 6), 6 },
363 { STM32H7_ADC_SQR1, GENMASK(16, 12), 12 },
364 { STM32H7_ADC_SQR1, GENMASK(22, 18), 18 },
365 { STM32H7_ADC_SQR1, GENMASK(28, 24), 24 },
366 { STM32H7_ADC_SQR2, GENMASK(4, 0), 0 },
367 { STM32H7_ADC_SQR2, GENMASK(10, 6), 6 },
368 { STM32H7_ADC_SQR2, GENMASK(16, 12), 12 },
369 { STM32H7_ADC_SQR2, GENMASK(22, 18), 18 },
370 { STM32H7_ADC_SQR2, GENMASK(28, 24), 24 },
371 { STM32H7_ADC_SQR3, GENMASK(4, 0), 0 },
372 { STM32H7_ADC_SQR3, GENMASK(10, 6), 6 },
373 { STM32H7_ADC_SQR3, GENMASK(16, 12), 12 },
374 { STM32H7_ADC_SQR3, GENMASK(22, 18), 18 },
375 { STM32H7_ADC_SQR3, GENMASK(28, 24), 24 },
376 { STM32H7_ADC_SQR4, GENMASK(4, 0), 0 },
377 { STM32H7_ADC_SQR4, GENMASK(10, 6), 6 },
378};
379
380/* STM32H7 external trigger sources for all instances */
381static struct stm32_adc_trig_info stm32h7_adc_trigs[] = {
382 { TIM1_CH1, STM32_EXT0 },
383 { TIM1_CH2, STM32_EXT1 },
384 { TIM1_CH3, STM32_EXT2 },
385 { TIM2_CH2, STM32_EXT3 },
386 { TIM3_TRGO, STM32_EXT4 },
387 { TIM4_CH4, STM32_EXT5 },
388 { TIM8_TRGO, STM32_EXT7 },
389 { TIM8_TRGO2, STM32_EXT8 },
390 { TIM1_TRGO, STM32_EXT9 },
391 { TIM1_TRGO2, STM32_EXT10 },
392 { TIM2_TRGO, STM32_EXT11 },
393 { TIM4_TRGO, STM32_EXT12 },
394 { TIM6_TRGO, STM32_EXT13 },
395 { TIM15_TRGO, STM32_EXT14 },
396 { TIM3_CH4, STM32_EXT15 },
397 { LPTIM1_OUT, STM32_EXT18 },
398 { LPTIM2_OUT, STM32_EXT19 },
399 { LPTIM3_OUT, STM32_EXT20 },
400 {},
401};
402
Olivier Deprez157378f2022-04-04 15:47:50 +0200403/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000404 * stm32h7_smp_bits - describe sampling time register index & bit fields
405 * Sorted so it can be indexed by channel number.
406 */
407static const struct stm32_adc_regs stm32h7_smp_bits[] = {
408 /* STM32H7_ADC_SMPR1, smpr[] index, mask, shift for SMP0 to SMP9 */
409 { 0, GENMASK(2, 0), 0 },
410 { 0, GENMASK(5, 3), 3 },
411 { 0, GENMASK(8, 6), 6 },
412 { 0, GENMASK(11, 9), 9 },
413 { 0, GENMASK(14, 12), 12 },
414 { 0, GENMASK(17, 15), 15 },
415 { 0, GENMASK(20, 18), 18 },
416 { 0, GENMASK(23, 21), 21 },
417 { 0, GENMASK(26, 24), 24 },
418 { 0, GENMASK(29, 27), 27 },
419 /* STM32H7_ADC_SMPR2, smpr[] index, mask, shift for SMP10 to SMP19 */
420 { 1, GENMASK(2, 0), 0 },
421 { 1, GENMASK(5, 3), 3 },
422 { 1, GENMASK(8, 6), 6 },
423 { 1, GENMASK(11, 9), 9 },
424 { 1, GENMASK(14, 12), 12 },
425 { 1, GENMASK(17, 15), 15 },
426 { 1, GENMASK(20, 18), 18 },
427 { 1, GENMASK(23, 21), 21 },
428 { 1, GENMASK(26, 24), 24 },
429 { 1, GENMASK(29, 27), 27 },
430};
431
432/* STM32H7 programmable sampling time (ADC clock cycles, rounded down) */
433static const unsigned int stm32h7_adc_smp_cycles[STM32_ADC_MAX_SMP + 1] = {
434 1, 2, 8, 16, 32, 64, 387, 810,
435};
436
437static const struct stm32_adc_regspec stm32h7_adc_regspec = {
438 .dr = STM32H7_ADC_DR,
439 .ier_eoc = { STM32H7_ADC_IER, STM32H7_EOCIE },
Olivier Deprez157378f2022-04-04 15:47:50 +0200440 .ier_ovr = { STM32H7_ADC_IER, STM32H7_OVRIE },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000441 .isr_eoc = { STM32H7_ADC_ISR, STM32H7_EOC },
Olivier Deprez157378f2022-04-04 15:47:50 +0200442 .isr_ovr = { STM32H7_ADC_ISR, STM32H7_OVR },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000443 .sqr = stm32h7_sq,
444 .exten = { STM32H7_ADC_CFGR, STM32H7_EXTEN_MASK, STM32H7_EXTEN_SHIFT },
445 .extsel = { STM32H7_ADC_CFGR, STM32H7_EXTSEL_MASK,
446 STM32H7_EXTSEL_SHIFT },
447 .res = { STM32H7_ADC_CFGR, STM32H7_RES_MASK, STM32H7_RES_SHIFT },
448 .smpr = { STM32H7_ADC_SMPR1, STM32H7_ADC_SMPR2 },
449 .smp_bits = stm32h7_smp_bits,
450};
451
452/**
453 * STM32 ADC registers access routines
454 * @adc: stm32 adc instance
455 * @reg: reg offset in adc instance
456 *
457 * Note: All instances share same base, with 0x0, 0x100 or 0x200 offset resp.
458 * for adc1, adc2 and adc3.
459 */
460static u32 stm32_adc_readl(struct stm32_adc *adc, u32 reg)
461{
462 return readl_relaxed(adc->common->base + adc->offset + reg);
463}
464
465#define stm32_adc_readl_addr(addr) stm32_adc_readl(adc, addr)
466
467#define stm32_adc_readl_poll_timeout(reg, val, cond, sleep_us, timeout_us) \
468 readx_poll_timeout(stm32_adc_readl_addr, reg, val, \
469 cond, sleep_us, timeout_us)
470
471static u16 stm32_adc_readw(struct stm32_adc *adc, u32 reg)
472{
473 return readw_relaxed(adc->common->base + adc->offset + reg);
474}
475
476static void stm32_adc_writel(struct stm32_adc *adc, u32 reg, u32 val)
477{
478 writel_relaxed(val, adc->common->base + adc->offset + reg);
479}
480
481static void stm32_adc_set_bits(struct stm32_adc *adc, u32 reg, u32 bits)
482{
483 unsigned long flags;
484
485 spin_lock_irqsave(&adc->lock, flags);
486 stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) | bits);
487 spin_unlock_irqrestore(&adc->lock, flags);
488}
489
490static void stm32_adc_clr_bits(struct stm32_adc *adc, u32 reg, u32 bits)
491{
492 unsigned long flags;
493
494 spin_lock_irqsave(&adc->lock, flags);
495 stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) & ~bits);
496 spin_unlock_irqrestore(&adc->lock, flags);
497}
498
499/**
500 * stm32_adc_conv_irq_enable() - Enable end of conversion interrupt
501 * @adc: stm32 adc instance
502 */
503static void stm32_adc_conv_irq_enable(struct stm32_adc *adc)
504{
505 stm32_adc_set_bits(adc, adc->cfg->regs->ier_eoc.reg,
506 adc->cfg->regs->ier_eoc.mask);
507};
508
509/**
510 * stm32_adc_conv_irq_disable() - Disable end of conversion interrupt
511 * @adc: stm32 adc instance
512 */
513static void stm32_adc_conv_irq_disable(struct stm32_adc *adc)
514{
515 stm32_adc_clr_bits(adc, adc->cfg->regs->ier_eoc.reg,
516 adc->cfg->regs->ier_eoc.mask);
517}
518
Olivier Deprez157378f2022-04-04 15:47:50 +0200519static void stm32_adc_ovr_irq_enable(struct stm32_adc *adc)
520{
521 stm32_adc_set_bits(adc, adc->cfg->regs->ier_ovr.reg,
522 adc->cfg->regs->ier_ovr.mask);
523}
524
525static void stm32_adc_ovr_irq_disable(struct stm32_adc *adc)
526{
527 stm32_adc_clr_bits(adc, adc->cfg->regs->ier_ovr.reg,
528 adc->cfg->regs->ier_ovr.mask);
529}
530
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000531static void stm32_adc_set_res(struct stm32_adc *adc)
532{
533 const struct stm32_adc_regs *res = &adc->cfg->regs->res;
534 u32 val;
535
536 val = stm32_adc_readl(adc, res->reg);
537 val = (val & ~res->mask) | (adc->res << res->shift);
538 stm32_adc_writel(adc, res->reg, val);
539}
540
David Brazdil0f672f62019-12-10 10:32:29 +0000541static int stm32_adc_hw_stop(struct device *dev)
542{
Olivier Deprez157378f2022-04-04 15:47:50 +0200543 struct iio_dev *indio_dev = dev_get_drvdata(dev);
544 struct stm32_adc *adc = iio_priv(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000545
546 if (adc->cfg->unprepare)
Olivier Deprez157378f2022-04-04 15:47:50 +0200547 adc->cfg->unprepare(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000548
549 if (adc->clk)
550 clk_disable_unprepare(adc->clk);
551
552 return 0;
553}
554
555static int stm32_adc_hw_start(struct device *dev)
556{
Olivier Deprez157378f2022-04-04 15:47:50 +0200557 struct iio_dev *indio_dev = dev_get_drvdata(dev);
558 struct stm32_adc *adc = iio_priv(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000559 int ret;
560
561 if (adc->clk) {
562 ret = clk_prepare_enable(adc->clk);
563 if (ret)
564 return ret;
565 }
566
567 stm32_adc_set_res(adc);
568
569 if (adc->cfg->prepare) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200570 ret = adc->cfg->prepare(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000571 if (ret)
572 goto err_clk_dis;
573 }
574
575 return 0;
576
577err_clk_dis:
578 if (adc->clk)
579 clk_disable_unprepare(adc->clk);
580
581 return ret;
582}
583
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000584/**
585 * stm32f4_adc_start_conv() - Start conversions for regular channels.
Olivier Deprez157378f2022-04-04 15:47:50 +0200586 * @indio_dev: IIO device instance
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000587 * @dma: use dma to transfer conversion result
588 *
589 * Start conversions for regular channels.
590 * Also take care of normal or DMA mode. Circular DMA may be used for regular
591 * conversions, in IIO buffer modes. Otherwise, use ADC interrupt with direct
592 * DR read instead (e.g. read_raw, or triggered buffer mode without DMA).
593 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200594static void stm32f4_adc_start_conv(struct iio_dev *indio_dev, bool dma)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000595{
Olivier Deprez157378f2022-04-04 15:47:50 +0200596 struct stm32_adc *adc = iio_priv(indio_dev);
597
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000598 stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN);
599
600 if (dma)
601 stm32_adc_set_bits(adc, STM32F4_ADC_CR2,
602 STM32F4_DMA | STM32F4_DDS);
603
604 stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_EOCS | STM32F4_ADON);
605
606 /* Wait for Power-up time (tSTAB from datasheet) */
607 usleep_range(2, 3);
608
609 /* Software start ? (e.g. trigger detection disabled ?) */
610 if (!(stm32_adc_readl(adc, STM32F4_ADC_CR2) & STM32F4_EXTEN_MASK))
611 stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_SWSTART);
612}
613
Olivier Deprez157378f2022-04-04 15:47:50 +0200614static void stm32f4_adc_stop_conv(struct iio_dev *indio_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000615{
Olivier Deprez157378f2022-04-04 15:47:50 +0200616 struct stm32_adc *adc = iio_priv(indio_dev);
617
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000618 stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK);
619 stm32_adc_clr_bits(adc, STM32F4_ADC_SR, STM32F4_STRT);
620
621 stm32_adc_clr_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN);
622 stm32_adc_clr_bits(adc, STM32F4_ADC_CR2,
623 STM32F4_ADON | STM32F4_DMA | STM32F4_DDS);
624}
625
Olivier Deprez157378f2022-04-04 15:47:50 +0200626static void stm32f4_adc_irq_clear(struct iio_dev *indio_dev, u32 msk)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000627{
Olivier Deprez157378f2022-04-04 15:47:50 +0200628 struct stm32_adc *adc = iio_priv(indio_dev);
629
630 stm32_adc_clr_bits(adc, adc->cfg->regs->isr_eoc.reg, msk);
631}
632
633static void stm32h7_adc_start_conv(struct iio_dev *indio_dev, bool dma)
634{
635 struct stm32_adc *adc = iio_priv(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000636 enum stm32h7_adc_dmngt dmngt;
637 unsigned long flags;
638 u32 val;
639
640 if (dma)
641 dmngt = STM32H7_DMNGT_DMA_CIRC;
642 else
643 dmngt = STM32H7_DMNGT_DR_ONLY;
644
645 spin_lock_irqsave(&adc->lock, flags);
646 val = stm32_adc_readl(adc, STM32H7_ADC_CFGR);
647 val = (val & ~STM32H7_DMNGT_MASK) | (dmngt << STM32H7_DMNGT_SHIFT);
648 stm32_adc_writel(adc, STM32H7_ADC_CFGR, val);
649 spin_unlock_irqrestore(&adc->lock, flags);
650
651 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTART);
652}
653
Olivier Deprez157378f2022-04-04 15:47:50 +0200654static void stm32h7_adc_stop_conv(struct iio_dev *indio_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000655{
Olivier Deprez157378f2022-04-04 15:47:50 +0200656 struct stm32_adc *adc = iio_priv(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000657 int ret;
658 u32 val;
659
660 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTP);
661
662 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
663 !(val & (STM32H7_ADSTART)),
664 100, STM32_ADC_TIMEOUT_US);
665 if (ret)
666 dev_warn(&indio_dev->dev, "stop failed\n");
667
668 stm32_adc_clr_bits(adc, STM32H7_ADC_CFGR, STM32H7_DMNGT_MASK);
669}
670
Olivier Deprez157378f2022-04-04 15:47:50 +0200671static void stm32h7_adc_irq_clear(struct iio_dev *indio_dev, u32 msk)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000672{
Olivier Deprez157378f2022-04-04 15:47:50 +0200673 struct stm32_adc *adc = iio_priv(indio_dev);
674 /* On STM32H7 IRQs are cleared by writing 1 into ISR register */
675 stm32_adc_set_bits(adc, adc->cfg->regs->isr_eoc.reg, msk);
676}
677
678static int stm32h7_adc_exit_pwr_down(struct iio_dev *indio_dev)
679{
680 struct stm32_adc *adc = iio_priv(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000681 int ret;
682 u32 val;
683
684 /* Exit deep power down, then enable ADC voltage regulator */
685 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD);
686 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADVREGEN);
687
688 if (adc->common->rate > STM32H7_BOOST_CLKRATE)
689 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_BOOST);
690
691 /* Wait for startup time */
692 if (!adc->cfg->has_vregready) {
693 usleep_range(10, 20);
694 return 0;
695 }
696
697 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR, val,
698 val & STM32MP1_VREGREADY, 100,
699 STM32_ADC_TIMEOUT_US);
700 if (ret) {
701 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD);
702 dev_err(&indio_dev->dev, "Failed to exit power down\n");
703 }
704
705 return ret;
706}
707
708static void stm32h7_adc_enter_pwr_down(struct stm32_adc *adc)
709{
710 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_BOOST);
711
712 /* Setting DEEPPWD disables ADC vreg and clears ADVREGEN */
713 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD);
714}
715
Olivier Deprez157378f2022-04-04 15:47:50 +0200716static int stm32h7_adc_enable(struct iio_dev *indio_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000717{
Olivier Deprez157378f2022-04-04 15:47:50 +0200718 struct stm32_adc *adc = iio_priv(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000719 int ret;
720 u32 val;
721
722 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADEN);
723
724 /* Poll for ADRDY to be set (after adc startup time) */
725 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR, val,
726 val & STM32H7_ADRDY,
727 100, STM32_ADC_TIMEOUT_US);
728 if (ret) {
729 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADDIS);
730 dev_err(&indio_dev->dev, "Failed to enable ADC\n");
731 } else {
732 /* Clear ADRDY by writing one */
733 stm32_adc_set_bits(adc, STM32H7_ADC_ISR, STM32H7_ADRDY);
734 }
735
736 return ret;
737}
738
Olivier Deprez157378f2022-04-04 15:47:50 +0200739static void stm32h7_adc_disable(struct iio_dev *indio_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000740{
Olivier Deprez157378f2022-04-04 15:47:50 +0200741 struct stm32_adc *adc = iio_priv(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000742 int ret;
743 u32 val;
744
745 /* Disable ADC and wait until it's effectively disabled */
746 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADDIS);
747 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
748 !(val & STM32H7_ADEN), 100,
749 STM32_ADC_TIMEOUT_US);
750 if (ret)
751 dev_warn(&indio_dev->dev, "Failed to disable\n");
752}
753
754/**
755 * stm32h7_adc_read_selfcalib() - read calibration shadow regs, save result
Olivier Deprez157378f2022-04-04 15:47:50 +0200756 * @indio_dev: IIO device instance
David Brazdil0f672f62019-12-10 10:32:29 +0000757 * Note: Must be called once ADC is enabled, so LINCALRDYW[1..6] are writable
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000758 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200759static int stm32h7_adc_read_selfcalib(struct iio_dev *indio_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000760{
Olivier Deprez157378f2022-04-04 15:47:50 +0200761 struct stm32_adc *adc = iio_priv(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000762 int i, ret;
763 u32 lincalrdyw_mask, val;
764
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000765 /* Read linearity calibration */
766 lincalrdyw_mask = STM32H7_LINCALRDYW6;
767 for (i = STM32H7_LINCALFACT_NUM - 1; i >= 0; i--) {
768 /* Clear STM32H7_LINCALRDYW[6..1]: transfer calib to CALFACT2 */
769 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask);
770
771 /* Poll: wait calib data to be ready in CALFACT2 register */
772 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
773 !(val & lincalrdyw_mask),
774 100, STM32_ADC_TIMEOUT_US);
775 if (ret) {
776 dev_err(&indio_dev->dev, "Failed to read calfact\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000777 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000778 }
779
780 val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2);
781 adc->cal.lincalfact[i] = (val & STM32H7_LINCALFACT_MASK);
782 adc->cal.lincalfact[i] >>= STM32H7_LINCALFACT_SHIFT;
783
784 lincalrdyw_mask >>= 1;
785 }
786
787 /* Read offset calibration */
788 val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT);
789 adc->cal.calfact_s = (val & STM32H7_CALFACT_S_MASK);
790 adc->cal.calfact_s >>= STM32H7_CALFACT_S_SHIFT;
791 adc->cal.calfact_d = (val & STM32H7_CALFACT_D_MASK);
792 adc->cal.calfact_d >>= STM32H7_CALFACT_D_SHIFT;
David Brazdil0f672f62019-12-10 10:32:29 +0000793 adc->cal.calibrated = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000794
David Brazdil0f672f62019-12-10 10:32:29 +0000795 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000796}
797
798/**
799 * stm32h7_adc_restore_selfcalib() - Restore saved self-calibration result
Olivier Deprez157378f2022-04-04 15:47:50 +0200800 * @indio_dev: IIO device instance
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000801 * Note: ADC must be enabled, with no on-going conversions.
802 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200803static int stm32h7_adc_restore_selfcalib(struct iio_dev *indio_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000804{
Olivier Deprez157378f2022-04-04 15:47:50 +0200805 struct stm32_adc *adc = iio_priv(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000806 int i, ret;
807 u32 lincalrdyw_mask, val;
808
809 val = (adc->cal.calfact_s << STM32H7_CALFACT_S_SHIFT) |
810 (adc->cal.calfact_d << STM32H7_CALFACT_D_SHIFT);
811 stm32_adc_writel(adc, STM32H7_ADC_CALFACT, val);
812
813 lincalrdyw_mask = STM32H7_LINCALRDYW6;
814 for (i = STM32H7_LINCALFACT_NUM - 1; i >= 0; i--) {
815 /*
816 * Write saved calibration data to shadow registers:
817 * Write CALFACT2, and set LINCALRDYW[6..1] bit to trigger
818 * data write. Then poll to wait for complete transfer.
819 */
820 val = adc->cal.lincalfact[i] << STM32H7_LINCALFACT_SHIFT;
821 stm32_adc_writel(adc, STM32H7_ADC_CALFACT2, val);
822 stm32_adc_set_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask);
823 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
824 val & lincalrdyw_mask,
825 100, STM32_ADC_TIMEOUT_US);
826 if (ret) {
827 dev_err(&indio_dev->dev, "Failed to write calfact\n");
828 return ret;
829 }
830
831 /*
832 * Read back calibration data, has two effects:
833 * - It ensures bits LINCALRDYW[6..1] are kept cleared
834 * for next time calibration needs to be restored.
835 * - BTW, bit clear triggers a read, then check data has been
836 * correctly written.
837 */
838 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask);
839 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
840 !(val & lincalrdyw_mask),
841 100, STM32_ADC_TIMEOUT_US);
842 if (ret) {
843 dev_err(&indio_dev->dev, "Failed to read calfact\n");
844 return ret;
845 }
846 val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2);
847 if (val != adc->cal.lincalfact[i] << STM32H7_LINCALFACT_SHIFT) {
848 dev_err(&indio_dev->dev, "calfact not consistent\n");
849 return -EIO;
850 }
851
852 lincalrdyw_mask >>= 1;
853 }
854
855 return 0;
856}
857
858/**
859 * Fixed timeout value for ADC calibration.
860 * worst cases:
861 * - low clock frequency
862 * - maximum prescalers
863 * Calibration requires:
864 * - 131,072 ADC clock cycle for the linear calibration
865 * - 20 ADC clock cycle for the offset calibration
866 *
867 * Set to 100ms for now
868 */
869#define STM32H7_ADC_CALIB_TIMEOUT_US 100000
870
871/**
David Brazdil0f672f62019-12-10 10:32:29 +0000872 * stm32h7_adc_selfcalib() - Procedure to calibrate ADC
Olivier Deprez157378f2022-04-04 15:47:50 +0200873 * @indio_dev: IIO device instance
David Brazdil0f672f62019-12-10 10:32:29 +0000874 * Note: Must be called once ADC is out of power down.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000875 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200876static int stm32h7_adc_selfcalib(struct iio_dev *indio_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000877{
Olivier Deprez157378f2022-04-04 15:47:50 +0200878 struct stm32_adc *adc = iio_priv(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000879 int ret;
880 u32 val;
881
David Brazdil0f672f62019-12-10 10:32:29 +0000882 if (adc->cal.calibrated)
883 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000884
885 /*
886 * Select calibration mode:
887 * - Offset calibration for single ended inputs
888 * - No linearity calibration (do it later, before reading it)
889 */
890 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADCALDIF);
891 stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADCALLIN);
892
893 /* Start calibration, then wait for completion */
894 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADCAL);
895 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
896 !(val & STM32H7_ADCAL), 100,
897 STM32H7_ADC_CALIB_TIMEOUT_US);
898 if (ret) {
899 dev_err(&indio_dev->dev, "calibration failed\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000900 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000901 }
902
903 /*
904 * Select calibration mode, then start calibration:
905 * - Offset calibration for differential input
906 * - Linearity calibration (needs to be done only once for single/diff)
907 * will run simultaneously with offset calibration.
908 */
909 stm32_adc_set_bits(adc, STM32H7_ADC_CR,
910 STM32H7_ADCALDIF | STM32H7_ADCALLIN);
911 stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADCAL);
912 ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
913 !(val & STM32H7_ADCAL), 100,
914 STM32H7_ADC_CALIB_TIMEOUT_US);
915 if (ret) {
916 dev_err(&indio_dev->dev, "calibration failed\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000917 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000918 }
919
David Brazdil0f672f62019-12-10 10:32:29 +0000920out:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000921 stm32_adc_clr_bits(adc, STM32H7_ADC_CR,
922 STM32H7_ADCALDIF | STM32H7_ADCALLIN);
923
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000924 return ret;
925}
926
927/**
928 * stm32h7_adc_prepare() - Leave power down mode to enable ADC.
Olivier Deprez157378f2022-04-04 15:47:50 +0200929 * @indio_dev: IIO device instance
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000930 * Leave power down mode.
931 * Configure channels as single ended or differential before enabling ADC.
932 * Enable ADC.
933 * Restore calibration data.
934 * Pre-select channels that may be used in PCSEL (required by input MUX / IO):
935 * - Only one input is selected for single ended (e.g. 'vinp')
936 * - Two inputs are selected for differential channels (e.g. 'vinp' & 'vinn')
937 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200938static int stm32h7_adc_prepare(struct iio_dev *indio_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000939{
Olivier Deprez157378f2022-04-04 15:47:50 +0200940 struct stm32_adc *adc = iio_priv(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000941 int calib, ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000942
Olivier Deprez157378f2022-04-04 15:47:50 +0200943 ret = stm32h7_adc_exit_pwr_down(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000944 if (ret)
945 return ret;
946
Olivier Deprez157378f2022-04-04 15:47:50 +0200947 ret = stm32h7_adc_selfcalib(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000948 if (ret < 0)
949 goto pwr_dwn;
950 calib = ret;
951
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000952 stm32_adc_writel(adc, STM32H7_ADC_DIFSEL, adc->difsel);
953
Olivier Deprez157378f2022-04-04 15:47:50 +0200954 ret = stm32h7_adc_enable(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000955 if (ret)
956 goto pwr_dwn;
957
David Brazdil0f672f62019-12-10 10:32:29 +0000958 /* Either restore or read calibration result for future reference */
959 if (calib)
Olivier Deprez157378f2022-04-04 15:47:50 +0200960 ret = stm32h7_adc_restore_selfcalib(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000961 else
Olivier Deprez157378f2022-04-04 15:47:50 +0200962 ret = stm32h7_adc_read_selfcalib(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000963 if (ret)
964 goto disable;
965
966 stm32_adc_writel(adc, STM32H7_ADC_PCSEL, adc->pcsel);
967
968 return 0;
969
970disable:
Olivier Deprez157378f2022-04-04 15:47:50 +0200971 stm32h7_adc_disable(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000972pwr_dwn:
973 stm32h7_adc_enter_pwr_down(adc);
974
975 return ret;
976}
977
Olivier Deprez157378f2022-04-04 15:47:50 +0200978static void stm32h7_adc_unprepare(struct iio_dev *indio_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000979{
Olivier Deprez157378f2022-04-04 15:47:50 +0200980 struct stm32_adc *adc = iio_priv(indio_dev);
981
982 stm32_adc_writel(adc, STM32H7_ADC_PCSEL, 0);
983 stm32h7_adc_disable(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000984 stm32h7_adc_enter_pwr_down(adc);
985}
986
987/**
988 * stm32_adc_conf_scan_seq() - Build regular channels scan sequence
989 * @indio_dev: IIO device
990 * @scan_mask: channels to be converted
991 *
992 * Conversion sequence :
993 * Apply sampling time settings for all channels.
994 * Configure ADC scan sequence based on selected channels in scan_mask.
995 * Add channels to SQR registers, from scan_mask LSB to MSB, then
996 * program sequence len.
997 */
998static int stm32_adc_conf_scan_seq(struct iio_dev *indio_dev,
999 const unsigned long *scan_mask)
1000{
1001 struct stm32_adc *adc = iio_priv(indio_dev);
1002 const struct stm32_adc_regs *sqr = adc->cfg->regs->sqr;
1003 const struct iio_chan_spec *chan;
1004 u32 val, bit;
1005 int i = 0;
1006
1007 /* Apply sampling time settings */
1008 stm32_adc_writel(adc, adc->cfg->regs->smpr[0], adc->smpr_val[0]);
1009 stm32_adc_writel(adc, adc->cfg->regs->smpr[1], adc->smpr_val[1]);
1010
1011 for_each_set_bit(bit, scan_mask, indio_dev->masklength) {
1012 chan = indio_dev->channels + bit;
1013 /*
1014 * Assign one channel per SQ entry in regular
1015 * sequence, starting with SQ1.
1016 */
1017 i++;
1018 if (i > STM32_ADC_MAX_SQ)
1019 return -EINVAL;
1020
1021 dev_dbg(&indio_dev->dev, "%s chan %d to SQ%d\n",
1022 __func__, chan->channel, i);
1023
1024 val = stm32_adc_readl(adc, sqr[i].reg);
1025 val &= ~sqr[i].mask;
1026 val |= chan->channel << sqr[i].shift;
1027 stm32_adc_writel(adc, sqr[i].reg, val);
1028 }
1029
1030 if (!i)
1031 return -EINVAL;
1032
1033 /* Sequence len */
1034 val = stm32_adc_readl(adc, sqr[0].reg);
1035 val &= ~sqr[0].mask;
1036 val |= ((i - 1) << sqr[0].shift);
1037 stm32_adc_writel(adc, sqr[0].reg, val);
1038
1039 return 0;
1040}
1041
1042/**
1043 * stm32_adc_get_trig_extsel() - Get external trigger selection
Olivier Deprez157378f2022-04-04 15:47:50 +02001044 * @indio_dev: IIO device structure
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001045 * @trig: trigger
1046 *
1047 * Returns trigger extsel value, if trig matches, -EINVAL otherwise.
1048 */
1049static int stm32_adc_get_trig_extsel(struct iio_dev *indio_dev,
1050 struct iio_trigger *trig)
1051{
1052 struct stm32_adc *adc = iio_priv(indio_dev);
1053 int i;
1054
1055 /* lookup triggers registered by stm32 timer trigger driver */
1056 for (i = 0; adc->cfg->trigs[i].name; i++) {
1057 /**
1058 * Checking both stm32 timer trigger type and trig name
1059 * should be safe against arbitrary trigger names.
1060 */
1061 if ((is_stm32_timer_trigger(trig) ||
1062 is_stm32_lptim_trigger(trig)) &&
1063 !strcmp(adc->cfg->trigs[i].name, trig->name)) {
1064 return adc->cfg->trigs[i].extsel;
1065 }
1066 }
1067
1068 return -EINVAL;
1069}
1070
1071/**
1072 * stm32_adc_set_trig() - Set a regular trigger
1073 * @indio_dev: IIO device
1074 * @trig: IIO trigger
1075 *
1076 * Set trigger source/polarity (e.g. SW, or HW with polarity) :
1077 * - if HW trigger disabled (e.g. trig == NULL, conversion launched by sw)
1078 * - if HW trigger enabled, set source & polarity
1079 */
1080static int stm32_adc_set_trig(struct iio_dev *indio_dev,
1081 struct iio_trigger *trig)
1082{
1083 struct stm32_adc *adc = iio_priv(indio_dev);
1084 u32 val, extsel = 0, exten = STM32_EXTEN_SWTRIG;
1085 unsigned long flags;
1086 int ret;
1087
1088 if (trig) {
1089 ret = stm32_adc_get_trig_extsel(indio_dev, trig);
1090 if (ret < 0)
1091 return ret;
1092
1093 /* set trigger source and polarity (default to rising edge) */
1094 extsel = ret;
1095 exten = adc->trigger_polarity + STM32_EXTEN_HWTRIG_RISING_EDGE;
1096 }
1097
1098 spin_lock_irqsave(&adc->lock, flags);
1099 val = stm32_adc_readl(adc, adc->cfg->regs->exten.reg);
1100 val &= ~(adc->cfg->regs->exten.mask | adc->cfg->regs->extsel.mask);
1101 val |= exten << adc->cfg->regs->exten.shift;
1102 val |= extsel << adc->cfg->regs->extsel.shift;
1103 stm32_adc_writel(adc, adc->cfg->regs->exten.reg, val);
1104 spin_unlock_irqrestore(&adc->lock, flags);
1105
1106 return 0;
1107}
1108
1109static int stm32_adc_set_trig_pol(struct iio_dev *indio_dev,
1110 const struct iio_chan_spec *chan,
1111 unsigned int type)
1112{
1113 struct stm32_adc *adc = iio_priv(indio_dev);
1114
1115 adc->trigger_polarity = type;
1116
1117 return 0;
1118}
1119
1120static int stm32_adc_get_trig_pol(struct iio_dev *indio_dev,
1121 const struct iio_chan_spec *chan)
1122{
1123 struct stm32_adc *adc = iio_priv(indio_dev);
1124
1125 return adc->trigger_polarity;
1126}
1127
1128static const char * const stm32_trig_pol_items[] = {
1129 "rising-edge", "falling-edge", "both-edges",
1130};
1131
1132static const struct iio_enum stm32_adc_trig_pol = {
1133 .items = stm32_trig_pol_items,
1134 .num_items = ARRAY_SIZE(stm32_trig_pol_items),
1135 .get = stm32_adc_get_trig_pol,
1136 .set = stm32_adc_set_trig_pol,
1137};
1138
1139/**
1140 * stm32_adc_single_conv() - Performs a single conversion
1141 * @indio_dev: IIO device
1142 * @chan: IIO channel
1143 * @res: conversion result
1144 *
1145 * The function performs a single conversion on a given channel:
1146 * - Apply sampling time settings
1147 * - Program sequencer with one channel (e.g. in SQ1 with len = 1)
1148 * - Use SW trigger
1149 * - Start conversion, then wait for interrupt completion.
1150 */
1151static int stm32_adc_single_conv(struct iio_dev *indio_dev,
1152 const struct iio_chan_spec *chan,
1153 int *res)
1154{
1155 struct stm32_adc *adc = iio_priv(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +00001156 struct device *dev = indio_dev->dev.parent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001157 const struct stm32_adc_regspec *regs = adc->cfg->regs;
1158 long timeout;
1159 u32 val;
1160 int ret;
1161
1162 reinit_completion(&adc->completion);
1163
1164 adc->bufi = 0;
1165
David Brazdil0f672f62019-12-10 10:32:29 +00001166 ret = pm_runtime_get_sync(dev);
1167 if (ret < 0) {
1168 pm_runtime_put_noidle(dev);
1169 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001170 }
1171
1172 /* Apply sampling time settings */
1173 stm32_adc_writel(adc, regs->smpr[0], adc->smpr_val[0]);
1174 stm32_adc_writel(adc, regs->smpr[1], adc->smpr_val[1]);
1175
1176 /* Program chan number in regular sequence (SQ1) */
1177 val = stm32_adc_readl(adc, regs->sqr[1].reg);
1178 val &= ~regs->sqr[1].mask;
1179 val |= chan->channel << regs->sqr[1].shift;
1180 stm32_adc_writel(adc, regs->sqr[1].reg, val);
1181
1182 /* Set regular sequence len (0 for 1 conversion) */
1183 stm32_adc_clr_bits(adc, regs->sqr[0].reg, regs->sqr[0].mask);
1184
1185 /* Trigger detection disabled (conversion can be launched in SW) */
1186 stm32_adc_clr_bits(adc, regs->exten.reg, regs->exten.mask);
1187
1188 stm32_adc_conv_irq_enable(adc);
1189
Olivier Deprez157378f2022-04-04 15:47:50 +02001190 adc->cfg->start_conv(indio_dev, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001191
1192 timeout = wait_for_completion_interruptible_timeout(
1193 &adc->completion, STM32_ADC_TIMEOUT);
1194 if (timeout == 0) {
1195 ret = -ETIMEDOUT;
1196 } else if (timeout < 0) {
1197 ret = timeout;
1198 } else {
1199 *res = adc->buffer[0];
1200 ret = IIO_VAL_INT;
1201 }
1202
Olivier Deprez157378f2022-04-04 15:47:50 +02001203 adc->cfg->stop_conv(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001204
1205 stm32_adc_conv_irq_disable(adc);
1206
David Brazdil0f672f62019-12-10 10:32:29 +00001207 pm_runtime_mark_last_busy(dev);
1208 pm_runtime_put_autosuspend(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001209
1210 return ret;
1211}
1212
1213static int stm32_adc_read_raw(struct iio_dev *indio_dev,
1214 struct iio_chan_spec const *chan,
1215 int *val, int *val2, long mask)
1216{
1217 struct stm32_adc *adc = iio_priv(indio_dev);
1218 int ret;
1219
1220 switch (mask) {
1221 case IIO_CHAN_INFO_RAW:
1222 ret = iio_device_claim_direct_mode(indio_dev);
1223 if (ret)
1224 return ret;
1225 if (chan->type == IIO_VOLTAGE)
1226 ret = stm32_adc_single_conv(indio_dev, chan, val);
1227 else
1228 ret = -EINVAL;
1229 iio_device_release_direct_mode(indio_dev);
1230 return ret;
1231
1232 case IIO_CHAN_INFO_SCALE:
1233 if (chan->differential) {
1234 *val = adc->common->vref_mv * 2;
1235 *val2 = chan->scan_type.realbits;
1236 } else {
1237 *val = adc->common->vref_mv;
1238 *val2 = chan->scan_type.realbits;
1239 }
1240 return IIO_VAL_FRACTIONAL_LOG2;
1241
1242 case IIO_CHAN_INFO_OFFSET:
1243 if (chan->differential)
1244 /* ADC_full_scale / 2 */
1245 *val = -((1 << chan->scan_type.realbits) / 2);
1246 else
1247 *val = 0;
1248 return IIO_VAL_INT;
1249
1250 default:
1251 return -EINVAL;
1252 }
1253}
1254
Olivier Deprez157378f2022-04-04 15:47:50 +02001255static void stm32_adc_irq_clear(struct iio_dev *indio_dev, u32 msk)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001256{
Olivier Deprez157378f2022-04-04 15:47:50 +02001257 struct stm32_adc *adc = iio_priv(indio_dev);
1258
1259 adc->cfg->irq_clear(indio_dev, msk);
1260}
1261
1262static irqreturn_t stm32_adc_threaded_isr(int irq, void *data)
1263{
1264 struct iio_dev *indio_dev = data;
1265 struct stm32_adc *adc = iio_priv(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001266 const struct stm32_adc_regspec *regs = adc->cfg->regs;
1267 u32 status = stm32_adc_readl(adc, regs->isr_eoc.reg);
Olivier Deprez157378f2022-04-04 15:47:50 +02001268 u32 mask = stm32_adc_readl(adc, regs->ier_eoc.reg);
1269
1270 /* Check ovr status right now, as ovr mask should be already disabled */
1271 if (status & regs->isr_ovr.mask) {
1272 /*
1273 * Clear ovr bit to avoid subsequent calls to IRQ handler.
1274 * This requires to stop ADC first. OVR bit state in ISR,
1275 * is propaged to CSR register by hardware.
1276 */
1277 adc->cfg->stop_conv(indio_dev);
1278 stm32_adc_irq_clear(indio_dev, regs->isr_ovr.mask);
1279 dev_err(&indio_dev->dev, "Overrun, stopping: restart needed\n");
1280 return IRQ_HANDLED;
1281 }
1282
1283 if (!(status & mask))
1284 dev_err_ratelimited(&indio_dev->dev,
1285 "Unexpected IRQ: IER=0x%08x, ISR=0x%08x\n",
1286 mask, status);
1287
1288 return IRQ_NONE;
1289}
1290
1291static irqreturn_t stm32_adc_isr(int irq, void *data)
1292{
1293 struct iio_dev *indio_dev = data;
1294 struct stm32_adc *adc = iio_priv(indio_dev);
1295 const struct stm32_adc_regspec *regs = adc->cfg->regs;
1296 u32 status = stm32_adc_readl(adc, regs->isr_eoc.reg);
1297 u32 mask = stm32_adc_readl(adc, regs->ier_eoc.reg);
1298
1299 if (!(status & mask))
1300 return IRQ_WAKE_THREAD;
1301
1302 if (status & regs->isr_ovr.mask) {
1303 /*
1304 * Overrun occurred on regular conversions: data for wrong
1305 * channel may be read. Unconditionally disable interrupts
1306 * to stop processing data and print error message.
1307 * Restarting the capture can be done by disabling, then
1308 * re-enabling it (e.g. write 0, then 1 to buffer/enable).
1309 */
1310 stm32_adc_ovr_irq_disable(adc);
1311 stm32_adc_conv_irq_disable(adc);
1312 return IRQ_WAKE_THREAD;
1313 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001314
1315 if (status & regs->isr_eoc.mask) {
1316 /* Reading DR also clears EOC status flag */
1317 adc->buffer[adc->bufi] = stm32_adc_readw(adc, regs->dr);
1318 if (iio_buffer_enabled(indio_dev)) {
1319 adc->bufi++;
1320 if (adc->bufi >= adc->num_conv) {
1321 stm32_adc_conv_irq_disable(adc);
1322 iio_trigger_poll(indio_dev->trig);
1323 }
1324 } else {
1325 complete(&adc->completion);
1326 }
1327 return IRQ_HANDLED;
1328 }
1329
1330 return IRQ_NONE;
1331}
1332
1333/**
1334 * stm32_adc_validate_trigger() - validate trigger for stm32 adc
1335 * @indio_dev: IIO device
1336 * @trig: new trigger
1337 *
1338 * Returns: 0 if trig matches one of the triggers registered by stm32 adc
1339 * driver, -EINVAL otherwise.
1340 */
1341static int stm32_adc_validate_trigger(struct iio_dev *indio_dev,
1342 struct iio_trigger *trig)
1343{
1344 return stm32_adc_get_trig_extsel(indio_dev, trig) < 0 ? -EINVAL : 0;
1345}
1346
1347static int stm32_adc_set_watermark(struct iio_dev *indio_dev, unsigned int val)
1348{
1349 struct stm32_adc *adc = iio_priv(indio_dev);
1350 unsigned int watermark = STM32_DMA_BUFFER_SIZE / 2;
1351 unsigned int rx_buf_sz = STM32_DMA_BUFFER_SIZE;
1352
1353 /*
1354 * dma cyclic transfers are used, buffer is split into two periods.
1355 * There should be :
1356 * - always one buffer (period) dma is working on
1357 * - one buffer (period) driver can push with iio_trigger_poll().
1358 */
1359 watermark = min(watermark, val * (unsigned)(sizeof(u16)));
1360 adc->rx_buf_sz = min(rx_buf_sz, watermark * 2 * adc->num_conv);
1361
1362 return 0;
1363}
1364
1365static int stm32_adc_update_scan_mode(struct iio_dev *indio_dev,
1366 const unsigned long *scan_mask)
1367{
1368 struct stm32_adc *adc = iio_priv(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +00001369 struct device *dev = indio_dev->dev.parent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001370 int ret;
1371
David Brazdil0f672f62019-12-10 10:32:29 +00001372 ret = pm_runtime_get_sync(dev);
1373 if (ret < 0) {
1374 pm_runtime_put_noidle(dev);
1375 return ret;
1376 }
1377
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001378 adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength);
1379
1380 ret = stm32_adc_conf_scan_seq(indio_dev, scan_mask);
David Brazdil0f672f62019-12-10 10:32:29 +00001381 pm_runtime_mark_last_busy(dev);
1382 pm_runtime_put_autosuspend(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001383
David Brazdil0f672f62019-12-10 10:32:29 +00001384 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001385}
1386
1387static int stm32_adc_of_xlate(struct iio_dev *indio_dev,
1388 const struct of_phandle_args *iiospec)
1389{
1390 int i;
1391
1392 for (i = 0; i < indio_dev->num_channels; i++)
1393 if (indio_dev->channels[i].channel == iiospec->args[0])
1394 return i;
1395
1396 return -EINVAL;
1397}
1398
1399/**
1400 * stm32_adc_debugfs_reg_access - read or write register value
Olivier Deprez157378f2022-04-04 15:47:50 +02001401 * @indio_dev: IIO device structure
1402 * @reg: register offset
1403 * @writeval: value to write
1404 * @readval: value to read
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001405 *
1406 * To read a value from an ADC register:
1407 * echo [ADC reg offset] > direct_reg_access
1408 * cat direct_reg_access
1409 *
1410 * To write a value in a ADC register:
1411 * echo [ADC_reg_offset] [value] > direct_reg_access
1412 */
1413static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev,
1414 unsigned reg, unsigned writeval,
1415 unsigned *readval)
1416{
1417 struct stm32_adc *adc = iio_priv(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +00001418 struct device *dev = indio_dev->dev.parent;
1419 int ret;
1420
1421 ret = pm_runtime_get_sync(dev);
1422 if (ret < 0) {
1423 pm_runtime_put_noidle(dev);
1424 return ret;
1425 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001426
1427 if (!readval)
1428 stm32_adc_writel(adc, reg, writeval);
1429 else
1430 *readval = stm32_adc_readl(adc, reg);
1431
David Brazdil0f672f62019-12-10 10:32:29 +00001432 pm_runtime_mark_last_busy(dev);
1433 pm_runtime_put_autosuspend(dev);
1434
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001435 return 0;
1436}
1437
1438static const struct iio_info stm32_adc_iio_info = {
1439 .read_raw = stm32_adc_read_raw,
1440 .validate_trigger = stm32_adc_validate_trigger,
1441 .hwfifo_set_watermark = stm32_adc_set_watermark,
1442 .update_scan_mode = stm32_adc_update_scan_mode,
1443 .debugfs_reg_access = stm32_adc_debugfs_reg_access,
1444 .of_xlate = stm32_adc_of_xlate,
1445};
1446
1447static unsigned int stm32_adc_dma_residue(struct stm32_adc *adc)
1448{
1449 struct dma_tx_state state;
1450 enum dma_status status;
1451
1452 status = dmaengine_tx_status(adc->dma_chan,
1453 adc->dma_chan->cookie,
1454 &state);
1455 if (status == DMA_IN_PROGRESS) {
1456 /* Residue is size in bytes from end of buffer */
1457 unsigned int i = adc->rx_buf_sz - state.residue;
1458 unsigned int size;
1459
1460 /* Return available bytes */
1461 if (i >= adc->bufi)
1462 size = i - adc->bufi;
1463 else
1464 size = adc->rx_buf_sz + i - adc->bufi;
1465
1466 return size;
1467 }
1468
1469 return 0;
1470}
1471
1472static void stm32_adc_dma_buffer_done(void *data)
1473{
1474 struct iio_dev *indio_dev = data;
Olivier Deprez0e641232021-09-23 10:07:05 +02001475 struct stm32_adc *adc = iio_priv(indio_dev);
1476 int residue = stm32_adc_dma_residue(adc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001477
Olivier Deprez0e641232021-09-23 10:07:05 +02001478 /*
1479 * In DMA mode the trigger services of IIO are not used
1480 * (e.g. no call to iio_trigger_poll).
1481 * Calling irq handler associated to the hardware trigger is not
1482 * relevant as the conversions have already been done. Data
1483 * transfers are performed directly in DMA callback instead.
1484 * This implementation avoids to call trigger irq handler that
1485 * may sleep, in an atomic context (DMA irq handler context).
1486 */
1487 dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi);
1488
1489 while (residue >= indio_dev->scan_bytes) {
1490 u16 *buffer = (u16 *)&adc->rx_buf[adc->bufi];
1491
1492 iio_push_to_buffers(indio_dev, buffer);
1493
1494 residue -= indio_dev->scan_bytes;
1495 adc->bufi += indio_dev->scan_bytes;
1496 if (adc->bufi >= adc->rx_buf_sz)
1497 adc->bufi = 0;
1498 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001499}
1500
1501static int stm32_adc_dma_start(struct iio_dev *indio_dev)
1502{
1503 struct stm32_adc *adc = iio_priv(indio_dev);
1504 struct dma_async_tx_descriptor *desc;
1505 dma_cookie_t cookie;
1506 int ret;
1507
1508 if (!adc->dma_chan)
1509 return 0;
1510
1511 dev_dbg(&indio_dev->dev, "%s size=%d watermark=%d\n", __func__,
1512 adc->rx_buf_sz, adc->rx_buf_sz / 2);
1513
1514 /* Prepare a DMA cyclic transaction */
1515 desc = dmaengine_prep_dma_cyclic(adc->dma_chan,
1516 adc->rx_dma_buf,
1517 adc->rx_buf_sz, adc->rx_buf_sz / 2,
1518 DMA_DEV_TO_MEM,
1519 DMA_PREP_INTERRUPT);
1520 if (!desc)
1521 return -EBUSY;
1522
1523 desc->callback = stm32_adc_dma_buffer_done;
1524 desc->callback_param = indio_dev;
1525
1526 cookie = dmaengine_submit(desc);
1527 ret = dma_submit_error(cookie);
1528 if (ret) {
David Brazdil0f672f62019-12-10 10:32:29 +00001529 dmaengine_terminate_sync(adc->dma_chan);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001530 return ret;
1531 }
1532
1533 /* Issue pending DMA requests */
1534 dma_async_issue_pending(adc->dma_chan);
1535
1536 return 0;
1537}
1538
Olivier Deprez157378f2022-04-04 15:47:50 +02001539static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001540{
1541 struct stm32_adc *adc = iio_priv(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +00001542 struct device *dev = indio_dev->dev.parent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001543 int ret;
1544
David Brazdil0f672f62019-12-10 10:32:29 +00001545 ret = pm_runtime_get_sync(dev);
1546 if (ret < 0) {
1547 pm_runtime_put_noidle(dev);
1548 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001549 }
1550
1551 ret = stm32_adc_set_trig(indio_dev, indio_dev->trig);
1552 if (ret) {
1553 dev_err(&indio_dev->dev, "Can't set trigger\n");
David Brazdil0f672f62019-12-10 10:32:29 +00001554 goto err_pm_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001555 }
1556
1557 ret = stm32_adc_dma_start(indio_dev);
1558 if (ret) {
1559 dev_err(&indio_dev->dev, "Can't start dma\n");
1560 goto err_clr_trig;
1561 }
1562
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001563 /* Reset adc buffer index */
1564 adc->bufi = 0;
1565
Olivier Deprez157378f2022-04-04 15:47:50 +02001566 stm32_adc_ovr_irq_enable(adc);
1567
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001568 if (!adc->dma_chan)
1569 stm32_adc_conv_irq_enable(adc);
1570
Olivier Deprez157378f2022-04-04 15:47:50 +02001571 adc->cfg->start_conv(indio_dev, !!adc->dma_chan);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001572
1573 return 0;
1574
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001575err_clr_trig:
1576 stm32_adc_set_trig(indio_dev, NULL);
David Brazdil0f672f62019-12-10 10:32:29 +00001577err_pm_put:
1578 pm_runtime_mark_last_busy(dev);
1579 pm_runtime_put_autosuspend(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001580
1581 return ret;
1582}
1583
Olivier Deprez157378f2022-04-04 15:47:50 +02001584static int stm32_adc_buffer_predisable(struct iio_dev *indio_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001585{
1586 struct stm32_adc *adc = iio_priv(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +00001587 struct device *dev = indio_dev->dev.parent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001588
Olivier Deprez157378f2022-04-04 15:47:50 +02001589 adc->cfg->stop_conv(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001590 if (!adc->dma_chan)
1591 stm32_adc_conv_irq_disable(adc);
1592
Olivier Deprez157378f2022-04-04 15:47:50 +02001593 stm32_adc_ovr_irq_disable(adc);
1594
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001595 if (adc->dma_chan)
David Brazdil0f672f62019-12-10 10:32:29 +00001596 dmaengine_terminate_sync(adc->dma_chan);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001597
1598 if (stm32_adc_set_trig(indio_dev, NULL))
1599 dev_err(&indio_dev->dev, "Can't clear trigger\n");
1600
David Brazdil0f672f62019-12-10 10:32:29 +00001601 pm_runtime_mark_last_busy(dev);
1602 pm_runtime_put_autosuspend(dev);
David Brazdil0f672f62019-12-10 10:32:29 +00001603
Olivier Deprez157378f2022-04-04 15:47:50 +02001604 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001605}
1606
1607static const struct iio_buffer_setup_ops stm32_adc_buffer_setup_ops = {
1608 .postenable = &stm32_adc_buffer_postenable,
1609 .predisable = &stm32_adc_buffer_predisable,
1610};
1611
1612static irqreturn_t stm32_adc_trigger_handler(int irq, void *p)
1613{
1614 struct iio_poll_func *pf = p;
1615 struct iio_dev *indio_dev = pf->indio_dev;
1616 struct stm32_adc *adc = iio_priv(indio_dev);
1617
1618 dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi);
1619
1620 if (!adc->dma_chan) {
1621 /* reset buffer index */
1622 adc->bufi = 0;
1623 iio_push_to_buffers_with_timestamp(indio_dev, adc->buffer,
1624 pf->timestamp);
1625 } else {
1626 int residue = stm32_adc_dma_residue(adc);
1627
1628 while (residue >= indio_dev->scan_bytes) {
1629 u16 *buffer = (u16 *)&adc->rx_buf[adc->bufi];
1630
1631 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
1632 pf->timestamp);
1633 residue -= indio_dev->scan_bytes;
1634 adc->bufi += indio_dev->scan_bytes;
1635 if (adc->bufi >= adc->rx_buf_sz)
1636 adc->bufi = 0;
1637 }
1638 }
1639
1640 iio_trigger_notify_done(indio_dev->trig);
1641
1642 /* re-enable eoc irq */
1643 if (!adc->dma_chan)
1644 stm32_adc_conv_irq_enable(adc);
1645
1646 return IRQ_HANDLED;
1647}
1648
1649static const struct iio_chan_spec_ext_info stm32_adc_ext_info[] = {
1650 IIO_ENUM("trigger_polarity", IIO_SHARED_BY_ALL, &stm32_adc_trig_pol),
1651 {
1652 .name = "trigger_polarity_available",
1653 .shared = IIO_SHARED_BY_ALL,
1654 .read = iio_enum_available_read,
1655 .private = (uintptr_t)&stm32_adc_trig_pol,
1656 },
1657 {},
1658};
1659
1660static int stm32_adc_of_get_resolution(struct iio_dev *indio_dev)
1661{
1662 struct device_node *node = indio_dev->dev.of_node;
1663 struct stm32_adc *adc = iio_priv(indio_dev);
1664 unsigned int i;
1665 u32 res;
1666
1667 if (of_property_read_u32(node, "assigned-resolution-bits", &res))
1668 res = adc->cfg->adc_info->resolutions[0];
1669
1670 for (i = 0; i < adc->cfg->adc_info->num_res; i++)
1671 if (res == adc->cfg->adc_info->resolutions[i])
1672 break;
1673 if (i >= adc->cfg->adc_info->num_res) {
1674 dev_err(&indio_dev->dev, "Bad resolution: %u bits\n", res);
1675 return -EINVAL;
1676 }
1677
1678 dev_dbg(&indio_dev->dev, "Using %u bits resolution\n", res);
1679 adc->res = i;
1680
1681 return 0;
1682}
1683
1684static void stm32_adc_smpr_init(struct stm32_adc *adc, int channel, u32 smp_ns)
1685{
1686 const struct stm32_adc_regs *smpr = &adc->cfg->regs->smp_bits[channel];
1687 u32 period_ns, shift = smpr->shift, mask = smpr->mask;
1688 unsigned int smp, r = smpr->reg;
1689
1690 /* Determine sampling time (ADC clock cycles) */
1691 period_ns = NSEC_PER_SEC / adc->common->rate;
1692 for (smp = 0; smp <= STM32_ADC_MAX_SMP; smp++)
1693 if ((period_ns * adc->cfg->smp_cycles[smp]) >= smp_ns)
1694 break;
1695 if (smp > STM32_ADC_MAX_SMP)
1696 smp = STM32_ADC_MAX_SMP;
1697
1698 /* pre-build sampling time registers (e.g. smpr1, smpr2) */
1699 adc->smpr_val[r] = (adc->smpr_val[r] & ~mask) | (smp << shift);
1700}
1701
1702static void stm32_adc_chan_init_one(struct iio_dev *indio_dev,
1703 struct iio_chan_spec *chan, u32 vinp,
1704 u32 vinn, int scan_index, bool differential)
1705{
1706 struct stm32_adc *adc = iio_priv(indio_dev);
1707 char *name = adc->chan_name[vinp];
1708
1709 chan->type = IIO_VOLTAGE;
1710 chan->channel = vinp;
1711 if (differential) {
1712 chan->differential = 1;
1713 chan->channel2 = vinn;
1714 snprintf(name, STM32_ADC_CH_SZ, "in%d-in%d", vinp, vinn);
1715 } else {
1716 snprintf(name, STM32_ADC_CH_SZ, "in%d", vinp);
1717 }
1718 chan->datasheet_name = name;
1719 chan->scan_index = scan_index;
1720 chan->indexed = 1;
1721 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
1722 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
1723 BIT(IIO_CHAN_INFO_OFFSET);
1724 chan->scan_type.sign = 'u';
1725 chan->scan_type.realbits = adc->cfg->adc_info->resolutions[adc->res];
1726 chan->scan_type.storagebits = 16;
1727 chan->ext_info = stm32_adc_ext_info;
1728
1729 /* pre-build selected channels mask */
1730 adc->pcsel |= BIT(chan->channel);
1731 if (differential) {
1732 /* pre-build diff channels mask */
1733 adc->difsel |= BIT(chan->channel);
1734 /* Also add negative input to pre-selected channels */
1735 adc->pcsel |= BIT(chan->channel2);
1736 }
1737}
1738
1739static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
1740{
1741 struct device_node *node = indio_dev->dev.of_node;
1742 struct stm32_adc *adc = iio_priv(indio_dev);
1743 const struct stm32_adc_info *adc_info = adc->cfg->adc_info;
1744 struct stm32_adc_diff_channel diff[STM32_ADC_CH_MAX];
1745 struct property *prop;
1746 const __be32 *cur;
1747 struct iio_chan_spec *channels;
1748 int scan_index = 0, num_channels = 0, num_diff = 0, ret, i;
1749 u32 val, smp = 0;
1750
1751 ret = of_property_count_u32_elems(node, "st,adc-channels");
1752 if (ret > adc_info->max_channels) {
1753 dev_err(&indio_dev->dev, "Bad st,adc-channels?\n");
1754 return -EINVAL;
1755 } else if (ret > 0) {
1756 num_channels += ret;
1757 }
1758
1759 ret = of_property_count_elems_of_size(node, "st,adc-diff-channels",
1760 sizeof(*diff));
1761 if (ret > adc_info->max_channels) {
1762 dev_err(&indio_dev->dev, "Bad st,adc-diff-channels?\n");
1763 return -EINVAL;
1764 } else if (ret > 0) {
1765 int size = ret * sizeof(*diff) / sizeof(u32);
1766
1767 num_diff = ret;
1768 num_channels += ret;
1769 ret = of_property_read_u32_array(node, "st,adc-diff-channels",
1770 (u32 *)diff, size);
1771 if (ret)
1772 return ret;
1773 }
1774
1775 if (!num_channels) {
1776 dev_err(&indio_dev->dev, "No channels configured\n");
1777 return -ENODATA;
1778 }
1779
1780 /* Optional sample time is provided either for each, or all channels */
1781 ret = of_property_count_u32_elems(node, "st,min-sample-time-nsecs");
1782 if (ret > 1 && ret != num_channels) {
1783 dev_err(&indio_dev->dev, "Invalid st,min-sample-time-nsecs\n");
1784 return -EINVAL;
1785 }
1786
1787 channels = devm_kcalloc(&indio_dev->dev, num_channels,
1788 sizeof(struct iio_chan_spec), GFP_KERNEL);
1789 if (!channels)
1790 return -ENOMEM;
1791
1792 of_property_for_each_u32(node, "st,adc-channels", prop, cur, val) {
1793 if (val >= adc_info->max_channels) {
1794 dev_err(&indio_dev->dev, "Invalid channel %d\n", val);
1795 return -EINVAL;
1796 }
1797
1798 /* Channel can't be configured both as single-ended & diff */
1799 for (i = 0; i < num_diff; i++) {
1800 if (val == diff[i].vinp) {
1801 dev_err(&indio_dev->dev,
1802 "channel %d miss-configured\n", val);
1803 return -EINVAL;
1804 }
1805 }
1806 stm32_adc_chan_init_one(indio_dev, &channels[scan_index], val,
1807 0, scan_index, false);
1808 scan_index++;
1809 }
1810
1811 for (i = 0; i < num_diff; i++) {
1812 if (diff[i].vinp >= adc_info->max_channels ||
1813 diff[i].vinn >= adc_info->max_channels) {
1814 dev_err(&indio_dev->dev, "Invalid channel in%d-in%d\n",
1815 diff[i].vinp, diff[i].vinn);
1816 return -EINVAL;
1817 }
1818 stm32_adc_chan_init_one(indio_dev, &channels[scan_index],
1819 diff[i].vinp, diff[i].vinn, scan_index,
1820 true);
1821 scan_index++;
1822 }
1823
1824 for (i = 0; i < scan_index; i++) {
1825 /*
1826 * Using of_property_read_u32_index(), smp value will only be
1827 * modified if valid u32 value can be decoded. This allows to
1828 * get either no value, 1 shared value for all indexes, or one
1829 * value per channel.
1830 */
1831 of_property_read_u32_index(node, "st,min-sample-time-nsecs",
1832 i, &smp);
1833 /* Prepare sampling time settings */
1834 stm32_adc_smpr_init(adc, channels[i].channel, smp);
1835 }
1836
1837 indio_dev->num_channels = scan_index;
1838 indio_dev->channels = channels;
1839
1840 return 0;
1841}
1842
Olivier Deprez0e641232021-09-23 10:07:05 +02001843static int stm32_adc_dma_request(struct device *dev, struct iio_dev *indio_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001844{
1845 struct stm32_adc *adc = iio_priv(indio_dev);
1846 struct dma_slave_config config;
1847 int ret;
1848
Olivier Deprez0e641232021-09-23 10:07:05 +02001849 adc->dma_chan = dma_request_chan(dev, "rx");
1850 if (IS_ERR(adc->dma_chan)) {
1851 ret = PTR_ERR(adc->dma_chan);
Olivier Deprez157378f2022-04-04 15:47:50 +02001852 if (ret != -ENODEV)
1853 return dev_err_probe(dev, ret,
1854 "DMA channel request failed with\n");
Olivier Deprez0e641232021-09-23 10:07:05 +02001855
1856 /* DMA is optional: fall back to IRQ mode */
1857 adc->dma_chan = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001858 return 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02001859 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001860
1861 adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev,
1862 STM32_DMA_BUFFER_SIZE,
1863 &adc->rx_dma_buf, GFP_KERNEL);
1864 if (!adc->rx_buf) {
1865 ret = -ENOMEM;
1866 goto err_release;
1867 }
1868
1869 /* Configure DMA channel to read data register */
1870 memset(&config, 0, sizeof(config));
1871 config.src_addr = (dma_addr_t)adc->common->phys_base;
1872 config.src_addr += adc->offset + adc->cfg->regs->dr;
1873 config.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1874
1875 ret = dmaengine_slave_config(adc->dma_chan, &config);
1876 if (ret)
1877 goto err_free;
1878
1879 return 0;
1880
1881err_free:
1882 dma_free_coherent(adc->dma_chan->device->dev, STM32_DMA_BUFFER_SIZE,
1883 adc->rx_buf, adc->rx_dma_buf);
1884err_release:
1885 dma_release_channel(adc->dma_chan);
1886
1887 return ret;
1888}
1889
1890static int stm32_adc_probe(struct platform_device *pdev)
1891{
1892 struct iio_dev *indio_dev;
1893 struct device *dev = &pdev->dev;
Olivier Deprez0e641232021-09-23 10:07:05 +02001894 irqreturn_t (*handler)(int irq, void *p) = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001895 struct stm32_adc *adc;
1896 int ret;
1897
1898 if (!pdev->dev.of_node)
1899 return -ENODEV;
1900
1901 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
1902 if (!indio_dev)
1903 return -ENOMEM;
1904
1905 adc = iio_priv(indio_dev);
1906 adc->common = dev_get_drvdata(pdev->dev.parent);
1907 spin_lock_init(&adc->lock);
1908 init_completion(&adc->completion);
1909 adc->cfg = (const struct stm32_adc_cfg *)
1910 of_match_device(dev->driver->of_match_table, dev)->data;
1911
1912 indio_dev->name = dev_name(&pdev->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001913 indio_dev->dev.of_node = pdev->dev.of_node;
1914 indio_dev->info = &stm32_adc_iio_info;
1915 indio_dev->modes = INDIO_DIRECT_MODE | INDIO_HARDWARE_TRIGGERED;
1916
Olivier Deprez157378f2022-04-04 15:47:50 +02001917 platform_set_drvdata(pdev, indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001918
1919 ret = of_property_read_u32(pdev->dev.of_node, "reg", &adc->offset);
1920 if (ret != 0) {
1921 dev_err(&pdev->dev, "missing reg property\n");
1922 return -EINVAL;
1923 }
1924
1925 adc->irq = platform_get_irq(pdev, 0);
David Brazdil0f672f62019-12-10 10:32:29 +00001926 if (adc->irq < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001927 return adc->irq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001928
Olivier Deprez157378f2022-04-04 15:47:50 +02001929 ret = devm_request_threaded_irq(&pdev->dev, adc->irq, stm32_adc_isr,
1930 stm32_adc_threaded_isr,
1931 0, pdev->name, indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001932 if (ret) {
1933 dev_err(&pdev->dev, "failed to request IRQ\n");
1934 return ret;
1935 }
1936
1937 adc->clk = devm_clk_get(&pdev->dev, NULL);
1938 if (IS_ERR(adc->clk)) {
1939 ret = PTR_ERR(adc->clk);
1940 if (ret == -ENOENT && !adc->cfg->clk_required) {
1941 adc->clk = NULL;
1942 } else {
1943 dev_err(&pdev->dev, "Can't get clock\n");
1944 return ret;
1945 }
1946 }
1947
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001948 ret = stm32_adc_of_get_resolution(indio_dev);
1949 if (ret < 0)
David Brazdil0f672f62019-12-10 10:32:29 +00001950 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001951
1952 ret = stm32_adc_chan_of_init(indio_dev);
1953 if (ret < 0)
David Brazdil0f672f62019-12-10 10:32:29 +00001954 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001955
Olivier Deprez0e641232021-09-23 10:07:05 +02001956 ret = stm32_adc_dma_request(dev, indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001957 if (ret < 0)
David Brazdil0f672f62019-12-10 10:32:29 +00001958 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001959
Olivier Deprez0e641232021-09-23 10:07:05 +02001960 if (!adc->dma_chan)
1961 handler = &stm32_adc_trigger_handler;
1962
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001963 ret = iio_triggered_buffer_setup(indio_dev,
Olivier Deprez0e641232021-09-23 10:07:05 +02001964 &iio_pollfunc_store_time, handler,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001965 &stm32_adc_buffer_setup_ops);
1966 if (ret) {
1967 dev_err(&pdev->dev, "buffer setup failed\n");
1968 goto err_dma_disable;
1969 }
1970
David Brazdil0f672f62019-12-10 10:32:29 +00001971 /* Get stm32-adc-core PM online */
1972 pm_runtime_get_noresume(dev);
1973 pm_runtime_set_active(dev);
1974 pm_runtime_set_autosuspend_delay(dev, STM32_ADC_HW_STOP_DELAY_MS);
1975 pm_runtime_use_autosuspend(dev);
1976 pm_runtime_enable(dev);
1977
1978 ret = stm32_adc_hw_start(dev);
1979 if (ret)
1980 goto err_buffer_cleanup;
1981
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001982 ret = iio_device_register(indio_dev);
1983 if (ret) {
1984 dev_err(&pdev->dev, "iio dev register failed\n");
David Brazdil0f672f62019-12-10 10:32:29 +00001985 goto err_hw_stop;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001986 }
1987
David Brazdil0f672f62019-12-10 10:32:29 +00001988 pm_runtime_mark_last_busy(dev);
1989 pm_runtime_put_autosuspend(dev);
1990
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001991 return 0;
1992
David Brazdil0f672f62019-12-10 10:32:29 +00001993err_hw_stop:
1994 stm32_adc_hw_stop(dev);
1995
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001996err_buffer_cleanup:
David Brazdil0f672f62019-12-10 10:32:29 +00001997 pm_runtime_disable(dev);
1998 pm_runtime_set_suspended(dev);
1999 pm_runtime_put_noidle(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002000 iio_triggered_buffer_cleanup(indio_dev);
2001
2002err_dma_disable:
2003 if (adc->dma_chan) {
2004 dma_free_coherent(adc->dma_chan->device->dev,
2005 STM32_DMA_BUFFER_SIZE,
2006 adc->rx_buf, adc->rx_dma_buf);
2007 dma_release_channel(adc->dma_chan);
2008 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002009
2010 return ret;
2011}
2012
2013static int stm32_adc_remove(struct platform_device *pdev)
2014{
Olivier Deprez157378f2022-04-04 15:47:50 +02002015 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
2016 struct stm32_adc *adc = iio_priv(indio_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002017
David Brazdil0f672f62019-12-10 10:32:29 +00002018 pm_runtime_get_sync(&pdev->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002019 iio_device_unregister(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +00002020 stm32_adc_hw_stop(&pdev->dev);
2021 pm_runtime_disable(&pdev->dev);
2022 pm_runtime_set_suspended(&pdev->dev);
2023 pm_runtime_put_noidle(&pdev->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002024 iio_triggered_buffer_cleanup(indio_dev);
2025 if (adc->dma_chan) {
2026 dma_free_coherent(adc->dma_chan->device->dev,
2027 STM32_DMA_BUFFER_SIZE,
2028 adc->rx_buf, adc->rx_dma_buf);
2029 dma_release_channel(adc->dma_chan);
2030 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002031
2032 return 0;
2033}
2034
David Brazdil0f672f62019-12-10 10:32:29 +00002035#if defined(CONFIG_PM_SLEEP)
2036static int stm32_adc_suspend(struct device *dev)
2037{
Olivier Deprez157378f2022-04-04 15:47:50 +02002038 struct iio_dev *indio_dev = dev_get_drvdata(dev);
David Brazdil0f672f62019-12-10 10:32:29 +00002039
2040 if (iio_buffer_enabled(indio_dev))
Olivier Deprez157378f2022-04-04 15:47:50 +02002041 stm32_adc_buffer_predisable(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +00002042
2043 return pm_runtime_force_suspend(dev);
2044}
2045
2046static int stm32_adc_resume(struct device *dev)
2047{
Olivier Deprez157378f2022-04-04 15:47:50 +02002048 struct iio_dev *indio_dev = dev_get_drvdata(dev);
David Brazdil0f672f62019-12-10 10:32:29 +00002049 int ret;
2050
2051 ret = pm_runtime_force_resume(dev);
2052 if (ret < 0)
2053 return ret;
2054
2055 if (!iio_buffer_enabled(indio_dev))
2056 return 0;
2057
2058 ret = stm32_adc_update_scan_mode(indio_dev,
2059 indio_dev->active_scan_mask);
2060 if (ret < 0)
2061 return ret;
2062
Olivier Deprez157378f2022-04-04 15:47:50 +02002063 return stm32_adc_buffer_postenable(indio_dev);
David Brazdil0f672f62019-12-10 10:32:29 +00002064}
2065#endif
2066
2067#if defined(CONFIG_PM)
2068static int stm32_adc_runtime_suspend(struct device *dev)
2069{
2070 return stm32_adc_hw_stop(dev);
2071}
2072
2073static int stm32_adc_runtime_resume(struct device *dev)
2074{
2075 return stm32_adc_hw_start(dev);
2076}
2077#endif
2078
2079static const struct dev_pm_ops stm32_adc_pm_ops = {
2080 SET_SYSTEM_SLEEP_PM_OPS(stm32_adc_suspend, stm32_adc_resume)
2081 SET_RUNTIME_PM_OPS(stm32_adc_runtime_suspend, stm32_adc_runtime_resume,
2082 NULL)
2083};
2084
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002085static const struct stm32_adc_cfg stm32f4_adc_cfg = {
2086 .regs = &stm32f4_adc_regspec,
2087 .adc_info = &stm32f4_adc_info,
2088 .trigs = stm32f4_adc_trigs,
2089 .clk_required = true,
2090 .start_conv = stm32f4_adc_start_conv,
2091 .stop_conv = stm32f4_adc_stop_conv,
2092 .smp_cycles = stm32f4_adc_smp_cycles,
Olivier Deprez157378f2022-04-04 15:47:50 +02002093 .irq_clear = stm32f4_adc_irq_clear,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002094};
2095
2096static const struct stm32_adc_cfg stm32h7_adc_cfg = {
2097 .regs = &stm32h7_adc_regspec,
2098 .adc_info = &stm32h7_adc_info,
2099 .trigs = stm32h7_adc_trigs,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002100 .start_conv = stm32h7_adc_start_conv,
2101 .stop_conv = stm32h7_adc_stop_conv,
2102 .prepare = stm32h7_adc_prepare,
2103 .unprepare = stm32h7_adc_unprepare,
2104 .smp_cycles = stm32h7_adc_smp_cycles,
Olivier Deprez157378f2022-04-04 15:47:50 +02002105 .irq_clear = stm32h7_adc_irq_clear,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002106};
2107
2108static const struct stm32_adc_cfg stm32mp1_adc_cfg = {
2109 .regs = &stm32h7_adc_regspec,
2110 .adc_info = &stm32h7_adc_info,
2111 .trigs = stm32h7_adc_trigs,
2112 .has_vregready = true,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002113 .start_conv = stm32h7_adc_start_conv,
2114 .stop_conv = stm32h7_adc_stop_conv,
2115 .prepare = stm32h7_adc_prepare,
2116 .unprepare = stm32h7_adc_unprepare,
2117 .smp_cycles = stm32h7_adc_smp_cycles,
Olivier Deprez157378f2022-04-04 15:47:50 +02002118 .irq_clear = stm32h7_adc_irq_clear,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002119};
2120
2121static const struct of_device_id stm32_adc_of_match[] = {
2122 { .compatible = "st,stm32f4-adc", .data = (void *)&stm32f4_adc_cfg },
2123 { .compatible = "st,stm32h7-adc", .data = (void *)&stm32h7_adc_cfg },
2124 { .compatible = "st,stm32mp1-adc", .data = (void *)&stm32mp1_adc_cfg },
2125 {},
2126};
2127MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
2128
2129static struct platform_driver stm32_adc_driver = {
2130 .probe = stm32_adc_probe,
2131 .remove = stm32_adc_remove,
2132 .driver = {
2133 .name = "stm32-adc",
2134 .of_match_table = stm32_adc_of_match,
David Brazdil0f672f62019-12-10 10:32:29 +00002135 .pm = &stm32_adc_pm_ops,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002136 },
2137};
2138module_platform_driver(stm32_adc_driver);
2139
2140MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
2141MODULE_DESCRIPTION("STMicroelectronics STM32 ADC IIO driver");
2142MODULE_LICENSE("GPL v2");
2143MODULE_ALIAS("platform:stm32-adc");