Benjamin Gaignard | 6e93e26 | 2017-12-05 15:55:59 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 2 | /* |
| 3 | * This file is part of STM32 ADC driver |
| 4 | * |
| 5 | * Copyright (C) 2016, STMicroelectronics - All Rights Reserved |
| 6 | * Author: Fabrice Gasnier <fabrice.gasnier@st.com>. |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/clk.h> |
| 10 | #include <linux/delay.h> |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 11 | #include <linux/dma-mapping.h> |
| 12 | #include <linux/dmaengine.h> |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 13 | #include <linux/iio/iio.h> |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 14 | #include <linux/iio/buffer.h> |
Fabrice Gasnier | f0b638a | 2017-08-28 12:04:14 +0200 | [diff] [blame] | 15 | #include <linux/iio/timer/stm32-lptim-trigger.h> |
Fabrice Gasnier | f24a33b | 2017-01-26 15:28:30 +0100 | [diff] [blame] | 16 | #include <linux/iio/timer/stm32-timer-trigger.h> |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 17 | #include <linux/iio/trigger.h> |
| 18 | #include <linux/iio/trigger_consumer.h> |
| 19 | #include <linux/iio/triggered_buffer.h> |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/io.h> |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 22 | #include <linux/iopoll.h> |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/of.h> |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 26 | #include <linux/of_device.h> |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 27 | |
| 28 | #include "stm32-adc-core.h" |
| 29 | |
| 30 | /* STM32F4 - Registers for each ADC instance */ |
| 31 | #define STM32F4_ADC_SR 0x00 |
| 32 | #define STM32F4_ADC_CR1 0x04 |
| 33 | #define STM32F4_ADC_CR2 0x08 |
| 34 | #define STM32F4_ADC_SMPR1 0x0C |
| 35 | #define STM32F4_ADC_SMPR2 0x10 |
| 36 | #define STM32F4_ADC_HTR 0x24 |
| 37 | #define STM32F4_ADC_LTR 0x28 |
| 38 | #define STM32F4_ADC_SQR1 0x2C |
| 39 | #define STM32F4_ADC_SQR2 0x30 |
| 40 | #define STM32F4_ADC_SQR3 0x34 |
| 41 | #define STM32F4_ADC_JSQR 0x38 |
| 42 | #define STM32F4_ADC_JDR1 0x3C |
| 43 | #define STM32F4_ADC_JDR2 0x40 |
| 44 | #define STM32F4_ADC_JDR3 0x44 |
| 45 | #define STM32F4_ADC_JDR4 0x48 |
| 46 | #define STM32F4_ADC_DR 0x4C |
| 47 | |
| 48 | /* STM32F4_ADC_SR - bit fields */ |
| 49 | #define STM32F4_STRT BIT(4) |
| 50 | #define STM32F4_EOC BIT(1) |
| 51 | |
| 52 | /* STM32F4_ADC_CR1 - bit fields */ |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 53 | #define STM32F4_RES_SHIFT 24 |
| 54 | #define STM32F4_RES_MASK GENMASK(25, 24) |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 55 | #define STM32F4_SCAN BIT(8) |
| 56 | #define STM32F4_EOCIE BIT(5) |
| 57 | |
| 58 | /* STM32F4_ADC_CR2 - bit fields */ |
| 59 | #define STM32F4_SWSTART BIT(30) |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 60 | #define STM32F4_EXTEN_SHIFT 28 |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 61 | #define STM32F4_EXTEN_MASK GENMASK(29, 28) |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 62 | #define STM32F4_EXTSEL_SHIFT 24 |
| 63 | #define STM32F4_EXTSEL_MASK GENMASK(27, 24) |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 64 | #define STM32F4_EOCS BIT(10) |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 65 | #define STM32F4_DDS BIT(9) |
| 66 | #define STM32F4_DMA BIT(8) |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 67 | #define STM32F4_ADON BIT(0) |
| 68 | |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 69 | /* STM32H7 - Registers for each ADC instance */ |
| 70 | #define STM32H7_ADC_ISR 0x00 |
| 71 | #define STM32H7_ADC_IER 0x04 |
| 72 | #define STM32H7_ADC_CR 0x08 |
| 73 | #define STM32H7_ADC_CFGR 0x0C |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 74 | #define STM32H7_ADC_SMPR1 0x14 |
| 75 | #define STM32H7_ADC_SMPR2 0x18 |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 76 | #define STM32H7_ADC_PCSEL 0x1C |
| 77 | #define STM32H7_ADC_SQR1 0x30 |
| 78 | #define STM32H7_ADC_SQR2 0x34 |
| 79 | #define STM32H7_ADC_SQR3 0x38 |
| 80 | #define STM32H7_ADC_SQR4 0x3C |
| 81 | #define STM32H7_ADC_DR 0x40 |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 82 | #define STM32H7_ADC_DIFSEL 0xC0 |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 83 | #define STM32H7_ADC_CALFACT 0xC4 |
| 84 | #define STM32H7_ADC_CALFACT2 0xC8 |
| 85 | |
| 86 | /* STM32H7_ADC_ISR - bit fields */ |
| 87 | #define STM32H7_EOC BIT(2) |
| 88 | #define STM32H7_ADRDY BIT(0) |
| 89 | |
| 90 | /* STM32H7_ADC_IER - bit fields */ |
| 91 | #define STM32H7_EOCIE STM32H7_EOC |
| 92 | |
| 93 | /* STM32H7_ADC_CR - bit fields */ |
| 94 | #define STM32H7_ADCAL BIT(31) |
| 95 | #define STM32H7_ADCALDIF BIT(30) |
| 96 | #define STM32H7_DEEPPWD BIT(29) |
| 97 | #define STM32H7_ADVREGEN BIT(28) |
| 98 | #define STM32H7_LINCALRDYW6 BIT(27) |
| 99 | #define STM32H7_LINCALRDYW5 BIT(26) |
| 100 | #define STM32H7_LINCALRDYW4 BIT(25) |
| 101 | #define STM32H7_LINCALRDYW3 BIT(24) |
| 102 | #define STM32H7_LINCALRDYW2 BIT(23) |
| 103 | #define STM32H7_LINCALRDYW1 BIT(22) |
| 104 | #define STM32H7_ADCALLIN BIT(16) |
| 105 | #define STM32H7_BOOST BIT(8) |
| 106 | #define STM32H7_ADSTP BIT(4) |
| 107 | #define STM32H7_ADSTART BIT(2) |
| 108 | #define STM32H7_ADDIS BIT(1) |
| 109 | #define STM32H7_ADEN BIT(0) |
| 110 | |
| 111 | /* STM32H7_ADC_CFGR bit fields */ |
| 112 | #define STM32H7_EXTEN_SHIFT 10 |
| 113 | #define STM32H7_EXTEN_MASK GENMASK(11, 10) |
| 114 | #define STM32H7_EXTSEL_SHIFT 5 |
| 115 | #define STM32H7_EXTSEL_MASK GENMASK(9, 5) |
| 116 | #define STM32H7_RES_SHIFT 2 |
| 117 | #define STM32H7_RES_MASK GENMASK(4, 2) |
| 118 | #define STM32H7_DMNGT_SHIFT 0 |
| 119 | #define STM32H7_DMNGT_MASK GENMASK(1, 0) |
| 120 | |
| 121 | enum stm32h7_adc_dmngt { |
| 122 | STM32H7_DMNGT_DR_ONLY, /* Regular data in DR only */ |
| 123 | STM32H7_DMNGT_DMA_ONESHOT, /* DMA one shot mode */ |
| 124 | STM32H7_DMNGT_DFSDM, /* DFSDM mode */ |
| 125 | STM32H7_DMNGT_DMA_CIRC, /* DMA circular mode */ |
| 126 | }; |
| 127 | |
| 128 | /* STM32H7_ADC_CALFACT - bit fields */ |
| 129 | #define STM32H7_CALFACT_D_SHIFT 16 |
| 130 | #define STM32H7_CALFACT_D_MASK GENMASK(26, 16) |
| 131 | #define STM32H7_CALFACT_S_SHIFT 0 |
| 132 | #define STM32H7_CALFACT_S_MASK GENMASK(10, 0) |
| 133 | |
| 134 | /* STM32H7_ADC_CALFACT2 - bit fields */ |
| 135 | #define STM32H7_LINCALFACT_SHIFT 0 |
| 136 | #define STM32H7_LINCALFACT_MASK GENMASK(29, 0) |
| 137 | |
| 138 | /* Number of linear calibration shadow registers / LINCALRDYW control bits */ |
| 139 | #define STM32H7_LINCALFACT_NUM 6 |
| 140 | |
| 141 | /* BOOST bit must be set on STM32H7 when ADC clock is above 20MHz */ |
| 142 | #define STM32H7_BOOST_CLKRATE 20000000UL |
| 143 | |
Fabrice Gasnier | 0bae72a | 2017-10-25 11:27:44 +0200 | [diff] [blame] | 144 | #define STM32_ADC_CH_MAX 20 /* max number of channels */ |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 145 | #define STM32_ADC_CH_SZ 10 /* max channel name size */ |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 146 | #define STM32_ADC_MAX_SQ 16 /* SQ1..SQ16 */ |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 147 | #define STM32_ADC_MAX_SMP 7 /* SMPx range is [0..7] */ |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 148 | #define STM32_ADC_TIMEOUT_US 100000 |
| 149 | #define STM32_ADC_TIMEOUT (msecs_to_jiffies(STM32_ADC_TIMEOUT_US / 1000)) |
| 150 | |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 151 | #define STM32_DMA_BUFFER_SIZE PAGE_SIZE |
| 152 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 153 | /* External trigger enable */ |
| 154 | enum stm32_adc_exten { |
| 155 | STM32_EXTEN_SWTRIG, |
| 156 | STM32_EXTEN_HWTRIG_RISING_EDGE, |
| 157 | STM32_EXTEN_HWTRIG_FALLING_EDGE, |
| 158 | STM32_EXTEN_HWTRIG_BOTH_EDGES, |
| 159 | }; |
| 160 | |
Fabrice Gasnier | f24a33b | 2017-01-26 15:28:30 +0100 | [diff] [blame] | 161 | /* extsel - trigger mux selection value */ |
| 162 | enum stm32_adc_extsel { |
| 163 | STM32_EXT0, |
| 164 | STM32_EXT1, |
| 165 | STM32_EXT2, |
| 166 | STM32_EXT3, |
| 167 | STM32_EXT4, |
| 168 | STM32_EXT5, |
| 169 | STM32_EXT6, |
| 170 | STM32_EXT7, |
| 171 | STM32_EXT8, |
| 172 | STM32_EXT9, |
| 173 | STM32_EXT10, |
| 174 | STM32_EXT11, |
| 175 | STM32_EXT12, |
| 176 | STM32_EXT13, |
| 177 | STM32_EXT14, |
| 178 | STM32_EXT15, |
Fabrice Gasnier | f0b638a | 2017-08-28 12:04:14 +0200 | [diff] [blame] | 179 | STM32_EXT16, |
| 180 | STM32_EXT17, |
| 181 | STM32_EXT18, |
| 182 | STM32_EXT19, |
| 183 | STM32_EXT20, |
Fabrice Gasnier | f24a33b | 2017-01-26 15:28:30 +0100 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | /** |
| 187 | * struct stm32_adc_trig_info - ADC trigger info |
| 188 | * @name: name of the trigger, corresponding to its source |
| 189 | * @extsel: trigger selection |
| 190 | */ |
| 191 | struct stm32_adc_trig_info { |
| 192 | const char *name; |
| 193 | enum stm32_adc_extsel extsel; |
| 194 | }; |
| 195 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 196 | /** |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 197 | * struct stm32_adc_calib - optional adc calibration data |
| 198 | * @calfact_s: Calibration offset for single ended channels |
| 199 | * @calfact_d: Calibration offset in differential |
| 200 | * @lincalfact: Linearity calibration factor |
| 201 | */ |
| 202 | struct stm32_adc_calib { |
| 203 | u32 calfact_s; |
| 204 | u32 calfact_d; |
| 205 | u32 lincalfact[STM32H7_LINCALFACT_NUM]; |
| 206 | }; |
| 207 | |
| 208 | /** |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 209 | * stm32_adc_regs - stm32 ADC misc registers & bitfield desc |
| 210 | * @reg: register offset |
| 211 | * @mask: bitfield mask |
| 212 | * @shift: left shift |
| 213 | */ |
| 214 | struct stm32_adc_regs { |
| 215 | int reg; |
| 216 | int mask; |
| 217 | int shift; |
| 218 | }; |
| 219 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 220 | /** |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 221 | * stm32_adc_regspec - stm32 registers definition, compatible dependent data |
| 222 | * @dr: data register offset |
| 223 | * @ier_eoc: interrupt enable register & eocie bitfield |
| 224 | * @isr_eoc: interrupt status register & eoc bitfield |
| 225 | * @sqr: reference to sequence registers array |
| 226 | * @exten: trigger control register & bitfield |
| 227 | * @extsel: trigger selection register & bitfield |
| 228 | * @res: resolution selection register & bitfield |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 229 | * @smpr: smpr1 & smpr2 registers offset array |
| 230 | * @smp_bits: smpr1 & smpr2 index and bitfields |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 231 | */ |
| 232 | struct stm32_adc_regspec { |
| 233 | const u32 dr; |
| 234 | const struct stm32_adc_regs ier_eoc; |
| 235 | const struct stm32_adc_regs isr_eoc; |
| 236 | const struct stm32_adc_regs *sqr; |
| 237 | const struct stm32_adc_regs exten; |
| 238 | const struct stm32_adc_regs extsel; |
| 239 | const struct stm32_adc_regs res; |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 240 | const u32 smpr[2]; |
| 241 | const struct stm32_adc_regs *smp_bits; |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | struct stm32_adc; |
| 245 | |
| 246 | /** |
| 247 | * stm32_adc_cfg - stm32 compatible configuration data |
| 248 | * @regs: registers descriptions |
| 249 | * @adc_info: per instance input channels definitions |
| 250 | * @trigs: external trigger sources |
Fabrice Gasnier | 204a6a2 | 2017-05-29 11:28:19 +0200 | [diff] [blame] | 251 | * @clk_required: clock is required |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 252 | * @selfcalib: optional routine for self-calibration |
| 253 | * @prepare: optional prepare routine (power-up, enable) |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 254 | * @start_conv: routine to start conversions |
| 255 | * @stop_conv: routine to stop conversions |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 256 | * @unprepare: optional unprepare routine (disable, power-down) |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 257 | * @smp_cycles: programmable sampling time (ADC clock cycles) |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 258 | */ |
| 259 | struct stm32_adc_cfg { |
| 260 | const struct stm32_adc_regspec *regs; |
| 261 | const struct stm32_adc_info *adc_info; |
| 262 | struct stm32_adc_trig_info *trigs; |
Fabrice Gasnier | 204a6a2 | 2017-05-29 11:28:19 +0200 | [diff] [blame] | 263 | bool clk_required; |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 264 | int (*selfcalib)(struct stm32_adc *); |
| 265 | int (*prepare)(struct stm32_adc *); |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 266 | void (*start_conv)(struct stm32_adc *, bool dma); |
| 267 | void (*stop_conv)(struct stm32_adc *); |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 268 | void (*unprepare)(struct stm32_adc *); |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 269 | const unsigned int *smp_cycles; |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | /** |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 273 | * struct stm32_adc - private data of each ADC IIO instance |
| 274 | * @common: reference to ADC block common data |
| 275 | * @offset: ADC instance register offset in ADC block |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 276 | * @cfg: compatible configuration data |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 277 | * @completion: end of single conversion completion |
| 278 | * @buffer: data buffer |
| 279 | * @clk: clock for this adc instance |
| 280 | * @irq: interrupt for this adc instance |
| 281 | * @lock: spinlock |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 282 | * @bufi: data buffer index |
| 283 | * @num_conv: expected number of scan conversions |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 284 | * @res: data resolution (e.g. RES bitfield value) |
Fabrice Gasnier | 732f2dc | 2017-01-26 15:28:31 +0100 | [diff] [blame] | 285 | * @trigger_polarity: external trigger polarity (e.g. exten) |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 286 | * @dma_chan: dma channel |
| 287 | * @rx_buf: dma rx buffer cpu address |
| 288 | * @rx_dma_buf: dma rx buffer bus address |
| 289 | * @rx_buf_sz: dma rx buffer size |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 290 | * @difsel bitmask to set single-ended/differential channel |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 291 | * @pcsel bitmask to preselect channels on some devices |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 292 | * @smpr_val: sampling time settings (e.g. smpr1 / smpr2) |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 293 | * @cal: optional calibration data on some devices |
Fabrice Gasnier | 0bae72a | 2017-10-25 11:27:44 +0200 | [diff] [blame] | 294 | * @chan_name: channel name array |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 295 | */ |
| 296 | struct stm32_adc { |
| 297 | struct stm32_adc_common *common; |
| 298 | u32 offset; |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 299 | const struct stm32_adc_cfg *cfg; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 300 | struct completion completion; |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 301 | u16 buffer[STM32_ADC_MAX_SQ]; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 302 | struct clk *clk; |
| 303 | int irq; |
| 304 | spinlock_t lock; /* interrupt lock */ |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 305 | unsigned int bufi; |
| 306 | unsigned int num_conv; |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 307 | u32 res; |
Fabrice Gasnier | 732f2dc | 2017-01-26 15:28:31 +0100 | [diff] [blame] | 308 | u32 trigger_polarity; |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 309 | struct dma_chan *dma_chan; |
| 310 | u8 *rx_buf; |
| 311 | dma_addr_t rx_dma_buf; |
| 312 | unsigned int rx_buf_sz; |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 313 | u32 difsel; |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 314 | u32 pcsel; |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 315 | u32 smpr_val[2]; |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 316 | struct stm32_adc_calib cal; |
Fabrice Gasnier | 0bae72a | 2017-10-25 11:27:44 +0200 | [diff] [blame] | 317 | char chan_name[STM32_ADC_CH_MAX][STM32_ADC_CH_SZ]; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 318 | }; |
| 319 | |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 320 | struct stm32_adc_diff_channel { |
| 321 | u32 vinp; |
| 322 | u32 vinn; |
| 323 | }; |
| 324 | |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 325 | /** |
| 326 | * struct stm32_adc_info - stm32 ADC, per instance config data |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 327 | * @max_channels: Number of channels |
| 328 | * @resolutions: available resolutions |
| 329 | * @num_res: number of available resolutions |
| 330 | */ |
| 331 | struct stm32_adc_info { |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 332 | int max_channels; |
| 333 | const unsigned int *resolutions; |
| 334 | const unsigned int num_res; |
| 335 | }; |
| 336 | |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 337 | static const unsigned int stm32f4_adc_resolutions[] = { |
| 338 | /* sorted values so the index matches RES[1:0] in STM32F4_ADC_CR1 */ |
| 339 | 12, 10, 8, 6, |
| 340 | }; |
| 341 | |
Fabrice Gasnier | 0bae72a | 2017-10-25 11:27:44 +0200 | [diff] [blame] | 342 | /* stm32f4 can have up to 16 channels */ |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 343 | static const struct stm32_adc_info stm32f4_adc_info = { |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 344 | .max_channels = 16, |
| 345 | .resolutions = stm32f4_adc_resolutions, |
| 346 | .num_res = ARRAY_SIZE(stm32f4_adc_resolutions), |
| 347 | }; |
| 348 | |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 349 | static const unsigned int stm32h7_adc_resolutions[] = { |
| 350 | /* sorted values so the index matches RES[2:0] in STM32H7_ADC_CFGR */ |
| 351 | 16, 14, 12, 10, 8, |
| 352 | }; |
| 353 | |
Fabrice Gasnier | 0bae72a | 2017-10-25 11:27:44 +0200 | [diff] [blame] | 354 | /* stm32h7 can have up to 20 channels */ |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 355 | static const struct stm32_adc_info stm32h7_adc_info = { |
Fabrice Gasnier | 0bae72a | 2017-10-25 11:27:44 +0200 | [diff] [blame] | 356 | .max_channels = STM32_ADC_CH_MAX, |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 357 | .resolutions = stm32h7_adc_resolutions, |
| 358 | .num_res = ARRAY_SIZE(stm32h7_adc_resolutions), |
| 359 | }; |
| 360 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 361 | /** |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 362 | * stm32f4_sq - describe regular sequence registers |
| 363 | * - L: sequence len (register & bit field) |
| 364 | * - SQ1..SQ16: sequence entries (register & bit field) |
| 365 | */ |
| 366 | static const struct stm32_adc_regs stm32f4_sq[STM32_ADC_MAX_SQ + 1] = { |
| 367 | /* L: len bit field description to be kept as first element */ |
| 368 | { STM32F4_ADC_SQR1, GENMASK(23, 20), 20 }, |
| 369 | /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */ |
| 370 | { STM32F4_ADC_SQR3, GENMASK(4, 0), 0 }, |
| 371 | { STM32F4_ADC_SQR3, GENMASK(9, 5), 5 }, |
| 372 | { STM32F4_ADC_SQR3, GENMASK(14, 10), 10 }, |
| 373 | { STM32F4_ADC_SQR3, GENMASK(19, 15), 15 }, |
| 374 | { STM32F4_ADC_SQR3, GENMASK(24, 20), 20 }, |
| 375 | { STM32F4_ADC_SQR3, GENMASK(29, 25), 25 }, |
| 376 | { STM32F4_ADC_SQR2, GENMASK(4, 0), 0 }, |
| 377 | { STM32F4_ADC_SQR2, GENMASK(9, 5), 5 }, |
| 378 | { STM32F4_ADC_SQR2, GENMASK(14, 10), 10 }, |
| 379 | { STM32F4_ADC_SQR2, GENMASK(19, 15), 15 }, |
| 380 | { STM32F4_ADC_SQR2, GENMASK(24, 20), 20 }, |
| 381 | { STM32F4_ADC_SQR2, GENMASK(29, 25), 25 }, |
| 382 | { STM32F4_ADC_SQR1, GENMASK(4, 0), 0 }, |
| 383 | { STM32F4_ADC_SQR1, GENMASK(9, 5), 5 }, |
| 384 | { STM32F4_ADC_SQR1, GENMASK(14, 10), 10 }, |
| 385 | { STM32F4_ADC_SQR1, GENMASK(19, 15), 15 }, |
| 386 | }; |
| 387 | |
Fabrice Gasnier | f24a33b | 2017-01-26 15:28:30 +0100 | [diff] [blame] | 388 | /* STM32F4 external trigger sources for all instances */ |
| 389 | static struct stm32_adc_trig_info stm32f4_adc_trigs[] = { |
| 390 | { TIM1_CH1, STM32_EXT0 }, |
| 391 | { TIM1_CH2, STM32_EXT1 }, |
| 392 | { TIM1_CH3, STM32_EXT2 }, |
| 393 | { TIM2_CH2, STM32_EXT3 }, |
| 394 | { TIM2_CH3, STM32_EXT4 }, |
| 395 | { TIM2_CH4, STM32_EXT5 }, |
| 396 | { TIM2_TRGO, STM32_EXT6 }, |
| 397 | { TIM3_CH1, STM32_EXT7 }, |
| 398 | { TIM3_TRGO, STM32_EXT8 }, |
| 399 | { TIM4_CH4, STM32_EXT9 }, |
| 400 | { TIM5_CH1, STM32_EXT10 }, |
| 401 | { TIM5_CH2, STM32_EXT11 }, |
| 402 | { TIM5_CH3, STM32_EXT12 }, |
| 403 | { TIM8_CH1, STM32_EXT13 }, |
| 404 | { TIM8_TRGO, STM32_EXT14 }, |
| 405 | {}, /* sentinel */ |
| 406 | }; |
| 407 | |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 408 | /** |
| 409 | * stm32f4_smp_bits[] - describe sampling time register index & bit fields |
| 410 | * Sorted so it can be indexed by channel number. |
| 411 | */ |
| 412 | static const struct stm32_adc_regs stm32f4_smp_bits[] = { |
| 413 | /* STM32F4_ADC_SMPR2: smpr[] index, mask, shift for SMP0 to SMP9 */ |
| 414 | { 1, GENMASK(2, 0), 0 }, |
| 415 | { 1, GENMASK(5, 3), 3 }, |
| 416 | { 1, GENMASK(8, 6), 6 }, |
| 417 | { 1, GENMASK(11, 9), 9 }, |
| 418 | { 1, GENMASK(14, 12), 12 }, |
| 419 | { 1, GENMASK(17, 15), 15 }, |
| 420 | { 1, GENMASK(20, 18), 18 }, |
| 421 | { 1, GENMASK(23, 21), 21 }, |
| 422 | { 1, GENMASK(26, 24), 24 }, |
| 423 | { 1, GENMASK(29, 27), 27 }, |
| 424 | /* STM32F4_ADC_SMPR1, smpr[] index, mask, shift for SMP10 to SMP18 */ |
| 425 | { 0, GENMASK(2, 0), 0 }, |
| 426 | { 0, GENMASK(5, 3), 3 }, |
| 427 | { 0, GENMASK(8, 6), 6 }, |
| 428 | { 0, GENMASK(11, 9), 9 }, |
| 429 | { 0, GENMASK(14, 12), 12 }, |
| 430 | { 0, GENMASK(17, 15), 15 }, |
| 431 | { 0, GENMASK(20, 18), 18 }, |
| 432 | { 0, GENMASK(23, 21), 21 }, |
| 433 | { 0, GENMASK(26, 24), 24 }, |
| 434 | }; |
| 435 | |
| 436 | /* STM32F4 programmable sampling time (ADC clock cycles) */ |
| 437 | static const unsigned int stm32f4_adc_smp_cycles[STM32_ADC_MAX_SMP + 1] = { |
| 438 | 3, 15, 28, 56, 84, 112, 144, 480, |
| 439 | }; |
| 440 | |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 441 | static const struct stm32_adc_regspec stm32f4_adc_regspec = { |
| 442 | .dr = STM32F4_ADC_DR, |
| 443 | .ier_eoc = { STM32F4_ADC_CR1, STM32F4_EOCIE }, |
| 444 | .isr_eoc = { STM32F4_ADC_SR, STM32F4_EOC }, |
| 445 | .sqr = stm32f4_sq, |
| 446 | .exten = { STM32F4_ADC_CR2, STM32F4_EXTEN_MASK, STM32F4_EXTEN_SHIFT }, |
| 447 | .extsel = { STM32F4_ADC_CR2, STM32F4_EXTSEL_MASK, |
| 448 | STM32F4_EXTSEL_SHIFT }, |
| 449 | .res = { STM32F4_ADC_CR1, STM32F4_RES_MASK, STM32F4_RES_SHIFT }, |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 450 | .smpr = { STM32F4_ADC_SMPR1, STM32F4_ADC_SMPR2 }, |
| 451 | .smp_bits = stm32f4_smp_bits, |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 452 | }; |
| 453 | |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 454 | static const struct stm32_adc_regs stm32h7_sq[STM32_ADC_MAX_SQ + 1] = { |
| 455 | /* L: len bit field description to be kept as first element */ |
| 456 | { STM32H7_ADC_SQR1, GENMASK(3, 0), 0 }, |
| 457 | /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */ |
| 458 | { STM32H7_ADC_SQR1, GENMASK(10, 6), 6 }, |
| 459 | { STM32H7_ADC_SQR1, GENMASK(16, 12), 12 }, |
| 460 | { STM32H7_ADC_SQR1, GENMASK(22, 18), 18 }, |
| 461 | { STM32H7_ADC_SQR1, GENMASK(28, 24), 24 }, |
| 462 | { STM32H7_ADC_SQR2, GENMASK(4, 0), 0 }, |
| 463 | { STM32H7_ADC_SQR2, GENMASK(10, 6), 6 }, |
| 464 | { STM32H7_ADC_SQR2, GENMASK(16, 12), 12 }, |
| 465 | { STM32H7_ADC_SQR2, GENMASK(22, 18), 18 }, |
| 466 | { STM32H7_ADC_SQR2, GENMASK(28, 24), 24 }, |
| 467 | { STM32H7_ADC_SQR3, GENMASK(4, 0), 0 }, |
| 468 | { STM32H7_ADC_SQR3, GENMASK(10, 6), 6 }, |
| 469 | { STM32H7_ADC_SQR3, GENMASK(16, 12), 12 }, |
| 470 | { STM32H7_ADC_SQR3, GENMASK(22, 18), 18 }, |
| 471 | { STM32H7_ADC_SQR3, GENMASK(28, 24), 24 }, |
| 472 | { STM32H7_ADC_SQR4, GENMASK(4, 0), 0 }, |
| 473 | { STM32H7_ADC_SQR4, GENMASK(10, 6), 6 }, |
| 474 | }; |
| 475 | |
| 476 | /* STM32H7 external trigger sources for all instances */ |
| 477 | static struct stm32_adc_trig_info stm32h7_adc_trigs[] = { |
| 478 | { TIM1_CH1, STM32_EXT0 }, |
| 479 | { TIM1_CH2, STM32_EXT1 }, |
| 480 | { TIM1_CH3, STM32_EXT2 }, |
| 481 | { TIM2_CH2, STM32_EXT3 }, |
| 482 | { TIM3_TRGO, STM32_EXT4 }, |
| 483 | { TIM4_CH4, STM32_EXT5 }, |
| 484 | { TIM8_TRGO, STM32_EXT7 }, |
| 485 | { TIM8_TRGO2, STM32_EXT8 }, |
| 486 | { TIM1_TRGO, STM32_EXT9 }, |
| 487 | { TIM1_TRGO2, STM32_EXT10 }, |
| 488 | { TIM2_TRGO, STM32_EXT11 }, |
| 489 | { TIM4_TRGO, STM32_EXT12 }, |
| 490 | { TIM6_TRGO, STM32_EXT13 }, |
Fabrice Gasnier | 3a06990 | 2017-10-18 13:39:27 +0200 | [diff] [blame] | 491 | { TIM15_TRGO, STM32_EXT14 }, |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 492 | { TIM3_CH4, STM32_EXT15 }, |
Fabrice Gasnier | f0b638a | 2017-08-28 12:04:14 +0200 | [diff] [blame] | 493 | { LPTIM1_OUT, STM32_EXT18 }, |
| 494 | { LPTIM2_OUT, STM32_EXT19 }, |
| 495 | { LPTIM3_OUT, STM32_EXT20 }, |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 496 | {}, |
| 497 | }; |
| 498 | |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 499 | /** |
| 500 | * stm32h7_smp_bits - describe sampling time register index & bit fields |
| 501 | * Sorted so it can be indexed by channel number. |
| 502 | */ |
| 503 | static const struct stm32_adc_regs stm32h7_smp_bits[] = { |
| 504 | /* STM32H7_ADC_SMPR1, smpr[] index, mask, shift for SMP0 to SMP9 */ |
| 505 | { 0, GENMASK(2, 0), 0 }, |
| 506 | { 0, GENMASK(5, 3), 3 }, |
| 507 | { 0, GENMASK(8, 6), 6 }, |
| 508 | { 0, GENMASK(11, 9), 9 }, |
| 509 | { 0, GENMASK(14, 12), 12 }, |
| 510 | { 0, GENMASK(17, 15), 15 }, |
| 511 | { 0, GENMASK(20, 18), 18 }, |
| 512 | { 0, GENMASK(23, 21), 21 }, |
| 513 | { 0, GENMASK(26, 24), 24 }, |
| 514 | { 0, GENMASK(29, 27), 27 }, |
| 515 | /* STM32H7_ADC_SMPR2, smpr[] index, mask, shift for SMP10 to SMP19 */ |
| 516 | { 1, GENMASK(2, 0), 0 }, |
| 517 | { 1, GENMASK(5, 3), 3 }, |
| 518 | { 1, GENMASK(8, 6), 6 }, |
| 519 | { 1, GENMASK(11, 9), 9 }, |
| 520 | { 1, GENMASK(14, 12), 12 }, |
| 521 | { 1, GENMASK(17, 15), 15 }, |
| 522 | { 1, GENMASK(20, 18), 18 }, |
| 523 | { 1, GENMASK(23, 21), 21 }, |
| 524 | { 1, GENMASK(26, 24), 24 }, |
| 525 | { 1, GENMASK(29, 27), 27 }, |
| 526 | }; |
| 527 | |
| 528 | /* STM32H7 programmable sampling time (ADC clock cycles, rounded down) */ |
| 529 | static const unsigned int stm32h7_adc_smp_cycles[STM32_ADC_MAX_SMP + 1] = { |
| 530 | 1, 2, 8, 16, 32, 64, 387, 810, |
| 531 | }; |
| 532 | |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 533 | static const struct stm32_adc_regspec stm32h7_adc_regspec = { |
| 534 | .dr = STM32H7_ADC_DR, |
| 535 | .ier_eoc = { STM32H7_ADC_IER, STM32H7_EOCIE }, |
| 536 | .isr_eoc = { STM32H7_ADC_ISR, STM32H7_EOC }, |
| 537 | .sqr = stm32h7_sq, |
| 538 | .exten = { STM32H7_ADC_CFGR, STM32H7_EXTEN_MASK, STM32H7_EXTEN_SHIFT }, |
| 539 | .extsel = { STM32H7_ADC_CFGR, STM32H7_EXTSEL_MASK, |
| 540 | STM32H7_EXTSEL_SHIFT }, |
| 541 | .res = { STM32H7_ADC_CFGR, STM32H7_RES_MASK, STM32H7_RES_SHIFT }, |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 542 | .smpr = { STM32H7_ADC_SMPR1, STM32H7_ADC_SMPR2 }, |
| 543 | .smp_bits = stm32h7_smp_bits, |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 544 | }; |
| 545 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 546 | /** |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 547 | * STM32 ADC registers access routines |
| 548 | * @adc: stm32 adc instance |
| 549 | * @reg: reg offset in adc instance |
| 550 | * |
| 551 | * Note: All instances share same base, with 0x0, 0x100 or 0x200 offset resp. |
| 552 | * for adc1, adc2 and adc3. |
| 553 | */ |
| 554 | static u32 stm32_adc_readl(struct stm32_adc *adc, u32 reg) |
| 555 | { |
| 556 | return readl_relaxed(adc->common->base + adc->offset + reg); |
| 557 | } |
| 558 | |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 559 | #define stm32_adc_readl_addr(addr) stm32_adc_readl(adc, addr) |
| 560 | |
| 561 | #define stm32_adc_readl_poll_timeout(reg, val, cond, sleep_us, timeout_us) \ |
| 562 | readx_poll_timeout(stm32_adc_readl_addr, reg, val, \ |
| 563 | cond, sleep_us, timeout_us) |
| 564 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 565 | static u16 stm32_adc_readw(struct stm32_adc *adc, u32 reg) |
| 566 | { |
| 567 | return readw_relaxed(adc->common->base + adc->offset + reg); |
| 568 | } |
| 569 | |
| 570 | static void stm32_adc_writel(struct stm32_adc *adc, u32 reg, u32 val) |
| 571 | { |
| 572 | writel_relaxed(val, adc->common->base + adc->offset + reg); |
| 573 | } |
| 574 | |
| 575 | static void stm32_adc_set_bits(struct stm32_adc *adc, u32 reg, u32 bits) |
| 576 | { |
| 577 | unsigned long flags; |
| 578 | |
| 579 | spin_lock_irqsave(&adc->lock, flags); |
| 580 | stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) | bits); |
| 581 | spin_unlock_irqrestore(&adc->lock, flags); |
| 582 | } |
| 583 | |
| 584 | static void stm32_adc_clr_bits(struct stm32_adc *adc, u32 reg, u32 bits) |
| 585 | { |
| 586 | unsigned long flags; |
| 587 | |
| 588 | spin_lock_irqsave(&adc->lock, flags); |
| 589 | stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) & ~bits); |
| 590 | spin_unlock_irqrestore(&adc->lock, flags); |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * stm32_adc_conv_irq_enable() - Enable end of conversion interrupt |
| 595 | * @adc: stm32 adc instance |
| 596 | */ |
| 597 | static void stm32_adc_conv_irq_enable(struct stm32_adc *adc) |
| 598 | { |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 599 | stm32_adc_set_bits(adc, adc->cfg->regs->ier_eoc.reg, |
| 600 | adc->cfg->regs->ier_eoc.mask); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 601 | }; |
| 602 | |
| 603 | /** |
| 604 | * stm32_adc_conv_irq_disable() - Disable end of conversion interrupt |
| 605 | * @adc: stm32 adc instance |
| 606 | */ |
| 607 | static void stm32_adc_conv_irq_disable(struct stm32_adc *adc) |
| 608 | { |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 609 | stm32_adc_clr_bits(adc, adc->cfg->regs->ier_eoc.reg, |
| 610 | adc->cfg->regs->ier_eoc.mask); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 611 | } |
| 612 | |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 613 | static void stm32_adc_set_res(struct stm32_adc *adc) |
| 614 | { |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 615 | const struct stm32_adc_regs *res = &adc->cfg->regs->res; |
| 616 | u32 val; |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 617 | |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 618 | val = stm32_adc_readl(adc, res->reg); |
| 619 | val = (val & ~res->mask) | (adc->res << res->shift); |
| 620 | stm32_adc_writel(adc, res->reg, val); |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 623 | /** |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 624 | * stm32f4_adc_start_conv() - Start conversions for regular channels. |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 625 | * @adc: stm32 adc instance |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 626 | * @dma: use dma to transfer conversion result |
| 627 | * |
| 628 | * Start conversions for regular channels. |
| 629 | * Also take care of normal or DMA mode. Circular DMA may be used for regular |
| 630 | * conversions, in IIO buffer modes. Otherwise, use ADC interrupt with direct |
| 631 | * DR read instead (e.g. read_raw, or triggered buffer mode without DMA). |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 632 | */ |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 633 | static void stm32f4_adc_start_conv(struct stm32_adc *adc, bool dma) |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 634 | { |
| 635 | stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN); |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 636 | |
| 637 | if (dma) |
| 638 | stm32_adc_set_bits(adc, STM32F4_ADC_CR2, |
| 639 | STM32F4_DMA | STM32F4_DDS); |
| 640 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 641 | stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_EOCS | STM32F4_ADON); |
| 642 | |
| 643 | /* Wait for Power-up time (tSTAB from datasheet) */ |
| 644 | usleep_range(2, 3); |
| 645 | |
| 646 | /* Software start ? (e.g. trigger detection disabled ?) */ |
| 647 | if (!(stm32_adc_readl(adc, STM32F4_ADC_CR2) & STM32F4_EXTEN_MASK)) |
| 648 | stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_SWSTART); |
| 649 | } |
| 650 | |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 651 | static void stm32f4_adc_stop_conv(struct stm32_adc *adc) |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 652 | { |
| 653 | stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK); |
| 654 | stm32_adc_clr_bits(adc, STM32F4_ADC_SR, STM32F4_STRT); |
| 655 | |
| 656 | stm32_adc_clr_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN); |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 657 | stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, |
| 658 | STM32F4_ADON | STM32F4_DMA | STM32F4_DDS); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 659 | } |
| 660 | |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 661 | static void stm32h7_adc_start_conv(struct stm32_adc *adc, bool dma) |
| 662 | { |
| 663 | enum stm32h7_adc_dmngt dmngt; |
| 664 | unsigned long flags; |
| 665 | u32 val; |
| 666 | |
| 667 | if (dma) |
| 668 | dmngt = STM32H7_DMNGT_DMA_CIRC; |
| 669 | else |
| 670 | dmngt = STM32H7_DMNGT_DR_ONLY; |
| 671 | |
| 672 | spin_lock_irqsave(&adc->lock, flags); |
| 673 | val = stm32_adc_readl(adc, STM32H7_ADC_CFGR); |
| 674 | val = (val & ~STM32H7_DMNGT_MASK) | (dmngt << STM32H7_DMNGT_SHIFT); |
| 675 | stm32_adc_writel(adc, STM32H7_ADC_CFGR, val); |
| 676 | spin_unlock_irqrestore(&adc->lock, flags); |
| 677 | |
| 678 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTART); |
| 679 | } |
| 680 | |
| 681 | static void stm32h7_adc_stop_conv(struct stm32_adc *adc) |
| 682 | { |
| 683 | struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
| 684 | int ret; |
| 685 | u32 val; |
| 686 | |
| 687 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTP); |
| 688 | |
| 689 | ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, |
| 690 | !(val & (STM32H7_ADSTART)), |
| 691 | 100, STM32_ADC_TIMEOUT_US); |
| 692 | if (ret) |
| 693 | dev_warn(&indio_dev->dev, "stop failed\n"); |
| 694 | |
| 695 | stm32_adc_clr_bits(adc, STM32H7_ADC_CFGR, STM32H7_DMNGT_MASK); |
| 696 | } |
| 697 | |
| 698 | static void stm32h7_adc_exit_pwr_down(struct stm32_adc *adc) |
| 699 | { |
| 700 | /* Exit deep power down, then enable ADC voltage regulator */ |
| 701 | stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD); |
| 702 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADVREGEN); |
| 703 | |
| 704 | if (adc->common->rate > STM32H7_BOOST_CLKRATE) |
| 705 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_BOOST); |
| 706 | |
| 707 | /* Wait for startup time */ |
| 708 | usleep_range(10, 20); |
| 709 | } |
| 710 | |
| 711 | static void stm32h7_adc_enter_pwr_down(struct stm32_adc *adc) |
| 712 | { |
| 713 | stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_BOOST); |
| 714 | |
| 715 | /* Setting DEEPPWD disables ADC vreg and clears ADVREGEN */ |
| 716 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD); |
| 717 | } |
| 718 | |
| 719 | static int stm32h7_adc_enable(struct stm32_adc *adc) |
| 720 | { |
| 721 | struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
| 722 | int ret; |
| 723 | u32 val; |
| 724 | |
| 725 | /* Clear ADRDY by writing one, then enable ADC */ |
| 726 | stm32_adc_set_bits(adc, STM32H7_ADC_ISR, STM32H7_ADRDY); |
| 727 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADEN); |
| 728 | |
| 729 | /* Poll for ADRDY to be set (after adc startup time) */ |
| 730 | ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_ISR, val, |
| 731 | val & STM32H7_ADRDY, |
| 732 | 100, STM32_ADC_TIMEOUT_US); |
| 733 | if (ret) { |
| 734 | stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADEN); |
| 735 | dev_err(&indio_dev->dev, "Failed to enable ADC\n"); |
| 736 | } |
| 737 | |
| 738 | return ret; |
| 739 | } |
| 740 | |
| 741 | static void stm32h7_adc_disable(struct stm32_adc *adc) |
| 742 | { |
| 743 | struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
| 744 | int ret; |
| 745 | u32 val; |
| 746 | |
| 747 | /* Disable ADC and wait until it's effectively disabled */ |
| 748 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADDIS); |
| 749 | ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, |
| 750 | !(val & STM32H7_ADEN), 100, |
| 751 | STM32_ADC_TIMEOUT_US); |
| 752 | if (ret) |
| 753 | dev_warn(&indio_dev->dev, "Failed to disable\n"); |
| 754 | } |
| 755 | |
| 756 | /** |
| 757 | * stm32h7_adc_read_selfcalib() - read calibration shadow regs, save result |
| 758 | * @adc: stm32 adc instance |
| 759 | */ |
| 760 | static int stm32h7_adc_read_selfcalib(struct stm32_adc *adc) |
| 761 | { |
| 762 | struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
| 763 | int i, ret; |
| 764 | u32 lincalrdyw_mask, val; |
| 765 | |
| 766 | /* Enable adc so LINCALRDYW1..6 bits are writable */ |
| 767 | ret = stm32h7_adc_enable(adc); |
| 768 | if (ret) |
| 769 | return ret; |
| 770 | |
| 771 | /* Read linearity calibration */ |
| 772 | lincalrdyw_mask = STM32H7_LINCALRDYW6; |
| 773 | for (i = STM32H7_LINCALFACT_NUM - 1; i >= 0; i--) { |
| 774 | /* Clear STM32H7_LINCALRDYW[6..1]: transfer calib to CALFACT2 */ |
| 775 | stm32_adc_clr_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask); |
| 776 | |
| 777 | /* Poll: wait calib data to be ready in CALFACT2 register */ |
| 778 | ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, |
| 779 | !(val & lincalrdyw_mask), |
| 780 | 100, STM32_ADC_TIMEOUT_US); |
| 781 | if (ret) { |
| 782 | dev_err(&indio_dev->dev, "Failed to read calfact\n"); |
| 783 | goto disable; |
| 784 | } |
| 785 | |
| 786 | val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2); |
| 787 | adc->cal.lincalfact[i] = (val & STM32H7_LINCALFACT_MASK); |
| 788 | adc->cal.lincalfact[i] >>= STM32H7_LINCALFACT_SHIFT; |
| 789 | |
| 790 | lincalrdyw_mask >>= 1; |
| 791 | } |
| 792 | |
| 793 | /* Read offset calibration */ |
| 794 | val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT); |
| 795 | adc->cal.calfact_s = (val & STM32H7_CALFACT_S_MASK); |
| 796 | adc->cal.calfact_s >>= STM32H7_CALFACT_S_SHIFT; |
| 797 | adc->cal.calfact_d = (val & STM32H7_CALFACT_D_MASK); |
| 798 | adc->cal.calfact_d >>= STM32H7_CALFACT_D_SHIFT; |
| 799 | |
| 800 | disable: |
| 801 | stm32h7_adc_disable(adc); |
| 802 | |
| 803 | return ret; |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * stm32h7_adc_restore_selfcalib() - Restore saved self-calibration result |
| 808 | * @adc: stm32 adc instance |
| 809 | * Note: ADC must be enabled, with no on-going conversions. |
| 810 | */ |
| 811 | static int stm32h7_adc_restore_selfcalib(struct stm32_adc *adc) |
| 812 | { |
| 813 | struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
| 814 | int i, ret; |
| 815 | u32 lincalrdyw_mask, val; |
| 816 | |
| 817 | val = (adc->cal.calfact_s << STM32H7_CALFACT_S_SHIFT) | |
| 818 | (adc->cal.calfact_d << STM32H7_CALFACT_D_SHIFT); |
| 819 | stm32_adc_writel(adc, STM32H7_ADC_CALFACT, val); |
| 820 | |
| 821 | lincalrdyw_mask = STM32H7_LINCALRDYW6; |
| 822 | for (i = STM32H7_LINCALFACT_NUM - 1; i >= 0; i--) { |
| 823 | /* |
| 824 | * Write saved calibration data to shadow registers: |
| 825 | * Write CALFACT2, and set LINCALRDYW[6..1] bit to trigger |
| 826 | * data write. Then poll to wait for complete transfer. |
| 827 | */ |
| 828 | val = adc->cal.lincalfact[i] << STM32H7_LINCALFACT_SHIFT; |
| 829 | stm32_adc_writel(adc, STM32H7_ADC_CALFACT2, val); |
| 830 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask); |
| 831 | ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, |
| 832 | val & lincalrdyw_mask, |
| 833 | 100, STM32_ADC_TIMEOUT_US); |
| 834 | if (ret) { |
| 835 | dev_err(&indio_dev->dev, "Failed to write calfact\n"); |
| 836 | return ret; |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * Read back calibration data, has two effects: |
| 841 | * - It ensures bits LINCALRDYW[6..1] are kept cleared |
| 842 | * for next time calibration needs to be restored. |
| 843 | * - BTW, bit clear triggers a read, then check data has been |
| 844 | * correctly written. |
| 845 | */ |
| 846 | stm32_adc_clr_bits(adc, STM32H7_ADC_CR, lincalrdyw_mask); |
| 847 | ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, |
| 848 | !(val & lincalrdyw_mask), |
| 849 | 100, STM32_ADC_TIMEOUT_US); |
| 850 | if (ret) { |
| 851 | dev_err(&indio_dev->dev, "Failed to read calfact\n"); |
| 852 | return ret; |
| 853 | } |
| 854 | val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2); |
| 855 | if (val != adc->cal.lincalfact[i] << STM32H7_LINCALFACT_SHIFT) { |
| 856 | dev_err(&indio_dev->dev, "calfact not consistent\n"); |
| 857 | return -EIO; |
| 858 | } |
| 859 | |
| 860 | lincalrdyw_mask >>= 1; |
| 861 | } |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Fixed timeout value for ADC calibration. |
| 868 | * worst cases: |
| 869 | * - low clock frequency |
| 870 | * - maximum prescalers |
| 871 | * Calibration requires: |
| 872 | * - 131,072 ADC clock cycle for the linear calibration |
| 873 | * - 20 ADC clock cycle for the offset calibration |
| 874 | * |
| 875 | * Set to 100ms for now |
| 876 | */ |
| 877 | #define STM32H7_ADC_CALIB_TIMEOUT_US 100000 |
| 878 | |
| 879 | /** |
| 880 | * stm32h7_adc_selfcalib() - Procedure to calibrate ADC (from power down) |
| 881 | * @adc: stm32 adc instance |
| 882 | * Exit from power down, calibrate ADC, then return to power down. |
| 883 | */ |
| 884 | static int stm32h7_adc_selfcalib(struct stm32_adc *adc) |
| 885 | { |
| 886 | struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
| 887 | int ret; |
| 888 | u32 val; |
| 889 | |
| 890 | stm32h7_adc_exit_pwr_down(adc); |
| 891 | |
| 892 | /* |
| 893 | * Select calibration mode: |
| 894 | * - Offset calibration for single ended inputs |
| 895 | * - No linearity calibration (do it later, before reading it) |
| 896 | */ |
| 897 | stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADCALDIF); |
| 898 | stm32_adc_clr_bits(adc, STM32H7_ADC_CR, STM32H7_ADCALLIN); |
| 899 | |
| 900 | /* Start calibration, then wait for completion */ |
| 901 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADCAL); |
| 902 | ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, |
| 903 | !(val & STM32H7_ADCAL), 100, |
| 904 | STM32H7_ADC_CALIB_TIMEOUT_US); |
| 905 | if (ret) { |
| 906 | dev_err(&indio_dev->dev, "calibration failed\n"); |
| 907 | goto pwr_dwn; |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * Select calibration mode, then start calibration: |
| 912 | * - Offset calibration for differential input |
| 913 | * - Linearity calibration (needs to be done only once for single/diff) |
| 914 | * will run simultaneously with offset calibration. |
| 915 | */ |
| 916 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, |
| 917 | STM32H7_ADCALDIF | STM32H7_ADCALLIN); |
| 918 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADCAL); |
| 919 | ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val, |
| 920 | !(val & STM32H7_ADCAL), 100, |
| 921 | STM32H7_ADC_CALIB_TIMEOUT_US); |
| 922 | if (ret) { |
| 923 | dev_err(&indio_dev->dev, "calibration failed\n"); |
| 924 | goto pwr_dwn; |
| 925 | } |
| 926 | |
| 927 | stm32_adc_clr_bits(adc, STM32H7_ADC_CR, |
| 928 | STM32H7_ADCALDIF | STM32H7_ADCALLIN); |
| 929 | |
| 930 | /* Read calibration result for future reference */ |
| 931 | ret = stm32h7_adc_read_selfcalib(adc); |
| 932 | |
| 933 | pwr_dwn: |
| 934 | stm32h7_adc_enter_pwr_down(adc); |
| 935 | |
| 936 | return ret; |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * stm32h7_adc_prepare() - Leave power down mode to enable ADC. |
| 941 | * @adc: stm32 adc instance |
| 942 | * Leave power down mode. |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 943 | * Configure channels as single ended or differential before enabling ADC. |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 944 | * Enable ADC. |
| 945 | * Restore calibration data. |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 946 | * Pre-select channels that may be used in PCSEL (required by input MUX / IO): |
| 947 | * - Only one input is selected for single ended (e.g. 'vinp') |
| 948 | * - Two inputs are selected for differential channels (e.g. 'vinp' & 'vinn') |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 949 | */ |
| 950 | static int stm32h7_adc_prepare(struct stm32_adc *adc) |
| 951 | { |
| 952 | int ret; |
| 953 | |
| 954 | stm32h7_adc_exit_pwr_down(adc); |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 955 | stm32_adc_writel(adc, STM32H7_ADC_DIFSEL, adc->difsel); |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 956 | |
| 957 | ret = stm32h7_adc_enable(adc); |
| 958 | if (ret) |
| 959 | goto pwr_dwn; |
| 960 | |
| 961 | ret = stm32h7_adc_restore_selfcalib(adc); |
| 962 | if (ret) |
| 963 | goto disable; |
| 964 | |
| 965 | stm32_adc_writel(adc, STM32H7_ADC_PCSEL, adc->pcsel); |
| 966 | |
| 967 | return 0; |
| 968 | |
| 969 | disable: |
| 970 | stm32h7_adc_disable(adc); |
| 971 | pwr_dwn: |
| 972 | stm32h7_adc_enter_pwr_down(adc); |
| 973 | |
| 974 | return ret; |
| 975 | } |
| 976 | |
| 977 | static void stm32h7_adc_unprepare(struct stm32_adc *adc) |
| 978 | { |
| 979 | stm32h7_adc_disable(adc); |
| 980 | stm32h7_adc_enter_pwr_down(adc); |
| 981 | } |
| 982 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 983 | /** |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 984 | * stm32_adc_conf_scan_seq() - Build regular channels scan sequence |
| 985 | * @indio_dev: IIO device |
| 986 | * @scan_mask: channels to be converted |
| 987 | * |
| 988 | * Conversion sequence : |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 989 | * Apply sampling time settings for all channels. |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 990 | * Configure ADC scan sequence based on selected channels in scan_mask. |
| 991 | * Add channels to SQR registers, from scan_mask LSB to MSB, then |
| 992 | * program sequence len. |
| 993 | */ |
| 994 | static int stm32_adc_conf_scan_seq(struct iio_dev *indio_dev, |
| 995 | const unsigned long *scan_mask) |
| 996 | { |
| 997 | struct stm32_adc *adc = iio_priv(indio_dev); |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 998 | const struct stm32_adc_regs *sqr = adc->cfg->regs->sqr; |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 999 | const struct iio_chan_spec *chan; |
| 1000 | u32 val, bit; |
| 1001 | int i = 0; |
| 1002 | |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 1003 | /* Apply sampling time settings */ |
| 1004 | stm32_adc_writel(adc, adc->cfg->regs->smpr[0], adc->smpr_val[0]); |
| 1005 | stm32_adc_writel(adc, adc->cfg->regs->smpr[1], adc->smpr_val[1]); |
| 1006 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1007 | for_each_set_bit(bit, scan_mask, indio_dev->masklength) { |
| 1008 | chan = indio_dev->channels + bit; |
| 1009 | /* |
| 1010 | * Assign one channel per SQ entry in regular |
| 1011 | * sequence, starting with SQ1. |
| 1012 | */ |
| 1013 | i++; |
| 1014 | if (i > STM32_ADC_MAX_SQ) |
| 1015 | return -EINVAL; |
| 1016 | |
| 1017 | dev_dbg(&indio_dev->dev, "%s chan %d to SQ%d\n", |
| 1018 | __func__, chan->channel, i); |
| 1019 | |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1020 | val = stm32_adc_readl(adc, sqr[i].reg); |
| 1021 | val &= ~sqr[i].mask; |
| 1022 | val |= chan->channel << sqr[i].shift; |
| 1023 | stm32_adc_writel(adc, sqr[i].reg, val); |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | if (!i) |
| 1027 | return -EINVAL; |
| 1028 | |
| 1029 | /* Sequence len */ |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1030 | val = stm32_adc_readl(adc, sqr[0].reg); |
| 1031 | val &= ~sqr[0].mask; |
| 1032 | val |= ((i - 1) << sqr[0].shift); |
| 1033 | stm32_adc_writel(adc, sqr[0].reg, val); |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1034 | |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * stm32_adc_get_trig_extsel() - Get external trigger selection |
| 1040 | * @trig: trigger |
| 1041 | * |
| 1042 | * Returns trigger extsel value, if trig matches, -EINVAL otherwise. |
| 1043 | */ |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1044 | static int stm32_adc_get_trig_extsel(struct iio_dev *indio_dev, |
| 1045 | struct iio_trigger *trig) |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1046 | { |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1047 | struct stm32_adc *adc = iio_priv(indio_dev); |
Fabrice Gasnier | f24a33b | 2017-01-26 15:28:30 +0100 | [diff] [blame] | 1048 | int i; |
| 1049 | |
| 1050 | /* lookup triggers registered by stm32 timer trigger driver */ |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1051 | for (i = 0; adc->cfg->trigs[i].name; i++) { |
Fabrice Gasnier | f24a33b | 2017-01-26 15:28:30 +0100 | [diff] [blame] | 1052 | /** |
| 1053 | * Checking both stm32 timer trigger type and trig name |
| 1054 | * should be safe against arbitrary trigger names. |
| 1055 | */ |
Fabrice Gasnier | f0b638a | 2017-08-28 12:04:14 +0200 | [diff] [blame] | 1056 | if ((is_stm32_timer_trigger(trig) || |
| 1057 | is_stm32_lptim_trigger(trig)) && |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1058 | !strcmp(adc->cfg->trigs[i].name, trig->name)) { |
| 1059 | return adc->cfg->trigs[i].extsel; |
Fabrice Gasnier | f24a33b | 2017-01-26 15:28:30 +0100 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1063 | return -EINVAL; |
| 1064 | } |
| 1065 | |
| 1066 | /** |
| 1067 | * stm32_adc_set_trig() - Set a regular trigger |
| 1068 | * @indio_dev: IIO device |
| 1069 | * @trig: IIO trigger |
| 1070 | * |
| 1071 | * Set trigger source/polarity (e.g. SW, or HW with polarity) : |
| 1072 | * - if HW trigger disabled (e.g. trig == NULL, conversion launched by sw) |
| 1073 | * - if HW trigger enabled, set source & polarity |
| 1074 | */ |
| 1075 | static int stm32_adc_set_trig(struct iio_dev *indio_dev, |
| 1076 | struct iio_trigger *trig) |
| 1077 | { |
| 1078 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1079 | u32 val, extsel = 0, exten = STM32_EXTEN_SWTRIG; |
| 1080 | unsigned long flags; |
| 1081 | int ret; |
| 1082 | |
| 1083 | if (trig) { |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1084 | ret = stm32_adc_get_trig_extsel(indio_dev, trig); |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1085 | if (ret < 0) |
| 1086 | return ret; |
| 1087 | |
| 1088 | /* set trigger source and polarity (default to rising edge) */ |
| 1089 | extsel = ret; |
Fabrice Gasnier | 732f2dc | 2017-01-26 15:28:31 +0100 | [diff] [blame] | 1090 | exten = adc->trigger_polarity + STM32_EXTEN_HWTRIG_RISING_EDGE; |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | spin_lock_irqsave(&adc->lock, flags); |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1094 | val = stm32_adc_readl(adc, adc->cfg->regs->exten.reg); |
| 1095 | val &= ~(adc->cfg->regs->exten.mask | adc->cfg->regs->extsel.mask); |
| 1096 | val |= exten << adc->cfg->regs->exten.shift; |
| 1097 | val |= extsel << adc->cfg->regs->extsel.shift; |
| 1098 | stm32_adc_writel(adc, adc->cfg->regs->exten.reg, val); |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1099 | spin_unlock_irqrestore(&adc->lock, flags); |
| 1100 | |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
Fabrice Gasnier | 732f2dc | 2017-01-26 15:28:31 +0100 | [diff] [blame] | 1104 | static int stm32_adc_set_trig_pol(struct iio_dev *indio_dev, |
| 1105 | const struct iio_chan_spec *chan, |
| 1106 | unsigned int type) |
| 1107 | { |
| 1108 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1109 | |
| 1110 | adc->trigger_polarity = type; |
| 1111 | |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | static int stm32_adc_get_trig_pol(struct iio_dev *indio_dev, |
| 1116 | const struct iio_chan_spec *chan) |
| 1117 | { |
| 1118 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1119 | |
| 1120 | return adc->trigger_polarity; |
| 1121 | } |
| 1122 | |
| 1123 | static const char * const stm32_trig_pol_items[] = { |
| 1124 | "rising-edge", "falling-edge", "both-edges", |
| 1125 | }; |
| 1126 | |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1127 | static const struct iio_enum stm32_adc_trig_pol = { |
Fabrice Gasnier | 732f2dc | 2017-01-26 15:28:31 +0100 | [diff] [blame] | 1128 | .items = stm32_trig_pol_items, |
| 1129 | .num_items = ARRAY_SIZE(stm32_trig_pol_items), |
| 1130 | .get = stm32_adc_get_trig_pol, |
| 1131 | .set = stm32_adc_set_trig_pol, |
| 1132 | }; |
| 1133 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1134 | /** |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1135 | * stm32_adc_single_conv() - Performs a single conversion |
| 1136 | * @indio_dev: IIO device |
| 1137 | * @chan: IIO channel |
| 1138 | * @res: conversion result |
| 1139 | * |
| 1140 | * The function performs a single conversion on a given channel: |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 1141 | * - Apply sampling time settings |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1142 | * - Program sequencer with one channel (e.g. in SQ1 with len = 1) |
| 1143 | * - Use SW trigger |
| 1144 | * - Start conversion, then wait for interrupt completion. |
| 1145 | */ |
| 1146 | static int stm32_adc_single_conv(struct iio_dev *indio_dev, |
| 1147 | const struct iio_chan_spec *chan, |
| 1148 | int *res) |
| 1149 | { |
| 1150 | struct stm32_adc *adc = iio_priv(indio_dev); |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1151 | const struct stm32_adc_regspec *regs = adc->cfg->regs; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1152 | long timeout; |
| 1153 | u32 val; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1154 | int ret; |
| 1155 | |
| 1156 | reinit_completion(&adc->completion); |
| 1157 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1158 | adc->bufi = 0; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1159 | |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 1160 | if (adc->cfg->prepare) { |
| 1161 | ret = adc->cfg->prepare(adc); |
| 1162 | if (ret) |
| 1163 | return ret; |
| 1164 | } |
| 1165 | |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 1166 | /* Apply sampling time settings */ |
| 1167 | stm32_adc_writel(adc, regs->smpr[0], adc->smpr_val[0]); |
| 1168 | stm32_adc_writel(adc, regs->smpr[1], adc->smpr_val[1]); |
| 1169 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1170 | /* Program chan number in regular sequence (SQ1) */ |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1171 | val = stm32_adc_readl(adc, regs->sqr[1].reg); |
| 1172 | val &= ~regs->sqr[1].mask; |
| 1173 | val |= chan->channel << regs->sqr[1].shift; |
| 1174 | stm32_adc_writel(adc, regs->sqr[1].reg, val); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1175 | |
| 1176 | /* Set regular sequence len (0 for 1 conversion) */ |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1177 | stm32_adc_clr_bits(adc, regs->sqr[0].reg, regs->sqr[0].mask); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1178 | |
| 1179 | /* Trigger detection disabled (conversion can be launched in SW) */ |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1180 | stm32_adc_clr_bits(adc, regs->exten.reg, regs->exten.mask); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1181 | |
| 1182 | stm32_adc_conv_irq_enable(adc); |
| 1183 | |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1184 | adc->cfg->start_conv(adc, false); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1185 | |
| 1186 | timeout = wait_for_completion_interruptible_timeout( |
| 1187 | &adc->completion, STM32_ADC_TIMEOUT); |
| 1188 | if (timeout == 0) { |
| 1189 | ret = -ETIMEDOUT; |
| 1190 | } else if (timeout < 0) { |
| 1191 | ret = timeout; |
| 1192 | } else { |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1193 | *res = adc->buffer[0]; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1194 | ret = IIO_VAL_INT; |
| 1195 | } |
| 1196 | |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1197 | adc->cfg->stop_conv(adc); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1198 | |
| 1199 | stm32_adc_conv_irq_disable(adc); |
| 1200 | |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 1201 | if (adc->cfg->unprepare) |
| 1202 | adc->cfg->unprepare(adc); |
| 1203 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1204 | return ret; |
| 1205 | } |
| 1206 | |
| 1207 | static int stm32_adc_read_raw(struct iio_dev *indio_dev, |
| 1208 | struct iio_chan_spec const *chan, |
| 1209 | int *val, int *val2, long mask) |
| 1210 | { |
| 1211 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1212 | int ret; |
| 1213 | |
| 1214 | switch (mask) { |
| 1215 | case IIO_CHAN_INFO_RAW: |
| 1216 | ret = iio_device_claim_direct_mode(indio_dev); |
| 1217 | if (ret) |
| 1218 | return ret; |
| 1219 | if (chan->type == IIO_VOLTAGE) |
| 1220 | ret = stm32_adc_single_conv(indio_dev, chan, val); |
| 1221 | else |
| 1222 | ret = -EINVAL; |
| 1223 | iio_device_release_direct_mode(indio_dev); |
| 1224 | return ret; |
| 1225 | |
| 1226 | case IIO_CHAN_INFO_SCALE: |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 1227 | if (chan->differential) { |
| 1228 | *val = adc->common->vref_mv * 2; |
| 1229 | *val2 = chan->scan_type.realbits; |
| 1230 | } else { |
| 1231 | *val = adc->common->vref_mv; |
| 1232 | *val2 = chan->scan_type.realbits; |
| 1233 | } |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1234 | return IIO_VAL_FRACTIONAL_LOG2; |
| 1235 | |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 1236 | case IIO_CHAN_INFO_OFFSET: |
| 1237 | if (chan->differential) |
| 1238 | /* ADC_full_scale / 2 */ |
| 1239 | *val = -((1 << chan->scan_type.realbits) / 2); |
| 1240 | else |
| 1241 | *val = 0; |
| 1242 | return IIO_VAL_INT; |
| 1243 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1244 | default: |
| 1245 | return -EINVAL; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | static irqreturn_t stm32_adc_isr(int irq, void *data) |
| 1250 | { |
| 1251 | struct stm32_adc *adc = data; |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1252 | struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1253 | const struct stm32_adc_regspec *regs = adc->cfg->regs; |
| 1254 | u32 status = stm32_adc_readl(adc, regs->isr_eoc.reg); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1255 | |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1256 | if (status & regs->isr_eoc.mask) { |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1257 | /* Reading DR also clears EOC status flag */ |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1258 | adc->buffer[adc->bufi] = stm32_adc_readw(adc, regs->dr); |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1259 | if (iio_buffer_enabled(indio_dev)) { |
| 1260 | adc->bufi++; |
| 1261 | if (adc->bufi >= adc->num_conv) { |
| 1262 | stm32_adc_conv_irq_disable(adc); |
| 1263 | iio_trigger_poll(indio_dev->trig); |
| 1264 | } |
| 1265 | } else { |
| 1266 | complete(&adc->completion); |
| 1267 | } |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1268 | return IRQ_HANDLED; |
| 1269 | } |
| 1270 | |
| 1271 | return IRQ_NONE; |
| 1272 | } |
| 1273 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1274 | /** |
| 1275 | * stm32_adc_validate_trigger() - validate trigger for stm32 adc |
| 1276 | * @indio_dev: IIO device |
| 1277 | * @trig: new trigger |
| 1278 | * |
| 1279 | * Returns: 0 if trig matches one of the triggers registered by stm32 adc |
| 1280 | * driver, -EINVAL otherwise. |
| 1281 | */ |
| 1282 | static int stm32_adc_validate_trigger(struct iio_dev *indio_dev, |
| 1283 | struct iio_trigger *trig) |
| 1284 | { |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1285 | return stm32_adc_get_trig_extsel(indio_dev, trig) < 0 ? -EINVAL : 0; |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1286 | } |
| 1287 | |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1288 | static int stm32_adc_set_watermark(struct iio_dev *indio_dev, unsigned int val) |
| 1289 | { |
| 1290 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1291 | unsigned int watermark = STM32_DMA_BUFFER_SIZE / 2; |
| 1292 | |
| 1293 | /* |
| 1294 | * dma cyclic transfers are used, buffer is split into two periods. |
| 1295 | * There should be : |
| 1296 | * - always one buffer (period) dma is working on |
| 1297 | * - one buffer (period) driver can push with iio_trigger_poll(). |
| 1298 | */ |
| 1299 | watermark = min(watermark, val * (unsigned)(sizeof(u16))); |
| 1300 | adc->rx_buf_sz = watermark * 2; |
| 1301 | |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1305 | static int stm32_adc_update_scan_mode(struct iio_dev *indio_dev, |
| 1306 | const unsigned long *scan_mask) |
| 1307 | { |
| 1308 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1309 | int ret; |
| 1310 | |
| 1311 | adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength); |
| 1312 | |
| 1313 | ret = stm32_adc_conf_scan_seq(indio_dev, scan_mask); |
| 1314 | if (ret) |
| 1315 | return ret; |
| 1316 | |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1320 | static int stm32_adc_of_xlate(struct iio_dev *indio_dev, |
| 1321 | const struct of_phandle_args *iiospec) |
| 1322 | { |
| 1323 | int i; |
| 1324 | |
| 1325 | for (i = 0; i < indio_dev->num_channels; i++) |
| 1326 | if (indio_dev->channels[i].channel == iiospec->args[0]) |
| 1327 | return i; |
| 1328 | |
| 1329 | return -EINVAL; |
| 1330 | } |
| 1331 | |
| 1332 | /** |
| 1333 | * stm32_adc_debugfs_reg_access - read or write register value |
| 1334 | * |
| 1335 | * To read a value from an ADC register: |
| 1336 | * echo [ADC reg offset] > direct_reg_access |
| 1337 | * cat direct_reg_access |
| 1338 | * |
| 1339 | * To write a value in a ADC register: |
| 1340 | * echo [ADC_reg_offset] [value] > direct_reg_access |
| 1341 | */ |
| 1342 | static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev, |
| 1343 | unsigned reg, unsigned writeval, |
| 1344 | unsigned *readval) |
| 1345 | { |
| 1346 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1347 | |
| 1348 | if (!readval) |
| 1349 | stm32_adc_writel(adc, reg, writeval); |
| 1350 | else |
| 1351 | *readval = stm32_adc_readl(adc, reg); |
| 1352 | |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | static const struct iio_info stm32_adc_iio_info = { |
| 1357 | .read_raw = stm32_adc_read_raw, |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1358 | .validate_trigger = stm32_adc_validate_trigger, |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1359 | .hwfifo_set_watermark = stm32_adc_set_watermark, |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1360 | .update_scan_mode = stm32_adc_update_scan_mode, |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1361 | .debugfs_reg_access = stm32_adc_debugfs_reg_access, |
| 1362 | .of_xlate = stm32_adc_of_xlate, |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1363 | }; |
| 1364 | |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1365 | static unsigned int stm32_adc_dma_residue(struct stm32_adc *adc) |
| 1366 | { |
| 1367 | struct dma_tx_state state; |
| 1368 | enum dma_status status; |
| 1369 | |
| 1370 | status = dmaengine_tx_status(adc->dma_chan, |
| 1371 | adc->dma_chan->cookie, |
| 1372 | &state); |
| 1373 | if (status == DMA_IN_PROGRESS) { |
| 1374 | /* Residue is size in bytes from end of buffer */ |
| 1375 | unsigned int i = adc->rx_buf_sz - state.residue; |
| 1376 | unsigned int size; |
| 1377 | |
| 1378 | /* Return available bytes */ |
| 1379 | if (i >= adc->bufi) |
| 1380 | size = i - adc->bufi; |
| 1381 | else |
| 1382 | size = adc->rx_buf_sz + i - adc->bufi; |
| 1383 | |
| 1384 | return size; |
| 1385 | } |
| 1386 | |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | static void stm32_adc_dma_buffer_done(void *data) |
| 1391 | { |
| 1392 | struct iio_dev *indio_dev = data; |
| 1393 | |
| 1394 | iio_trigger_poll_chained(indio_dev->trig); |
| 1395 | } |
| 1396 | |
| 1397 | static int stm32_adc_dma_start(struct iio_dev *indio_dev) |
| 1398 | { |
| 1399 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1400 | struct dma_async_tx_descriptor *desc; |
| 1401 | dma_cookie_t cookie; |
| 1402 | int ret; |
| 1403 | |
| 1404 | if (!adc->dma_chan) |
| 1405 | return 0; |
| 1406 | |
| 1407 | dev_dbg(&indio_dev->dev, "%s size=%d watermark=%d\n", __func__, |
| 1408 | adc->rx_buf_sz, adc->rx_buf_sz / 2); |
| 1409 | |
| 1410 | /* Prepare a DMA cyclic transaction */ |
| 1411 | desc = dmaengine_prep_dma_cyclic(adc->dma_chan, |
| 1412 | adc->rx_dma_buf, |
| 1413 | adc->rx_buf_sz, adc->rx_buf_sz / 2, |
| 1414 | DMA_DEV_TO_MEM, |
| 1415 | DMA_PREP_INTERRUPT); |
| 1416 | if (!desc) |
| 1417 | return -EBUSY; |
| 1418 | |
| 1419 | desc->callback = stm32_adc_dma_buffer_done; |
| 1420 | desc->callback_param = indio_dev; |
| 1421 | |
| 1422 | cookie = dmaengine_submit(desc); |
| 1423 | ret = dma_submit_error(cookie); |
| 1424 | if (ret) { |
| 1425 | dmaengine_terminate_all(adc->dma_chan); |
| 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | /* Issue pending DMA requests */ |
| 1430 | dma_async_issue_pending(adc->dma_chan); |
| 1431 | |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1435 | static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev) |
| 1436 | { |
| 1437 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1438 | int ret; |
| 1439 | |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 1440 | if (adc->cfg->prepare) { |
| 1441 | ret = adc->cfg->prepare(adc); |
| 1442 | if (ret) |
| 1443 | return ret; |
| 1444 | } |
| 1445 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1446 | ret = stm32_adc_set_trig(indio_dev, indio_dev->trig); |
| 1447 | if (ret) { |
| 1448 | dev_err(&indio_dev->dev, "Can't set trigger\n"); |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 1449 | goto err_unprepare; |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1452 | ret = stm32_adc_dma_start(indio_dev); |
| 1453 | if (ret) { |
| 1454 | dev_err(&indio_dev->dev, "Can't start dma\n"); |
| 1455 | goto err_clr_trig; |
| 1456 | } |
| 1457 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1458 | ret = iio_triggered_buffer_postenable(indio_dev); |
| 1459 | if (ret < 0) |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1460 | goto err_stop_dma; |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1461 | |
| 1462 | /* Reset adc buffer index */ |
| 1463 | adc->bufi = 0; |
| 1464 | |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1465 | if (!adc->dma_chan) |
| 1466 | stm32_adc_conv_irq_enable(adc); |
| 1467 | |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1468 | adc->cfg->start_conv(adc, !!adc->dma_chan); |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1469 | |
| 1470 | return 0; |
| 1471 | |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1472 | err_stop_dma: |
| 1473 | if (adc->dma_chan) |
| 1474 | dmaengine_terminate_all(adc->dma_chan); |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1475 | err_clr_trig: |
| 1476 | stm32_adc_set_trig(indio_dev, NULL); |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 1477 | err_unprepare: |
| 1478 | if (adc->cfg->unprepare) |
| 1479 | adc->cfg->unprepare(adc); |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1480 | |
| 1481 | return ret; |
| 1482 | } |
| 1483 | |
| 1484 | static int stm32_adc_buffer_predisable(struct iio_dev *indio_dev) |
| 1485 | { |
| 1486 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1487 | int ret; |
| 1488 | |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1489 | adc->cfg->stop_conv(adc); |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1490 | if (!adc->dma_chan) |
| 1491 | stm32_adc_conv_irq_disable(adc); |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1492 | |
| 1493 | ret = iio_triggered_buffer_predisable(indio_dev); |
| 1494 | if (ret < 0) |
| 1495 | dev_err(&indio_dev->dev, "predisable failed\n"); |
| 1496 | |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1497 | if (adc->dma_chan) |
| 1498 | dmaengine_terminate_all(adc->dma_chan); |
| 1499 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1500 | if (stm32_adc_set_trig(indio_dev, NULL)) |
| 1501 | dev_err(&indio_dev->dev, "Can't clear trigger\n"); |
| 1502 | |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 1503 | if (adc->cfg->unprepare) |
| 1504 | adc->cfg->unprepare(adc); |
| 1505 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1506 | return ret; |
| 1507 | } |
| 1508 | |
| 1509 | static const struct iio_buffer_setup_ops stm32_adc_buffer_setup_ops = { |
| 1510 | .postenable = &stm32_adc_buffer_postenable, |
| 1511 | .predisable = &stm32_adc_buffer_predisable, |
| 1512 | }; |
| 1513 | |
| 1514 | static irqreturn_t stm32_adc_trigger_handler(int irq, void *p) |
| 1515 | { |
| 1516 | struct iio_poll_func *pf = p; |
| 1517 | struct iio_dev *indio_dev = pf->indio_dev; |
| 1518 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1519 | |
| 1520 | dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi); |
| 1521 | |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1522 | if (!adc->dma_chan) { |
| 1523 | /* reset buffer index */ |
| 1524 | adc->bufi = 0; |
| 1525 | iio_push_to_buffers_with_timestamp(indio_dev, adc->buffer, |
| 1526 | pf->timestamp); |
| 1527 | } else { |
| 1528 | int residue = stm32_adc_dma_residue(adc); |
| 1529 | |
| 1530 | while (residue >= indio_dev->scan_bytes) { |
| 1531 | u16 *buffer = (u16 *)&adc->rx_buf[adc->bufi]; |
| 1532 | |
| 1533 | iio_push_to_buffers_with_timestamp(indio_dev, buffer, |
| 1534 | pf->timestamp); |
| 1535 | residue -= indio_dev->scan_bytes; |
| 1536 | adc->bufi += indio_dev->scan_bytes; |
| 1537 | if (adc->bufi >= adc->rx_buf_sz) |
| 1538 | adc->bufi = 0; |
| 1539 | } |
| 1540 | } |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1541 | |
| 1542 | iio_trigger_notify_done(indio_dev->trig); |
| 1543 | |
| 1544 | /* re-enable eoc irq */ |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1545 | if (!adc->dma_chan) |
| 1546 | stm32_adc_conv_irq_enable(adc); |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1547 | |
| 1548 | return IRQ_HANDLED; |
| 1549 | } |
| 1550 | |
Fabrice Gasnier | 732f2dc | 2017-01-26 15:28:31 +0100 | [diff] [blame] | 1551 | static const struct iio_chan_spec_ext_info stm32_adc_ext_info[] = { |
| 1552 | IIO_ENUM("trigger_polarity", IIO_SHARED_BY_ALL, &stm32_adc_trig_pol), |
| 1553 | { |
| 1554 | .name = "trigger_polarity_available", |
| 1555 | .shared = IIO_SHARED_BY_ALL, |
| 1556 | .read = iio_enum_available_read, |
| 1557 | .private = (uintptr_t)&stm32_adc_trig_pol, |
| 1558 | }, |
| 1559 | {}, |
| 1560 | }; |
| 1561 | |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 1562 | static int stm32_adc_of_get_resolution(struct iio_dev *indio_dev) |
| 1563 | { |
| 1564 | struct device_node *node = indio_dev->dev.of_node; |
| 1565 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1566 | unsigned int i; |
| 1567 | u32 res; |
| 1568 | |
| 1569 | if (of_property_read_u32(node, "assigned-resolution-bits", &res)) |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1570 | res = adc->cfg->adc_info->resolutions[0]; |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 1571 | |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1572 | for (i = 0; i < adc->cfg->adc_info->num_res; i++) |
| 1573 | if (res == adc->cfg->adc_info->resolutions[i]) |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 1574 | break; |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1575 | if (i >= adc->cfg->adc_info->num_res) { |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 1576 | dev_err(&indio_dev->dev, "Bad resolution: %u bits\n", res); |
| 1577 | return -EINVAL; |
| 1578 | } |
| 1579 | |
| 1580 | dev_dbg(&indio_dev->dev, "Using %u bits resolution\n", res); |
| 1581 | adc->res = i; |
| 1582 | |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 1586 | static void stm32_adc_smpr_init(struct stm32_adc *adc, int channel, u32 smp_ns) |
| 1587 | { |
| 1588 | const struct stm32_adc_regs *smpr = &adc->cfg->regs->smp_bits[channel]; |
| 1589 | u32 period_ns, shift = smpr->shift, mask = smpr->mask; |
| 1590 | unsigned int smp, r = smpr->reg; |
| 1591 | |
| 1592 | /* Determine sampling time (ADC clock cycles) */ |
| 1593 | period_ns = NSEC_PER_SEC / adc->common->rate; |
| 1594 | for (smp = 0; smp <= STM32_ADC_MAX_SMP; smp++) |
| 1595 | if ((period_ns * adc->cfg->smp_cycles[smp]) >= smp_ns) |
| 1596 | break; |
| 1597 | if (smp > STM32_ADC_MAX_SMP) |
| 1598 | smp = STM32_ADC_MAX_SMP; |
| 1599 | |
| 1600 | /* pre-build sampling time registers (e.g. smpr1, smpr2) */ |
| 1601 | adc->smpr_val[r] = (adc->smpr_val[r] & ~mask) | (smp << shift); |
| 1602 | } |
| 1603 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1604 | static void stm32_adc_chan_init_one(struct iio_dev *indio_dev, |
Fabrice Gasnier | 0bae72a | 2017-10-25 11:27:44 +0200 | [diff] [blame] | 1605 | struct iio_chan_spec *chan, u32 vinp, |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 1606 | u32 vinn, int scan_index, bool differential) |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1607 | { |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 1608 | struct stm32_adc *adc = iio_priv(indio_dev); |
Fabrice Gasnier | 0bae72a | 2017-10-25 11:27:44 +0200 | [diff] [blame] | 1609 | char *name = adc->chan_name[vinp]; |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 1610 | |
Fabrice Gasnier | 0bae72a | 2017-10-25 11:27:44 +0200 | [diff] [blame] | 1611 | chan->type = IIO_VOLTAGE; |
| 1612 | chan->channel = vinp; |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 1613 | if (differential) { |
| 1614 | chan->differential = 1; |
| 1615 | chan->channel2 = vinn; |
| 1616 | snprintf(name, STM32_ADC_CH_SZ, "in%d-in%d", vinp, vinn); |
| 1617 | } else { |
| 1618 | snprintf(name, STM32_ADC_CH_SZ, "in%d", vinp); |
| 1619 | } |
Fabrice Gasnier | 0bae72a | 2017-10-25 11:27:44 +0200 | [diff] [blame] | 1620 | chan->datasheet_name = name; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1621 | chan->scan_index = scan_index; |
| 1622 | chan->indexed = 1; |
| 1623 | chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 1624 | chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | |
| 1625 | BIT(IIO_CHAN_INFO_OFFSET); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1626 | chan->scan_type.sign = 'u'; |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1627 | chan->scan_type.realbits = adc->cfg->adc_info->resolutions[adc->res]; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1628 | chan->scan_type.storagebits = 16; |
Fabrice Gasnier | 732f2dc | 2017-01-26 15:28:31 +0100 | [diff] [blame] | 1629 | chan->ext_info = stm32_adc_ext_info; |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 1630 | |
| 1631 | /* pre-build selected channels mask */ |
| 1632 | adc->pcsel |= BIT(chan->channel); |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 1633 | if (differential) { |
| 1634 | /* pre-build diff channels mask */ |
| 1635 | adc->difsel |= BIT(chan->channel); |
| 1636 | /* Also add negative input to pre-selected channels */ |
| 1637 | adc->pcsel |= BIT(chan->channel2); |
| 1638 | } |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | static int stm32_adc_chan_of_init(struct iio_dev *indio_dev) |
| 1642 | { |
| 1643 | struct device_node *node = indio_dev->dev.of_node; |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1644 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1645 | const struct stm32_adc_info *adc_info = adc->cfg->adc_info; |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 1646 | struct stm32_adc_diff_channel diff[STM32_ADC_CH_MAX]; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1647 | struct property *prop; |
| 1648 | const __be32 *cur; |
| 1649 | struct iio_chan_spec *channels; |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 1650 | int scan_index = 0, num_channels = 0, num_diff = 0, ret, i; |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 1651 | u32 val, smp = 0; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1652 | |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 1653 | ret = of_property_count_u32_elems(node, "st,adc-channels"); |
| 1654 | if (ret > adc_info->max_channels) { |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1655 | dev_err(&indio_dev->dev, "Bad st,adc-channels?\n"); |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 1656 | return -EINVAL; |
| 1657 | } else if (ret > 0) { |
| 1658 | num_channels += ret; |
| 1659 | } |
| 1660 | |
| 1661 | ret = of_property_count_elems_of_size(node, "st,adc-diff-channels", |
| 1662 | sizeof(*diff)); |
| 1663 | if (ret > adc_info->max_channels) { |
| 1664 | dev_err(&indio_dev->dev, "Bad st,adc-diff-channels?\n"); |
| 1665 | return -EINVAL; |
| 1666 | } else if (ret > 0) { |
| 1667 | int size = ret * sizeof(*diff) / sizeof(u32); |
| 1668 | |
| 1669 | num_diff = ret; |
| 1670 | num_channels += ret; |
| 1671 | ret = of_property_read_u32_array(node, "st,adc-diff-channels", |
| 1672 | (u32 *)diff, size); |
| 1673 | if (ret) |
| 1674 | return ret; |
| 1675 | } |
| 1676 | |
| 1677 | if (!num_channels) { |
| 1678 | dev_err(&indio_dev->dev, "No channels configured\n"); |
| 1679 | return -ENODATA; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1680 | } |
| 1681 | |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 1682 | /* Optional sample time is provided either for each, or all channels */ |
| 1683 | ret = of_property_count_u32_elems(node, "st,min-sample-time-nsecs"); |
| 1684 | if (ret > 1 && ret != num_channels) { |
| 1685 | dev_err(&indio_dev->dev, "Invalid st,min-sample-time-nsecs\n"); |
| 1686 | return -EINVAL; |
| 1687 | } |
| 1688 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1689 | channels = devm_kcalloc(&indio_dev->dev, num_channels, |
| 1690 | sizeof(struct iio_chan_spec), GFP_KERNEL); |
| 1691 | if (!channels) |
| 1692 | return -ENOMEM; |
| 1693 | |
| 1694 | of_property_for_each_u32(node, "st,adc-channels", prop, cur, val) { |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1695 | if (val >= adc_info->max_channels) { |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1696 | dev_err(&indio_dev->dev, "Invalid channel %d\n", val); |
| 1697 | return -EINVAL; |
| 1698 | } |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 1699 | |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 1700 | /* Channel can't be configured both as single-ended & diff */ |
| 1701 | for (i = 0; i < num_diff; i++) { |
| 1702 | if (val == diff[i].vinp) { |
| 1703 | dev_err(&indio_dev->dev, |
| 1704 | "channel %d miss-configured\n", val); |
| 1705 | return -EINVAL; |
| 1706 | } |
| 1707 | } |
| 1708 | stm32_adc_chan_init_one(indio_dev, &channels[scan_index], val, |
| 1709 | 0, scan_index, false); |
| 1710 | scan_index++; |
| 1711 | } |
| 1712 | |
| 1713 | for (i = 0; i < num_diff; i++) { |
| 1714 | if (diff[i].vinp >= adc_info->max_channels || |
| 1715 | diff[i].vinn >= adc_info->max_channels) { |
| 1716 | dev_err(&indio_dev->dev, "Invalid channel in%d-in%d\n", |
| 1717 | diff[i].vinp, diff[i].vinn); |
| 1718 | return -EINVAL; |
| 1719 | } |
| 1720 | stm32_adc_chan_init_one(indio_dev, &channels[scan_index], |
| 1721 | diff[i].vinp, diff[i].vinn, scan_index, |
| 1722 | true); |
| 1723 | scan_index++; |
| 1724 | } |
| 1725 | |
| 1726 | for (i = 0; i < scan_index; i++) { |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 1727 | /* |
| 1728 | * Using of_property_read_u32_index(), smp value will only be |
| 1729 | * modified if valid u32 value can be decoded. This allows to |
| 1730 | * get either no value, 1 shared value for all indexes, or one |
| 1731 | * value per channel. |
| 1732 | */ |
| 1733 | of_property_read_u32_index(node, "st,min-sample-time-nsecs", |
Fabrice Gasnier | 3fb2e24 | 2017-10-25 11:27:45 +0200 | [diff] [blame] | 1734 | i, &smp); |
| 1735 | /* Prepare sampling time settings */ |
| 1736 | stm32_adc_smpr_init(adc, channels[i].channel, smp); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | indio_dev->num_channels = scan_index; |
| 1740 | indio_dev->channels = channels; |
| 1741 | |
| 1742 | return 0; |
| 1743 | } |
| 1744 | |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1745 | static int stm32_adc_dma_request(struct iio_dev *indio_dev) |
| 1746 | { |
| 1747 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 1748 | struct dma_slave_config config; |
| 1749 | int ret; |
| 1750 | |
| 1751 | adc->dma_chan = dma_request_slave_channel(&indio_dev->dev, "rx"); |
| 1752 | if (!adc->dma_chan) |
| 1753 | return 0; |
| 1754 | |
| 1755 | adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev, |
| 1756 | STM32_DMA_BUFFER_SIZE, |
| 1757 | &adc->rx_dma_buf, GFP_KERNEL); |
| 1758 | if (!adc->rx_buf) { |
| 1759 | ret = -ENOMEM; |
| 1760 | goto err_release; |
| 1761 | } |
| 1762 | |
| 1763 | /* Configure DMA channel to read data register */ |
| 1764 | memset(&config, 0, sizeof(config)); |
| 1765 | config.src_addr = (dma_addr_t)adc->common->phys_base; |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1766 | config.src_addr += adc->offset + adc->cfg->regs->dr; |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1767 | config.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 1768 | |
| 1769 | ret = dmaengine_slave_config(adc->dma_chan, &config); |
| 1770 | if (ret) |
| 1771 | goto err_free; |
| 1772 | |
| 1773 | return 0; |
| 1774 | |
| 1775 | err_free: |
| 1776 | dma_free_coherent(adc->dma_chan->device->dev, STM32_DMA_BUFFER_SIZE, |
| 1777 | adc->rx_buf, adc->rx_dma_buf); |
| 1778 | err_release: |
| 1779 | dma_release_channel(adc->dma_chan); |
| 1780 | |
| 1781 | return ret; |
| 1782 | } |
| 1783 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1784 | static int stm32_adc_probe(struct platform_device *pdev) |
| 1785 | { |
| 1786 | struct iio_dev *indio_dev; |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1787 | struct device *dev = &pdev->dev; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1788 | struct stm32_adc *adc; |
| 1789 | int ret; |
| 1790 | |
| 1791 | if (!pdev->dev.of_node) |
| 1792 | return -ENODEV; |
| 1793 | |
| 1794 | indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc)); |
| 1795 | if (!indio_dev) |
| 1796 | return -ENOMEM; |
| 1797 | |
| 1798 | adc = iio_priv(indio_dev); |
| 1799 | adc->common = dev_get_drvdata(pdev->dev.parent); |
| 1800 | spin_lock_init(&adc->lock); |
| 1801 | init_completion(&adc->completion); |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1802 | adc->cfg = (const struct stm32_adc_cfg *) |
| 1803 | of_match_device(dev->driver->of_match_table, dev)->data; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1804 | |
| 1805 | indio_dev->name = dev_name(&pdev->dev); |
| 1806 | indio_dev->dev.parent = &pdev->dev; |
| 1807 | indio_dev->dev.of_node = pdev->dev.of_node; |
| 1808 | indio_dev->info = &stm32_adc_iio_info; |
Fabrice Gasnier | f0b638a | 2017-08-28 12:04:14 +0200 | [diff] [blame] | 1809 | indio_dev->modes = INDIO_DIRECT_MODE | INDIO_HARDWARE_TRIGGERED; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1810 | |
| 1811 | platform_set_drvdata(pdev, adc); |
| 1812 | |
| 1813 | ret = of_property_read_u32(pdev->dev.of_node, "reg", &adc->offset); |
| 1814 | if (ret != 0) { |
| 1815 | dev_err(&pdev->dev, "missing reg property\n"); |
| 1816 | return -EINVAL; |
| 1817 | } |
| 1818 | |
| 1819 | adc->irq = platform_get_irq(pdev, 0); |
| 1820 | if (adc->irq < 0) { |
| 1821 | dev_err(&pdev->dev, "failed to get irq\n"); |
| 1822 | return adc->irq; |
| 1823 | } |
| 1824 | |
| 1825 | ret = devm_request_irq(&pdev->dev, adc->irq, stm32_adc_isr, |
| 1826 | 0, pdev->name, adc); |
| 1827 | if (ret) { |
| 1828 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
| 1829 | return ret; |
| 1830 | } |
| 1831 | |
| 1832 | adc->clk = devm_clk_get(&pdev->dev, NULL); |
| 1833 | if (IS_ERR(adc->clk)) { |
Fabrice Gasnier | 204a6a2 | 2017-05-29 11:28:19 +0200 | [diff] [blame] | 1834 | ret = PTR_ERR(adc->clk); |
| 1835 | if (ret == -ENOENT && !adc->cfg->clk_required) { |
| 1836 | adc->clk = NULL; |
| 1837 | } else { |
| 1838 | dev_err(&pdev->dev, "Can't get clock\n"); |
| 1839 | return ret; |
| 1840 | } |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1841 | } |
| 1842 | |
Fabrice Gasnier | 204a6a2 | 2017-05-29 11:28:19 +0200 | [diff] [blame] | 1843 | if (adc->clk) { |
| 1844 | ret = clk_prepare_enable(adc->clk); |
| 1845 | if (ret < 0) { |
| 1846 | dev_err(&pdev->dev, "clk enable failed\n"); |
| 1847 | return ret; |
| 1848 | } |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1849 | } |
| 1850 | |
Fabrice Gasnier | 25a85be | 2017-03-31 14:32:38 +0200 | [diff] [blame] | 1851 | ret = stm32_adc_of_get_resolution(indio_dev); |
| 1852 | if (ret < 0) |
| 1853 | goto err_clk_disable; |
| 1854 | stm32_adc_set_res(adc); |
| 1855 | |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 1856 | if (adc->cfg->selfcalib) { |
| 1857 | ret = adc->cfg->selfcalib(adc); |
| 1858 | if (ret) |
| 1859 | goto err_clk_disable; |
| 1860 | } |
| 1861 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1862 | ret = stm32_adc_chan_of_init(indio_dev); |
| 1863 | if (ret < 0) |
| 1864 | goto err_clk_disable; |
| 1865 | |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1866 | ret = stm32_adc_dma_request(indio_dev); |
| 1867 | if (ret < 0) |
| 1868 | goto err_clk_disable; |
| 1869 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1870 | ret = iio_triggered_buffer_setup(indio_dev, |
| 1871 | &iio_pollfunc_store_time, |
| 1872 | &stm32_adc_trigger_handler, |
| 1873 | &stm32_adc_buffer_setup_ops); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1874 | if (ret) { |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1875 | dev_err(&pdev->dev, "buffer setup failed\n"); |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1876 | goto err_dma_disable; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1877 | } |
| 1878 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1879 | ret = iio_device_register(indio_dev); |
| 1880 | if (ret) { |
| 1881 | dev_err(&pdev->dev, "iio dev register failed\n"); |
| 1882 | goto err_buffer_cleanup; |
| 1883 | } |
| 1884 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1885 | return 0; |
| 1886 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1887 | err_buffer_cleanup: |
| 1888 | iio_triggered_buffer_cleanup(indio_dev); |
| 1889 | |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1890 | err_dma_disable: |
| 1891 | if (adc->dma_chan) { |
| 1892 | dma_free_coherent(adc->dma_chan->device->dev, |
| 1893 | STM32_DMA_BUFFER_SIZE, |
| 1894 | adc->rx_buf, adc->rx_dma_buf); |
| 1895 | dma_release_channel(adc->dma_chan); |
| 1896 | } |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1897 | err_clk_disable: |
Fabrice Gasnier | 204a6a2 | 2017-05-29 11:28:19 +0200 | [diff] [blame] | 1898 | if (adc->clk) |
| 1899 | clk_disable_unprepare(adc->clk); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1900 | |
| 1901 | return ret; |
| 1902 | } |
| 1903 | |
| 1904 | static int stm32_adc_remove(struct platform_device *pdev) |
| 1905 | { |
| 1906 | struct stm32_adc *adc = platform_get_drvdata(pdev); |
| 1907 | struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
| 1908 | |
| 1909 | iio_device_unregister(indio_dev); |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 1910 | iio_triggered_buffer_cleanup(indio_dev); |
Fabrice Gasnier | 2763ea0 | 2017-01-26 15:28:33 +0100 | [diff] [blame] | 1911 | if (adc->dma_chan) { |
| 1912 | dma_free_coherent(adc->dma_chan->device->dev, |
| 1913 | STM32_DMA_BUFFER_SIZE, |
| 1914 | adc->rx_buf, adc->rx_dma_buf); |
| 1915 | dma_release_channel(adc->dma_chan); |
| 1916 | } |
Fabrice Gasnier | 204a6a2 | 2017-05-29 11:28:19 +0200 | [diff] [blame] | 1917 | if (adc->clk) |
| 1918 | clk_disable_unprepare(adc->clk); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1919 | |
| 1920 | return 0; |
| 1921 | } |
| 1922 | |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1923 | static const struct stm32_adc_cfg stm32f4_adc_cfg = { |
| 1924 | .regs = &stm32f4_adc_regspec, |
| 1925 | .adc_info = &stm32f4_adc_info, |
| 1926 | .trigs = stm32f4_adc_trigs, |
Fabrice Gasnier | 204a6a2 | 2017-05-29 11:28:19 +0200 | [diff] [blame] | 1927 | .clk_required = true, |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1928 | .start_conv = stm32f4_adc_start_conv, |
| 1929 | .stop_conv = stm32f4_adc_stop_conv, |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 1930 | .smp_cycles = stm32f4_adc_smp_cycles, |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1931 | }; |
| 1932 | |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 1933 | static const struct stm32_adc_cfg stm32h7_adc_cfg = { |
| 1934 | .regs = &stm32h7_adc_regspec, |
| 1935 | .adc_info = &stm32h7_adc_info, |
| 1936 | .trigs = stm32h7_adc_trigs, |
| 1937 | .selfcalib = stm32h7_adc_selfcalib, |
| 1938 | .start_conv = stm32h7_adc_start_conv, |
| 1939 | .stop_conv = stm32h7_adc_stop_conv, |
| 1940 | .prepare = stm32h7_adc_prepare, |
| 1941 | .unprepare = stm32h7_adc_unprepare, |
Fabrice Gasnier | ee2ac1c | 2017-07-24 18:10:40 +0200 | [diff] [blame] | 1942 | .smp_cycles = stm32h7_adc_smp_cycles, |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 1943 | }; |
| 1944 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1945 | static const struct of_device_id stm32_adc_of_match[] = { |
Fabrice Gasnier | 64ad7f6 | 2017-05-29 11:28:18 +0200 | [diff] [blame] | 1946 | { .compatible = "st,stm32f4-adc", .data = (void *)&stm32f4_adc_cfg }, |
Fabrice Gasnier | 95e339b | 2017-05-29 11:28:20 +0200 | [diff] [blame] | 1947 | { .compatible = "st,stm32h7-adc", .data = (void *)&stm32h7_adc_cfg }, |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1948 | {}, |
| 1949 | }; |
| 1950 | MODULE_DEVICE_TABLE(of, stm32_adc_of_match); |
| 1951 | |
| 1952 | static struct platform_driver stm32_adc_driver = { |
| 1953 | .probe = stm32_adc_probe, |
| 1954 | .remove = stm32_adc_remove, |
| 1955 | .driver = { |
| 1956 | .name = "stm32-adc", |
| 1957 | .of_match_table = stm32_adc_of_match, |
| 1958 | }, |
| 1959 | }; |
| 1960 | module_platform_driver(stm32_adc_driver); |
| 1961 | |
| 1962 | MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>"); |
| 1963 | MODULE_DESCRIPTION("STMicroelectronics STM32 ADC IIO driver"); |
| 1964 | MODULE_LICENSE("GPL v2"); |
| 1965 | MODULE_ALIAS("platform:stm32-adc"); |