stm32-adc.c 28 KB

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