16e93e261SBenjamin Gaignard // SPDX-License-Identifier: GPL-2.0 20f883b22SFabrice Gasnier /* 30f883b22SFabrice Gasnier * This file is part of STM32 ADC driver 40f883b22SFabrice Gasnier * 50f883b22SFabrice Gasnier * Copyright (C) 2016, STMicroelectronics - All Rights Reserved 60f883b22SFabrice Gasnier * Author: Fabrice Gasnier <fabrice.gasnier@st.com>. 70f883b22SFabrice Gasnier */ 80f883b22SFabrice Gasnier 90f883b22SFabrice Gasnier #include <linux/clk.h> 100f883b22SFabrice Gasnier #include <linux/delay.h> 112763ea05SFabrice Gasnier #include <linux/dma-mapping.h> 122763ea05SFabrice Gasnier #include <linux/dmaengine.h> 130f883b22SFabrice Gasnier #include <linux/iio/iio.h> 14da9b9485SFabrice Gasnier #include <linux/iio/buffer.h> 15f0b638a7SFabrice Gasnier #include <linux/iio/timer/stm32-lptim-trigger.h> 16f24a33b3SFabrice Gasnier #include <linux/iio/timer/stm32-timer-trigger.h> 17da9b9485SFabrice Gasnier #include <linux/iio/trigger.h> 18da9b9485SFabrice Gasnier #include <linux/iio/trigger_consumer.h> 19da9b9485SFabrice Gasnier #include <linux/iio/triggered_buffer.h> 200f883b22SFabrice Gasnier #include <linux/interrupt.h> 210f883b22SFabrice Gasnier #include <linux/io.h> 2295e339b6SFabrice Gasnier #include <linux/iopoll.h> 230f883b22SFabrice Gasnier #include <linux/module.h> 240f883b22SFabrice Gasnier #include <linux/platform_device.h> 259bdbb113SFabrice Gasnier #include <linux/pm_runtime.h> 260f883b22SFabrice Gasnier #include <linux/of.h> 2764ad7f64SFabrice Gasnier #include <linux/of_device.h> 280f883b22SFabrice Gasnier 290f883b22SFabrice Gasnier #include "stm32-adc-core.h" 300f883b22SFabrice Gasnier 3195e339b6SFabrice Gasnier /* Number of linear calibration shadow registers / LINCALRDYW control bits */ 3295e339b6SFabrice Gasnier #define STM32H7_LINCALFACT_NUM 6 3395e339b6SFabrice Gasnier 3495e339b6SFabrice Gasnier /* BOOST bit must be set on STM32H7 when ADC clock is above 20MHz */ 3595e339b6SFabrice Gasnier #define STM32H7_BOOST_CLKRATE 20000000UL 3695e339b6SFabrice Gasnier 370bae72aaSFabrice Gasnier #define STM32_ADC_CH_MAX 20 /* max number of channels */ 383fb2e24eSFabrice Gasnier #define STM32_ADC_CH_SZ 10 /* max channel name size */ 39da9b9485SFabrice Gasnier #define STM32_ADC_MAX_SQ 16 /* SQ1..SQ16 */ 40ee2ac1cdSFabrice Gasnier #define STM32_ADC_MAX_SMP 7 /* SMPx range is [0..7] */ 410f883b22SFabrice Gasnier #define STM32_ADC_TIMEOUT_US 100000 420f883b22SFabrice Gasnier #define STM32_ADC_TIMEOUT (msecs_to_jiffies(STM32_ADC_TIMEOUT_US / 1000)) 439bdbb113SFabrice Gasnier #define STM32_ADC_HW_STOP_DELAY_MS 100 440f883b22SFabrice Gasnier 452763ea05SFabrice Gasnier #define STM32_DMA_BUFFER_SIZE PAGE_SIZE 462763ea05SFabrice Gasnier 47da9b9485SFabrice Gasnier /* External trigger enable */ 48da9b9485SFabrice Gasnier enum stm32_adc_exten { 49da9b9485SFabrice Gasnier STM32_EXTEN_SWTRIG, 50da9b9485SFabrice Gasnier STM32_EXTEN_HWTRIG_RISING_EDGE, 51da9b9485SFabrice Gasnier STM32_EXTEN_HWTRIG_FALLING_EDGE, 52da9b9485SFabrice Gasnier STM32_EXTEN_HWTRIG_BOTH_EDGES, 53da9b9485SFabrice Gasnier }; 54da9b9485SFabrice Gasnier 55f24a33b3SFabrice Gasnier /* extsel - trigger mux selection value */ 56f24a33b3SFabrice Gasnier enum stm32_adc_extsel { 57f24a33b3SFabrice Gasnier STM32_EXT0, 58f24a33b3SFabrice Gasnier STM32_EXT1, 59f24a33b3SFabrice Gasnier STM32_EXT2, 60f24a33b3SFabrice Gasnier STM32_EXT3, 61f24a33b3SFabrice Gasnier STM32_EXT4, 62f24a33b3SFabrice Gasnier STM32_EXT5, 63f24a33b3SFabrice Gasnier STM32_EXT6, 64f24a33b3SFabrice Gasnier STM32_EXT7, 65f24a33b3SFabrice Gasnier STM32_EXT8, 66f24a33b3SFabrice Gasnier STM32_EXT9, 67f24a33b3SFabrice Gasnier STM32_EXT10, 68f24a33b3SFabrice Gasnier STM32_EXT11, 69f24a33b3SFabrice Gasnier STM32_EXT12, 70f24a33b3SFabrice Gasnier STM32_EXT13, 71f24a33b3SFabrice Gasnier STM32_EXT14, 72f24a33b3SFabrice Gasnier STM32_EXT15, 73f0b638a7SFabrice Gasnier STM32_EXT16, 74f0b638a7SFabrice Gasnier STM32_EXT17, 75f0b638a7SFabrice Gasnier STM32_EXT18, 76f0b638a7SFabrice Gasnier STM32_EXT19, 77f0b638a7SFabrice Gasnier STM32_EXT20, 78f24a33b3SFabrice Gasnier }; 79f24a33b3SFabrice Gasnier 80f24a33b3SFabrice Gasnier /** 81f24a33b3SFabrice Gasnier * struct stm32_adc_trig_info - ADC trigger info 82f24a33b3SFabrice Gasnier * @name: name of the trigger, corresponding to its source 83f24a33b3SFabrice Gasnier * @extsel: trigger selection 84f24a33b3SFabrice Gasnier */ 85f24a33b3SFabrice Gasnier struct stm32_adc_trig_info { 86f24a33b3SFabrice Gasnier const char *name; 87f24a33b3SFabrice Gasnier enum stm32_adc_extsel extsel; 88f24a33b3SFabrice Gasnier }; 89f24a33b3SFabrice Gasnier 90da9b9485SFabrice Gasnier /** 9195e339b6SFabrice Gasnier * struct stm32_adc_calib - optional adc calibration data 9295e339b6SFabrice Gasnier * @calfact_s: Calibration offset for single ended channels 9395e339b6SFabrice Gasnier * @calfact_d: Calibration offset in differential 9495e339b6SFabrice Gasnier * @lincalfact: Linearity calibration factor 950da98c7bSFabrice Gasnier * @calibrated: Indicates calibration status 9695e339b6SFabrice Gasnier */ 9795e339b6SFabrice Gasnier struct stm32_adc_calib { 9895e339b6SFabrice Gasnier u32 calfact_s; 9995e339b6SFabrice Gasnier u32 calfact_d; 10095e339b6SFabrice Gasnier u32 lincalfact[STM32H7_LINCALFACT_NUM]; 1010da98c7bSFabrice Gasnier bool calibrated; 10295e339b6SFabrice Gasnier }; 10395e339b6SFabrice Gasnier 10495e339b6SFabrice Gasnier /** 1051cd92d42SFabrice Gasnier * struct stm32_adc_regs - stm32 ADC misc registers & bitfield desc 106da9b9485SFabrice Gasnier * @reg: register offset 107da9b9485SFabrice Gasnier * @mask: bitfield mask 108da9b9485SFabrice Gasnier * @shift: left shift 109da9b9485SFabrice Gasnier */ 110da9b9485SFabrice Gasnier struct stm32_adc_regs { 111da9b9485SFabrice Gasnier int reg; 112da9b9485SFabrice Gasnier int mask; 113da9b9485SFabrice Gasnier int shift; 114da9b9485SFabrice Gasnier }; 115da9b9485SFabrice Gasnier 1160f883b22SFabrice Gasnier /** 1171cd92d42SFabrice Gasnier * struct stm32_adc_regspec - stm32 registers definition 11864ad7f64SFabrice Gasnier * @dr: data register offset 11964ad7f64SFabrice Gasnier * @ier_eoc: interrupt enable register & eocie bitfield 120cc06e67dSFabrice Gasnier * @ier_ovr: interrupt enable register & overrun bitfield 12164ad7f64SFabrice Gasnier * @isr_eoc: interrupt status register & eoc bitfield 122cc06e67dSFabrice Gasnier * @isr_ovr: interrupt status register & overrun bitfield 12364ad7f64SFabrice Gasnier * @sqr: reference to sequence registers array 12464ad7f64SFabrice Gasnier * @exten: trigger control register & bitfield 12564ad7f64SFabrice Gasnier * @extsel: trigger selection register & bitfield 12664ad7f64SFabrice Gasnier * @res: resolution selection register & bitfield 127ee2ac1cdSFabrice Gasnier * @smpr: smpr1 & smpr2 registers offset array 128ee2ac1cdSFabrice Gasnier * @smp_bits: smpr1 & smpr2 index and bitfields 12964ad7f64SFabrice Gasnier */ 13064ad7f64SFabrice Gasnier struct stm32_adc_regspec { 13164ad7f64SFabrice Gasnier const u32 dr; 13264ad7f64SFabrice Gasnier const struct stm32_adc_regs ier_eoc; 133cc06e67dSFabrice Gasnier const struct stm32_adc_regs ier_ovr; 13464ad7f64SFabrice Gasnier const struct stm32_adc_regs isr_eoc; 135cc06e67dSFabrice Gasnier const struct stm32_adc_regs isr_ovr; 13664ad7f64SFabrice Gasnier const struct stm32_adc_regs *sqr; 13764ad7f64SFabrice Gasnier const struct stm32_adc_regs exten; 13864ad7f64SFabrice Gasnier const struct stm32_adc_regs extsel; 13964ad7f64SFabrice Gasnier const struct stm32_adc_regs res; 140ee2ac1cdSFabrice Gasnier const u32 smpr[2]; 141ee2ac1cdSFabrice Gasnier const struct stm32_adc_regs *smp_bits; 14264ad7f64SFabrice Gasnier }; 14364ad7f64SFabrice Gasnier 14464ad7f64SFabrice Gasnier struct stm32_adc; 14564ad7f64SFabrice Gasnier 14664ad7f64SFabrice Gasnier /** 1471cd92d42SFabrice Gasnier * struct stm32_adc_cfg - stm32 compatible configuration data 14864ad7f64SFabrice Gasnier * @regs: registers descriptions 14964ad7f64SFabrice Gasnier * @adc_info: per instance input channels definitions 15064ad7f64SFabrice Gasnier * @trigs: external trigger sources 151204a6a25SFabrice Gasnier * @clk_required: clock is required 152d58c67d1SFabrice Gasnier * @has_vregready: vregready status flag presence 15395e339b6SFabrice Gasnier * @prepare: optional prepare routine (power-up, enable) 15464ad7f64SFabrice Gasnier * @start_conv: routine to start conversions 15564ad7f64SFabrice Gasnier * @stop_conv: routine to stop conversions 15695e339b6SFabrice Gasnier * @unprepare: optional unprepare routine (disable, power-down) 157ee2ac1cdSFabrice Gasnier * @smp_cycles: programmable sampling time (ADC clock cycles) 15864ad7f64SFabrice Gasnier */ 15964ad7f64SFabrice Gasnier struct stm32_adc_cfg { 16064ad7f64SFabrice Gasnier const struct stm32_adc_regspec *regs; 16164ad7f64SFabrice Gasnier const struct stm32_adc_info *adc_info; 16264ad7f64SFabrice Gasnier struct stm32_adc_trig_info *trigs; 163204a6a25SFabrice Gasnier bool clk_required; 164d58c67d1SFabrice Gasnier bool has_vregready; 165cd64d357SAlexandru Ardelean int (*prepare)(struct iio_dev *); 166cd64d357SAlexandru Ardelean void (*start_conv)(struct iio_dev *, bool dma); 167cd64d357SAlexandru Ardelean void (*stop_conv)(struct iio_dev *); 168cd64d357SAlexandru Ardelean void (*unprepare)(struct iio_dev *); 169ee2ac1cdSFabrice Gasnier const unsigned int *smp_cycles; 17064ad7f64SFabrice Gasnier }; 17164ad7f64SFabrice Gasnier 17264ad7f64SFabrice Gasnier /** 1730f883b22SFabrice Gasnier * struct stm32_adc - private data of each ADC IIO instance 1740f883b22SFabrice Gasnier * @common: reference to ADC block common data 1750f883b22SFabrice Gasnier * @offset: ADC instance register offset in ADC block 17664ad7f64SFabrice Gasnier * @cfg: compatible configuration data 1770f883b22SFabrice Gasnier * @completion: end of single conversion completion 1780f883b22SFabrice Gasnier * @buffer: data buffer 1790f883b22SFabrice Gasnier * @clk: clock for this adc instance 1800f883b22SFabrice Gasnier * @irq: interrupt for this adc instance 1810f883b22SFabrice Gasnier * @lock: spinlock 182da9b9485SFabrice Gasnier * @bufi: data buffer index 183da9b9485SFabrice Gasnier * @num_conv: expected number of scan conversions 18425a85bedSFabrice Gasnier * @res: data resolution (e.g. RES bitfield value) 185732f2dc4SFabrice Gasnier * @trigger_polarity: external trigger polarity (e.g. exten) 1862763ea05SFabrice Gasnier * @dma_chan: dma channel 1872763ea05SFabrice Gasnier * @rx_buf: dma rx buffer cpu address 1882763ea05SFabrice Gasnier * @rx_dma_buf: dma rx buffer bus address 1892763ea05SFabrice Gasnier * @rx_buf_sz: dma rx buffer size 1901cd92d42SFabrice Gasnier * @difsel: bitmask to set single-ended/differential channel 1911cd92d42SFabrice Gasnier * @pcsel: bitmask to preselect channels on some devices 192ee2ac1cdSFabrice Gasnier * @smpr_val: sampling time settings (e.g. smpr1 / smpr2) 19395e339b6SFabrice Gasnier * @cal: optional calibration data on some devices 1940bae72aaSFabrice Gasnier * @chan_name: channel name array 1950f883b22SFabrice Gasnier */ 1960f883b22SFabrice Gasnier struct stm32_adc { 1970f883b22SFabrice Gasnier struct stm32_adc_common *common; 1980f883b22SFabrice Gasnier u32 offset; 19964ad7f64SFabrice Gasnier const struct stm32_adc_cfg *cfg; 2000f883b22SFabrice Gasnier struct completion completion; 201da9b9485SFabrice Gasnier u16 buffer[STM32_ADC_MAX_SQ]; 2020f883b22SFabrice Gasnier struct clk *clk; 2030f883b22SFabrice Gasnier int irq; 2040f883b22SFabrice Gasnier spinlock_t lock; /* interrupt lock */ 205da9b9485SFabrice Gasnier unsigned int bufi; 206da9b9485SFabrice Gasnier unsigned int num_conv; 20725a85bedSFabrice Gasnier u32 res; 208732f2dc4SFabrice Gasnier u32 trigger_polarity; 2092763ea05SFabrice Gasnier struct dma_chan *dma_chan; 2102763ea05SFabrice Gasnier u8 *rx_buf; 2112763ea05SFabrice Gasnier dma_addr_t rx_dma_buf; 2122763ea05SFabrice Gasnier unsigned int rx_buf_sz; 2133fb2e24eSFabrice Gasnier u32 difsel; 21495e339b6SFabrice Gasnier u32 pcsel; 215ee2ac1cdSFabrice Gasnier u32 smpr_val[2]; 21695e339b6SFabrice Gasnier struct stm32_adc_calib cal; 2170bae72aaSFabrice Gasnier char chan_name[STM32_ADC_CH_MAX][STM32_ADC_CH_SZ]; 2180f883b22SFabrice Gasnier }; 2190f883b22SFabrice Gasnier 2203fb2e24eSFabrice Gasnier struct stm32_adc_diff_channel { 2213fb2e24eSFabrice Gasnier u32 vinp; 2223fb2e24eSFabrice Gasnier u32 vinn; 2233fb2e24eSFabrice Gasnier }; 2243fb2e24eSFabrice Gasnier 22564ad7f64SFabrice Gasnier /** 22664ad7f64SFabrice Gasnier * struct stm32_adc_info - stm32 ADC, per instance config data 22764ad7f64SFabrice Gasnier * @max_channels: Number of channels 22864ad7f64SFabrice Gasnier * @resolutions: available resolutions 22964ad7f64SFabrice Gasnier * @num_res: number of available resolutions 23064ad7f64SFabrice Gasnier */ 23164ad7f64SFabrice Gasnier struct stm32_adc_info { 23264ad7f64SFabrice Gasnier int max_channels; 23364ad7f64SFabrice Gasnier const unsigned int *resolutions; 23464ad7f64SFabrice Gasnier const unsigned int num_res; 23564ad7f64SFabrice Gasnier }; 23664ad7f64SFabrice Gasnier 23725a85bedSFabrice Gasnier static const unsigned int stm32f4_adc_resolutions[] = { 23825a85bedSFabrice Gasnier /* sorted values so the index matches RES[1:0] in STM32F4_ADC_CR1 */ 23925a85bedSFabrice Gasnier 12, 10, 8, 6, 24025a85bedSFabrice Gasnier }; 24125a85bedSFabrice Gasnier 2420bae72aaSFabrice Gasnier /* stm32f4 can have up to 16 channels */ 24364ad7f64SFabrice Gasnier static const struct stm32_adc_info stm32f4_adc_info = { 24464ad7f64SFabrice Gasnier .max_channels = 16, 24564ad7f64SFabrice Gasnier .resolutions = stm32f4_adc_resolutions, 24664ad7f64SFabrice Gasnier .num_res = ARRAY_SIZE(stm32f4_adc_resolutions), 24764ad7f64SFabrice Gasnier }; 24864ad7f64SFabrice Gasnier 24995e339b6SFabrice Gasnier static const unsigned int stm32h7_adc_resolutions[] = { 25095e339b6SFabrice Gasnier /* sorted values so the index matches RES[2:0] in STM32H7_ADC_CFGR */ 25195e339b6SFabrice Gasnier 16, 14, 12, 10, 8, 25295e339b6SFabrice Gasnier }; 25395e339b6SFabrice Gasnier 2540bae72aaSFabrice Gasnier /* stm32h7 can have up to 20 channels */ 25595e339b6SFabrice Gasnier static const struct stm32_adc_info stm32h7_adc_info = { 2560bae72aaSFabrice Gasnier .max_channels = STM32_ADC_CH_MAX, 25795e339b6SFabrice Gasnier .resolutions = stm32h7_adc_resolutions, 25895e339b6SFabrice Gasnier .num_res = ARRAY_SIZE(stm32h7_adc_resolutions), 25995e339b6SFabrice Gasnier }; 26095e339b6SFabrice Gasnier 2611cd92d42SFabrice Gasnier /* 262da9b9485SFabrice Gasnier * stm32f4_sq - describe regular sequence registers 263da9b9485SFabrice Gasnier * - L: sequence len (register & bit field) 264da9b9485SFabrice Gasnier * - SQ1..SQ16: sequence entries (register & bit field) 265da9b9485SFabrice Gasnier */ 266da9b9485SFabrice Gasnier static const struct stm32_adc_regs stm32f4_sq[STM32_ADC_MAX_SQ + 1] = { 267da9b9485SFabrice Gasnier /* L: len bit field description to be kept as first element */ 268da9b9485SFabrice Gasnier { STM32F4_ADC_SQR1, GENMASK(23, 20), 20 }, 269da9b9485SFabrice Gasnier /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */ 270da9b9485SFabrice Gasnier { STM32F4_ADC_SQR3, GENMASK(4, 0), 0 }, 271da9b9485SFabrice Gasnier { STM32F4_ADC_SQR3, GENMASK(9, 5), 5 }, 272da9b9485SFabrice Gasnier { STM32F4_ADC_SQR3, GENMASK(14, 10), 10 }, 273da9b9485SFabrice Gasnier { STM32F4_ADC_SQR3, GENMASK(19, 15), 15 }, 274da9b9485SFabrice Gasnier { STM32F4_ADC_SQR3, GENMASK(24, 20), 20 }, 275da9b9485SFabrice Gasnier { STM32F4_ADC_SQR3, GENMASK(29, 25), 25 }, 276da9b9485SFabrice Gasnier { STM32F4_ADC_SQR2, GENMASK(4, 0), 0 }, 277da9b9485SFabrice Gasnier { STM32F4_ADC_SQR2, GENMASK(9, 5), 5 }, 278da9b9485SFabrice Gasnier { STM32F4_ADC_SQR2, GENMASK(14, 10), 10 }, 279da9b9485SFabrice Gasnier { STM32F4_ADC_SQR2, GENMASK(19, 15), 15 }, 280da9b9485SFabrice Gasnier { STM32F4_ADC_SQR2, GENMASK(24, 20), 20 }, 281da9b9485SFabrice Gasnier { STM32F4_ADC_SQR2, GENMASK(29, 25), 25 }, 282da9b9485SFabrice Gasnier { STM32F4_ADC_SQR1, GENMASK(4, 0), 0 }, 283da9b9485SFabrice Gasnier { STM32F4_ADC_SQR1, GENMASK(9, 5), 5 }, 284da9b9485SFabrice Gasnier { STM32F4_ADC_SQR1, GENMASK(14, 10), 10 }, 285da9b9485SFabrice Gasnier { STM32F4_ADC_SQR1, GENMASK(19, 15), 15 }, 286da9b9485SFabrice Gasnier }; 287da9b9485SFabrice Gasnier 288f24a33b3SFabrice Gasnier /* STM32F4 external trigger sources for all instances */ 289f24a33b3SFabrice Gasnier static struct stm32_adc_trig_info stm32f4_adc_trigs[] = { 290f24a33b3SFabrice Gasnier { TIM1_CH1, STM32_EXT0 }, 291f24a33b3SFabrice Gasnier { TIM1_CH2, STM32_EXT1 }, 292f24a33b3SFabrice Gasnier { TIM1_CH3, STM32_EXT2 }, 293f24a33b3SFabrice Gasnier { TIM2_CH2, STM32_EXT3 }, 294f24a33b3SFabrice Gasnier { TIM2_CH3, STM32_EXT4 }, 295f24a33b3SFabrice Gasnier { TIM2_CH4, STM32_EXT5 }, 296f24a33b3SFabrice Gasnier { TIM2_TRGO, STM32_EXT6 }, 297f24a33b3SFabrice Gasnier { TIM3_CH1, STM32_EXT7 }, 298f24a33b3SFabrice Gasnier { TIM3_TRGO, STM32_EXT8 }, 299f24a33b3SFabrice Gasnier { TIM4_CH4, STM32_EXT9 }, 300f24a33b3SFabrice Gasnier { TIM5_CH1, STM32_EXT10 }, 301f24a33b3SFabrice Gasnier { TIM5_CH2, STM32_EXT11 }, 302f24a33b3SFabrice Gasnier { TIM5_CH3, STM32_EXT12 }, 303f24a33b3SFabrice Gasnier { TIM8_CH1, STM32_EXT13 }, 304f24a33b3SFabrice Gasnier { TIM8_TRGO, STM32_EXT14 }, 305f24a33b3SFabrice Gasnier {}, /* sentinel */ 306f24a33b3SFabrice Gasnier }; 307f24a33b3SFabrice Gasnier 3081cd92d42SFabrice Gasnier /* 309ee2ac1cdSFabrice Gasnier * stm32f4_smp_bits[] - describe sampling time register index & bit fields 310ee2ac1cdSFabrice Gasnier * Sorted so it can be indexed by channel number. 311ee2ac1cdSFabrice Gasnier */ 312ee2ac1cdSFabrice Gasnier static const struct stm32_adc_regs stm32f4_smp_bits[] = { 313ee2ac1cdSFabrice Gasnier /* STM32F4_ADC_SMPR2: smpr[] index, mask, shift for SMP0 to SMP9 */ 314ee2ac1cdSFabrice Gasnier { 1, GENMASK(2, 0), 0 }, 315ee2ac1cdSFabrice Gasnier { 1, GENMASK(5, 3), 3 }, 316ee2ac1cdSFabrice Gasnier { 1, GENMASK(8, 6), 6 }, 317ee2ac1cdSFabrice Gasnier { 1, GENMASK(11, 9), 9 }, 318ee2ac1cdSFabrice Gasnier { 1, GENMASK(14, 12), 12 }, 319ee2ac1cdSFabrice Gasnier { 1, GENMASK(17, 15), 15 }, 320ee2ac1cdSFabrice Gasnier { 1, GENMASK(20, 18), 18 }, 321ee2ac1cdSFabrice Gasnier { 1, GENMASK(23, 21), 21 }, 322ee2ac1cdSFabrice Gasnier { 1, GENMASK(26, 24), 24 }, 323ee2ac1cdSFabrice Gasnier { 1, GENMASK(29, 27), 27 }, 324ee2ac1cdSFabrice Gasnier /* STM32F4_ADC_SMPR1, smpr[] index, mask, shift for SMP10 to SMP18 */ 325ee2ac1cdSFabrice Gasnier { 0, GENMASK(2, 0), 0 }, 326ee2ac1cdSFabrice Gasnier { 0, GENMASK(5, 3), 3 }, 327ee2ac1cdSFabrice Gasnier { 0, GENMASK(8, 6), 6 }, 328ee2ac1cdSFabrice Gasnier { 0, GENMASK(11, 9), 9 }, 329ee2ac1cdSFabrice Gasnier { 0, GENMASK(14, 12), 12 }, 330ee2ac1cdSFabrice Gasnier { 0, GENMASK(17, 15), 15 }, 331ee2ac1cdSFabrice Gasnier { 0, GENMASK(20, 18), 18 }, 332ee2ac1cdSFabrice Gasnier { 0, GENMASK(23, 21), 21 }, 333ee2ac1cdSFabrice Gasnier { 0, GENMASK(26, 24), 24 }, 334ee2ac1cdSFabrice Gasnier }; 335ee2ac1cdSFabrice Gasnier 336ee2ac1cdSFabrice Gasnier /* STM32F4 programmable sampling time (ADC clock cycles) */ 337ee2ac1cdSFabrice Gasnier static const unsigned int stm32f4_adc_smp_cycles[STM32_ADC_MAX_SMP + 1] = { 338ee2ac1cdSFabrice Gasnier 3, 15, 28, 56, 84, 112, 144, 480, 339ee2ac1cdSFabrice Gasnier }; 340ee2ac1cdSFabrice Gasnier 34164ad7f64SFabrice Gasnier static const struct stm32_adc_regspec stm32f4_adc_regspec = { 34264ad7f64SFabrice Gasnier .dr = STM32F4_ADC_DR, 34364ad7f64SFabrice Gasnier .ier_eoc = { STM32F4_ADC_CR1, STM32F4_EOCIE }, 344cc06e67dSFabrice Gasnier .ier_ovr = { STM32F4_ADC_CR1, STM32F4_OVRIE }, 34564ad7f64SFabrice Gasnier .isr_eoc = { STM32F4_ADC_SR, STM32F4_EOC }, 346cc06e67dSFabrice Gasnier .isr_ovr = { STM32F4_ADC_SR, STM32F4_OVR }, 34764ad7f64SFabrice Gasnier .sqr = stm32f4_sq, 34864ad7f64SFabrice Gasnier .exten = { STM32F4_ADC_CR2, STM32F4_EXTEN_MASK, STM32F4_EXTEN_SHIFT }, 34964ad7f64SFabrice Gasnier .extsel = { STM32F4_ADC_CR2, STM32F4_EXTSEL_MASK, 35064ad7f64SFabrice Gasnier STM32F4_EXTSEL_SHIFT }, 35164ad7f64SFabrice Gasnier .res = { STM32F4_ADC_CR1, STM32F4_RES_MASK, STM32F4_RES_SHIFT }, 352ee2ac1cdSFabrice Gasnier .smpr = { STM32F4_ADC_SMPR1, STM32F4_ADC_SMPR2 }, 353ee2ac1cdSFabrice Gasnier .smp_bits = stm32f4_smp_bits, 35464ad7f64SFabrice Gasnier }; 35564ad7f64SFabrice Gasnier 35695e339b6SFabrice Gasnier static const struct stm32_adc_regs stm32h7_sq[STM32_ADC_MAX_SQ + 1] = { 35795e339b6SFabrice Gasnier /* L: len bit field description to be kept as first element */ 35895e339b6SFabrice Gasnier { STM32H7_ADC_SQR1, GENMASK(3, 0), 0 }, 35995e339b6SFabrice Gasnier /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */ 36095e339b6SFabrice Gasnier { STM32H7_ADC_SQR1, GENMASK(10, 6), 6 }, 36195e339b6SFabrice Gasnier { STM32H7_ADC_SQR1, GENMASK(16, 12), 12 }, 36295e339b6SFabrice Gasnier { STM32H7_ADC_SQR1, GENMASK(22, 18), 18 }, 36395e339b6SFabrice Gasnier { STM32H7_ADC_SQR1, GENMASK(28, 24), 24 }, 36495e339b6SFabrice Gasnier { STM32H7_ADC_SQR2, GENMASK(4, 0), 0 }, 36595e339b6SFabrice Gasnier { STM32H7_ADC_SQR2, GENMASK(10, 6), 6 }, 36695e339b6SFabrice Gasnier { STM32H7_ADC_SQR2, GENMASK(16, 12), 12 }, 36795e339b6SFabrice Gasnier { STM32H7_ADC_SQR2, GENMASK(22, 18), 18 }, 36895e339b6SFabrice Gasnier { STM32H7_ADC_SQR2, GENMASK(28, 24), 24 }, 36995e339b6SFabrice Gasnier { STM32H7_ADC_SQR3, GENMASK(4, 0), 0 }, 37095e339b6SFabrice Gasnier { STM32H7_ADC_SQR3, GENMASK(10, 6), 6 }, 37195e339b6SFabrice Gasnier { STM32H7_ADC_SQR3, GENMASK(16, 12), 12 }, 37295e339b6SFabrice Gasnier { STM32H7_ADC_SQR3, GENMASK(22, 18), 18 }, 37395e339b6SFabrice Gasnier { STM32H7_ADC_SQR3, GENMASK(28, 24), 24 }, 37495e339b6SFabrice Gasnier { STM32H7_ADC_SQR4, GENMASK(4, 0), 0 }, 37595e339b6SFabrice Gasnier { STM32H7_ADC_SQR4, GENMASK(10, 6), 6 }, 37695e339b6SFabrice Gasnier }; 37795e339b6SFabrice Gasnier 37895e339b6SFabrice Gasnier /* STM32H7 external trigger sources for all instances */ 37995e339b6SFabrice Gasnier static struct stm32_adc_trig_info stm32h7_adc_trigs[] = { 38095e339b6SFabrice Gasnier { TIM1_CH1, STM32_EXT0 }, 38195e339b6SFabrice Gasnier { TIM1_CH2, STM32_EXT1 }, 38295e339b6SFabrice Gasnier { TIM1_CH3, STM32_EXT2 }, 38395e339b6SFabrice Gasnier { TIM2_CH2, STM32_EXT3 }, 38495e339b6SFabrice Gasnier { TIM3_TRGO, STM32_EXT4 }, 38595e339b6SFabrice Gasnier { TIM4_CH4, STM32_EXT5 }, 38695e339b6SFabrice Gasnier { TIM8_TRGO, STM32_EXT7 }, 38795e339b6SFabrice Gasnier { TIM8_TRGO2, STM32_EXT8 }, 38895e339b6SFabrice Gasnier { TIM1_TRGO, STM32_EXT9 }, 38995e339b6SFabrice Gasnier { TIM1_TRGO2, STM32_EXT10 }, 39095e339b6SFabrice Gasnier { TIM2_TRGO, STM32_EXT11 }, 39195e339b6SFabrice Gasnier { TIM4_TRGO, STM32_EXT12 }, 39295e339b6SFabrice Gasnier { TIM6_TRGO, STM32_EXT13 }, 3933a069904SFabrice Gasnier { TIM15_TRGO, STM32_EXT14 }, 39495e339b6SFabrice Gasnier { TIM3_CH4, STM32_EXT15 }, 395f0b638a7SFabrice Gasnier { LPTIM1_OUT, STM32_EXT18 }, 396f0b638a7SFabrice Gasnier { LPTIM2_OUT, STM32_EXT19 }, 397f0b638a7SFabrice Gasnier { LPTIM3_OUT, STM32_EXT20 }, 39895e339b6SFabrice Gasnier {}, 39995e339b6SFabrice Gasnier }; 40095e339b6SFabrice Gasnier 4011cd92d42SFabrice Gasnier /* 402ee2ac1cdSFabrice Gasnier * stm32h7_smp_bits - describe sampling time register index & bit fields 403ee2ac1cdSFabrice Gasnier * Sorted so it can be indexed by channel number. 404ee2ac1cdSFabrice Gasnier */ 405ee2ac1cdSFabrice Gasnier static const struct stm32_adc_regs stm32h7_smp_bits[] = { 406ee2ac1cdSFabrice Gasnier /* STM32H7_ADC_SMPR1, smpr[] index, mask, shift for SMP0 to SMP9 */ 407ee2ac1cdSFabrice Gasnier { 0, GENMASK(2, 0), 0 }, 408ee2ac1cdSFabrice Gasnier { 0, GENMASK(5, 3), 3 }, 409ee2ac1cdSFabrice Gasnier { 0, GENMASK(8, 6), 6 }, 410ee2ac1cdSFabrice Gasnier { 0, GENMASK(11, 9), 9 }, 411ee2ac1cdSFabrice Gasnier { 0, GENMASK(14, 12), 12 }, 412ee2ac1cdSFabrice Gasnier { 0, GENMASK(17, 15), 15 }, 413ee2ac1cdSFabrice Gasnier { 0, GENMASK(20, 18), 18 }, 414ee2ac1cdSFabrice Gasnier { 0, GENMASK(23, 21), 21 }, 415ee2ac1cdSFabrice Gasnier { 0, GENMASK(26, 24), 24 }, 416ee2ac1cdSFabrice Gasnier { 0, GENMASK(29, 27), 27 }, 417ee2ac1cdSFabrice Gasnier /* STM32H7_ADC_SMPR2, smpr[] index, mask, shift for SMP10 to SMP19 */ 418ee2ac1cdSFabrice Gasnier { 1, GENMASK(2, 0), 0 }, 419ee2ac1cdSFabrice Gasnier { 1, GENMASK(5, 3), 3 }, 420ee2ac1cdSFabrice Gasnier { 1, GENMASK(8, 6), 6 }, 421ee2ac1cdSFabrice Gasnier { 1, GENMASK(11, 9), 9 }, 422ee2ac1cdSFabrice Gasnier { 1, GENMASK(14, 12), 12 }, 423ee2ac1cdSFabrice Gasnier { 1, GENMASK(17, 15), 15 }, 424ee2ac1cdSFabrice Gasnier { 1, GENMASK(20, 18), 18 }, 425ee2ac1cdSFabrice Gasnier { 1, GENMASK(23, 21), 21 }, 426ee2ac1cdSFabrice Gasnier { 1, GENMASK(26, 24), 24 }, 427ee2ac1cdSFabrice Gasnier { 1, GENMASK(29, 27), 27 }, 428ee2ac1cdSFabrice Gasnier }; 429ee2ac1cdSFabrice Gasnier 430ee2ac1cdSFabrice Gasnier /* STM32H7 programmable sampling time (ADC clock cycles, rounded down) */ 431ee2ac1cdSFabrice Gasnier static const unsigned int stm32h7_adc_smp_cycles[STM32_ADC_MAX_SMP + 1] = { 432ee2ac1cdSFabrice Gasnier 1, 2, 8, 16, 32, 64, 387, 810, 433ee2ac1cdSFabrice Gasnier }; 434ee2ac1cdSFabrice Gasnier 43595e339b6SFabrice Gasnier static const struct stm32_adc_regspec stm32h7_adc_regspec = { 43695e339b6SFabrice Gasnier .dr = STM32H7_ADC_DR, 43795e339b6SFabrice Gasnier .ier_eoc = { STM32H7_ADC_IER, STM32H7_EOCIE }, 438cc06e67dSFabrice Gasnier .ier_ovr = { STM32H7_ADC_IER, STM32H7_OVRIE }, 43995e339b6SFabrice Gasnier .isr_eoc = { STM32H7_ADC_ISR, STM32H7_EOC }, 440cc06e67dSFabrice Gasnier .isr_ovr = { STM32H7_ADC_ISR, STM32H7_OVR }, 44195e339b6SFabrice Gasnier .sqr = stm32h7_sq, 44295e339b6SFabrice Gasnier .exten = { STM32H7_ADC_CFGR, STM32H7_EXTEN_MASK, STM32H7_EXTEN_SHIFT }, 44395e339b6SFabrice Gasnier .extsel = { STM32H7_ADC_CFGR, STM32H7_EXTSEL_MASK, 44495e339b6SFabrice Gasnier STM32H7_EXTSEL_SHIFT }, 44595e339b6SFabrice Gasnier .res = { STM32H7_ADC_CFGR, STM32H7_RES_MASK, STM32H7_RES_SHIFT }, 446ee2ac1cdSFabrice Gasnier .smpr = { STM32H7_ADC_SMPR1, STM32H7_ADC_SMPR2 }, 447ee2ac1cdSFabrice Gasnier .smp_bits = stm32h7_smp_bits, 44895e339b6SFabrice Gasnier }; 44995e339b6SFabrice Gasnier 450da9b9485SFabrice Gasnier /** 4510f883b22SFabrice Gasnier * STM32 ADC registers access routines 4520f883b22SFabrice Gasnier * @adc: stm32 adc instance 4530f883b22SFabrice Gasnier * @reg: reg offset in adc instance 4540f883b22SFabrice Gasnier * 4550f883b22SFabrice Gasnier * Note: All instances share same base, with 0x0, 0x100 or 0x200 offset resp. 4560f883b22SFabrice Gasnier * for adc1, adc2 and adc3. 4570f883b22SFabrice Gasnier */ 4580f883b22SFabrice Gasnier static u32 stm32_adc_readl(struct stm32_adc *adc, u32 reg) 4590f883b22SFabrice Gasnier { 4600f883b22SFabrice Gasnier return readl_relaxed(adc->common->base + adc->offset + reg); 4610f883b22SFabrice Gasnier } 4620f883b22SFabrice Gasnier 46395e339b6SFabrice Gasnier #define stm32_adc_readl_addr(addr) stm32_adc_readl(adc, addr) 46495e339b6SFabrice Gasnier 46595e339b6SFabrice Gasnier #define stm32_adc_readl_poll_timeout(reg, val, cond, sleep_us, timeout_us) \ 46695e339b6SFabrice Gasnier readx_poll_timeout(stm32_adc_readl_addr, reg, val, \ 46795e339b6SFabrice Gasnier cond, sleep_us, timeout_us) 46895e339b6SFabrice Gasnier 4690f883b22SFabrice Gasnier static u16 stm32_adc_readw(struct stm32_adc *adc, u32 reg) 4700f883b22SFabrice Gasnier { 4710f883b22SFabrice Gasnier return readw_relaxed(adc->common->base + adc->offset + reg); 4720f883b22SFabrice Gasnier } 4730f883b22SFabrice Gasnier 4740f883b22SFabrice Gasnier static void stm32_adc_writel(struct stm32_adc *adc, u32 reg, u32 val) 4750f883b22SFabrice Gasnier { 4760f883b22SFabrice Gasnier writel_relaxed(val, adc->common->base + adc->offset + reg); 4770f883b22SFabrice Gasnier } 4780f883b22SFabrice Gasnier 4790f883b22SFabrice Gasnier static void stm32_adc_set_bits(struct stm32_adc *adc, u32 reg, u32 bits) 4800f883b22SFabrice Gasnier { 4810f883b22SFabrice Gasnier unsigned long flags; 4820f883b22SFabrice Gasnier 4830f883b22SFabrice Gasnier spin_lock_irqsave(&adc->lock, flags); 4840f883b22SFabrice Gasnier stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) | bits); 4850f883b22SFabrice Gasnier spin_unlock_irqrestore(&adc->lock, flags); 4860f883b22SFabrice Gasnier } 4870f883b22SFabrice Gasnier 4880f883b22SFabrice Gasnier static void stm32_adc_clr_bits(struct stm32_adc *adc, u32 reg, u32 bits) 4890f883b22SFabrice Gasnier { 4900f883b22SFabrice Gasnier unsigned long flags; 4910f883b22SFabrice Gasnier 4920f883b22SFabrice Gasnier spin_lock_irqsave(&adc->lock, flags); 4930f883b22SFabrice Gasnier stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) & ~bits); 4940f883b22SFabrice Gasnier spin_unlock_irqrestore(&adc->lock, flags); 4950f883b22SFabrice Gasnier } 4960f883b22SFabrice Gasnier 4970f883b22SFabrice Gasnier /** 4980f883b22SFabrice Gasnier * stm32_adc_conv_irq_enable() - Enable end of conversion interrupt 4990f883b22SFabrice Gasnier * @adc: stm32 adc instance 5000f883b22SFabrice Gasnier */ 5010f883b22SFabrice Gasnier static void stm32_adc_conv_irq_enable(struct stm32_adc *adc) 5020f883b22SFabrice Gasnier { 50364ad7f64SFabrice Gasnier stm32_adc_set_bits(adc, adc->cfg->regs->ier_eoc.reg, 50464ad7f64SFabrice Gasnier adc->cfg->regs->ier_eoc.mask); 5050f883b22SFabrice Gasnier }; 5060f883b22SFabrice Gasnier 5070f883b22SFabrice Gasnier /** 5080f883b22SFabrice Gasnier * stm32_adc_conv_irq_disable() - Disable end of conversion interrupt 5090f883b22SFabrice Gasnier * @adc: stm32 adc instance 5100f883b22SFabrice Gasnier */ 5110f883b22SFabrice Gasnier static void stm32_adc_conv_irq_disable(struct stm32_adc *adc) 5120f883b22SFabrice Gasnier { 51364ad7f64SFabrice Gasnier stm32_adc_clr_bits(adc, adc->cfg->regs->ier_eoc.reg, 51464ad7f64SFabrice Gasnier adc->cfg->regs->ier_eoc.mask); 5150f883b22SFabrice Gasnier } 5160f883b22SFabrice Gasnier 517cc06e67dSFabrice Gasnier static void stm32_adc_ovr_irq_enable(struct stm32_adc *adc) 518cc06e67dSFabrice Gasnier { 519cc06e67dSFabrice Gasnier stm32_adc_set_bits(adc, adc->cfg->regs->ier_ovr.reg, 520cc06e67dSFabrice Gasnier adc->cfg->regs->ier_ovr.mask); 521cc06e67dSFabrice Gasnier } 522cc06e67dSFabrice Gasnier 523cc06e67dSFabrice Gasnier static void stm32_adc_ovr_irq_disable(struct stm32_adc *adc) 524cc06e67dSFabrice Gasnier { 525cc06e67dSFabrice Gasnier stm32_adc_clr_bits(adc, adc->cfg->regs->ier_ovr.reg, 526cc06e67dSFabrice Gasnier adc->cfg->regs->ier_ovr.mask); 527cc06e67dSFabrice Gasnier } 528cc06e67dSFabrice Gasnier 52925a85bedSFabrice Gasnier static void stm32_adc_set_res(struct stm32_adc *adc) 53025a85bedSFabrice Gasnier { 53164ad7f64SFabrice Gasnier const struct stm32_adc_regs *res = &adc->cfg->regs->res; 53264ad7f64SFabrice Gasnier u32 val; 53325a85bedSFabrice Gasnier 53464ad7f64SFabrice Gasnier val = stm32_adc_readl(adc, res->reg); 53564ad7f64SFabrice Gasnier val = (val & ~res->mask) | (adc->res << res->shift); 53664ad7f64SFabrice Gasnier stm32_adc_writel(adc, res->reg, val); 53725a85bedSFabrice Gasnier } 53825a85bedSFabrice Gasnier 5399bdbb113SFabrice Gasnier static int stm32_adc_hw_stop(struct device *dev) 5409bdbb113SFabrice Gasnier { 541cd64d357SAlexandru Ardelean struct iio_dev *indio_dev = dev_get_drvdata(dev); 542cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 5439bdbb113SFabrice Gasnier 5449bdbb113SFabrice Gasnier if (adc->cfg->unprepare) 545cd64d357SAlexandru Ardelean adc->cfg->unprepare(indio_dev); 5469bdbb113SFabrice Gasnier 5479bdbb113SFabrice Gasnier if (adc->clk) 5489bdbb113SFabrice Gasnier clk_disable_unprepare(adc->clk); 5499bdbb113SFabrice Gasnier 5509bdbb113SFabrice Gasnier return 0; 5519bdbb113SFabrice Gasnier } 5529bdbb113SFabrice Gasnier 5539bdbb113SFabrice Gasnier static int stm32_adc_hw_start(struct device *dev) 5549bdbb113SFabrice Gasnier { 555cd64d357SAlexandru Ardelean struct iio_dev *indio_dev = dev_get_drvdata(dev); 556cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 5579bdbb113SFabrice Gasnier int ret; 5589bdbb113SFabrice Gasnier 5599bdbb113SFabrice Gasnier if (adc->clk) { 5609bdbb113SFabrice Gasnier ret = clk_prepare_enable(adc->clk); 5619bdbb113SFabrice Gasnier if (ret) 5629bdbb113SFabrice Gasnier return ret; 5639bdbb113SFabrice Gasnier } 5649bdbb113SFabrice Gasnier 5659bdbb113SFabrice Gasnier stm32_adc_set_res(adc); 5669bdbb113SFabrice Gasnier 5679bdbb113SFabrice Gasnier if (adc->cfg->prepare) { 568cd64d357SAlexandru Ardelean ret = adc->cfg->prepare(indio_dev); 5699bdbb113SFabrice Gasnier if (ret) 5709bdbb113SFabrice Gasnier goto err_clk_dis; 5719bdbb113SFabrice Gasnier } 5729bdbb113SFabrice Gasnier 5739bdbb113SFabrice Gasnier return 0; 5749bdbb113SFabrice Gasnier 5759bdbb113SFabrice Gasnier err_clk_dis: 5769bdbb113SFabrice Gasnier if (adc->clk) 5779bdbb113SFabrice Gasnier clk_disable_unprepare(adc->clk); 5789bdbb113SFabrice Gasnier 5799bdbb113SFabrice Gasnier return ret; 5809bdbb113SFabrice Gasnier } 5819bdbb113SFabrice Gasnier 5820f883b22SFabrice Gasnier /** 58364ad7f64SFabrice Gasnier * stm32f4_adc_start_conv() - Start conversions for regular channels. 584cd64d357SAlexandru Ardelean * @indio_dev: IIO device instance 5852763ea05SFabrice Gasnier * @dma: use dma to transfer conversion result 5862763ea05SFabrice Gasnier * 5872763ea05SFabrice Gasnier * Start conversions for regular channels. 5882763ea05SFabrice Gasnier * Also take care of normal or DMA mode. Circular DMA may be used for regular 5892763ea05SFabrice Gasnier * conversions, in IIO buffer modes. Otherwise, use ADC interrupt with direct 5902763ea05SFabrice Gasnier * DR read instead (e.g. read_raw, or triggered buffer mode without DMA). 5910f883b22SFabrice Gasnier */ 592cd64d357SAlexandru Ardelean static void stm32f4_adc_start_conv(struct iio_dev *indio_dev, bool dma) 5930f883b22SFabrice Gasnier { 594cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 595cd64d357SAlexandru Ardelean 5960f883b22SFabrice Gasnier stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN); 5972763ea05SFabrice Gasnier 5982763ea05SFabrice Gasnier if (dma) 5992763ea05SFabrice Gasnier stm32_adc_set_bits(adc, STM32F4_ADC_CR2, 6002763ea05SFabrice Gasnier STM32F4_DMA | STM32F4_DDS); 6012763ea05SFabrice Gasnier 6020f883b22SFabrice Gasnier stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_EOCS | STM32F4_ADON); 6030f883b22SFabrice Gasnier 6040f883b22SFabrice Gasnier /* Wait for Power-up time (tSTAB from datasheet) */ 6050f883b22SFabrice Gasnier usleep_range(2, 3); 6060f883b22SFabrice Gasnier 6070f883b22SFabrice Gasnier /* Software start ? (e.g. trigger detection disabled ?) */ 6080f883b22SFabrice Gasnier if (!(stm32_adc_readl(adc, STM32F4_ADC_CR2) & STM32F4_EXTEN_MASK)) 6090f883b22SFabrice Gasnier stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_SWSTART); 6100f883b22SFabrice Gasnier } 6110f883b22SFabrice Gasnier 612cd64d357SAlexandru Ardelean static void stm32f4_adc_stop_conv(struct iio_dev *indio_dev) 6130f883b22SFabrice Gasnier { 614cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 615cd64d357SAlexandru Ardelean 6160f883b22SFabrice Gasnier stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK); 6170f883b22SFabrice Gasnier stm32_adc_clr_bits(adc, STM32F4_ADC_SR, STM32F4_STRT); 6180f883b22SFabrice Gasnier 6190f883b22SFabrice Gasnier stm32_adc_clr_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN); 6202763ea05SFabrice Gasnier stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, 6212763ea05SFabrice Gasnier STM32F4_ADON | STM32F4_DMA | STM32F4_DDS); 6220f883b22SFabrice Gasnier } 6230f883b22SFabrice Gasnier 624cd64d357SAlexandru Ardelean static void stm32h7_adc_start_conv(struct iio_dev *indio_dev, bool dma) 62595e339b6SFabrice Gasnier { 626cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 62795e339b6SFabrice Gasnier enum stm32h7_adc_dmngt dmngt; 62895e339b6SFabrice Gasnier unsigned long flags; 62995e339b6SFabrice Gasnier u32 val; 63095e339b6SFabrice Gasnier 63195e339b6SFabrice Gasnier if (dma) 63295e339b6SFabrice Gasnier dmngt = STM32H7_DMNGT_DMA_CIRC; 63395e339b6SFabrice Gasnier else 63495e339b6SFabrice Gasnier dmngt = STM32H7_DMNGT_DR_ONLY; 63595e339b6SFabrice Gasnier 63695e339b6SFabrice Gasnier spin_lock_irqsave(&adc->lock, flags); 63795e339b6SFabrice Gasnier val = stm32_adc_readl(adc, STM32H7_ADC_CFGR); 63895e339b6SFabrice Gasnier val = (val & ~STM32H7_DMNGT_MASK) | (dmngt << STM32H7_DMNGT_SHIFT); 63995e339b6SFabrice Gasnier stm32_adc_writel(adc, STM32H7_ADC_CFGR, val); 64095e339b6SFabrice Gasnier spin_unlock_irqrestore(&adc->lock, flags); 64195e339b6SFabrice Gasnier 64295e339b6SFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTART); 64395e339b6SFabrice Gasnier } 64495e339b6SFabrice Gasnier 645cd64d357SAlexandru Ardelean static void stm32h7_adc_stop_conv(struct iio_dev *indio_dev) 64695e339b6SFabrice Gasnier { 647cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 64895e339b6SFabrice Gasnier int ret; 64995e339b6SFabrice Gasnier u32 val; 65095e339b6SFabrice Gasnier 65195e339b6SFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTP); 65295e339b6SFabrice Gasnier 65395e339b6SFabrice Gasnier ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, 65495e339b6SFabrice Gasnier !(val & (STM32H7_ADSTART)), 65595e339b6SFabrice Gasnier 100, STM32_ADC_TIMEOUT_US); 65695e339b6SFabrice Gasnier if (ret) 65795e339b6SFabrice Gasnier dev_warn(&indio_dev->dev, "stop failed\n"); 65895e339b6SFabrice Gasnier 65995e339b6SFabrice Gasnier stm32_adc_clr_bits(adc, STM32H7_ADC_CFGR, STM32H7_DMNGT_MASK); 66095e339b6SFabrice Gasnier } 66195e339b6SFabrice Gasnier 662cd64d357SAlexandru Ardelean static int stm32h7_adc_exit_pwr_down(struct iio_dev *indio_dev) 66395e339b6SFabrice Gasnier { 664cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 665d58c67d1SFabrice Gasnier int ret; 666d58c67d1SFabrice Gasnier u32 val; 667d58c67d1SFabrice Gasnier 66895e339b6SFabrice Gasnier /* Exit deep power down, then enable ADC voltage regulator */ 66995e339b6SFabrice Gasnier stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD); 67095e339b6SFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADVREGEN); 67195e339b6SFabrice Gasnier 67295e339b6SFabrice Gasnier if (adc->common->rate > STM32H7_BOOST_CLKRATE) 67395e339b6SFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_BOOST); 67495e339b6SFabrice Gasnier 67595e339b6SFabrice Gasnier /* Wait for startup time */ 676d58c67d1SFabrice Gasnier if (!adc->cfg->has_vregready) { 67795e339b6SFabrice Gasnier usleep_range(10, 20); 678d58c67d1SFabrice Gasnier return 0; 679d58c67d1SFabrice Gasnier } 680d58c67d1SFabrice Gasnier 681d58c67d1SFabrice Gasnier ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR, val, 682d58c67d1SFabrice Gasnier val & STM32MP1_VREGREADY, 100, 683d58c67d1SFabrice Gasnier STM32_ADC_TIMEOUT_US); 684d58c67d1SFabrice Gasnier if (ret) { 685d58c67d1SFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD); 686d58c67d1SFabrice Gasnier dev_err(&indio_dev->dev, "Failed to exit power down\n"); 687d58c67d1SFabrice Gasnier } 688d58c67d1SFabrice Gasnier 689d58c67d1SFabrice Gasnier return ret; 69095e339b6SFabrice Gasnier } 69195e339b6SFabrice Gasnier 69295e339b6SFabrice Gasnier static void stm32h7_adc_enter_pwr_down(struct stm32_adc *adc) 69395e339b6SFabrice Gasnier { 69495e339b6SFabrice Gasnier stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_BOOST); 69595e339b6SFabrice Gasnier 69695e339b6SFabrice Gasnier /* Setting DEEPPWD disables ADC vreg and clears ADVREGEN */ 69795e339b6SFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD); 69895e339b6SFabrice Gasnier } 69995e339b6SFabrice Gasnier 700cd64d357SAlexandru Ardelean static int stm32h7_adc_enable(struct iio_dev *indio_dev) 70195e339b6SFabrice Gasnier { 702cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 70395e339b6SFabrice Gasnier int ret; 70495e339b6SFabrice Gasnier u32 val; 70595e339b6SFabrice Gasnier 70695e339b6SFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADEN); 70795e339b6SFabrice Gasnier 70895e339b6SFabrice Gasnier /* Poll for ADRDY to be set (after adc startup time) */ 70995e339b6SFabrice Gasnier ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR, val, 71095e339b6SFabrice Gasnier val & STM32H7_ADRDY, 71195e339b6SFabrice Gasnier 100, STM32_ADC_TIMEOUT_US); 71295e339b6SFabrice Gasnier if (ret) { 713a3b5655eSFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADDIS); 71495e339b6SFabrice Gasnier dev_err(&indio_dev->dev, "Failed to enable ADC\n"); 715a3b5655eSFabrice Gasnier } else { 716a3b5655eSFabrice Gasnier /* Clear ADRDY by writing one */ 717a3b5655eSFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_ISR, STM32H7_ADRDY); 71895e339b6SFabrice Gasnier } 71995e339b6SFabrice Gasnier 72095e339b6SFabrice Gasnier return ret; 72195e339b6SFabrice Gasnier } 72295e339b6SFabrice Gasnier 723cd64d357SAlexandru Ardelean static void stm32h7_adc_disable(struct iio_dev *indio_dev) 72495e339b6SFabrice Gasnier { 725cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 72695e339b6SFabrice Gasnier int ret; 72795e339b6SFabrice Gasnier u32 val; 72895e339b6SFabrice Gasnier 72995e339b6SFabrice Gasnier /* Disable ADC and wait until it's effectively disabled */ 73095e339b6SFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADDIS); 73195e339b6SFabrice Gasnier ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, 73295e339b6SFabrice Gasnier !(val & STM32H7_ADEN), 100, 73395e339b6SFabrice Gasnier STM32_ADC_TIMEOUT_US); 73495e339b6SFabrice Gasnier if (ret) 73595e339b6SFabrice Gasnier dev_warn(&indio_dev->dev, "Failed to disable\n"); 73695e339b6SFabrice Gasnier } 73795e339b6SFabrice Gasnier 73895e339b6SFabrice Gasnier /** 73995e339b6SFabrice Gasnier * stm32h7_adc_read_selfcalib() - read calibration shadow regs, save result 740cd64d357SAlexandru Ardelean * @indio_dev: IIO device instance 7410da98c7bSFabrice Gasnier * Note: Must be called once ADC is enabled, so LINCALRDYW[1..6] are writable 74295e339b6SFabrice Gasnier */ 743cd64d357SAlexandru Ardelean static int stm32h7_adc_read_selfcalib(struct iio_dev *indio_dev) 74495e339b6SFabrice Gasnier { 745cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 74695e339b6SFabrice Gasnier int i, ret; 74795e339b6SFabrice Gasnier u32 lincalrdyw_mask, val; 74895e339b6SFabrice Gasnier 74995e339b6SFabrice Gasnier /* Read linearity calibration */ 75095e339b6SFabrice Gasnier lincalrdyw_mask = STM32H7_LINCALRDYW6; 75195e339b6SFabrice Gasnier for (i = STM32H7_LINCALFACT_NUM - 1; i >= 0; i--) { 75295e339b6SFabrice Gasnier /* Clear STM32H7_LINCALRDYW[6..1]: transfer calib to CALFACT2 */ 75395e339b6SFabrice Gasnier stm32_adc_clr_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask); 75495e339b6SFabrice Gasnier 75595e339b6SFabrice Gasnier /* Poll: wait calib data to be ready in CALFACT2 register */ 75695e339b6SFabrice Gasnier ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, 75795e339b6SFabrice Gasnier !(val & lincalrdyw_mask), 75895e339b6SFabrice Gasnier 100, STM32_ADC_TIMEOUT_US); 75995e339b6SFabrice Gasnier if (ret) { 76095e339b6SFabrice Gasnier dev_err(&indio_dev->dev, "Failed to read calfact\n"); 7610da98c7bSFabrice Gasnier return ret; 76295e339b6SFabrice Gasnier } 76395e339b6SFabrice Gasnier 76495e339b6SFabrice Gasnier val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2); 76595e339b6SFabrice Gasnier adc->cal.lincalfact[i] = (val & STM32H7_LINCALFACT_MASK); 76695e339b6SFabrice Gasnier adc->cal.lincalfact[i] >>= STM32H7_LINCALFACT_SHIFT; 76795e339b6SFabrice Gasnier 76895e339b6SFabrice Gasnier lincalrdyw_mask >>= 1; 76995e339b6SFabrice Gasnier } 77095e339b6SFabrice Gasnier 77195e339b6SFabrice Gasnier /* Read offset calibration */ 77295e339b6SFabrice Gasnier val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT); 77395e339b6SFabrice Gasnier adc->cal.calfact_s = (val & STM32H7_CALFACT_S_MASK); 77495e339b6SFabrice Gasnier adc->cal.calfact_s >>= STM32H7_CALFACT_S_SHIFT; 77595e339b6SFabrice Gasnier adc->cal.calfact_d = (val & STM32H7_CALFACT_D_MASK); 77695e339b6SFabrice Gasnier adc->cal.calfact_d >>= STM32H7_CALFACT_D_SHIFT; 7770da98c7bSFabrice Gasnier adc->cal.calibrated = true; 77895e339b6SFabrice Gasnier 7790da98c7bSFabrice Gasnier return 0; 78095e339b6SFabrice Gasnier } 78195e339b6SFabrice Gasnier 78295e339b6SFabrice Gasnier /** 78395e339b6SFabrice Gasnier * stm32h7_adc_restore_selfcalib() - Restore saved self-calibration result 784cd64d357SAlexandru Ardelean * @indio_dev: IIO device instance 78595e339b6SFabrice Gasnier * Note: ADC must be enabled, with no on-going conversions. 78695e339b6SFabrice Gasnier */ 787cd64d357SAlexandru Ardelean static int stm32h7_adc_restore_selfcalib(struct iio_dev *indio_dev) 78895e339b6SFabrice Gasnier { 789cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 79095e339b6SFabrice Gasnier int i, ret; 79195e339b6SFabrice Gasnier u32 lincalrdyw_mask, val; 79295e339b6SFabrice Gasnier 79395e339b6SFabrice Gasnier val = (adc->cal.calfact_s << STM32H7_CALFACT_S_SHIFT) | 79495e339b6SFabrice Gasnier (adc->cal.calfact_d << STM32H7_CALFACT_D_SHIFT); 79595e339b6SFabrice Gasnier stm32_adc_writel(adc, STM32H7_ADC_CALFACT, val); 79695e339b6SFabrice Gasnier 79795e339b6SFabrice Gasnier lincalrdyw_mask = STM32H7_LINCALRDYW6; 79895e339b6SFabrice Gasnier for (i = STM32H7_LINCALFACT_NUM - 1; i >= 0; i--) { 79995e339b6SFabrice Gasnier /* 80095e339b6SFabrice Gasnier * Write saved calibration data to shadow registers: 80195e339b6SFabrice Gasnier * Write CALFACT2, and set LINCALRDYW[6..1] bit to trigger 80295e339b6SFabrice Gasnier * data write. Then poll to wait for complete transfer. 80395e339b6SFabrice Gasnier */ 80495e339b6SFabrice Gasnier val = adc->cal.lincalfact[i] << STM32H7_LINCALFACT_SHIFT; 80595e339b6SFabrice Gasnier stm32_adc_writel(adc, STM32H7_ADC_CALFACT2, val); 80695e339b6SFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask); 80795e339b6SFabrice Gasnier ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, 80895e339b6SFabrice Gasnier val & lincalrdyw_mask, 80995e339b6SFabrice Gasnier 100, STM32_ADC_TIMEOUT_US); 81095e339b6SFabrice Gasnier if (ret) { 81195e339b6SFabrice Gasnier dev_err(&indio_dev->dev, "Failed to write calfact\n"); 81295e339b6SFabrice Gasnier return ret; 81395e339b6SFabrice Gasnier } 81495e339b6SFabrice Gasnier 81595e339b6SFabrice Gasnier /* 81695e339b6SFabrice Gasnier * Read back calibration data, has two effects: 81795e339b6SFabrice Gasnier * - It ensures bits LINCALRDYW[6..1] are kept cleared 81895e339b6SFabrice Gasnier * for next time calibration needs to be restored. 81995e339b6SFabrice Gasnier * - BTW, bit clear triggers a read, then check data has been 82095e339b6SFabrice Gasnier * correctly written. 82195e339b6SFabrice Gasnier */ 82295e339b6SFabrice Gasnier stm32_adc_clr_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask); 82395e339b6SFabrice Gasnier ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, 82495e339b6SFabrice Gasnier !(val & lincalrdyw_mask), 82595e339b6SFabrice Gasnier 100, STM32_ADC_TIMEOUT_US); 82695e339b6SFabrice Gasnier if (ret) { 82795e339b6SFabrice Gasnier dev_err(&indio_dev->dev, "Failed to read calfact\n"); 82895e339b6SFabrice Gasnier return ret; 82995e339b6SFabrice Gasnier } 83095e339b6SFabrice Gasnier val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2); 83195e339b6SFabrice Gasnier if (val != adc->cal.lincalfact[i] << STM32H7_LINCALFACT_SHIFT) { 83295e339b6SFabrice Gasnier dev_err(&indio_dev->dev, "calfact not consistent\n"); 83395e339b6SFabrice Gasnier return -EIO; 83495e339b6SFabrice Gasnier } 83595e339b6SFabrice Gasnier 83695e339b6SFabrice Gasnier lincalrdyw_mask >>= 1; 83795e339b6SFabrice Gasnier } 83895e339b6SFabrice Gasnier 83995e339b6SFabrice Gasnier return 0; 84095e339b6SFabrice Gasnier } 84195e339b6SFabrice Gasnier 84295e339b6SFabrice Gasnier /** 84395e339b6SFabrice Gasnier * Fixed timeout value for ADC calibration. 84495e339b6SFabrice Gasnier * worst cases: 84595e339b6SFabrice Gasnier * - low clock frequency 84695e339b6SFabrice Gasnier * - maximum prescalers 84795e339b6SFabrice Gasnier * Calibration requires: 84895e339b6SFabrice Gasnier * - 131,072 ADC clock cycle for the linear calibration 84995e339b6SFabrice Gasnier * - 20 ADC clock cycle for the offset calibration 85095e339b6SFabrice Gasnier * 85195e339b6SFabrice Gasnier * Set to 100ms for now 85295e339b6SFabrice Gasnier */ 85395e339b6SFabrice Gasnier #define STM32H7_ADC_CALIB_TIMEOUT_US 100000 85495e339b6SFabrice Gasnier 85595e339b6SFabrice Gasnier /** 8560da98c7bSFabrice Gasnier * stm32h7_adc_selfcalib() - Procedure to calibrate ADC 857cd64d357SAlexandru Ardelean * @indio_dev: IIO device instance 8580da98c7bSFabrice Gasnier * Note: Must be called once ADC is out of power down. 85995e339b6SFabrice Gasnier */ 860cd64d357SAlexandru Ardelean static int stm32h7_adc_selfcalib(struct iio_dev *indio_dev) 86195e339b6SFabrice Gasnier { 862cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 86395e339b6SFabrice Gasnier int ret; 86495e339b6SFabrice Gasnier u32 val; 86595e339b6SFabrice Gasnier 8660da98c7bSFabrice Gasnier if (adc->cal.calibrated) 8670da98c7bSFabrice Gasnier return true; 86895e339b6SFabrice Gasnier 86995e339b6SFabrice Gasnier /* 87095e339b6SFabrice Gasnier * Select calibration mode: 87195e339b6SFabrice Gasnier * - Offset calibration for single ended inputs 87295e339b6SFabrice Gasnier * - No linearity calibration (do it later, before reading it) 87395e339b6SFabrice Gasnier */ 87495e339b6SFabrice Gasnier stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADCALDIF); 87595e339b6SFabrice Gasnier stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADCALLIN); 87695e339b6SFabrice Gasnier 87795e339b6SFabrice Gasnier /* Start calibration, then wait for completion */ 87895e339b6SFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADCAL); 87995e339b6SFabrice Gasnier ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, 88095e339b6SFabrice Gasnier !(val & STM32H7_ADCAL), 100, 88195e339b6SFabrice Gasnier STM32H7_ADC_CALIB_TIMEOUT_US); 88295e339b6SFabrice Gasnier if (ret) { 88395e339b6SFabrice Gasnier dev_err(&indio_dev->dev, "calibration failed\n"); 8840da98c7bSFabrice Gasnier goto out; 88595e339b6SFabrice Gasnier } 88695e339b6SFabrice Gasnier 88795e339b6SFabrice Gasnier /* 88895e339b6SFabrice Gasnier * Select calibration mode, then start calibration: 88995e339b6SFabrice Gasnier * - Offset calibration for differential input 89095e339b6SFabrice Gasnier * - Linearity calibration (needs to be done only once for single/diff) 89195e339b6SFabrice Gasnier * will run simultaneously with offset calibration. 89295e339b6SFabrice Gasnier */ 89395e339b6SFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, 89495e339b6SFabrice Gasnier STM32H7_ADCALDIF | STM32H7_ADCALLIN); 89595e339b6SFabrice Gasnier stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADCAL); 89695e339b6SFabrice Gasnier ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, 89795e339b6SFabrice Gasnier !(val & STM32H7_ADCAL), 100, 89895e339b6SFabrice Gasnier STM32H7_ADC_CALIB_TIMEOUT_US); 89995e339b6SFabrice Gasnier if (ret) { 90095e339b6SFabrice Gasnier dev_err(&indio_dev->dev, "calibration failed\n"); 9010da98c7bSFabrice Gasnier goto out; 90295e339b6SFabrice Gasnier } 90395e339b6SFabrice Gasnier 9040da98c7bSFabrice Gasnier out: 90595e339b6SFabrice Gasnier stm32_adc_clr_bits(adc, STM32H7_ADC_CR, 90695e339b6SFabrice Gasnier STM32H7_ADCALDIF | STM32H7_ADCALLIN); 90795e339b6SFabrice Gasnier 90895e339b6SFabrice Gasnier return ret; 90995e339b6SFabrice Gasnier } 91095e339b6SFabrice Gasnier 91195e339b6SFabrice Gasnier /** 91295e339b6SFabrice Gasnier * stm32h7_adc_prepare() - Leave power down mode to enable ADC. 913cd64d357SAlexandru Ardelean * @indio_dev: IIO device instance 91495e339b6SFabrice Gasnier * Leave power down mode. 9153fb2e24eSFabrice Gasnier * Configure channels as single ended or differential before enabling ADC. 91695e339b6SFabrice Gasnier * Enable ADC. 91795e339b6SFabrice Gasnier * Restore calibration data. 9183fb2e24eSFabrice Gasnier * Pre-select channels that may be used in PCSEL (required by input MUX / IO): 9193fb2e24eSFabrice Gasnier * - Only one input is selected for single ended (e.g. 'vinp') 9203fb2e24eSFabrice Gasnier * - Two inputs are selected for differential channels (e.g. 'vinp' & 'vinn') 92195e339b6SFabrice Gasnier */ 922cd64d357SAlexandru Ardelean static int stm32h7_adc_prepare(struct iio_dev *indio_dev) 92395e339b6SFabrice Gasnier { 924cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 9250da98c7bSFabrice Gasnier int calib, ret; 92695e339b6SFabrice Gasnier 927cd64d357SAlexandru Ardelean ret = stm32h7_adc_exit_pwr_down(indio_dev); 928d58c67d1SFabrice Gasnier if (ret) 929d58c67d1SFabrice Gasnier return ret; 930d58c67d1SFabrice Gasnier 931cd64d357SAlexandru Ardelean ret = stm32h7_adc_selfcalib(indio_dev); 9320da98c7bSFabrice Gasnier if (ret < 0) 9330da98c7bSFabrice Gasnier goto pwr_dwn; 9340da98c7bSFabrice Gasnier calib = ret; 9350da98c7bSFabrice Gasnier 9363fb2e24eSFabrice Gasnier stm32_adc_writel(adc, STM32H7_ADC_DIFSEL, adc->difsel); 93795e339b6SFabrice Gasnier 938cd64d357SAlexandru Ardelean ret = stm32h7_adc_enable(indio_dev); 93995e339b6SFabrice Gasnier if (ret) 94095e339b6SFabrice Gasnier goto pwr_dwn; 94195e339b6SFabrice Gasnier 9420da98c7bSFabrice Gasnier /* Either restore or read calibration result for future reference */ 9430da98c7bSFabrice Gasnier if (calib) 944cd64d357SAlexandru Ardelean ret = stm32h7_adc_restore_selfcalib(indio_dev); 9450da98c7bSFabrice Gasnier else 946cd64d357SAlexandru Ardelean ret = stm32h7_adc_read_selfcalib(indio_dev); 94795e339b6SFabrice Gasnier if (ret) 94895e339b6SFabrice Gasnier goto disable; 94995e339b6SFabrice Gasnier 95095e339b6SFabrice Gasnier stm32_adc_writel(adc, STM32H7_ADC_PCSEL, adc->pcsel); 95195e339b6SFabrice Gasnier 95295e339b6SFabrice Gasnier return 0; 95395e339b6SFabrice Gasnier 95495e339b6SFabrice Gasnier disable: 955cd64d357SAlexandru Ardelean stm32h7_adc_disable(indio_dev); 95695e339b6SFabrice Gasnier pwr_dwn: 95795e339b6SFabrice Gasnier stm32h7_adc_enter_pwr_down(adc); 95895e339b6SFabrice Gasnier 95995e339b6SFabrice Gasnier return ret; 96095e339b6SFabrice Gasnier } 96195e339b6SFabrice Gasnier 962cd64d357SAlexandru Ardelean static void stm32h7_adc_unprepare(struct iio_dev *indio_dev) 96395e339b6SFabrice Gasnier { 964cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 965cd64d357SAlexandru Ardelean 966cd64d357SAlexandru Ardelean stm32h7_adc_disable(indio_dev); 96795e339b6SFabrice Gasnier stm32h7_adc_enter_pwr_down(adc); 96895e339b6SFabrice Gasnier } 96995e339b6SFabrice Gasnier 9700f883b22SFabrice Gasnier /** 971da9b9485SFabrice Gasnier * stm32_adc_conf_scan_seq() - Build regular channels scan sequence 972da9b9485SFabrice Gasnier * @indio_dev: IIO device 973da9b9485SFabrice Gasnier * @scan_mask: channels to be converted 974da9b9485SFabrice Gasnier * 975da9b9485SFabrice Gasnier * Conversion sequence : 976ee2ac1cdSFabrice Gasnier * Apply sampling time settings for all channels. 977da9b9485SFabrice Gasnier * Configure ADC scan sequence based on selected channels in scan_mask. 978da9b9485SFabrice Gasnier * Add channels to SQR registers, from scan_mask LSB to MSB, then 979da9b9485SFabrice Gasnier * program sequence len. 980da9b9485SFabrice Gasnier */ 981da9b9485SFabrice Gasnier static int stm32_adc_conf_scan_seq(struct iio_dev *indio_dev, 982da9b9485SFabrice Gasnier const unsigned long *scan_mask) 983da9b9485SFabrice Gasnier { 984da9b9485SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 98564ad7f64SFabrice Gasnier const struct stm32_adc_regs *sqr = adc->cfg->regs->sqr; 986da9b9485SFabrice Gasnier const struct iio_chan_spec *chan; 987da9b9485SFabrice Gasnier u32 val, bit; 988da9b9485SFabrice Gasnier int i = 0; 989da9b9485SFabrice Gasnier 990ee2ac1cdSFabrice Gasnier /* Apply sampling time settings */ 991ee2ac1cdSFabrice Gasnier stm32_adc_writel(adc, adc->cfg->regs->smpr[0], adc->smpr_val[0]); 992ee2ac1cdSFabrice Gasnier stm32_adc_writel(adc, adc->cfg->regs->smpr[1], adc->smpr_val[1]); 993ee2ac1cdSFabrice Gasnier 994da9b9485SFabrice Gasnier for_each_set_bit(bit, scan_mask, indio_dev->masklength) { 995da9b9485SFabrice Gasnier chan = indio_dev->channels + bit; 996da9b9485SFabrice Gasnier /* 997da9b9485SFabrice Gasnier * Assign one channel per SQ entry in regular 998da9b9485SFabrice Gasnier * sequence, starting with SQ1. 999da9b9485SFabrice Gasnier */ 1000da9b9485SFabrice Gasnier i++; 1001da9b9485SFabrice Gasnier if (i > STM32_ADC_MAX_SQ) 1002da9b9485SFabrice Gasnier return -EINVAL; 1003da9b9485SFabrice Gasnier 1004da9b9485SFabrice Gasnier dev_dbg(&indio_dev->dev, "%s chan %d to SQ%d\n", 1005da9b9485SFabrice Gasnier __func__, chan->channel, i); 1006da9b9485SFabrice Gasnier 100764ad7f64SFabrice Gasnier val = stm32_adc_readl(adc, sqr[i].reg); 100864ad7f64SFabrice Gasnier val &= ~sqr[i].mask; 100964ad7f64SFabrice Gasnier val |= chan->channel << sqr[i].shift; 101064ad7f64SFabrice Gasnier stm32_adc_writel(adc, sqr[i].reg, val); 1011da9b9485SFabrice Gasnier } 1012da9b9485SFabrice Gasnier 1013da9b9485SFabrice Gasnier if (!i) 1014da9b9485SFabrice Gasnier return -EINVAL; 1015da9b9485SFabrice Gasnier 1016da9b9485SFabrice Gasnier /* Sequence len */ 101764ad7f64SFabrice Gasnier val = stm32_adc_readl(adc, sqr[0].reg); 101864ad7f64SFabrice Gasnier val &= ~sqr[0].mask; 101964ad7f64SFabrice Gasnier val |= ((i - 1) << sqr[0].shift); 102064ad7f64SFabrice Gasnier stm32_adc_writel(adc, sqr[0].reg, val); 1021da9b9485SFabrice Gasnier 1022da9b9485SFabrice Gasnier return 0; 1023da9b9485SFabrice Gasnier } 1024da9b9485SFabrice Gasnier 1025da9b9485SFabrice Gasnier /** 1026da9b9485SFabrice Gasnier * stm32_adc_get_trig_extsel() - Get external trigger selection 10271cd92d42SFabrice Gasnier * @indio_dev: IIO device structure 1028da9b9485SFabrice Gasnier * @trig: trigger 1029da9b9485SFabrice Gasnier * 1030da9b9485SFabrice Gasnier * Returns trigger extsel value, if trig matches, -EINVAL otherwise. 1031da9b9485SFabrice Gasnier */ 103264ad7f64SFabrice Gasnier static int stm32_adc_get_trig_extsel(struct iio_dev *indio_dev, 103364ad7f64SFabrice Gasnier struct iio_trigger *trig) 1034da9b9485SFabrice Gasnier { 103564ad7f64SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 1036f24a33b3SFabrice Gasnier int i; 1037f24a33b3SFabrice Gasnier 1038f24a33b3SFabrice Gasnier /* lookup triggers registered by stm32 timer trigger driver */ 103964ad7f64SFabrice Gasnier for (i = 0; adc->cfg->trigs[i].name; i++) { 1040f24a33b3SFabrice Gasnier /** 1041f24a33b3SFabrice Gasnier * Checking both stm32 timer trigger type and trig name 1042f24a33b3SFabrice Gasnier * should be safe against arbitrary trigger names. 1043f24a33b3SFabrice Gasnier */ 1044f0b638a7SFabrice Gasnier if ((is_stm32_timer_trigger(trig) || 1045f0b638a7SFabrice Gasnier is_stm32_lptim_trigger(trig)) && 104664ad7f64SFabrice Gasnier !strcmp(adc->cfg->trigs[i].name, trig->name)) { 104764ad7f64SFabrice Gasnier return adc->cfg->trigs[i].extsel; 1048f24a33b3SFabrice Gasnier } 1049f24a33b3SFabrice Gasnier } 1050f24a33b3SFabrice Gasnier 1051da9b9485SFabrice Gasnier return -EINVAL; 1052da9b9485SFabrice Gasnier } 1053da9b9485SFabrice Gasnier 1054da9b9485SFabrice Gasnier /** 1055da9b9485SFabrice Gasnier * stm32_adc_set_trig() - Set a regular trigger 1056da9b9485SFabrice Gasnier * @indio_dev: IIO device 1057da9b9485SFabrice Gasnier * @trig: IIO trigger 1058da9b9485SFabrice Gasnier * 1059da9b9485SFabrice Gasnier * Set trigger source/polarity (e.g. SW, or HW with polarity) : 1060da9b9485SFabrice Gasnier * - if HW trigger disabled (e.g. trig == NULL, conversion launched by sw) 1061da9b9485SFabrice Gasnier * - if HW trigger enabled, set source & polarity 1062da9b9485SFabrice Gasnier */ 1063da9b9485SFabrice Gasnier static int stm32_adc_set_trig(struct iio_dev *indio_dev, 1064da9b9485SFabrice Gasnier struct iio_trigger *trig) 1065da9b9485SFabrice Gasnier { 1066da9b9485SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 1067da9b9485SFabrice Gasnier u32 val, extsel = 0, exten = STM32_EXTEN_SWTRIG; 1068da9b9485SFabrice Gasnier unsigned long flags; 1069da9b9485SFabrice Gasnier int ret; 1070da9b9485SFabrice Gasnier 1071da9b9485SFabrice Gasnier if (trig) { 107264ad7f64SFabrice Gasnier ret = stm32_adc_get_trig_extsel(indio_dev, trig); 1073da9b9485SFabrice Gasnier if (ret < 0) 1074da9b9485SFabrice Gasnier return ret; 1075da9b9485SFabrice Gasnier 1076da9b9485SFabrice Gasnier /* set trigger source and polarity (default to rising edge) */ 1077da9b9485SFabrice Gasnier extsel = ret; 1078732f2dc4SFabrice Gasnier exten = adc->trigger_polarity + STM32_EXTEN_HWTRIG_RISING_EDGE; 1079da9b9485SFabrice Gasnier } 1080da9b9485SFabrice Gasnier 1081da9b9485SFabrice Gasnier spin_lock_irqsave(&adc->lock, flags); 108264ad7f64SFabrice Gasnier val = stm32_adc_readl(adc, adc->cfg->regs->exten.reg); 108364ad7f64SFabrice Gasnier val &= ~(adc->cfg->regs->exten.mask | adc->cfg->regs->extsel.mask); 108464ad7f64SFabrice Gasnier val |= exten << adc->cfg->regs->exten.shift; 108564ad7f64SFabrice Gasnier val |= extsel << adc->cfg->regs->extsel.shift; 108664ad7f64SFabrice Gasnier stm32_adc_writel(adc, adc->cfg->regs->exten.reg, val); 1087da9b9485SFabrice Gasnier spin_unlock_irqrestore(&adc->lock, flags); 1088da9b9485SFabrice Gasnier 1089da9b9485SFabrice Gasnier return 0; 1090da9b9485SFabrice Gasnier } 1091da9b9485SFabrice Gasnier 1092732f2dc4SFabrice Gasnier static int stm32_adc_set_trig_pol(struct iio_dev *indio_dev, 1093732f2dc4SFabrice Gasnier const struct iio_chan_spec *chan, 1094732f2dc4SFabrice Gasnier unsigned int type) 1095732f2dc4SFabrice Gasnier { 1096732f2dc4SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 1097732f2dc4SFabrice Gasnier 1098732f2dc4SFabrice Gasnier adc->trigger_polarity = type; 1099732f2dc4SFabrice Gasnier 1100732f2dc4SFabrice Gasnier return 0; 1101732f2dc4SFabrice Gasnier } 1102732f2dc4SFabrice Gasnier 1103732f2dc4SFabrice Gasnier static int stm32_adc_get_trig_pol(struct iio_dev *indio_dev, 1104732f2dc4SFabrice Gasnier const struct iio_chan_spec *chan) 1105732f2dc4SFabrice Gasnier { 1106732f2dc4SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 1107732f2dc4SFabrice Gasnier 1108732f2dc4SFabrice Gasnier return adc->trigger_polarity; 1109732f2dc4SFabrice Gasnier } 1110732f2dc4SFabrice Gasnier 1111732f2dc4SFabrice Gasnier static const char * const stm32_trig_pol_items[] = { 1112732f2dc4SFabrice Gasnier "rising-edge", "falling-edge", "both-edges", 1113732f2dc4SFabrice Gasnier }; 1114732f2dc4SFabrice Gasnier 11152763ea05SFabrice Gasnier static const struct iio_enum stm32_adc_trig_pol = { 1116732f2dc4SFabrice Gasnier .items = stm32_trig_pol_items, 1117732f2dc4SFabrice Gasnier .num_items = ARRAY_SIZE(stm32_trig_pol_items), 1118732f2dc4SFabrice Gasnier .get = stm32_adc_get_trig_pol, 1119732f2dc4SFabrice Gasnier .set = stm32_adc_set_trig_pol, 1120732f2dc4SFabrice Gasnier }; 1121732f2dc4SFabrice Gasnier 1122da9b9485SFabrice Gasnier /** 11230f883b22SFabrice Gasnier * stm32_adc_single_conv() - Performs a single conversion 11240f883b22SFabrice Gasnier * @indio_dev: IIO device 11250f883b22SFabrice Gasnier * @chan: IIO channel 11260f883b22SFabrice Gasnier * @res: conversion result 11270f883b22SFabrice Gasnier * 11280f883b22SFabrice Gasnier * The function performs a single conversion on a given channel: 1129ee2ac1cdSFabrice Gasnier * - Apply sampling time settings 11300f883b22SFabrice Gasnier * - Program sequencer with one channel (e.g. in SQ1 with len = 1) 11310f883b22SFabrice Gasnier * - Use SW trigger 11320f883b22SFabrice Gasnier * - Start conversion, then wait for interrupt completion. 11330f883b22SFabrice Gasnier */ 11340f883b22SFabrice Gasnier static int stm32_adc_single_conv(struct iio_dev *indio_dev, 11350f883b22SFabrice Gasnier const struct iio_chan_spec *chan, 11360f883b22SFabrice Gasnier int *res) 11370f883b22SFabrice Gasnier { 11380f883b22SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 11399bdbb113SFabrice Gasnier struct device *dev = indio_dev->dev.parent; 114064ad7f64SFabrice Gasnier const struct stm32_adc_regspec *regs = adc->cfg->regs; 11410f883b22SFabrice Gasnier long timeout; 11420f883b22SFabrice Gasnier u32 val; 11430f883b22SFabrice Gasnier int ret; 11440f883b22SFabrice Gasnier 11450f883b22SFabrice Gasnier reinit_completion(&adc->completion); 11460f883b22SFabrice Gasnier 1147da9b9485SFabrice Gasnier adc->bufi = 0; 11480f883b22SFabrice Gasnier 11499bdbb113SFabrice Gasnier ret = pm_runtime_get_sync(dev); 11509bdbb113SFabrice Gasnier if (ret < 0) { 11519bdbb113SFabrice Gasnier pm_runtime_put_noidle(dev); 115295e339b6SFabrice Gasnier return ret; 115395e339b6SFabrice Gasnier } 115495e339b6SFabrice Gasnier 1155ee2ac1cdSFabrice Gasnier /* Apply sampling time settings */ 1156ee2ac1cdSFabrice Gasnier stm32_adc_writel(adc, regs->smpr[0], adc->smpr_val[0]); 1157ee2ac1cdSFabrice Gasnier stm32_adc_writel(adc, regs->smpr[1], adc->smpr_val[1]); 1158ee2ac1cdSFabrice Gasnier 1159da9b9485SFabrice Gasnier /* Program chan number in regular sequence (SQ1) */ 116064ad7f64SFabrice Gasnier val = stm32_adc_readl(adc, regs->sqr[1].reg); 116164ad7f64SFabrice Gasnier val &= ~regs->sqr[1].mask; 116264ad7f64SFabrice Gasnier val |= chan->channel << regs->sqr[1].shift; 116364ad7f64SFabrice Gasnier stm32_adc_writel(adc, regs->sqr[1].reg, val); 11640f883b22SFabrice Gasnier 11650f883b22SFabrice Gasnier /* Set regular sequence len (0 for 1 conversion) */ 116664ad7f64SFabrice Gasnier stm32_adc_clr_bits(adc, regs->sqr[0].reg, regs->sqr[0].mask); 11670f883b22SFabrice Gasnier 11680f883b22SFabrice Gasnier /* Trigger detection disabled (conversion can be launched in SW) */ 116964ad7f64SFabrice Gasnier stm32_adc_clr_bits(adc, regs->exten.reg, regs->exten.mask); 11700f883b22SFabrice Gasnier 11710f883b22SFabrice Gasnier stm32_adc_conv_irq_enable(adc); 11720f883b22SFabrice Gasnier 1173cd64d357SAlexandru Ardelean adc->cfg->start_conv(indio_dev, false); 11740f883b22SFabrice Gasnier 11750f883b22SFabrice Gasnier timeout = wait_for_completion_interruptible_timeout( 11760f883b22SFabrice Gasnier &adc->completion, STM32_ADC_TIMEOUT); 11770f883b22SFabrice Gasnier if (timeout == 0) { 11780f883b22SFabrice Gasnier ret = -ETIMEDOUT; 11790f883b22SFabrice Gasnier } else if (timeout < 0) { 11800f883b22SFabrice Gasnier ret = timeout; 11810f883b22SFabrice Gasnier } else { 1182da9b9485SFabrice Gasnier *res = adc->buffer[0]; 11830f883b22SFabrice Gasnier ret = IIO_VAL_INT; 11840f883b22SFabrice Gasnier } 11850f883b22SFabrice Gasnier 1186cd64d357SAlexandru Ardelean adc->cfg->stop_conv(indio_dev); 11870f883b22SFabrice Gasnier 11880f883b22SFabrice Gasnier stm32_adc_conv_irq_disable(adc); 11890f883b22SFabrice Gasnier 11909bdbb113SFabrice Gasnier pm_runtime_mark_last_busy(dev); 11919bdbb113SFabrice Gasnier pm_runtime_put_autosuspend(dev); 119295e339b6SFabrice Gasnier 11930f883b22SFabrice Gasnier return ret; 11940f883b22SFabrice Gasnier } 11950f883b22SFabrice Gasnier 11960f883b22SFabrice Gasnier static int stm32_adc_read_raw(struct iio_dev *indio_dev, 11970f883b22SFabrice Gasnier struct iio_chan_spec const *chan, 11980f883b22SFabrice Gasnier int *val, int *val2, long mask) 11990f883b22SFabrice Gasnier { 12000f883b22SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 12010f883b22SFabrice Gasnier int ret; 12020f883b22SFabrice Gasnier 12030f883b22SFabrice Gasnier switch (mask) { 12040f883b22SFabrice Gasnier case IIO_CHAN_INFO_RAW: 12050f883b22SFabrice Gasnier ret = iio_device_claim_direct_mode(indio_dev); 12060f883b22SFabrice Gasnier if (ret) 12070f883b22SFabrice Gasnier return ret; 12080f883b22SFabrice Gasnier if (chan->type == IIO_VOLTAGE) 12090f883b22SFabrice Gasnier ret = stm32_adc_single_conv(indio_dev, chan, val); 12100f883b22SFabrice Gasnier else 12110f883b22SFabrice Gasnier ret = -EINVAL; 12120f883b22SFabrice Gasnier iio_device_release_direct_mode(indio_dev); 12130f883b22SFabrice Gasnier return ret; 12140f883b22SFabrice Gasnier 12150f883b22SFabrice Gasnier case IIO_CHAN_INFO_SCALE: 12163fb2e24eSFabrice Gasnier if (chan->differential) { 12173fb2e24eSFabrice Gasnier *val = adc->common->vref_mv * 2; 12183fb2e24eSFabrice Gasnier *val2 = chan->scan_type.realbits; 12193fb2e24eSFabrice Gasnier } else { 12200f883b22SFabrice Gasnier *val = adc->common->vref_mv; 12210f883b22SFabrice Gasnier *val2 = chan->scan_type.realbits; 12223fb2e24eSFabrice Gasnier } 12230f883b22SFabrice Gasnier return IIO_VAL_FRACTIONAL_LOG2; 12240f883b22SFabrice Gasnier 12253fb2e24eSFabrice Gasnier case IIO_CHAN_INFO_OFFSET: 12263fb2e24eSFabrice Gasnier if (chan->differential) 12273fb2e24eSFabrice Gasnier /* ADC_full_scale / 2 */ 12283fb2e24eSFabrice Gasnier *val = -((1 << chan->scan_type.realbits) / 2); 12293fb2e24eSFabrice Gasnier else 12303fb2e24eSFabrice Gasnier *val = 0; 12313fb2e24eSFabrice Gasnier return IIO_VAL_INT; 12323fb2e24eSFabrice Gasnier 12330f883b22SFabrice Gasnier default: 12340f883b22SFabrice Gasnier return -EINVAL; 12350f883b22SFabrice Gasnier } 12360f883b22SFabrice Gasnier } 12370f883b22SFabrice Gasnier 1238cc06e67dSFabrice Gasnier static irqreturn_t stm32_adc_threaded_isr(int irq, void *data) 1239cc06e67dSFabrice Gasnier { 1240cd64d357SAlexandru Ardelean struct iio_dev *indio_dev = data; 1241cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 1242cc06e67dSFabrice Gasnier const struct stm32_adc_regspec *regs = adc->cfg->regs; 1243cc06e67dSFabrice Gasnier u32 status = stm32_adc_readl(adc, regs->isr_eoc.reg); 1244cc06e67dSFabrice Gasnier 1245cc06e67dSFabrice Gasnier if (status & regs->isr_ovr.mask) 1246cc06e67dSFabrice Gasnier dev_err(&indio_dev->dev, "Overrun, stopping: restart needed\n"); 1247cc06e67dSFabrice Gasnier 1248cc06e67dSFabrice Gasnier return IRQ_HANDLED; 1249cc06e67dSFabrice Gasnier } 1250cc06e67dSFabrice Gasnier 12510f883b22SFabrice Gasnier static irqreturn_t stm32_adc_isr(int irq, void *data) 12520f883b22SFabrice Gasnier { 1253cd64d357SAlexandru Ardelean struct iio_dev *indio_dev = data; 1254cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 125564ad7f64SFabrice Gasnier const struct stm32_adc_regspec *regs = adc->cfg->regs; 125664ad7f64SFabrice Gasnier u32 status = stm32_adc_readl(adc, regs->isr_eoc.reg); 12570f883b22SFabrice Gasnier 1258cc06e67dSFabrice Gasnier if (status & regs->isr_ovr.mask) { 1259cc06e67dSFabrice Gasnier /* 1260cc06e67dSFabrice Gasnier * Overrun occurred on regular conversions: data for wrong 1261cc06e67dSFabrice Gasnier * channel may be read. Unconditionally disable interrupts 1262cc06e67dSFabrice Gasnier * to stop processing data and print error message. 1263cc06e67dSFabrice Gasnier * Restarting the capture can be done by disabling, then 1264cc06e67dSFabrice Gasnier * re-enabling it (e.g. write 0, then 1 to buffer/enable). 1265cc06e67dSFabrice Gasnier */ 1266cc06e67dSFabrice Gasnier stm32_adc_ovr_irq_disable(adc); 1267cc06e67dSFabrice Gasnier stm32_adc_conv_irq_disable(adc); 1268cc06e67dSFabrice Gasnier return IRQ_WAKE_THREAD; 1269cc06e67dSFabrice Gasnier } 1270cc06e67dSFabrice Gasnier 127164ad7f64SFabrice Gasnier if (status & regs->isr_eoc.mask) { 1272da9b9485SFabrice Gasnier /* Reading DR also clears EOC status flag */ 127364ad7f64SFabrice Gasnier adc->buffer[adc->bufi] = stm32_adc_readw(adc, regs->dr); 1274da9b9485SFabrice Gasnier if (iio_buffer_enabled(indio_dev)) { 1275da9b9485SFabrice Gasnier adc->bufi++; 1276da9b9485SFabrice Gasnier if (adc->bufi >= adc->num_conv) { 1277da9b9485SFabrice Gasnier stm32_adc_conv_irq_disable(adc); 1278da9b9485SFabrice Gasnier iio_trigger_poll(indio_dev->trig); 1279da9b9485SFabrice Gasnier } 1280da9b9485SFabrice Gasnier } else { 12810f883b22SFabrice Gasnier complete(&adc->completion); 1282da9b9485SFabrice Gasnier } 12830f883b22SFabrice Gasnier return IRQ_HANDLED; 12840f883b22SFabrice Gasnier } 12850f883b22SFabrice Gasnier 12860f883b22SFabrice Gasnier return IRQ_NONE; 12870f883b22SFabrice Gasnier } 12880f883b22SFabrice Gasnier 1289da9b9485SFabrice Gasnier /** 1290da9b9485SFabrice Gasnier * stm32_adc_validate_trigger() - validate trigger for stm32 adc 1291da9b9485SFabrice Gasnier * @indio_dev: IIO device 1292da9b9485SFabrice Gasnier * @trig: new trigger 1293da9b9485SFabrice Gasnier * 1294da9b9485SFabrice Gasnier * Returns: 0 if trig matches one of the triggers registered by stm32 adc 1295da9b9485SFabrice Gasnier * driver, -EINVAL otherwise. 1296da9b9485SFabrice Gasnier */ 1297da9b9485SFabrice Gasnier static int stm32_adc_validate_trigger(struct iio_dev *indio_dev, 1298da9b9485SFabrice Gasnier struct iio_trigger *trig) 1299da9b9485SFabrice Gasnier { 130064ad7f64SFabrice Gasnier return stm32_adc_get_trig_extsel(indio_dev, trig) < 0 ? -EINVAL : 0; 1301da9b9485SFabrice Gasnier } 1302da9b9485SFabrice Gasnier 13032763ea05SFabrice Gasnier static int stm32_adc_set_watermark(struct iio_dev *indio_dev, unsigned int val) 13042763ea05SFabrice Gasnier { 13052763ea05SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 13062763ea05SFabrice Gasnier unsigned int watermark = STM32_DMA_BUFFER_SIZE / 2; 130704e491caSFabrice Gasnier unsigned int rx_buf_sz = STM32_DMA_BUFFER_SIZE; 13082763ea05SFabrice Gasnier 13092763ea05SFabrice Gasnier /* 13102763ea05SFabrice Gasnier * dma cyclic transfers are used, buffer is split into two periods. 13112763ea05SFabrice Gasnier * There should be : 13122763ea05SFabrice Gasnier * - always one buffer (period) dma is working on 1313*499da8bdSOlivier Moysan * - one buffer (period) driver can push data. 13142763ea05SFabrice Gasnier */ 13152763ea05SFabrice Gasnier watermark = min(watermark, val * (unsigned)(sizeof(u16))); 131604e491caSFabrice Gasnier adc->rx_buf_sz = min(rx_buf_sz, watermark * 2 * adc->num_conv); 13172763ea05SFabrice Gasnier 13182763ea05SFabrice Gasnier return 0; 13192763ea05SFabrice Gasnier } 13202763ea05SFabrice Gasnier 1321da9b9485SFabrice Gasnier static int stm32_adc_update_scan_mode(struct iio_dev *indio_dev, 1322da9b9485SFabrice Gasnier const unsigned long *scan_mask) 1323da9b9485SFabrice Gasnier { 1324da9b9485SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 13259bdbb113SFabrice Gasnier struct device *dev = indio_dev->dev.parent; 1326da9b9485SFabrice Gasnier int ret; 1327da9b9485SFabrice Gasnier 13289bdbb113SFabrice Gasnier ret = pm_runtime_get_sync(dev); 13299bdbb113SFabrice Gasnier if (ret < 0) { 13309bdbb113SFabrice Gasnier pm_runtime_put_noidle(dev); 13319bdbb113SFabrice Gasnier return ret; 13329bdbb113SFabrice Gasnier } 13339bdbb113SFabrice Gasnier 1334da9b9485SFabrice Gasnier adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength); 1335da9b9485SFabrice Gasnier 1336da9b9485SFabrice Gasnier ret = stm32_adc_conf_scan_seq(indio_dev, scan_mask); 13379bdbb113SFabrice Gasnier pm_runtime_mark_last_busy(dev); 13389bdbb113SFabrice Gasnier pm_runtime_put_autosuspend(dev); 1339da9b9485SFabrice Gasnier 13409bdbb113SFabrice Gasnier return ret; 1341da9b9485SFabrice Gasnier } 1342da9b9485SFabrice Gasnier 13430f883b22SFabrice Gasnier static int stm32_adc_of_xlate(struct iio_dev *indio_dev, 13440f883b22SFabrice Gasnier const struct of_phandle_args *iiospec) 13450f883b22SFabrice Gasnier { 13460f883b22SFabrice Gasnier int i; 13470f883b22SFabrice Gasnier 13480f883b22SFabrice Gasnier for (i = 0; i < indio_dev->num_channels; i++) 13490f883b22SFabrice Gasnier if (indio_dev->channels[i].channel == iiospec->args[0]) 13500f883b22SFabrice Gasnier return i; 13510f883b22SFabrice Gasnier 13520f883b22SFabrice Gasnier return -EINVAL; 13530f883b22SFabrice Gasnier } 13540f883b22SFabrice Gasnier 13550f883b22SFabrice Gasnier /** 13560f883b22SFabrice Gasnier * stm32_adc_debugfs_reg_access - read or write register value 13571cd92d42SFabrice Gasnier * @indio_dev: IIO device structure 13581cd92d42SFabrice Gasnier * @reg: register offset 13591cd92d42SFabrice Gasnier * @writeval: value to write 13601cd92d42SFabrice Gasnier * @readval: value to read 13610f883b22SFabrice Gasnier * 13620f883b22SFabrice Gasnier * To read a value from an ADC register: 13630f883b22SFabrice Gasnier * echo [ADC reg offset] > direct_reg_access 13640f883b22SFabrice Gasnier * cat direct_reg_access 13650f883b22SFabrice Gasnier * 13660f883b22SFabrice Gasnier * To write a value in a ADC register: 13670f883b22SFabrice Gasnier * echo [ADC_reg_offset] [value] > direct_reg_access 13680f883b22SFabrice Gasnier */ 13690f883b22SFabrice Gasnier static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev, 13700f883b22SFabrice Gasnier unsigned reg, unsigned writeval, 13710f883b22SFabrice Gasnier unsigned *readval) 13720f883b22SFabrice Gasnier { 13730f883b22SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 13749bdbb113SFabrice Gasnier struct device *dev = indio_dev->dev.parent; 13759bdbb113SFabrice Gasnier int ret; 13769bdbb113SFabrice Gasnier 13779bdbb113SFabrice Gasnier ret = pm_runtime_get_sync(dev); 13789bdbb113SFabrice Gasnier if (ret < 0) { 13799bdbb113SFabrice Gasnier pm_runtime_put_noidle(dev); 13809bdbb113SFabrice Gasnier return ret; 13819bdbb113SFabrice Gasnier } 13820f883b22SFabrice Gasnier 13830f883b22SFabrice Gasnier if (!readval) 13840f883b22SFabrice Gasnier stm32_adc_writel(adc, reg, writeval); 13850f883b22SFabrice Gasnier else 13860f883b22SFabrice Gasnier *readval = stm32_adc_readl(adc, reg); 13870f883b22SFabrice Gasnier 13889bdbb113SFabrice Gasnier pm_runtime_mark_last_busy(dev); 13899bdbb113SFabrice Gasnier pm_runtime_put_autosuspend(dev); 13909bdbb113SFabrice Gasnier 13910f883b22SFabrice Gasnier return 0; 13920f883b22SFabrice Gasnier } 13930f883b22SFabrice Gasnier 13940f883b22SFabrice Gasnier static const struct iio_info stm32_adc_iio_info = { 13950f883b22SFabrice Gasnier .read_raw = stm32_adc_read_raw, 1396da9b9485SFabrice Gasnier .validate_trigger = stm32_adc_validate_trigger, 13972763ea05SFabrice Gasnier .hwfifo_set_watermark = stm32_adc_set_watermark, 1398da9b9485SFabrice Gasnier .update_scan_mode = stm32_adc_update_scan_mode, 13990f883b22SFabrice Gasnier .debugfs_reg_access = stm32_adc_debugfs_reg_access, 14000f883b22SFabrice Gasnier .of_xlate = stm32_adc_of_xlate, 14010f883b22SFabrice Gasnier }; 14020f883b22SFabrice Gasnier 14032763ea05SFabrice Gasnier static unsigned int stm32_adc_dma_residue(struct stm32_adc *adc) 14042763ea05SFabrice Gasnier { 14052763ea05SFabrice Gasnier struct dma_tx_state state; 14062763ea05SFabrice Gasnier enum dma_status status; 14072763ea05SFabrice Gasnier 14082763ea05SFabrice Gasnier status = dmaengine_tx_status(adc->dma_chan, 14092763ea05SFabrice Gasnier adc->dma_chan->cookie, 14102763ea05SFabrice Gasnier &state); 14112763ea05SFabrice Gasnier if (status == DMA_IN_PROGRESS) { 14122763ea05SFabrice Gasnier /* Residue is size in bytes from end of buffer */ 14132763ea05SFabrice Gasnier unsigned int i = adc->rx_buf_sz - state.residue; 14142763ea05SFabrice Gasnier unsigned int size; 14152763ea05SFabrice Gasnier 14162763ea05SFabrice Gasnier /* Return available bytes */ 14172763ea05SFabrice Gasnier if (i >= adc->bufi) 14182763ea05SFabrice Gasnier size = i - adc->bufi; 14192763ea05SFabrice Gasnier else 14202763ea05SFabrice Gasnier size = adc->rx_buf_sz + i - adc->bufi; 14212763ea05SFabrice Gasnier 14222763ea05SFabrice Gasnier return size; 14232763ea05SFabrice Gasnier } 14242763ea05SFabrice Gasnier 14252763ea05SFabrice Gasnier return 0; 14262763ea05SFabrice Gasnier } 14272763ea05SFabrice Gasnier 14282763ea05SFabrice Gasnier static void stm32_adc_dma_buffer_done(void *data) 14292763ea05SFabrice Gasnier { 14302763ea05SFabrice Gasnier struct iio_dev *indio_dev = data; 1431e2042d29SOlivier Moysan struct stm32_adc *adc = iio_priv(indio_dev); 1432e2042d29SOlivier Moysan int residue = stm32_adc_dma_residue(adc); 14332763ea05SFabrice Gasnier 1434e2042d29SOlivier Moysan /* 1435e2042d29SOlivier Moysan * In DMA mode the trigger services of IIO are not used 1436e2042d29SOlivier Moysan * (e.g. no call to iio_trigger_poll). 1437e2042d29SOlivier Moysan * Calling irq handler associated to the hardware trigger is not 1438e2042d29SOlivier Moysan * relevant as the conversions have already been done. Data 1439e2042d29SOlivier Moysan * transfers are performed directly in DMA callback instead. 1440e2042d29SOlivier Moysan * This implementation avoids to call trigger irq handler that 1441e2042d29SOlivier Moysan * may sleep, in an atomic context (DMA irq handler context). 1442e2042d29SOlivier Moysan */ 1443e2042d29SOlivier Moysan dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi); 1444e2042d29SOlivier Moysan 1445e2042d29SOlivier Moysan while (residue >= indio_dev->scan_bytes) { 1446e2042d29SOlivier Moysan u16 *buffer = (u16 *)&adc->rx_buf[adc->bufi]; 1447e2042d29SOlivier Moysan 1448e2042d29SOlivier Moysan iio_push_to_buffers(indio_dev, buffer); 1449e2042d29SOlivier Moysan 1450e2042d29SOlivier Moysan residue -= indio_dev->scan_bytes; 1451e2042d29SOlivier Moysan adc->bufi += indio_dev->scan_bytes; 1452e2042d29SOlivier Moysan if (adc->bufi >= adc->rx_buf_sz) 1453e2042d29SOlivier Moysan adc->bufi = 0; 1454e2042d29SOlivier Moysan } 14552763ea05SFabrice Gasnier } 14562763ea05SFabrice Gasnier 14572763ea05SFabrice Gasnier static int stm32_adc_dma_start(struct iio_dev *indio_dev) 14582763ea05SFabrice Gasnier { 14592763ea05SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 14602763ea05SFabrice Gasnier struct dma_async_tx_descriptor *desc; 14612763ea05SFabrice Gasnier dma_cookie_t cookie; 14622763ea05SFabrice Gasnier int ret; 14632763ea05SFabrice Gasnier 14642763ea05SFabrice Gasnier if (!adc->dma_chan) 14652763ea05SFabrice Gasnier return 0; 14662763ea05SFabrice Gasnier 14672763ea05SFabrice Gasnier dev_dbg(&indio_dev->dev, "%s size=%d watermark=%d\n", __func__, 14682763ea05SFabrice Gasnier adc->rx_buf_sz, adc->rx_buf_sz / 2); 14692763ea05SFabrice Gasnier 14702763ea05SFabrice Gasnier /* Prepare a DMA cyclic transaction */ 14712763ea05SFabrice Gasnier desc = dmaengine_prep_dma_cyclic(adc->dma_chan, 14722763ea05SFabrice Gasnier adc->rx_dma_buf, 14732763ea05SFabrice Gasnier adc->rx_buf_sz, adc->rx_buf_sz / 2, 14742763ea05SFabrice Gasnier DMA_DEV_TO_MEM, 14752763ea05SFabrice Gasnier DMA_PREP_INTERRUPT); 14762763ea05SFabrice Gasnier if (!desc) 14772763ea05SFabrice Gasnier return -EBUSY; 14782763ea05SFabrice Gasnier 14792763ea05SFabrice Gasnier desc->callback = stm32_adc_dma_buffer_done; 14802763ea05SFabrice Gasnier desc->callback_param = indio_dev; 14812763ea05SFabrice Gasnier 14822763ea05SFabrice Gasnier cookie = dmaengine_submit(desc); 14832763ea05SFabrice Gasnier ret = dma_submit_error(cookie); 14842763ea05SFabrice Gasnier if (ret) { 1485e6afcf6cSFabrice Gasnier dmaengine_terminate_sync(adc->dma_chan); 14862763ea05SFabrice Gasnier return ret; 14872763ea05SFabrice Gasnier } 14882763ea05SFabrice Gasnier 14892763ea05SFabrice Gasnier /* Issue pending DMA requests */ 14902763ea05SFabrice Gasnier dma_async_issue_pending(adc->dma_chan); 14912763ea05SFabrice Gasnier 14922763ea05SFabrice Gasnier return 0; 14932763ea05SFabrice Gasnier } 14942763ea05SFabrice Gasnier 1495f11d59d8SLars-Peter Clausen static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev) 1496da9b9485SFabrice Gasnier { 1497da9b9485SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 14989bdbb113SFabrice Gasnier struct device *dev = indio_dev->dev.parent; 1499da9b9485SFabrice Gasnier int ret; 1500da9b9485SFabrice Gasnier 15019bdbb113SFabrice Gasnier ret = pm_runtime_get_sync(dev); 15029bdbb113SFabrice Gasnier if (ret < 0) { 15039bdbb113SFabrice Gasnier pm_runtime_put_noidle(dev); 150495e339b6SFabrice Gasnier return ret; 150595e339b6SFabrice Gasnier } 150695e339b6SFabrice Gasnier 1507da9b9485SFabrice Gasnier ret = stm32_adc_set_trig(indio_dev, indio_dev->trig); 1508da9b9485SFabrice Gasnier if (ret) { 1509da9b9485SFabrice Gasnier dev_err(&indio_dev->dev, "Can't set trigger\n"); 15109bdbb113SFabrice Gasnier goto err_pm_put; 1511da9b9485SFabrice Gasnier } 1512da9b9485SFabrice Gasnier 15132763ea05SFabrice Gasnier ret = stm32_adc_dma_start(indio_dev); 15142763ea05SFabrice Gasnier if (ret) { 15152763ea05SFabrice Gasnier dev_err(&indio_dev->dev, "Can't start dma\n"); 15162763ea05SFabrice Gasnier goto err_clr_trig; 15172763ea05SFabrice Gasnier } 15182763ea05SFabrice Gasnier 1519da9b9485SFabrice Gasnier /* Reset adc buffer index */ 1520da9b9485SFabrice Gasnier adc->bufi = 0; 1521da9b9485SFabrice Gasnier 1522cc06e67dSFabrice Gasnier stm32_adc_ovr_irq_enable(adc); 1523cc06e67dSFabrice Gasnier 15242763ea05SFabrice Gasnier if (!adc->dma_chan) 1525da9b9485SFabrice Gasnier stm32_adc_conv_irq_enable(adc); 15262763ea05SFabrice Gasnier 1527cd64d357SAlexandru Ardelean adc->cfg->start_conv(indio_dev, !!adc->dma_chan); 1528da9b9485SFabrice Gasnier 1529da9b9485SFabrice Gasnier return 0; 1530da9b9485SFabrice Gasnier 1531da9b9485SFabrice Gasnier err_clr_trig: 1532da9b9485SFabrice Gasnier stm32_adc_set_trig(indio_dev, NULL); 15339bdbb113SFabrice Gasnier err_pm_put: 15349bdbb113SFabrice Gasnier pm_runtime_mark_last_busy(dev); 15359bdbb113SFabrice Gasnier pm_runtime_put_autosuspend(dev); 1536da9b9485SFabrice Gasnier 1537da9b9485SFabrice Gasnier return ret; 1538da9b9485SFabrice Gasnier } 1539da9b9485SFabrice Gasnier 1540f11d59d8SLars-Peter Clausen static int stm32_adc_buffer_predisable(struct iio_dev *indio_dev) 1541da9b9485SFabrice Gasnier { 1542da9b9485SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 15439bdbb113SFabrice Gasnier struct device *dev = indio_dev->dev.parent; 1544da9b9485SFabrice Gasnier 1545cd64d357SAlexandru Ardelean adc->cfg->stop_conv(indio_dev); 15462763ea05SFabrice Gasnier if (!adc->dma_chan) 1547da9b9485SFabrice Gasnier stm32_adc_conv_irq_disable(adc); 1548da9b9485SFabrice Gasnier 1549cc06e67dSFabrice Gasnier stm32_adc_ovr_irq_disable(adc); 1550cc06e67dSFabrice Gasnier 15512763ea05SFabrice Gasnier if (adc->dma_chan) 1552e6afcf6cSFabrice Gasnier dmaengine_terminate_sync(adc->dma_chan); 15532763ea05SFabrice Gasnier 1554da9b9485SFabrice Gasnier if (stm32_adc_set_trig(indio_dev, NULL)) 1555da9b9485SFabrice Gasnier dev_err(&indio_dev->dev, "Can't clear trigger\n"); 1556da9b9485SFabrice Gasnier 15579bdbb113SFabrice Gasnier pm_runtime_mark_last_busy(dev); 15589bdbb113SFabrice Gasnier pm_runtime_put_autosuspend(dev); 155949ad8d28SFabrice Gasnier 1560f11d59d8SLars-Peter Clausen return 0; 1561da9b9485SFabrice Gasnier } 1562da9b9485SFabrice Gasnier 1563da9b9485SFabrice Gasnier static const struct iio_buffer_setup_ops stm32_adc_buffer_setup_ops = { 1564da9b9485SFabrice Gasnier .postenable = &stm32_adc_buffer_postenable, 1565da9b9485SFabrice Gasnier .predisable = &stm32_adc_buffer_predisable, 1566da9b9485SFabrice Gasnier }; 1567da9b9485SFabrice Gasnier 1568da9b9485SFabrice Gasnier static irqreturn_t stm32_adc_trigger_handler(int irq, void *p) 1569da9b9485SFabrice Gasnier { 1570da9b9485SFabrice Gasnier struct iio_poll_func *pf = p; 1571da9b9485SFabrice Gasnier struct iio_dev *indio_dev = pf->indio_dev; 1572da9b9485SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 1573da9b9485SFabrice Gasnier 1574da9b9485SFabrice Gasnier dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi); 1575da9b9485SFabrice Gasnier 1576da9b9485SFabrice Gasnier /* reset buffer index */ 1577da9b9485SFabrice Gasnier adc->bufi = 0; 1578da9b9485SFabrice Gasnier iio_push_to_buffers_with_timestamp(indio_dev, adc->buffer, 1579da9b9485SFabrice Gasnier pf->timestamp); 1580da9b9485SFabrice Gasnier iio_trigger_notify_done(indio_dev->trig); 1581da9b9485SFabrice Gasnier 1582da9b9485SFabrice Gasnier /* re-enable eoc irq */ 1583da9b9485SFabrice Gasnier stm32_adc_conv_irq_enable(adc); 1584da9b9485SFabrice Gasnier 1585da9b9485SFabrice Gasnier return IRQ_HANDLED; 1586da9b9485SFabrice Gasnier } 1587da9b9485SFabrice Gasnier 1588732f2dc4SFabrice Gasnier static const struct iio_chan_spec_ext_info stm32_adc_ext_info[] = { 1589732f2dc4SFabrice Gasnier IIO_ENUM("trigger_polarity", IIO_SHARED_BY_ALL, &stm32_adc_trig_pol), 1590732f2dc4SFabrice Gasnier { 1591732f2dc4SFabrice Gasnier .name = "trigger_polarity_available", 1592732f2dc4SFabrice Gasnier .shared = IIO_SHARED_BY_ALL, 1593732f2dc4SFabrice Gasnier .read = iio_enum_available_read, 1594732f2dc4SFabrice Gasnier .private = (uintptr_t)&stm32_adc_trig_pol, 1595732f2dc4SFabrice Gasnier }, 1596732f2dc4SFabrice Gasnier {}, 1597732f2dc4SFabrice Gasnier }; 1598732f2dc4SFabrice Gasnier 159925a85bedSFabrice Gasnier static int stm32_adc_of_get_resolution(struct iio_dev *indio_dev) 160025a85bedSFabrice Gasnier { 160125a85bedSFabrice Gasnier struct device_node *node = indio_dev->dev.of_node; 160225a85bedSFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 160325a85bedSFabrice Gasnier unsigned int i; 160425a85bedSFabrice Gasnier u32 res; 160525a85bedSFabrice Gasnier 160625a85bedSFabrice Gasnier if (of_property_read_u32(node, "assigned-resolution-bits", &res)) 160764ad7f64SFabrice Gasnier res = adc->cfg->adc_info->resolutions[0]; 160825a85bedSFabrice Gasnier 160964ad7f64SFabrice Gasnier for (i = 0; i < adc->cfg->adc_info->num_res; i++) 161064ad7f64SFabrice Gasnier if (res == adc->cfg->adc_info->resolutions[i]) 161125a85bedSFabrice Gasnier break; 161264ad7f64SFabrice Gasnier if (i >= adc->cfg->adc_info->num_res) { 161325a85bedSFabrice Gasnier dev_err(&indio_dev->dev, "Bad resolution: %u bits\n", res); 161425a85bedSFabrice Gasnier return -EINVAL; 161525a85bedSFabrice Gasnier } 161625a85bedSFabrice Gasnier 161725a85bedSFabrice Gasnier dev_dbg(&indio_dev->dev, "Using %u bits resolution\n", res); 161825a85bedSFabrice Gasnier adc->res = i; 161925a85bedSFabrice Gasnier 162025a85bedSFabrice Gasnier return 0; 162125a85bedSFabrice Gasnier } 162225a85bedSFabrice Gasnier 1623ee2ac1cdSFabrice Gasnier static void stm32_adc_smpr_init(struct stm32_adc *adc, int channel, u32 smp_ns) 1624ee2ac1cdSFabrice Gasnier { 1625ee2ac1cdSFabrice Gasnier const struct stm32_adc_regs *smpr = &adc->cfg->regs->smp_bits[channel]; 1626ee2ac1cdSFabrice Gasnier u32 period_ns, shift = smpr->shift, mask = smpr->mask; 1627ee2ac1cdSFabrice Gasnier unsigned int smp, r = smpr->reg; 1628ee2ac1cdSFabrice Gasnier 1629ee2ac1cdSFabrice Gasnier /* Determine sampling time (ADC clock cycles) */ 1630ee2ac1cdSFabrice Gasnier period_ns = NSEC_PER_SEC / adc->common->rate; 1631ee2ac1cdSFabrice Gasnier for (smp = 0; smp <= STM32_ADC_MAX_SMP; smp++) 1632ee2ac1cdSFabrice Gasnier if ((period_ns * adc->cfg->smp_cycles[smp]) >= smp_ns) 1633ee2ac1cdSFabrice Gasnier break; 1634ee2ac1cdSFabrice Gasnier if (smp > STM32_ADC_MAX_SMP) 1635ee2ac1cdSFabrice Gasnier smp = STM32_ADC_MAX_SMP; 1636ee2ac1cdSFabrice Gasnier 1637ee2ac1cdSFabrice Gasnier /* pre-build sampling time registers (e.g. smpr1, smpr2) */ 1638ee2ac1cdSFabrice Gasnier adc->smpr_val[r] = (adc->smpr_val[r] & ~mask) | (smp << shift); 1639ee2ac1cdSFabrice Gasnier } 1640ee2ac1cdSFabrice Gasnier 16410f883b22SFabrice Gasnier static void stm32_adc_chan_init_one(struct iio_dev *indio_dev, 16420bae72aaSFabrice Gasnier struct iio_chan_spec *chan, u32 vinp, 16433fb2e24eSFabrice Gasnier u32 vinn, int scan_index, bool differential) 16440f883b22SFabrice Gasnier { 164525a85bedSFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 16460bae72aaSFabrice Gasnier char *name = adc->chan_name[vinp]; 164725a85bedSFabrice Gasnier 16480bae72aaSFabrice Gasnier chan->type = IIO_VOLTAGE; 16490bae72aaSFabrice Gasnier chan->channel = vinp; 16503fb2e24eSFabrice Gasnier if (differential) { 16513fb2e24eSFabrice Gasnier chan->differential = 1; 16523fb2e24eSFabrice Gasnier chan->channel2 = vinn; 16533fb2e24eSFabrice Gasnier snprintf(name, STM32_ADC_CH_SZ, "in%d-in%d", vinp, vinn); 16543fb2e24eSFabrice Gasnier } else { 16550bae72aaSFabrice Gasnier snprintf(name, STM32_ADC_CH_SZ, "in%d", vinp); 16563fb2e24eSFabrice Gasnier } 16570bae72aaSFabrice Gasnier chan->datasheet_name = name; 16580f883b22SFabrice Gasnier chan->scan_index = scan_index; 16590f883b22SFabrice Gasnier chan->indexed = 1; 16600f883b22SFabrice Gasnier chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); 16613fb2e24eSFabrice Gasnier chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | 16623fb2e24eSFabrice Gasnier BIT(IIO_CHAN_INFO_OFFSET); 16630f883b22SFabrice Gasnier chan->scan_type.sign = 'u'; 166464ad7f64SFabrice Gasnier chan->scan_type.realbits = adc->cfg->adc_info->resolutions[adc->res]; 16650f883b22SFabrice Gasnier chan->scan_type.storagebits = 16; 1666732f2dc4SFabrice Gasnier chan->ext_info = stm32_adc_ext_info; 166795e339b6SFabrice Gasnier 166895e339b6SFabrice Gasnier /* pre-build selected channels mask */ 166995e339b6SFabrice Gasnier adc->pcsel |= BIT(chan->channel); 16703fb2e24eSFabrice Gasnier if (differential) { 16713fb2e24eSFabrice Gasnier /* pre-build diff channels mask */ 16723fb2e24eSFabrice Gasnier adc->difsel |= BIT(chan->channel); 16733fb2e24eSFabrice Gasnier /* Also add negative input to pre-selected channels */ 16743fb2e24eSFabrice Gasnier adc->pcsel |= BIT(chan->channel2); 16753fb2e24eSFabrice Gasnier } 16760f883b22SFabrice Gasnier } 16770f883b22SFabrice Gasnier 16780f883b22SFabrice Gasnier static int stm32_adc_chan_of_init(struct iio_dev *indio_dev) 16790f883b22SFabrice Gasnier { 16800f883b22SFabrice Gasnier struct device_node *node = indio_dev->dev.of_node; 168164ad7f64SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 168264ad7f64SFabrice Gasnier const struct stm32_adc_info *adc_info = adc->cfg->adc_info; 16833fb2e24eSFabrice Gasnier struct stm32_adc_diff_channel diff[STM32_ADC_CH_MAX]; 16840f883b22SFabrice Gasnier struct property *prop; 16850f883b22SFabrice Gasnier const __be32 *cur; 16860f883b22SFabrice Gasnier struct iio_chan_spec *channels; 16873fb2e24eSFabrice Gasnier int scan_index = 0, num_channels = 0, num_diff = 0, ret, i; 1688ee2ac1cdSFabrice Gasnier u32 val, smp = 0; 16890f883b22SFabrice Gasnier 16903fb2e24eSFabrice Gasnier ret = of_property_count_u32_elems(node, "st,adc-channels"); 16913fb2e24eSFabrice Gasnier if (ret > adc_info->max_channels) { 16920f883b22SFabrice Gasnier dev_err(&indio_dev->dev, "Bad st,adc-channels?\n"); 16933fb2e24eSFabrice Gasnier return -EINVAL; 16943fb2e24eSFabrice Gasnier } else if (ret > 0) { 16953fb2e24eSFabrice Gasnier num_channels += ret; 16963fb2e24eSFabrice Gasnier } 16973fb2e24eSFabrice Gasnier 16983fb2e24eSFabrice Gasnier ret = of_property_count_elems_of_size(node, "st,adc-diff-channels", 16993fb2e24eSFabrice Gasnier sizeof(*diff)); 17003fb2e24eSFabrice Gasnier if (ret > adc_info->max_channels) { 17013fb2e24eSFabrice Gasnier dev_err(&indio_dev->dev, "Bad st,adc-diff-channels?\n"); 17023fb2e24eSFabrice Gasnier return -EINVAL; 17033fb2e24eSFabrice Gasnier } else if (ret > 0) { 17043fb2e24eSFabrice Gasnier int size = ret * sizeof(*diff) / sizeof(u32); 17053fb2e24eSFabrice Gasnier 17063fb2e24eSFabrice Gasnier num_diff = ret; 17073fb2e24eSFabrice Gasnier num_channels += ret; 17083fb2e24eSFabrice Gasnier ret = of_property_read_u32_array(node, "st,adc-diff-channels", 17093fb2e24eSFabrice Gasnier (u32 *)diff, size); 17103fb2e24eSFabrice Gasnier if (ret) 17113fb2e24eSFabrice Gasnier return ret; 17123fb2e24eSFabrice Gasnier } 17133fb2e24eSFabrice Gasnier 17143fb2e24eSFabrice Gasnier if (!num_channels) { 17153fb2e24eSFabrice Gasnier dev_err(&indio_dev->dev, "No channels configured\n"); 17163fb2e24eSFabrice Gasnier return -ENODATA; 17170f883b22SFabrice Gasnier } 17180f883b22SFabrice Gasnier 1719ee2ac1cdSFabrice Gasnier /* Optional sample time is provided either for each, or all channels */ 1720ee2ac1cdSFabrice Gasnier ret = of_property_count_u32_elems(node, "st,min-sample-time-nsecs"); 1721ee2ac1cdSFabrice Gasnier if (ret > 1 && ret != num_channels) { 1722ee2ac1cdSFabrice Gasnier dev_err(&indio_dev->dev, "Invalid st,min-sample-time-nsecs\n"); 1723ee2ac1cdSFabrice Gasnier return -EINVAL; 1724ee2ac1cdSFabrice Gasnier } 1725ee2ac1cdSFabrice Gasnier 17260f883b22SFabrice Gasnier channels = devm_kcalloc(&indio_dev->dev, num_channels, 17270f883b22SFabrice Gasnier sizeof(struct iio_chan_spec), GFP_KERNEL); 17280f883b22SFabrice Gasnier if (!channels) 17290f883b22SFabrice Gasnier return -ENOMEM; 17300f883b22SFabrice Gasnier 17310f883b22SFabrice Gasnier of_property_for_each_u32(node, "st,adc-channels", prop, cur, val) { 173264ad7f64SFabrice Gasnier if (val >= adc_info->max_channels) { 17330f883b22SFabrice Gasnier dev_err(&indio_dev->dev, "Invalid channel %d\n", val); 17340f883b22SFabrice Gasnier return -EINVAL; 17350f883b22SFabrice Gasnier } 1736ee2ac1cdSFabrice Gasnier 17373fb2e24eSFabrice Gasnier /* Channel can't be configured both as single-ended & diff */ 17383fb2e24eSFabrice Gasnier for (i = 0; i < num_diff; i++) { 17393fb2e24eSFabrice Gasnier if (val == diff[i].vinp) { 17403fb2e24eSFabrice Gasnier dev_err(&indio_dev->dev, 17413fb2e24eSFabrice Gasnier "channel %d miss-configured\n", val); 17423fb2e24eSFabrice Gasnier return -EINVAL; 17433fb2e24eSFabrice Gasnier } 17443fb2e24eSFabrice Gasnier } 17453fb2e24eSFabrice Gasnier stm32_adc_chan_init_one(indio_dev, &channels[scan_index], val, 17463fb2e24eSFabrice Gasnier 0, scan_index, false); 17473fb2e24eSFabrice Gasnier scan_index++; 17483fb2e24eSFabrice Gasnier } 17493fb2e24eSFabrice Gasnier 17503fb2e24eSFabrice Gasnier for (i = 0; i < num_diff; i++) { 17513fb2e24eSFabrice Gasnier if (diff[i].vinp >= adc_info->max_channels || 17523fb2e24eSFabrice Gasnier diff[i].vinn >= adc_info->max_channels) { 17533fb2e24eSFabrice Gasnier dev_err(&indio_dev->dev, "Invalid channel in%d-in%d\n", 17543fb2e24eSFabrice Gasnier diff[i].vinp, diff[i].vinn); 17553fb2e24eSFabrice Gasnier return -EINVAL; 17563fb2e24eSFabrice Gasnier } 17573fb2e24eSFabrice Gasnier stm32_adc_chan_init_one(indio_dev, &channels[scan_index], 17583fb2e24eSFabrice Gasnier diff[i].vinp, diff[i].vinn, scan_index, 17593fb2e24eSFabrice Gasnier true); 17603fb2e24eSFabrice Gasnier scan_index++; 17613fb2e24eSFabrice Gasnier } 17623fb2e24eSFabrice Gasnier 17633fb2e24eSFabrice Gasnier for (i = 0; i < scan_index; i++) { 1764ee2ac1cdSFabrice Gasnier /* 1765ee2ac1cdSFabrice Gasnier * Using of_property_read_u32_index(), smp value will only be 1766ee2ac1cdSFabrice Gasnier * modified if valid u32 value can be decoded. This allows to 1767ee2ac1cdSFabrice Gasnier * get either no value, 1 shared value for all indexes, or one 1768ee2ac1cdSFabrice Gasnier * value per channel. 1769ee2ac1cdSFabrice Gasnier */ 1770ee2ac1cdSFabrice Gasnier of_property_read_u32_index(node, "st,min-sample-time-nsecs", 17713fb2e24eSFabrice Gasnier i, &smp); 17723fb2e24eSFabrice Gasnier /* Prepare sampling time settings */ 17733fb2e24eSFabrice Gasnier stm32_adc_smpr_init(adc, channels[i].channel, smp); 17740f883b22SFabrice Gasnier } 17750f883b22SFabrice Gasnier 17760f883b22SFabrice Gasnier indio_dev->num_channels = scan_index; 17770f883b22SFabrice Gasnier indio_dev->channels = channels; 17780f883b22SFabrice Gasnier 17790f883b22SFabrice Gasnier return 0; 17800f883b22SFabrice Gasnier } 17810f883b22SFabrice Gasnier 178252cd91c2SFabrice Gasnier static int stm32_adc_dma_request(struct device *dev, struct iio_dev *indio_dev) 17832763ea05SFabrice Gasnier { 17842763ea05SFabrice Gasnier struct stm32_adc *adc = iio_priv(indio_dev); 17852763ea05SFabrice Gasnier struct dma_slave_config config; 17862763ea05SFabrice Gasnier int ret; 17872763ea05SFabrice Gasnier 178852cd91c2SFabrice Gasnier adc->dma_chan = dma_request_chan(dev, "rx"); 1789735404b8SPeter Ujfalusi if (IS_ERR(adc->dma_chan)) { 1790735404b8SPeter Ujfalusi ret = PTR_ERR(adc->dma_chan); 1791ce30eeb6SKrzysztof Kozlowski if (ret != -ENODEV) 1792ce30eeb6SKrzysztof Kozlowski return dev_err_probe(dev, ret, 1793ce30eeb6SKrzysztof Kozlowski "DMA channel request failed with\n"); 1794735404b8SPeter Ujfalusi 1795735404b8SPeter Ujfalusi /* DMA is optional: fall back to IRQ mode */ 1796735404b8SPeter Ujfalusi adc->dma_chan = NULL; 17972763ea05SFabrice Gasnier return 0; 1798735404b8SPeter Ujfalusi } 17992763ea05SFabrice Gasnier 18002763ea05SFabrice Gasnier adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev, 18012763ea05SFabrice Gasnier STM32_DMA_BUFFER_SIZE, 18022763ea05SFabrice Gasnier &adc->rx_dma_buf, GFP_KERNEL); 18032763ea05SFabrice Gasnier if (!adc->rx_buf) { 18042763ea05SFabrice Gasnier ret = -ENOMEM; 18052763ea05SFabrice Gasnier goto err_release; 18062763ea05SFabrice Gasnier } 18072763ea05SFabrice Gasnier 18082763ea05SFabrice Gasnier /* Configure DMA channel to read data register */ 18092763ea05SFabrice Gasnier memset(&config, 0, sizeof(config)); 18102763ea05SFabrice Gasnier config.src_addr = (dma_addr_t)adc->common->phys_base; 181164ad7f64SFabrice Gasnier config.src_addr += adc->offset + adc->cfg->regs->dr; 18122763ea05SFabrice Gasnier config.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; 18132763ea05SFabrice Gasnier 18142763ea05SFabrice Gasnier ret = dmaengine_slave_config(adc->dma_chan, &config); 18152763ea05SFabrice Gasnier if (ret) 18162763ea05SFabrice Gasnier goto err_free; 18172763ea05SFabrice Gasnier 18182763ea05SFabrice Gasnier return 0; 18192763ea05SFabrice Gasnier 18202763ea05SFabrice Gasnier err_free: 18212763ea05SFabrice Gasnier dma_free_coherent(adc->dma_chan->device->dev, STM32_DMA_BUFFER_SIZE, 18222763ea05SFabrice Gasnier adc->rx_buf, adc->rx_dma_buf); 18232763ea05SFabrice Gasnier err_release: 18242763ea05SFabrice Gasnier dma_release_channel(adc->dma_chan); 18252763ea05SFabrice Gasnier 18262763ea05SFabrice Gasnier return ret; 18272763ea05SFabrice Gasnier } 18282763ea05SFabrice Gasnier 18290f883b22SFabrice Gasnier static int stm32_adc_probe(struct platform_device *pdev) 18300f883b22SFabrice Gasnier { 18310f883b22SFabrice Gasnier struct iio_dev *indio_dev; 183264ad7f64SFabrice Gasnier struct device *dev = &pdev->dev; 1833e2042d29SOlivier Moysan irqreturn_t (*handler)(int irq, void *p) = NULL; 18340f883b22SFabrice Gasnier struct stm32_adc *adc; 18350f883b22SFabrice Gasnier int ret; 18360f883b22SFabrice Gasnier 18370f883b22SFabrice Gasnier if (!pdev->dev.of_node) 18380f883b22SFabrice Gasnier return -ENODEV; 18390f883b22SFabrice Gasnier 18400f883b22SFabrice Gasnier indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc)); 18410f883b22SFabrice Gasnier if (!indio_dev) 18420f883b22SFabrice Gasnier return -ENOMEM; 18430f883b22SFabrice Gasnier 18440f883b22SFabrice Gasnier adc = iio_priv(indio_dev); 18450f883b22SFabrice Gasnier adc->common = dev_get_drvdata(pdev->dev.parent); 18460f883b22SFabrice Gasnier spin_lock_init(&adc->lock); 18470f883b22SFabrice Gasnier init_completion(&adc->completion); 184864ad7f64SFabrice Gasnier adc->cfg = (const struct stm32_adc_cfg *) 184964ad7f64SFabrice Gasnier of_match_device(dev->driver->of_match_table, dev)->data; 18500f883b22SFabrice Gasnier 18510f883b22SFabrice Gasnier indio_dev->name = dev_name(&pdev->dev); 18520f883b22SFabrice Gasnier indio_dev->dev.of_node = pdev->dev.of_node; 18530f883b22SFabrice Gasnier indio_dev->info = &stm32_adc_iio_info; 1854f0b638a7SFabrice Gasnier indio_dev->modes = INDIO_DIRECT_MODE | INDIO_HARDWARE_TRIGGERED; 18550f883b22SFabrice Gasnier 1856cd64d357SAlexandru Ardelean platform_set_drvdata(pdev, indio_dev); 18570f883b22SFabrice Gasnier 18580f883b22SFabrice Gasnier ret = of_property_read_u32(pdev->dev.of_node, "reg", &adc->offset); 18590f883b22SFabrice Gasnier if (ret != 0) { 18600f883b22SFabrice Gasnier dev_err(&pdev->dev, "missing reg property\n"); 18610f883b22SFabrice Gasnier return -EINVAL; 18620f883b22SFabrice Gasnier } 18630f883b22SFabrice Gasnier 18640f883b22SFabrice Gasnier adc->irq = platform_get_irq(pdev, 0); 18657c279229SStephen Boyd if (adc->irq < 0) 18660f883b22SFabrice Gasnier return adc->irq; 18670f883b22SFabrice Gasnier 1868cc06e67dSFabrice Gasnier ret = devm_request_threaded_irq(&pdev->dev, adc->irq, stm32_adc_isr, 1869cc06e67dSFabrice Gasnier stm32_adc_threaded_isr, 1870cd64d357SAlexandru Ardelean 0, pdev->name, indio_dev); 18710f883b22SFabrice Gasnier if (ret) { 18720f883b22SFabrice Gasnier dev_err(&pdev->dev, "failed to request IRQ\n"); 18730f883b22SFabrice Gasnier return ret; 18740f883b22SFabrice Gasnier } 18750f883b22SFabrice Gasnier 18760f883b22SFabrice Gasnier adc->clk = devm_clk_get(&pdev->dev, NULL); 18770f883b22SFabrice Gasnier if (IS_ERR(adc->clk)) { 1878204a6a25SFabrice Gasnier ret = PTR_ERR(adc->clk); 1879204a6a25SFabrice Gasnier if (ret == -ENOENT && !adc->cfg->clk_required) { 1880204a6a25SFabrice Gasnier adc->clk = NULL; 1881204a6a25SFabrice Gasnier } else { 18820f883b22SFabrice Gasnier dev_err(&pdev->dev, "Can't get clock\n"); 1883204a6a25SFabrice Gasnier return ret; 1884204a6a25SFabrice Gasnier } 18850f883b22SFabrice Gasnier } 18860f883b22SFabrice Gasnier 188725a85bedSFabrice Gasnier ret = stm32_adc_of_get_resolution(indio_dev); 188825a85bedSFabrice Gasnier if (ret < 0) 18899bdbb113SFabrice Gasnier return ret; 189025a85bedSFabrice Gasnier 18910f883b22SFabrice Gasnier ret = stm32_adc_chan_of_init(indio_dev); 18920f883b22SFabrice Gasnier if (ret < 0) 18939bdbb113SFabrice Gasnier return ret; 18940f883b22SFabrice Gasnier 189552cd91c2SFabrice Gasnier ret = stm32_adc_dma_request(dev, indio_dev); 18962763ea05SFabrice Gasnier if (ret < 0) 18979bdbb113SFabrice Gasnier return ret; 18982763ea05SFabrice Gasnier 1899e2042d29SOlivier Moysan if (!adc->dma_chan) 1900e2042d29SOlivier Moysan handler = &stm32_adc_trigger_handler; 1901e2042d29SOlivier Moysan 1902da9b9485SFabrice Gasnier ret = iio_triggered_buffer_setup(indio_dev, 1903e2042d29SOlivier Moysan &iio_pollfunc_store_time, handler, 1904da9b9485SFabrice Gasnier &stm32_adc_buffer_setup_ops); 19050f883b22SFabrice Gasnier if (ret) { 1906da9b9485SFabrice Gasnier dev_err(&pdev->dev, "buffer setup failed\n"); 19072763ea05SFabrice Gasnier goto err_dma_disable; 19080f883b22SFabrice Gasnier } 19090f883b22SFabrice Gasnier 19109bdbb113SFabrice Gasnier /* Get stm32-adc-core PM online */ 19119bdbb113SFabrice Gasnier pm_runtime_get_noresume(dev); 19129bdbb113SFabrice Gasnier pm_runtime_set_active(dev); 19139bdbb113SFabrice Gasnier pm_runtime_set_autosuspend_delay(dev, STM32_ADC_HW_STOP_DELAY_MS); 19149bdbb113SFabrice Gasnier pm_runtime_use_autosuspend(dev); 19159bdbb113SFabrice Gasnier pm_runtime_enable(dev); 19169bdbb113SFabrice Gasnier 19179bdbb113SFabrice Gasnier ret = stm32_adc_hw_start(dev); 19189bdbb113SFabrice Gasnier if (ret) 19199bdbb113SFabrice Gasnier goto err_buffer_cleanup; 19209bdbb113SFabrice Gasnier 1921da9b9485SFabrice Gasnier ret = iio_device_register(indio_dev); 1922da9b9485SFabrice Gasnier if (ret) { 1923da9b9485SFabrice Gasnier dev_err(&pdev->dev, "iio dev register failed\n"); 19249bdbb113SFabrice Gasnier goto err_hw_stop; 1925da9b9485SFabrice Gasnier } 1926da9b9485SFabrice Gasnier 19279bdbb113SFabrice Gasnier pm_runtime_mark_last_busy(dev); 19289bdbb113SFabrice Gasnier pm_runtime_put_autosuspend(dev); 19299bdbb113SFabrice Gasnier 19300f883b22SFabrice Gasnier return 0; 19310f883b22SFabrice Gasnier 19329bdbb113SFabrice Gasnier err_hw_stop: 19339bdbb113SFabrice Gasnier stm32_adc_hw_stop(dev); 19349bdbb113SFabrice Gasnier 1935da9b9485SFabrice Gasnier err_buffer_cleanup: 19369bdbb113SFabrice Gasnier pm_runtime_disable(dev); 19379bdbb113SFabrice Gasnier pm_runtime_set_suspended(dev); 19389bdbb113SFabrice Gasnier pm_runtime_put_noidle(dev); 1939da9b9485SFabrice Gasnier iio_triggered_buffer_cleanup(indio_dev); 1940da9b9485SFabrice Gasnier 19412763ea05SFabrice Gasnier err_dma_disable: 19422763ea05SFabrice Gasnier if (adc->dma_chan) { 19432763ea05SFabrice Gasnier dma_free_coherent(adc->dma_chan->device->dev, 19442763ea05SFabrice Gasnier STM32_DMA_BUFFER_SIZE, 19452763ea05SFabrice Gasnier adc->rx_buf, adc->rx_dma_buf); 19462763ea05SFabrice Gasnier dma_release_channel(adc->dma_chan); 19472763ea05SFabrice Gasnier } 19480f883b22SFabrice Gasnier 19490f883b22SFabrice Gasnier return ret; 19500f883b22SFabrice Gasnier } 19510f883b22SFabrice Gasnier 19520f883b22SFabrice Gasnier static int stm32_adc_remove(struct platform_device *pdev) 19530f883b22SFabrice Gasnier { 1954cd64d357SAlexandru Ardelean struct iio_dev *indio_dev = platform_get_drvdata(pdev); 1955cd64d357SAlexandru Ardelean struct stm32_adc *adc = iio_priv(indio_dev); 19560f883b22SFabrice Gasnier 19579bdbb113SFabrice Gasnier pm_runtime_get_sync(&pdev->dev); 19580f883b22SFabrice Gasnier iio_device_unregister(indio_dev); 19599bdbb113SFabrice Gasnier stm32_adc_hw_stop(&pdev->dev); 19609bdbb113SFabrice Gasnier pm_runtime_disable(&pdev->dev); 19619bdbb113SFabrice Gasnier pm_runtime_set_suspended(&pdev->dev); 19629bdbb113SFabrice Gasnier pm_runtime_put_noidle(&pdev->dev); 1963da9b9485SFabrice Gasnier iio_triggered_buffer_cleanup(indio_dev); 19642763ea05SFabrice Gasnier if (adc->dma_chan) { 19652763ea05SFabrice Gasnier dma_free_coherent(adc->dma_chan->device->dev, 19662763ea05SFabrice Gasnier STM32_DMA_BUFFER_SIZE, 19672763ea05SFabrice Gasnier adc->rx_buf, adc->rx_dma_buf); 19682763ea05SFabrice Gasnier dma_release_channel(adc->dma_chan); 19692763ea05SFabrice Gasnier } 19700f883b22SFabrice Gasnier 19710f883b22SFabrice Gasnier return 0; 19720f883b22SFabrice Gasnier } 19730f883b22SFabrice Gasnier 197449ad8d28SFabrice Gasnier #if defined(CONFIG_PM_SLEEP) 197549ad8d28SFabrice Gasnier static int stm32_adc_suspend(struct device *dev) 197649ad8d28SFabrice Gasnier { 1977cd64d357SAlexandru Ardelean struct iio_dev *indio_dev = dev_get_drvdata(dev); 197849ad8d28SFabrice Gasnier 197949ad8d28SFabrice Gasnier if (iio_buffer_enabled(indio_dev)) 1980f11d59d8SLars-Peter Clausen stm32_adc_buffer_predisable(indio_dev); 198149ad8d28SFabrice Gasnier 198249ad8d28SFabrice Gasnier return pm_runtime_force_suspend(dev); 198349ad8d28SFabrice Gasnier } 198449ad8d28SFabrice Gasnier 198549ad8d28SFabrice Gasnier static int stm32_adc_resume(struct device *dev) 198649ad8d28SFabrice Gasnier { 1987cd64d357SAlexandru Ardelean struct iio_dev *indio_dev = dev_get_drvdata(dev); 198849ad8d28SFabrice Gasnier int ret; 198949ad8d28SFabrice Gasnier 199049ad8d28SFabrice Gasnier ret = pm_runtime_force_resume(dev); 199149ad8d28SFabrice Gasnier if (ret < 0) 199249ad8d28SFabrice Gasnier return ret; 199349ad8d28SFabrice Gasnier 199449ad8d28SFabrice Gasnier if (!iio_buffer_enabled(indio_dev)) 199549ad8d28SFabrice Gasnier return 0; 199649ad8d28SFabrice Gasnier 199749ad8d28SFabrice Gasnier ret = stm32_adc_update_scan_mode(indio_dev, 199849ad8d28SFabrice Gasnier indio_dev->active_scan_mask); 199949ad8d28SFabrice Gasnier if (ret < 0) 200049ad8d28SFabrice Gasnier return ret; 200149ad8d28SFabrice Gasnier 2002f11d59d8SLars-Peter Clausen return stm32_adc_buffer_postenable(indio_dev); 200349ad8d28SFabrice Gasnier } 200449ad8d28SFabrice Gasnier #endif 200549ad8d28SFabrice Gasnier 20069bdbb113SFabrice Gasnier #if defined(CONFIG_PM) 20079bdbb113SFabrice Gasnier static int stm32_adc_runtime_suspend(struct device *dev) 20089bdbb113SFabrice Gasnier { 20099bdbb113SFabrice Gasnier return stm32_adc_hw_stop(dev); 20109bdbb113SFabrice Gasnier } 20119bdbb113SFabrice Gasnier 20129bdbb113SFabrice Gasnier static int stm32_adc_runtime_resume(struct device *dev) 20139bdbb113SFabrice Gasnier { 20149bdbb113SFabrice Gasnier return stm32_adc_hw_start(dev); 20159bdbb113SFabrice Gasnier } 20169bdbb113SFabrice Gasnier #endif 20179bdbb113SFabrice Gasnier 20189bdbb113SFabrice Gasnier static const struct dev_pm_ops stm32_adc_pm_ops = { 201949ad8d28SFabrice Gasnier SET_SYSTEM_SLEEP_PM_OPS(stm32_adc_suspend, stm32_adc_resume) 20209bdbb113SFabrice Gasnier SET_RUNTIME_PM_OPS(stm32_adc_runtime_suspend, stm32_adc_runtime_resume, 20219bdbb113SFabrice Gasnier NULL) 20229bdbb113SFabrice Gasnier }; 20239bdbb113SFabrice Gasnier 202464ad7f64SFabrice Gasnier static const struct stm32_adc_cfg stm32f4_adc_cfg = { 202564ad7f64SFabrice Gasnier .regs = &stm32f4_adc_regspec, 202664ad7f64SFabrice Gasnier .adc_info = &stm32f4_adc_info, 202764ad7f64SFabrice Gasnier .trigs = stm32f4_adc_trigs, 2028204a6a25SFabrice Gasnier .clk_required = true, 202964ad7f64SFabrice Gasnier .start_conv = stm32f4_adc_start_conv, 203064ad7f64SFabrice Gasnier .stop_conv = stm32f4_adc_stop_conv, 2031ee2ac1cdSFabrice Gasnier .smp_cycles = stm32f4_adc_smp_cycles, 203264ad7f64SFabrice Gasnier }; 203364ad7f64SFabrice Gasnier 203495e339b6SFabrice Gasnier static const struct stm32_adc_cfg stm32h7_adc_cfg = { 203595e339b6SFabrice Gasnier .regs = &stm32h7_adc_regspec, 203695e339b6SFabrice Gasnier .adc_info = &stm32h7_adc_info, 203795e339b6SFabrice Gasnier .trigs = stm32h7_adc_trigs, 203895e339b6SFabrice Gasnier .start_conv = stm32h7_adc_start_conv, 203995e339b6SFabrice Gasnier .stop_conv = stm32h7_adc_stop_conv, 204095e339b6SFabrice Gasnier .prepare = stm32h7_adc_prepare, 204195e339b6SFabrice Gasnier .unprepare = stm32h7_adc_unprepare, 2042ee2ac1cdSFabrice Gasnier .smp_cycles = stm32h7_adc_smp_cycles, 204395e339b6SFabrice Gasnier }; 204495e339b6SFabrice Gasnier 2045d58c67d1SFabrice Gasnier static const struct stm32_adc_cfg stm32mp1_adc_cfg = { 2046d58c67d1SFabrice Gasnier .regs = &stm32h7_adc_regspec, 2047d58c67d1SFabrice Gasnier .adc_info = &stm32h7_adc_info, 2048d58c67d1SFabrice Gasnier .trigs = stm32h7_adc_trigs, 2049d58c67d1SFabrice Gasnier .has_vregready = true, 2050d58c67d1SFabrice Gasnier .start_conv = stm32h7_adc_start_conv, 2051d58c67d1SFabrice Gasnier .stop_conv = stm32h7_adc_stop_conv, 2052d58c67d1SFabrice Gasnier .prepare = stm32h7_adc_prepare, 2053d58c67d1SFabrice Gasnier .unprepare = stm32h7_adc_unprepare, 2054d58c67d1SFabrice Gasnier .smp_cycles = stm32h7_adc_smp_cycles, 2055d58c67d1SFabrice Gasnier }; 2056d58c67d1SFabrice Gasnier 20570f883b22SFabrice Gasnier static const struct of_device_id stm32_adc_of_match[] = { 205864ad7f64SFabrice Gasnier { .compatible = "st,stm32f4-adc", .data = (void *)&stm32f4_adc_cfg }, 205995e339b6SFabrice Gasnier { .compatible = "st,stm32h7-adc", .data = (void *)&stm32h7_adc_cfg }, 2060d58c67d1SFabrice Gasnier { .compatible = "st,stm32mp1-adc", .data = (void *)&stm32mp1_adc_cfg }, 20610f883b22SFabrice Gasnier {}, 20620f883b22SFabrice Gasnier }; 20630f883b22SFabrice Gasnier MODULE_DEVICE_TABLE(of, stm32_adc_of_match); 20640f883b22SFabrice Gasnier 20650f883b22SFabrice Gasnier static struct platform_driver stm32_adc_driver = { 20660f883b22SFabrice Gasnier .probe = stm32_adc_probe, 20670f883b22SFabrice Gasnier .remove = stm32_adc_remove, 20680f883b22SFabrice Gasnier .driver = { 20690f883b22SFabrice Gasnier .name = "stm32-adc", 20700f883b22SFabrice Gasnier .of_match_table = stm32_adc_of_match, 20719bdbb113SFabrice Gasnier .pm = &stm32_adc_pm_ops, 20720f883b22SFabrice Gasnier }, 20730f883b22SFabrice Gasnier }; 20740f883b22SFabrice Gasnier module_platform_driver(stm32_adc_driver); 20750f883b22SFabrice Gasnier 20760f883b22SFabrice Gasnier MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>"); 20770f883b22SFabrice Gasnier MODULE_DESCRIPTION("STMicroelectronics STM32 ADC IIO driver"); 20780f883b22SFabrice Gasnier MODULE_LICENSE("GPL v2"); 20790f883b22SFabrice Gasnier MODULE_ALIAS("platform:stm32-adc"); 2080