Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of STM32 ADC driver |
| 3 | * |
| 4 | * Copyright (C) 2016, STMicroelectronics - All Rights Reserved |
| 5 | * Author: Fabrice Gasnier <fabrice.gasnier@st.com>. |
| 6 | * |
| 7 | * License type: GPLv2 |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License version 2 as published by |
| 11 | * the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. |
| 16 | * See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/iio/iio.h> |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 25 | #include <linux/iio/buffer.h> |
Fabrice Gasnier | f24a33b | 2017-01-26 15:28:30 +0100 | [diff] [blame^] | 26 | #include <linux/iio/timer/stm32-timer-trigger.h> |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 27 | #include <linux/iio/trigger.h> |
| 28 | #include <linux/iio/trigger_consumer.h> |
| 29 | #include <linux/iio/triggered_buffer.h> |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/io.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/platform_device.h> |
| 34 | #include <linux/of.h> |
| 35 | |
| 36 | #include "stm32-adc-core.h" |
| 37 | |
| 38 | /* STM32F4 - Registers for each ADC instance */ |
| 39 | #define STM32F4_ADC_SR 0x00 |
| 40 | #define STM32F4_ADC_CR1 0x04 |
| 41 | #define STM32F4_ADC_CR2 0x08 |
| 42 | #define STM32F4_ADC_SMPR1 0x0C |
| 43 | #define STM32F4_ADC_SMPR2 0x10 |
| 44 | #define STM32F4_ADC_HTR 0x24 |
| 45 | #define STM32F4_ADC_LTR 0x28 |
| 46 | #define STM32F4_ADC_SQR1 0x2C |
| 47 | #define STM32F4_ADC_SQR2 0x30 |
| 48 | #define STM32F4_ADC_SQR3 0x34 |
| 49 | #define STM32F4_ADC_JSQR 0x38 |
| 50 | #define STM32F4_ADC_JDR1 0x3C |
| 51 | #define STM32F4_ADC_JDR2 0x40 |
| 52 | #define STM32F4_ADC_JDR3 0x44 |
| 53 | #define STM32F4_ADC_JDR4 0x48 |
| 54 | #define STM32F4_ADC_DR 0x4C |
| 55 | |
| 56 | /* STM32F4_ADC_SR - bit fields */ |
| 57 | #define STM32F4_STRT BIT(4) |
| 58 | #define STM32F4_EOC BIT(1) |
| 59 | |
| 60 | /* STM32F4_ADC_CR1 - bit fields */ |
| 61 | #define STM32F4_SCAN BIT(8) |
| 62 | #define STM32F4_EOCIE BIT(5) |
| 63 | |
| 64 | /* STM32F4_ADC_CR2 - bit fields */ |
| 65 | #define STM32F4_SWSTART BIT(30) |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 66 | #define STM32F4_EXTEN_SHIFT 28 |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 67 | #define STM32F4_EXTEN_MASK GENMASK(29, 28) |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 68 | #define STM32F4_EXTSEL_SHIFT 24 |
| 69 | #define STM32F4_EXTSEL_MASK GENMASK(27, 24) |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 70 | #define STM32F4_EOCS BIT(10) |
| 71 | #define STM32F4_ADON BIT(0) |
| 72 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 73 | #define STM32_ADC_MAX_SQ 16 /* SQ1..SQ16 */ |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 74 | #define STM32_ADC_TIMEOUT_US 100000 |
| 75 | #define STM32_ADC_TIMEOUT (msecs_to_jiffies(STM32_ADC_TIMEOUT_US / 1000)) |
| 76 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 77 | /* External trigger enable */ |
| 78 | enum stm32_adc_exten { |
| 79 | STM32_EXTEN_SWTRIG, |
| 80 | STM32_EXTEN_HWTRIG_RISING_EDGE, |
| 81 | STM32_EXTEN_HWTRIG_FALLING_EDGE, |
| 82 | STM32_EXTEN_HWTRIG_BOTH_EDGES, |
| 83 | }; |
| 84 | |
Fabrice Gasnier | f24a33b | 2017-01-26 15:28:30 +0100 | [diff] [blame^] | 85 | /* extsel - trigger mux selection value */ |
| 86 | enum stm32_adc_extsel { |
| 87 | STM32_EXT0, |
| 88 | STM32_EXT1, |
| 89 | STM32_EXT2, |
| 90 | STM32_EXT3, |
| 91 | STM32_EXT4, |
| 92 | STM32_EXT5, |
| 93 | STM32_EXT6, |
| 94 | STM32_EXT7, |
| 95 | STM32_EXT8, |
| 96 | STM32_EXT9, |
| 97 | STM32_EXT10, |
| 98 | STM32_EXT11, |
| 99 | STM32_EXT12, |
| 100 | STM32_EXT13, |
| 101 | STM32_EXT14, |
| 102 | STM32_EXT15, |
| 103 | }; |
| 104 | |
| 105 | /** |
| 106 | * struct stm32_adc_trig_info - ADC trigger info |
| 107 | * @name: name of the trigger, corresponding to its source |
| 108 | * @extsel: trigger selection |
| 109 | */ |
| 110 | struct stm32_adc_trig_info { |
| 111 | const char *name; |
| 112 | enum stm32_adc_extsel extsel; |
| 113 | }; |
| 114 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 115 | /** |
| 116 | * stm32_adc_regs - stm32 ADC misc registers & bitfield desc |
| 117 | * @reg: register offset |
| 118 | * @mask: bitfield mask |
| 119 | * @shift: left shift |
| 120 | */ |
| 121 | struct stm32_adc_regs { |
| 122 | int reg; |
| 123 | int mask; |
| 124 | int shift; |
| 125 | }; |
| 126 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 127 | /** |
| 128 | * struct stm32_adc - private data of each ADC IIO instance |
| 129 | * @common: reference to ADC block common data |
| 130 | * @offset: ADC instance register offset in ADC block |
| 131 | * @completion: end of single conversion completion |
| 132 | * @buffer: data buffer |
| 133 | * @clk: clock for this adc instance |
| 134 | * @irq: interrupt for this adc instance |
| 135 | * @lock: spinlock |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 136 | * @bufi: data buffer index |
| 137 | * @num_conv: expected number of scan conversions |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 138 | */ |
| 139 | struct stm32_adc { |
| 140 | struct stm32_adc_common *common; |
| 141 | u32 offset; |
| 142 | struct completion completion; |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 143 | u16 buffer[STM32_ADC_MAX_SQ]; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 144 | struct clk *clk; |
| 145 | int irq; |
| 146 | spinlock_t lock; /* interrupt lock */ |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 147 | unsigned int bufi; |
| 148 | unsigned int num_conv; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | /** |
| 152 | * struct stm32_adc_chan_spec - specification of stm32 adc channel |
| 153 | * @type: IIO channel type |
| 154 | * @channel: channel number (single ended) |
| 155 | * @name: channel name (single ended) |
| 156 | */ |
| 157 | struct stm32_adc_chan_spec { |
| 158 | enum iio_chan_type type; |
| 159 | int channel; |
| 160 | const char *name; |
| 161 | }; |
| 162 | |
| 163 | /* Input definitions common for all STM32F4 instances */ |
| 164 | static const struct stm32_adc_chan_spec stm32f4_adc123_channels[] = { |
| 165 | { IIO_VOLTAGE, 0, "in0" }, |
| 166 | { IIO_VOLTAGE, 1, "in1" }, |
| 167 | { IIO_VOLTAGE, 2, "in2" }, |
| 168 | { IIO_VOLTAGE, 3, "in3" }, |
| 169 | { IIO_VOLTAGE, 4, "in4" }, |
| 170 | { IIO_VOLTAGE, 5, "in5" }, |
| 171 | { IIO_VOLTAGE, 6, "in6" }, |
| 172 | { IIO_VOLTAGE, 7, "in7" }, |
| 173 | { IIO_VOLTAGE, 8, "in8" }, |
| 174 | { IIO_VOLTAGE, 9, "in9" }, |
| 175 | { IIO_VOLTAGE, 10, "in10" }, |
| 176 | { IIO_VOLTAGE, 11, "in11" }, |
| 177 | { IIO_VOLTAGE, 12, "in12" }, |
| 178 | { IIO_VOLTAGE, 13, "in13" }, |
| 179 | { IIO_VOLTAGE, 14, "in14" }, |
| 180 | { IIO_VOLTAGE, 15, "in15" }, |
| 181 | }; |
| 182 | |
| 183 | /** |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 184 | * stm32f4_sq - describe regular sequence registers |
| 185 | * - L: sequence len (register & bit field) |
| 186 | * - SQ1..SQ16: sequence entries (register & bit field) |
| 187 | */ |
| 188 | static const struct stm32_adc_regs stm32f4_sq[STM32_ADC_MAX_SQ + 1] = { |
| 189 | /* L: len bit field description to be kept as first element */ |
| 190 | { STM32F4_ADC_SQR1, GENMASK(23, 20), 20 }, |
| 191 | /* SQ1..SQ16 registers & bit fields (reg, mask, shift) */ |
| 192 | { STM32F4_ADC_SQR3, GENMASK(4, 0), 0 }, |
| 193 | { STM32F4_ADC_SQR3, GENMASK(9, 5), 5 }, |
| 194 | { STM32F4_ADC_SQR3, GENMASK(14, 10), 10 }, |
| 195 | { STM32F4_ADC_SQR3, GENMASK(19, 15), 15 }, |
| 196 | { STM32F4_ADC_SQR3, GENMASK(24, 20), 20 }, |
| 197 | { STM32F4_ADC_SQR3, GENMASK(29, 25), 25 }, |
| 198 | { STM32F4_ADC_SQR2, GENMASK(4, 0), 0 }, |
| 199 | { STM32F4_ADC_SQR2, GENMASK(9, 5), 5 }, |
| 200 | { STM32F4_ADC_SQR2, GENMASK(14, 10), 10 }, |
| 201 | { STM32F4_ADC_SQR2, GENMASK(19, 15), 15 }, |
| 202 | { STM32F4_ADC_SQR2, GENMASK(24, 20), 20 }, |
| 203 | { STM32F4_ADC_SQR2, GENMASK(29, 25), 25 }, |
| 204 | { STM32F4_ADC_SQR1, GENMASK(4, 0), 0 }, |
| 205 | { STM32F4_ADC_SQR1, GENMASK(9, 5), 5 }, |
| 206 | { STM32F4_ADC_SQR1, GENMASK(14, 10), 10 }, |
| 207 | { STM32F4_ADC_SQR1, GENMASK(19, 15), 15 }, |
| 208 | }; |
| 209 | |
Fabrice Gasnier | f24a33b | 2017-01-26 15:28:30 +0100 | [diff] [blame^] | 210 | /* STM32F4 external trigger sources for all instances */ |
| 211 | static struct stm32_adc_trig_info stm32f4_adc_trigs[] = { |
| 212 | { TIM1_CH1, STM32_EXT0 }, |
| 213 | { TIM1_CH2, STM32_EXT1 }, |
| 214 | { TIM1_CH3, STM32_EXT2 }, |
| 215 | { TIM2_CH2, STM32_EXT3 }, |
| 216 | { TIM2_CH3, STM32_EXT4 }, |
| 217 | { TIM2_CH4, STM32_EXT5 }, |
| 218 | { TIM2_TRGO, STM32_EXT6 }, |
| 219 | { TIM3_CH1, STM32_EXT7 }, |
| 220 | { TIM3_TRGO, STM32_EXT8 }, |
| 221 | { TIM4_CH4, STM32_EXT9 }, |
| 222 | { TIM5_CH1, STM32_EXT10 }, |
| 223 | { TIM5_CH2, STM32_EXT11 }, |
| 224 | { TIM5_CH3, STM32_EXT12 }, |
| 225 | { TIM8_CH1, STM32_EXT13 }, |
| 226 | { TIM8_TRGO, STM32_EXT14 }, |
| 227 | {}, /* sentinel */ |
| 228 | }; |
| 229 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 230 | /** |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 231 | * STM32 ADC registers access routines |
| 232 | * @adc: stm32 adc instance |
| 233 | * @reg: reg offset in adc instance |
| 234 | * |
| 235 | * Note: All instances share same base, with 0x0, 0x100 or 0x200 offset resp. |
| 236 | * for adc1, adc2 and adc3. |
| 237 | */ |
| 238 | static u32 stm32_adc_readl(struct stm32_adc *adc, u32 reg) |
| 239 | { |
| 240 | return readl_relaxed(adc->common->base + adc->offset + reg); |
| 241 | } |
| 242 | |
| 243 | static u16 stm32_adc_readw(struct stm32_adc *adc, u32 reg) |
| 244 | { |
| 245 | return readw_relaxed(adc->common->base + adc->offset + reg); |
| 246 | } |
| 247 | |
| 248 | static void stm32_adc_writel(struct stm32_adc *adc, u32 reg, u32 val) |
| 249 | { |
| 250 | writel_relaxed(val, adc->common->base + adc->offset + reg); |
| 251 | } |
| 252 | |
| 253 | static void stm32_adc_set_bits(struct stm32_adc *adc, u32 reg, u32 bits) |
| 254 | { |
| 255 | unsigned long flags; |
| 256 | |
| 257 | spin_lock_irqsave(&adc->lock, flags); |
| 258 | stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) | bits); |
| 259 | spin_unlock_irqrestore(&adc->lock, flags); |
| 260 | } |
| 261 | |
| 262 | static void stm32_adc_clr_bits(struct stm32_adc *adc, u32 reg, u32 bits) |
| 263 | { |
| 264 | unsigned long flags; |
| 265 | |
| 266 | spin_lock_irqsave(&adc->lock, flags); |
| 267 | stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) & ~bits); |
| 268 | spin_unlock_irqrestore(&adc->lock, flags); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * stm32_adc_conv_irq_enable() - Enable end of conversion interrupt |
| 273 | * @adc: stm32 adc instance |
| 274 | */ |
| 275 | static void stm32_adc_conv_irq_enable(struct stm32_adc *adc) |
| 276 | { |
| 277 | stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_EOCIE); |
| 278 | }; |
| 279 | |
| 280 | /** |
| 281 | * stm32_adc_conv_irq_disable() - Disable end of conversion interrupt |
| 282 | * @adc: stm32 adc instance |
| 283 | */ |
| 284 | static void stm32_adc_conv_irq_disable(struct stm32_adc *adc) |
| 285 | { |
| 286 | stm32_adc_clr_bits(adc, STM32F4_ADC_CR1, STM32F4_EOCIE); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * stm32_adc_start_conv() - Start conversions for regular channels. |
| 291 | * @adc: stm32 adc instance |
| 292 | */ |
| 293 | static void stm32_adc_start_conv(struct stm32_adc *adc) |
| 294 | { |
| 295 | stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN); |
| 296 | stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_EOCS | STM32F4_ADON); |
| 297 | |
| 298 | /* Wait for Power-up time (tSTAB from datasheet) */ |
| 299 | usleep_range(2, 3); |
| 300 | |
| 301 | /* Software start ? (e.g. trigger detection disabled ?) */ |
| 302 | if (!(stm32_adc_readl(adc, STM32F4_ADC_CR2) & STM32F4_EXTEN_MASK)) |
| 303 | stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_SWSTART); |
| 304 | } |
| 305 | |
| 306 | static void stm32_adc_stop_conv(struct stm32_adc *adc) |
| 307 | { |
| 308 | stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK); |
| 309 | stm32_adc_clr_bits(adc, STM32F4_ADC_SR, STM32F4_STRT); |
| 310 | |
| 311 | stm32_adc_clr_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN); |
| 312 | stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_ADON); |
| 313 | } |
| 314 | |
| 315 | /** |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 316 | * stm32_adc_conf_scan_seq() - Build regular channels scan sequence |
| 317 | * @indio_dev: IIO device |
| 318 | * @scan_mask: channels to be converted |
| 319 | * |
| 320 | * Conversion sequence : |
| 321 | * Configure ADC scan sequence based on selected channels in scan_mask. |
| 322 | * Add channels to SQR registers, from scan_mask LSB to MSB, then |
| 323 | * program sequence len. |
| 324 | */ |
| 325 | static int stm32_adc_conf_scan_seq(struct iio_dev *indio_dev, |
| 326 | const unsigned long *scan_mask) |
| 327 | { |
| 328 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 329 | const struct iio_chan_spec *chan; |
| 330 | u32 val, bit; |
| 331 | int i = 0; |
| 332 | |
| 333 | for_each_set_bit(bit, scan_mask, indio_dev->masklength) { |
| 334 | chan = indio_dev->channels + bit; |
| 335 | /* |
| 336 | * Assign one channel per SQ entry in regular |
| 337 | * sequence, starting with SQ1. |
| 338 | */ |
| 339 | i++; |
| 340 | if (i > STM32_ADC_MAX_SQ) |
| 341 | return -EINVAL; |
| 342 | |
| 343 | dev_dbg(&indio_dev->dev, "%s chan %d to SQ%d\n", |
| 344 | __func__, chan->channel, i); |
| 345 | |
| 346 | val = stm32_adc_readl(adc, stm32f4_sq[i].reg); |
| 347 | val &= ~stm32f4_sq[i].mask; |
| 348 | val |= chan->channel << stm32f4_sq[i].shift; |
| 349 | stm32_adc_writel(adc, stm32f4_sq[i].reg, val); |
| 350 | } |
| 351 | |
| 352 | if (!i) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | /* Sequence len */ |
| 356 | val = stm32_adc_readl(adc, stm32f4_sq[0].reg); |
| 357 | val &= ~stm32f4_sq[0].mask; |
| 358 | val |= ((i - 1) << stm32f4_sq[0].shift); |
| 359 | stm32_adc_writel(adc, stm32f4_sq[0].reg, val); |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * stm32_adc_get_trig_extsel() - Get external trigger selection |
| 366 | * @trig: trigger |
| 367 | * |
| 368 | * Returns trigger extsel value, if trig matches, -EINVAL otherwise. |
| 369 | */ |
| 370 | static int stm32_adc_get_trig_extsel(struct iio_trigger *trig) |
| 371 | { |
Fabrice Gasnier | f24a33b | 2017-01-26 15:28:30 +0100 | [diff] [blame^] | 372 | int i; |
| 373 | |
| 374 | /* lookup triggers registered by stm32 timer trigger driver */ |
| 375 | for (i = 0; stm32f4_adc_trigs[i].name; i++) { |
| 376 | /** |
| 377 | * Checking both stm32 timer trigger type and trig name |
| 378 | * should be safe against arbitrary trigger names. |
| 379 | */ |
| 380 | if (is_stm32_timer_trigger(trig) && |
| 381 | !strcmp(stm32f4_adc_trigs[i].name, trig->name)) { |
| 382 | return stm32f4_adc_trigs[i].extsel; |
| 383 | } |
| 384 | } |
| 385 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 386 | return -EINVAL; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * stm32_adc_set_trig() - Set a regular trigger |
| 391 | * @indio_dev: IIO device |
| 392 | * @trig: IIO trigger |
| 393 | * |
| 394 | * Set trigger source/polarity (e.g. SW, or HW with polarity) : |
| 395 | * - if HW trigger disabled (e.g. trig == NULL, conversion launched by sw) |
| 396 | * - if HW trigger enabled, set source & polarity |
| 397 | */ |
| 398 | static int stm32_adc_set_trig(struct iio_dev *indio_dev, |
| 399 | struct iio_trigger *trig) |
| 400 | { |
| 401 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 402 | u32 val, extsel = 0, exten = STM32_EXTEN_SWTRIG; |
| 403 | unsigned long flags; |
| 404 | int ret; |
| 405 | |
| 406 | if (trig) { |
| 407 | ret = stm32_adc_get_trig_extsel(trig); |
| 408 | if (ret < 0) |
| 409 | return ret; |
| 410 | |
| 411 | /* set trigger source and polarity (default to rising edge) */ |
| 412 | extsel = ret; |
| 413 | exten = STM32_EXTEN_HWTRIG_RISING_EDGE; |
| 414 | } |
| 415 | |
| 416 | spin_lock_irqsave(&adc->lock, flags); |
| 417 | val = stm32_adc_readl(adc, STM32F4_ADC_CR2); |
| 418 | val &= ~(STM32F4_EXTEN_MASK | STM32F4_EXTSEL_MASK); |
| 419 | val |= exten << STM32F4_EXTEN_SHIFT; |
| 420 | val |= extsel << STM32F4_EXTSEL_SHIFT; |
| 421 | stm32_adc_writel(adc, STM32F4_ADC_CR2, val); |
| 422 | spin_unlock_irqrestore(&adc->lock, flags); |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /** |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 428 | * stm32_adc_single_conv() - Performs a single conversion |
| 429 | * @indio_dev: IIO device |
| 430 | * @chan: IIO channel |
| 431 | * @res: conversion result |
| 432 | * |
| 433 | * The function performs a single conversion on a given channel: |
| 434 | * - Program sequencer with one channel (e.g. in SQ1 with len = 1) |
| 435 | * - Use SW trigger |
| 436 | * - Start conversion, then wait for interrupt completion. |
| 437 | */ |
| 438 | static int stm32_adc_single_conv(struct iio_dev *indio_dev, |
| 439 | const struct iio_chan_spec *chan, |
| 440 | int *res) |
| 441 | { |
| 442 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 443 | long timeout; |
| 444 | u32 val; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 445 | int ret; |
| 446 | |
| 447 | reinit_completion(&adc->completion); |
| 448 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 449 | adc->bufi = 0; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 450 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 451 | /* Program chan number in regular sequence (SQ1) */ |
| 452 | val = stm32_adc_readl(adc, stm32f4_sq[1].reg); |
| 453 | val &= ~stm32f4_sq[1].mask; |
| 454 | val |= chan->channel << stm32f4_sq[1].shift; |
| 455 | stm32_adc_writel(adc, stm32f4_sq[1].reg, val); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 456 | |
| 457 | /* Set regular sequence len (0 for 1 conversion) */ |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 458 | stm32_adc_clr_bits(adc, stm32f4_sq[0].reg, stm32f4_sq[0].mask); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 459 | |
| 460 | /* Trigger detection disabled (conversion can be launched in SW) */ |
| 461 | stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK); |
| 462 | |
| 463 | stm32_adc_conv_irq_enable(adc); |
| 464 | |
| 465 | stm32_adc_start_conv(adc); |
| 466 | |
| 467 | timeout = wait_for_completion_interruptible_timeout( |
| 468 | &adc->completion, STM32_ADC_TIMEOUT); |
| 469 | if (timeout == 0) { |
| 470 | ret = -ETIMEDOUT; |
| 471 | } else if (timeout < 0) { |
| 472 | ret = timeout; |
| 473 | } else { |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 474 | *res = adc->buffer[0]; |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 475 | ret = IIO_VAL_INT; |
| 476 | } |
| 477 | |
| 478 | stm32_adc_stop_conv(adc); |
| 479 | |
| 480 | stm32_adc_conv_irq_disable(adc); |
| 481 | |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | static int stm32_adc_read_raw(struct iio_dev *indio_dev, |
| 486 | struct iio_chan_spec const *chan, |
| 487 | int *val, int *val2, long mask) |
| 488 | { |
| 489 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 490 | int ret; |
| 491 | |
| 492 | switch (mask) { |
| 493 | case IIO_CHAN_INFO_RAW: |
| 494 | ret = iio_device_claim_direct_mode(indio_dev); |
| 495 | if (ret) |
| 496 | return ret; |
| 497 | if (chan->type == IIO_VOLTAGE) |
| 498 | ret = stm32_adc_single_conv(indio_dev, chan, val); |
| 499 | else |
| 500 | ret = -EINVAL; |
| 501 | iio_device_release_direct_mode(indio_dev); |
| 502 | return ret; |
| 503 | |
| 504 | case IIO_CHAN_INFO_SCALE: |
| 505 | *val = adc->common->vref_mv; |
| 506 | *val2 = chan->scan_type.realbits; |
| 507 | return IIO_VAL_FRACTIONAL_LOG2; |
| 508 | |
| 509 | default: |
| 510 | return -EINVAL; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | static irqreturn_t stm32_adc_isr(int irq, void *data) |
| 515 | { |
| 516 | struct stm32_adc *adc = data; |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 517 | struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 518 | u32 status = stm32_adc_readl(adc, STM32F4_ADC_SR); |
| 519 | |
| 520 | if (status & STM32F4_EOC) { |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 521 | /* Reading DR also clears EOC status flag */ |
| 522 | adc->buffer[adc->bufi] = stm32_adc_readw(adc, STM32F4_ADC_DR); |
| 523 | if (iio_buffer_enabled(indio_dev)) { |
| 524 | adc->bufi++; |
| 525 | if (adc->bufi >= adc->num_conv) { |
| 526 | stm32_adc_conv_irq_disable(adc); |
| 527 | iio_trigger_poll(indio_dev->trig); |
| 528 | } |
| 529 | } else { |
| 530 | complete(&adc->completion); |
| 531 | } |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 532 | return IRQ_HANDLED; |
| 533 | } |
| 534 | |
| 535 | return IRQ_NONE; |
| 536 | } |
| 537 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 538 | /** |
| 539 | * stm32_adc_validate_trigger() - validate trigger for stm32 adc |
| 540 | * @indio_dev: IIO device |
| 541 | * @trig: new trigger |
| 542 | * |
| 543 | * Returns: 0 if trig matches one of the triggers registered by stm32 adc |
| 544 | * driver, -EINVAL otherwise. |
| 545 | */ |
| 546 | static int stm32_adc_validate_trigger(struct iio_dev *indio_dev, |
| 547 | struct iio_trigger *trig) |
| 548 | { |
| 549 | return stm32_adc_get_trig_extsel(trig) < 0 ? -EINVAL : 0; |
| 550 | } |
| 551 | |
| 552 | static int stm32_adc_update_scan_mode(struct iio_dev *indio_dev, |
| 553 | const unsigned long *scan_mask) |
| 554 | { |
| 555 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 556 | int ret; |
| 557 | |
| 558 | adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength); |
| 559 | |
| 560 | ret = stm32_adc_conf_scan_seq(indio_dev, scan_mask); |
| 561 | if (ret) |
| 562 | return ret; |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 567 | static int stm32_adc_of_xlate(struct iio_dev *indio_dev, |
| 568 | const struct of_phandle_args *iiospec) |
| 569 | { |
| 570 | int i; |
| 571 | |
| 572 | for (i = 0; i < indio_dev->num_channels; i++) |
| 573 | if (indio_dev->channels[i].channel == iiospec->args[0]) |
| 574 | return i; |
| 575 | |
| 576 | return -EINVAL; |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * stm32_adc_debugfs_reg_access - read or write register value |
| 581 | * |
| 582 | * To read a value from an ADC register: |
| 583 | * echo [ADC reg offset] > direct_reg_access |
| 584 | * cat direct_reg_access |
| 585 | * |
| 586 | * To write a value in a ADC register: |
| 587 | * echo [ADC_reg_offset] [value] > direct_reg_access |
| 588 | */ |
| 589 | static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev, |
| 590 | unsigned reg, unsigned writeval, |
| 591 | unsigned *readval) |
| 592 | { |
| 593 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 594 | |
| 595 | if (!readval) |
| 596 | stm32_adc_writel(adc, reg, writeval); |
| 597 | else |
| 598 | *readval = stm32_adc_readl(adc, reg); |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static const struct iio_info stm32_adc_iio_info = { |
| 604 | .read_raw = stm32_adc_read_raw, |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 605 | .validate_trigger = stm32_adc_validate_trigger, |
| 606 | .update_scan_mode = stm32_adc_update_scan_mode, |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 607 | .debugfs_reg_access = stm32_adc_debugfs_reg_access, |
| 608 | .of_xlate = stm32_adc_of_xlate, |
| 609 | .driver_module = THIS_MODULE, |
| 610 | }; |
| 611 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 612 | static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev) |
| 613 | { |
| 614 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 615 | int ret; |
| 616 | |
| 617 | ret = stm32_adc_set_trig(indio_dev, indio_dev->trig); |
| 618 | if (ret) { |
| 619 | dev_err(&indio_dev->dev, "Can't set trigger\n"); |
| 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | ret = iio_triggered_buffer_postenable(indio_dev); |
| 624 | if (ret < 0) |
| 625 | goto err_clr_trig; |
| 626 | |
| 627 | /* Reset adc buffer index */ |
| 628 | adc->bufi = 0; |
| 629 | |
| 630 | stm32_adc_conv_irq_enable(adc); |
| 631 | stm32_adc_start_conv(adc); |
| 632 | |
| 633 | return 0; |
| 634 | |
| 635 | err_clr_trig: |
| 636 | stm32_adc_set_trig(indio_dev, NULL); |
| 637 | |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | static int stm32_adc_buffer_predisable(struct iio_dev *indio_dev) |
| 642 | { |
| 643 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 644 | int ret; |
| 645 | |
| 646 | stm32_adc_stop_conv(adc); |
| 647 | stm32_adc_conv_irq_disable(adc); |
| 648 | |
| 649 | ret = iio_triggered_buffer_predisable(indio_dev); |
| 650 | if (ret < 0) |
| 651 | dev_err(&indio_dev->dev, "predisable failed\n"); |
| 652 | |
| 653 | if (stm32_adc_set_trig(indio_dev, NULL)) |
| 654 | dev_err(&indio_dev->dev, "Can't clear trigger\n"); |
| 655 | |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | static const struct iio_buffer_setup_ops stm32_adc_buffer_setup_ops = { |
| 660 | .postenable = &stm32_adc_buffer_postenable, |
| 661 | .predisable = &stm32_adc_buffer_predisable, |
| 662 | }; |
| 663 | |
| 664 | static irqreturn_t stm32_adc_trigger_handler(int irq, void *p) |
| 665 | { |
| 666 | struct iio_poll_func *pf = p; |
| 667 | struct iio_dev *indio_dev = pf->indio_dev; |
| 668 | struct stm32_adc *adc = iio_priv(indio_dev); |
| 669 | |
| 670 | dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi); |
| 671 | |
| 672 | /* reset buffer index */ |
| 673 | adc->bufi = 0; |
| 674 | iio_push_to_buffers_with_timestamp(indio_dev, adc->buffer, |
| 675 | pf->timestamp); |
| 676 | |
| 677 | iio_trigger_notify_done(indio_dev->trig); |
| 678 | |
| 679 | /* re-enable eoc irq */ |
| 680 | stm32_adc_conv_irq_enable(adc); |
| 681 | |
| 682 | return IRQ_HANDLED; |
| 683 | } |
| 684 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 685 | static void stm32_adc_chan_init_one(struct iio_dev *indio_dev, |
| 686 | struct iio_chan_spec *chan, |
| 687 | const struct stm32_adc_chan_spec *channel, |
| 688 | int scan_index) |
| 689 | { |
| 690 | chan->type = channel->type; |
| 691 | chan->channel = channel->channel; |
| 692 | chan->datasheet_name = channel->name; |
| 693 | chan->scan_index = scan_index; |
| 694 | chan->indexed = 1; |
| 695 | chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); |
| 696 | chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE); |
| 697 | chan->scan_type.sign = 'u'; |
| 698 | chan->scan_type.realbits = 12; |
| 699 | chan->scan_type.storagebits = 16; |
| 700 | } |
| 701 | |
| 702 | static int stm32_adc_chan_of_init(struct iio_dev *indio_dev) |
| 703 | { |
| 704 | struct device_node *node = indio_dev->dev.of_node; |
| 705 | struct property *prop; |
| 706 | const __be32 *cur; |
| 707 | struct iio_chan_spec *channels; |
| 708 | int scan_index = 0, num_channels; |
| 709 | u32 val; |
| 710 | |
| 711 | num_channels = of_property_count_u32_elems(node, "st,adc-channels"); |
| 712 | if (num_channels < 0 || |
| 713 | num_channels >= ARRAY_SIZE(stm32f4_adc123_channels)) { |
| 714 | dev_err(&indio_dev->dev, "Bad st,adc-channels?\n"); |
| 715 | return num_channels < 0 ? num_channels : -EINVAL; |
| 716 | } |
| 717 | |
| 718 | channels = devm_kcalloc(&indio_dev->dev, num_channels, |
| 719 | sizeof(struct iio_chan_spec), GFP_KERNEL); |
| 720 | if (!channels) |
| 721 | return -ENOMEM; |
| 722 | |
| 723 | of_property_for_each_u32(node, "st,adc-channels", prop, cur, val) { |
| 724 | if (val >= ARRAY_SIZE(stm32f4_adc123_channels)) { |
| 725 | dev_err(&indio_dev->dev, "Invalid channel %d\n", val); |
| 726 | return -EINVAL; |
| 727 | } |
| 728 | stm32_adc_chan_init_one(indio_dev, &channels[scan_index], |
| 729 | &stm32f4_adc123_channels[val], |
| 730 | scan_index); |
| 731 | scan_index++; |
| 732 | } |
| 733 | |
| 734 | indio_dev->num_channels = scan_index; |
| 735 | indio_dev->channels = channels; |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | static int stm32_adc_probe(struct platform_device *pdev) |
| 741 | { |
| 742 | struct iio_dev *indio_dev; |
| 743 | struct stm32_adc *adc; |
| 744 | int ret; |
| 745 | |
| 746 | if (!pdev->dev.of_node) |
| 747 | return -ENODEV; |
| 748 | |
| 749 | indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc)); |
| 750 | if (!indio_dev) |
| 751 | return -ENOMEM; |
| 752 | |
| 753 | adc = iio_priv(indio_dev); |
| 754 | adc->common = dev_get_drvdata(pdev->dev.parent); |
| 755 | spin_lock_init(&adc->lock); |
| 756 | init_completion(&adc->completion); |
| 757 | |
| 758 | indio_dev->name = dev_name(&pdev->dev); |
| 759 | indio_dev->dev.parent = &pdev->dev; |
| 760 | indio_dev->dev.of_node = pdev->dev.of_node; |
| 761 | indio_dev->info = &stm32_adc_iio_info; |
| 762 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 763 | |
| 764 | platform_set_drvdata(pdev, adc); |
| 765 | |
| 766 | ret = of_property_read_u32(pdev->dev.of_node, "reg", &adc->offset); |
| 767 | if (ret != 0) { |
| 768 | dev_err(&pdev->dev, "missing reg property\n"); |
| 769 | return -EINVAL; |
| 770 | } |
| 771 | |
| 772 | adc->irq = platform_get_irq(pdev, 0); |
| 773 | if (adc->irq < 0) { |
| 774 | dev_err(&pdev->dev, "failed to get irq\n"); |
| 775 | return adc->irq; |
| 776 | } |
| 777 | |
| 778 | ret = devm_request_irq(&pdev->dev, adc->irq, stm32_adc_isr, |
| 779 | 0, pdev->name, adc); |
| 780 | if (ret) { |
| 781 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
| 782 | return ret; |
| 783 | } |
| 784 | |
| 785 | adc->clk = devm_clk_get(&pdev->dev, NULL); |
| 786 | if (IS_ERR(adc->clk)) { |
| 787 | dev_err(&pdev->dev, "Can't get clock\n"); |
| 788 | return PTR_ERR(adc->clk); |
| 789 | } |
| 790 | |
| 791 | ret = clk_prepare_enable(adc->clk); |
| 792 | if (ret < 0) { |
| 793 | dev_err(&pdev->dev, "clk enable failed\n"); |
| 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | ret = stm32_adc_chan_of_init(indio_dev); |
| 798 | if (ret < 0) |
| 799 | goto err_clk_disable; |
| 800 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 801 | ret = iio_triggered_buffer_setup(indio_dev, |
| 802 | &iio_pollfunc_store_time, |
| 803 | &stm32_adc_trigger_handler, |
| 804 | &stm32_adc_buffer_setup_ops); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 805 | if (ret) { |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 806 | dev_err(&pdev->dev, "buffer setup failed\n"); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 807 | goto err_clk_disable; |
| 808 | } |
| 809 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 810 | ret = iio_device_register(indio_dev); |
| 811 | if (ret) { |
| 812 | dev_err(&pdev->dev, "iio dev register failed\n"); |
| 813 | goto err_buffer_cleanup; |
| 814 | } |
| 815 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 816 | return 0; |
| 817 | |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 818 | err_buffer_cleanup: |
| 819 | iio_triggered_buffer_cleanup(indio_dev); |
| 820 | |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 821 | err_clk_disable: |
| 822 | clk_disable_unprepare(adc->clk); |
| 823 | |
| 824 | return ret; |
| 825 | } |
| 826 | |
| 827 | static int stm32_adc_remove(struct platform_device *pdev) |
| 828 | { |
| 829 | struct stm32_adc *adc = platform_get_drvdata(pdev); |
| 830 | struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
| 831 | |
| 832 | iio_device_unregister(indio_dev); |
Fabrice Gasnier | da9b948 | 2017-01-26 15:28:29 +0100 | [diff] [blame] | 833 | iio_triggered_buffer_cleanup(indio_dev); |
Fabrice Gasnier | 0f883b2 | 2016-11-15 16:30:58 +0100 | [diff] [blame] | 834 | clk_disable_unprepare(adc->clk); |
| 835 | |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | static const struct of_device_id stm32_adc_of_match[] = { |
| 840 | { .compatible = "st,stm32f4-adc" }, |
| 841 | {}, |
| 842 | }; |
| 843 | MODULE_DEVICE_TABLE(of, stm32_adc_of_match); |
| 844 | |
| 845 | static struct platform_driver stm32_adc_driver = { |
| 846 | .probe = stm32_adc_probe, |
| 847 | .remove = stm32_adc_remove, |
| 848 | .driver = { |
| 849 | .name = "stm32-adc", |
| 850 | .of_match_table = stm32_adc_of_match, |
| 851 | }, |
| 852 | }; |
| 853 | module_platform_driver(stm32_adc_driver); |
| 854 | |
| 855 | MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>"); |
| 856 | MODULE_DESCRIPTION("STMicroelectronics STM32 ADC IIO driver"); |
| 857 | MODULE_LICENSE("GPL v2"); |
| 858 | MODULE_ALIAS("platform:stm32-adc"); |