stm32-adc.c 29 KB

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