stm32-dfsdm-adc.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file is the ADC part of the STM32 DFSDM driver
  4. *
  5. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  6. * Author: Arnaud Pouliquen <arnaud.pouliquen@st.com>.
  7. */
  8. #include <linux/dmaengine.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/iio/adc/stm32-dfsdm-adc.h>
  11. #include <linux/iio/buffer.h>
  12. #include <linux/iio/hw-consumer.h>
  13. #include <linux/iio/sysfs.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include "stm32-dfsdm.h"
  21. #define DFSDM_DMA_BUFFER_SIZE (4 * PAGE_SIZE)
  22. /* Conversion timeout */
  23. #define DFSDM_TIMEOUT_US 100000
  24. #define DFSDM_TIMEOUT (msecs_to_jiffies(DFSDM_TIMEOUT_US / 1000))
  25. /* Oversampling attribute default */
  26. #define DFSDM_DEFAULT_OVERSAMPLING 100
  27. /* Oversampling max values */
  28. #define DFSDM_MAX_INT_OVERSAMPLING 256
  29. #define DFSDM_MAX_FL_OVERSAMPLING 1024
  30. /* Max sample resolutions */
  31. #define DFSDM_MAX_RES BIT(31)
  32. #define DFSDM_DATA_RES BIT(23)
  33. enum sd_converter_type {
  34. DFSDM_AUDIO,
  35. DFSDM_IIO,
  36. };
  37. struct stm32_dfsdm_dev_data {
  38. int type;
  39. int (*init)(struct device *dev, struct iio_dev *indio_dev);
  40. unsigned int num_channels;
  41. const struct regmap_config *regmap_cfg;
  42. };
  43. struct stm32_dfsdm_adc {
  44. struct stm32_dfsdm *dfsdm;
  45. const struct stm32_dfsdm_dev_data *dev_data;
  46. unsigned int fl_id;
  47. /* ADC specific */
  48. unsigned int oversamp;
  49. struct iio_hw_consumer *hwc;
  50. struct completion completion;
  51. u32 *buffer;
  52. /* Audio specific */
  53. unsigned int spi_freq; /* SPI bus clock frequency */
  54. unsigned int sample_freq; /* Sample frequency after filter decimation */
  55. int (*cb)(const void *data, size_t size, void *cb_priv);
  56. void *cb_priv;
  57. /* DMA */
  58. u8 *rx_buf;
  59. unsigned int bufi; /* Buffer current position */
  60. unsigned int buf_sz; /* Buffer size */
  61. struct dma_chan *dma_chan;
  62. dma_addr_t dma_buf;
  63. };
  64. struct stm32_dfsdm_str2field {
  65. const char *name;
  66. unsigned int val;
  67. };
  68. /* DFSDM channel serial interface type */
  69. static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_type[] = {
  70. { "SPI_R", 0 }, /* SPI with data on rising edge */
  71. { "SPI_F", 1 }, /* SPI with data on falling edge */
  72. { "MANCH_R", 2 }, /* Manchester codec, rising edge = logic 0 */
  73. { "MANCH_F", 3 }, /* Manchester codec, falling edge = logic 1 */
  74. {},
  75. };
  76. /* DFSDM channel clock source */
  77. static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_src[] = {
  78. /* External SPI clock (CLKIN x) */
  79. { "CLKIN", DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL },
  80. /* Internal SPI clock (CLKOUT) */
  81. { "CLKOUT", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL },
  82. /* Internal SPI clock divided by 2 (falling edge) */
  83. { "CLKOUT_F", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING },
  84. /* Internal SPI clock divided by 2 (falling edge) */
  85. { "CLKOUT_R", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING },
  86. {},
  87. };
  88. static int stm32_dfsdm_str2val(const char *str,
  89. const struct stm32_dfsdm_str2field *list)
  90. {
  91. const struct stm32_dfsdm_str2field *p = list;
  92. for (p = list; p && p->name; p++)
  93. if (!strcmp(p->name, str))
  94. return p->val;
  95. return -EINVAL;
  96. }
  97. static int stm32_dfsdm_set_osrs(struct stm32_dfsdm_filter *fl,
  98. unsigned int fast, unsigned int oversamp)
  99. {
  100. unsigned int i, d, fosr, iosr;
  101. u64 res;
  102. s64 delta;
  103. unsigned int m = 1; /* multiplication factor */
  104. unsigned int p = fl->ford; /* filter order (ford) */
  105. pr_debug("%s: Requested oversampling: %d\n", __func__, oversamp);
  106. /*
  107. * This function tries to compute filter oversampling and integrator
  108. * oversampling, base on oversampling ratio requested by user.
  109. *
  110. * Decimation d depends on the filter order and the oversampling ratios.
  111. * ford: filter order
  112. * fosr: filter over sampling ratio
  113. * iosr: integrator over sampling ratio
  114. */
  115. if (fl->ford == DFSDM_FASTSINC_ORDER) {
  116. m = 2;
  117. p = 2;
  118. }
  119. /*
  120. * Look for filter and integrator oversampling ratios which allows
  121. * to reach 24 bits data output resolution.
  122. * Leave as soon as if exact resolution if reached.
  123. * Otherwise the higher resolution below 32 bits is kept.
  124. */
  125. fl->res = 0;
  126. for (fosr = 1; fosr <= DFSDM_MAX_FL_OVERSAMPLING; fosr++) {
  127. for (iosr = 1; iosr <= DFSDM_MAX_INT_OVERSAMPLING; iosr++) {
  128. if (fast)
  129. d = fosr * iosr;
  130. else if (fl->ford == DFSDM_FASTSINC_ORDER)
  131. d = fosr * (iosr + 3) + 2;
  132. else
  133. d = fosr * (iosr - 1 + p) + p;
  134. if (d > oversamp)
  135. break;
  136. else if (d != oversamp)
  137. continue;
  138. /*
  139. * Check resolution (limited to signed 32 bits)
  140. * res <= 2^31
  141. * Sincx filters:
  142. * res = m * fosr^p x iosr (with m=1, p=ford)
  143. * FastSinc filter
  144. * res = m * fosr^p x iosr (with m=2, p=2)
  145. */
  146. res = fosr;
  147. for (i = p - 1; i > 0; i--) {
  148. res = res * (u64)fosr;
  149. if (res > DFSDM_MAX_RES)
  150. break;
  151. }
  152. if (res > DFSDM_MAX_RES)
  153. continue;
  154. res = res * (u64)m * (u64)iosr;
  155. if (res > DFSDM_MAX_RES)
  156. continue;
  157. delta = res - DFSDM_DATA_RES;
  158. if (res >= fl->res) {
  159. fl->res = res;
  160. fl->fosr = fosr;
  161. fl->iosr = iosr;
  162. fl->fast = fast;
  163. pr_debug("%s: fosr = %d, iosr = %d\n",
  164. __func__, fl->fosr, fl->iosr);
  165. }
  166. if (!delta)
  167. return 0;
  168. }
  169. }
  170. if (!fl->res)
  171. return -EINVAL;
  172. return 0;
  173. }
  174. static int stm32_dfsdm_start_channel(struct stm32_dfsdm *dfsdm,
  175. unsigned int ch_id)
  176. {
  177. return regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(ch_id),
  178. DFSDM_CHCFGR1_CHEN_MASK,
  179. DFSDM_CHCFGR1_CHEN(1));
  180. }
  181. static void stm32_dfsdm_stop_channel(struct stm32_dfsdm *dfsdm,
  182. unsigned int ch_id)
  183. {
  184. regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(ch_id),
  185. DFSDM_CHCFGR1_CHEN_MASK, DFSDM_CHCFGR1_CHEN(0));
  186. }
  187. static int stm32_dfsdm_chan_configure(struct stm32_dfsdm *dfsdm,
  188. struct stm32_dfsdm_channel *ch)
  189. {
  190. unsigned int id = ch->id;
  191. struct regmap *regmap = dfsdm->regmap;
  192. int ret;
  193. ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
  194. DFSDM_CHCFGR1_SITP_MASK,
  195. DFSDM_CHCFGR1_SITP(ch->type));
  196. if (ret < 0)
  197. return ret;
  198. ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
  199. DFSDM_CHCFGR1_SPICKSEL_MASK,
  200. DFSDM_CHCFGR1_SPICKSEL(ch->src));
  201. if (ret < 0)
  202. return ret;
  203. return regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
  204. DFSDM_CHCFGR1_CHINSEL_MASK,
  205. DFSDM_CHCFGR1_CHINSEL(ch->alt_si));
  206. }
  207. static int stm32_dfsdm_start_filter(struct stm32_dfsdm *dfsdm,
  208. unsigned int fl_id)
  209. {
  210. int ret;
  211. /* Enable filter */
  212. ret = regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
  213. DFSDM_CR1_DFEN_MASK, DFSDM_CR1_DFEN(1));
  214. if (ret < 0)
  215. return ret;
  216. /* Start conversion */
  217. return regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
  218. DFSDM_CR1_RSWSTART_MASK,
  219. DFSDM_CR1_RSWSTART(1));
  220. }
  221. static void stm32_dfsdm_stop_filter(struct stm32_dfsdm *dfsdm,
  222. unsigned int fl_id)
  223. {
  224. /* Disable conversion */
  225. regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
  226. DFSDM_CR1_DFEN_MASK, DFSDM_CR1_DFEN(0));
  227. }
  228. static int stm32_dfsdm_filter_configure(struct stm32_dfsdm *dfsdm,
  229. unsigned int fl_id, unsigned int ch_id)
  230. {
  231. struct regmap *regmap = dfsdm->regmap;
  232. struct stm32_dfsdm_filter *fl = &dfsdm->fl_list[fl_id];
  233. int ret;
  234. /* Average integrator oversampling */
  235. ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_IOSR_MASK,
  236. DFSDM_FCR_IOSR(fl->iosr - 1));
  237. if (ret)
  238. return ret;
  239. /* Filter order and Oversampling */
  240. ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_FOSR_MASK,
  241. DFSDM_FCR_FOSR(fl->fosr - 1));
  242. if (ret)
  243. return ret;
  244. ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_FORD_MASK,
  245. DFSDM_FCR_FORD(fl->ford));
  246. if (ret)
  247. return ret;
  248. /* No scan mode supported for the moment */
  249. ret = regmap_update_bits(regmap, DFSDM_CR1(fl_id), DFSDM_CR1_RCH_MASK,
  250. DFSDM_CR1_RCH(ch_id));
  251. if (ret)
  252. return ret;
  253. return regmap_update_bits(regmap, DFSDM_CR1(fl_id),
  254. DFSDM_CR1_RSYNC_MASK,
  255. DFSDM_CR1_RSYNC(fl->sync_mode));
  256. }
  257. static int stm32_dfsdm_channel_parse_of(struct stm32_dfsdm *dfsdm,
  258. struct iio_dev *indio_dev,
  259. struct iio_chan_spec *ch)
  260. {
  261. struct stm32_dfsdm_channel *df_ch;
  262. const char *of_str;
  263. int chan_idx = ch->scan_index;
  264. int ret, val;
  265. ret = of_property_read_u32_index(indio_dev->dev.of_node,
  266. "st,adc-channels", chan_idx,
  267. &ch->channel);
  268. if (ret < 0) {
  269. dev_err(&indio_dev->dev,
  270. " Error parsing 'st,adc-channels' for idx %d\n",
  271. chan_idx);
  272. return ret;
  273. }
  274. if (ch->channel >= dfsdm->num_chs) {
  275. dev_err(&indio_dev->dev,
  276. " Error bad channel number %d (max = %d)\n",
  277. ch->channel, dfsdm->num_chs);
  278. return -EINVAL;
  279. }
  280. ret = of_property_read_string_index(indio_dev->dev.of_node,
  281. "st,adc-channel-names", chan_idx,
  282. &ch->datasheet_name);
  283. if (ret < 0) {
  284. dev_err(&indio_dev->dev,
  285. " Error parsing 'st,adc-channel-names' for idx %d\n",
  286. chan_idx);
  287. return ret;
  288. }
  289. df_ch = &dfsdm->ch_list[ch->channel];
  290. df_ch->id = ch->channel;
  291. ret = of_property_read_string_index(indio_dev->dev.of_node,
  292. "st,adc-channel-types", chan_idx,
  293. &of_str);
  294. if (!ret) {
  295. val = stm32_dfsdm_str2val(of_str, stm32_dfsdm_chan_type);
  296. if (val < 0)
  297. return val;
  298. } else {
  299. val = 0;
  300. }
  301. df_ch->type = val;
  302. ret = of_property_read_string_index(indio_dev->dev.of_node,
  303. "st,adc-channel-clk-src", chan_idx,
  304. &of_str);
  305. if (!ret) {
  306. val = stm32_dfsdm_str2val(of_str, stm32_dfsdm_chan_src);
  307. if (val < 0)
  308. return val;
  309. } else {
  310. val = 0;
  311. }
  312. df_ch->src = val;
  313. ret = of_property_read_u32_index(indio_dev->dev.of_node,
  314. "st,adc-alt-channel", chan_idx,
  315. &df_ch->alt_si);
  316. if (ret < 0)
  317. df_ch->alt_si = 0;
  318. return 0;
  319. }
  320. static ssize_t dfsdm_adc_audio_get_spiclk(struct iio_dev *indio_dev,
  321. uintptr_t priv,
  322. const struct iio_chan_spec *chan,
  323. char *buf)
  324. {
  325. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  326. return snprintf(buf, PAGE_SIZE, "%d\n", adc->spi_freq);
  327. }
  328. static ssize_t dfsdm_adc_audio_set_spiclk(struct iio_dev *indio_dev,
  329. uintptr_t priv,
  330. const struct iio_chan_spec *chan,
  331. const char *buf, size_t len)
  332. {
  333. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  334. struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
  335. struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel];
  336. unsigned int sample_freq = adc->sample_freq;
  337. unsigned int spi_freq;
  338. int ret;
  339. dev_err(&indio_dev->dev, "enter %s\n", __func__);
  340. /* If DFSDM is master on SPI, SPI freq can not be updated */
  341. if (ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL)
  342. return -EPERM;
  343. ret = kstrtoint(buf, 0, &spi_freq);
  344. if (ret)
  345. return ret;
  346. if (!spi_freq)
  347. return -EINVAL;
  348. if (sample_freq) {
  349. if (spi_freq % sample_freq)
  350. dev_warn(&indio_dev->dev,
  351. "Sampling rate not accurate (%d)\n",
  352. spi_freq / (spi_freq / sample_freq));
  353. ret = stm32_dfsdm_set_osrs(fl, 0, (spi_freq / sample_freq));
  354. if (ret < 0) {
  355. dev_err(&indio_dev->dev,
  356. "No filter parameters that match!\n");
  357. return ret;
  358. }
  359. }
  360. adc->spi_freq = spi_freq;
  361. return len;
  362. }
  363. static int stm32_dfsdm_start_conv(struct stm32_dfsdm_adc *adc,
  364. const struct iio_chan_spec *chan,
  365. bool dma)
  366. {
  367. struct regmap *regmap = adc->dfsdm->regmap;
  368. int ret;
  369. unsigned int dma_en = 0, cont_en = 0;
  370. ret = stm32_dfsdm_start_channel(adc->dfsdm, chan->channel);
  371. if (ret < 0)
  372. return ret;
  373. ret = stm32_dfsdm_filter_configure(adc->dfsdm, adc->fl_id,
  374. chan->channel);
  375. if (ret < 0)
  376. goto stop_channels;
  377. if (dma) {
  378. /* Enable DMA transfer*/
  379. dma_en = DFSDM_CR1_RDMAEN(1);
  380. /* Enable conversion triggered by SPI clock*/
  381. cont_en = DFSDM_CR1_RCONT(1);
  382. }
  383. /* Enable DMA transfer*/
  384. ret = regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
  385. DFSDM_CR1_RDMAEN_MASK, dma_en);
  386. if (ret < 0)
  387. goto stop_channels;
  388. /* Enable conversion triggered by SPI clock*/
  389. ret = regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
  390. DFSDM_CR1_RCONT_MASK, cont_en);
  391. if (ret < 0)
  392. goto stop_channels;
  393. ret = stm32_dfsdm_start_filter(adc->dfsdm, adc->fl_id);
  394. if (ret < 0)
  395. goto stop_channels;
  396. return 0;
  397. stop_channels:
  398. regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
  399. DFSDM_CR1_RDMAEN_MASK, 0);
  400. regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
  401. DFSDM_CR1_RCONT_MASK, 0);
  402. stm32_dfsdm_stop_channel(adc->dfsdm, chan->channel);
  403. return ret;
  404. }
  405. static void stm32_dfsdm_stop_conv(struct stm32_dfsdm_adc *adc,
  406. const struct iio_chan_spec *chan)
  407. {
  408. struct regmap *regmap = adc->dfsdm->regmap;
  409. stm32_dfsdm_stop_filter(adc->dfsdm, adc->fl_id);
  410. /* Clean conversion options */
  411. regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
  412. DFSDM_CR1_RDMAEN_MASK, 0);
  413. regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
  414. DFSDM_CR1_RCONT_MASK, 0);
  415. stm32_dfsdm_stop_channel(adc->dfsdm, chan->channel);
  416. }
  417. static int stm32_dfsdm_set_watermark(struct iio_dev *indio_dev,
  418. unsigned int val)
  419. {
  420. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  421. unsigned int watermark = DFSDM_DMA_BUFFER_SIZE / 2;
  422. /*
  423. * DMA cyclic transfers are used, buffer is split into two periods.
  424. * There should be :
  425. * - always one buffer (period) DMA is working on
  426. * - one buffer (period) driver pushed to ASoC side.
  427. */
  428. watermark = min(watermark, val * (unsigned int)(sizeof(u32)));
  429. adc->buf_sz = watermark * 2;
  430. return 0;
  431. }
  432. static unsigned int stm32_dfsdm_adc_dma_residue(struct stm32_dfsdm_adc *adc)
  433. {
  434. struct dma_tx_state state;
  435. enum dma_status status;
  436. status = dmaengine_tx_status(adc->dma_chan,
  437. adc->dma_chan->cookie,
  438. &state);
  439. if (status == DMA_IN_PROGRESS) {
  440. /* Residue is size in bytes from end of buffer */
  441. unsigned int i = adc->buf_sz - state.residue;
  442. unsigned int size;
  443. /* Return available bytes */
  444. if (i >= adc->bufi)
  445. size = i - adc->bufi;
  446. else
  447. size = adc->buf_sz + i - adc->bufi;
  448. return size;
  449. }
  450. return 0;
  451. }
  452. static void stm32_dfsdm_audio_dma_buffer_done(void *data)
  453. {
  454. struct iio_dev *indio_dev = data;
  455. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  456. int available = stm32_dfsdm_adc_dma_residue(adc);
  457. size_t old_pos;
  458. /*
  459. * FIXME: In Kernel interface does not support cyclic DMA buffer,and
  460. * offers only an interface to push data samples per samples.
  461. * For this reason IIO buffer interface is not used and interface is
  462. * bypassed using a private callback registered by ASoC.
  463. * This should be a temporary solution waiting a cyclic DMA engine
  464. * support in IIO.
  465. */
  466. dev_dbg(&indio_dev->dev, "%s: pos = %d, available = %d\n", __func__,
  467. adc->bufi, available);
  468. old_pos = adc->bufi;
  469. while (available >= indio_dev->scan_bytes) {
  470. u32 *buffer = (u32 *)&adc->rx_buf[adc->bufi];
  471. /* Mask 8 LSB that contains the channel ID */
  472. *buffer = (*buffer & 0xFFFFFF00) << 8;
  473. available -= indio_dev->scan_bytes;
  474. adc->bufi += indio_dev->scan_bytes;
  475. if (adc->bufi >= adc->buf_sz) {
  476. if (adc->cb)
  477. adc->cb(&adc->rx_buf[old_pos],
  478. adc->buf_sz - old_pos, adc->cb_priv);
  479. adc->bufi = 0;
  480. old_pos = 0;
  481. }
  482. }
  483. if (adc->cb)
  484. adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos,
  485. adc->cb_priv);
  486. }
  487. static int stm32_dfsdm_adc_dma_start(struct iio_dev *indio_dev)
  488. {
  489. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  490. struct dma_async_tx_descriptor *desc;
  491. dma_cookie_t cookie;
  492. int ret;
  493. if (!adc->dma_chan)
  494. return -EINVAL;
  495. dev_dbg(&indio_dev->dev, "%s size=%d watermark=%d\n", __func__,
  496. adc->buf_sz, adc->buf_sz / 2);
  497. /* Prepare a DMA cyclic transaction */
  498. desc = dmaengine_prep_dma_cyclic(adc->dma_chan,
  499. adc->dma_buf,
  500. adc->buf_sz, adc->buf_sz / 2,
  501. DMA_DEV_TO_MEM,
  502. DMA_PREP_INTERRUPT);
  503. if (!desc)
  504. return -EBUSY;
  505. desc->callback = stm32_dfsdm_audio_dma_buffer_done;
  506. desc->callback_param = indio_dev;
  507. cookie = dmaengine_submit(desc);
  508. ret = dma_submit_error(cookie);
  509. if (ret) {
  510. dmaengine_terminate_all(adc->dma_chan);
  511. return ret;
  512. }
  513. /* Issue pending DMA requests */
  514. dma_async_issue_pending(adc->dma_chan);
  515. return 0;
  516. }
  517. static int stm32_dfsdm_postenable(struct iio_dev *indio_dev)
  518. {
  519. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  520. const struct iio_chan_spec *chan = &indio_dev->channels[0];
  521. int ret;
  522. /* Reset adc buffer index */
  523. adc->bufi = 0;
  524. ret = stm32_dfsdm_start_dfsdm(adc->dfsdm);
  525. if (ret < 0)
  526. return ret;
  527. ret = stm32_dfsdm_start_conv(adc, chan, true);
  528. if (ret) {
  529. dev_err(&indio_dev->dev, "Can't start conversion\n");
  530. goto stop_dfsdm;
  531. }
  532. if (adc->dma_chan) {
  533. ret = stm32_dfsdm_adc_dma_start(indio_dev);
  534. if (ret) {
  535. dev_err(&indio_dev->dev, "Can't start DMA\n");
  536. goto err_stop_conv;
  537. }
  538. }
  539. return 0;
  540. err_stop_conv:
  541. stm32_dfsdm_stop_conv(adc, chan);
  542. stop_dfsdm:
  543. stm32_dfsdm_stop_dfsdm(adc->dfsdm);
  544. return ret;
  545. }
  546. static int stm32_dfsdm_predisable(struct iio_dev *indio_dev)
  547. {
  548. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  549. const struct iio_chan_spec *chan = &indio_dev->channels[0];
  550. if (adc->dma_chan)
  551. dmaengine_terminate_all(adc->dma_chan);
  552. stm32_dfsdm_stop_conv(adc, chan);
  553. stm32_dfsdm_stop_dfsdm(adc->dfsdm);
  554. return 0;
  555. }
  556. static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops = {
  557. .postenable = &stm32_dfsdm_postenable,
  558. .predisable = &stm32_dfsdm_predisable,
  559. };
  560. /**
  561. * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
  562. * DMA transfer period is achieved.
  563. *
  564. * @iio_dev: Handle to IIO device.
  565. * @cb: Pointer to callback function:
  566. * - data: pointer to data buffer
  567. * - size: size in byte of the data buffer
  568. * - private: pointer to consumer private structure.
  569. * @private: Pointer to consumer private structure.
  570. */
  571. int stm32_dfsdm_get_buff_cb(struct iio_dev *iio_dev,
  572. int (*cb)(const void *data, size_t size,
  573. void *private),
  574. void *private)
  575. {
  576. struct stm32_dfsdm_adc *adc;
  577. if (!iio_dev)
  578. return -EINVAL;
  579. adc = iio_priv(iio_dev);
  580. adc->cb = cb;
  581. adc->cb_priv = private;
  582. return 0;
  583. }
  584. EXPORT_SYMBOL_GPL(stm32_dfsdm_get_buff_cb);
  585. /**
  586. * stm32_dfsdm_release_buff_cb - unregister buffer callback
  587. *
  588. * @iio_dev: Handle to IIO device.
  589. */
  590. int stm32_dfsdm_release_buff_cb(struct iio_dev *iio_dev)
  591. {
  592. struct stm32_dfsdm_adc *adc;
  593. if (!iio_dev)
  594. return -EINVAL;
  595. adc = iio_priv(iio_dev);
  596. adc->cb = NULL;
  597. adc->cb_priv = NULL;
  598. return 0;
  599. }
  600. EXPORT_SYMBOL_GPL(stm32_dfsdm_release_buff_cb);
  601. static int stm32_dfsdm_single_conv(struct iio_dev *indio_dev,
  602. const struct iio_chan_spec *chan, int *res)
  603. {
  604. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  605. long timeout;
  606. int ret;
  607. reinit_completion(&adc->completion);
  608. adc->buffer = res;
  609. ret = stm32_dfsdm_start_dfsdm(adc->dfsdm);
  610. if (ret < 0)
  611. return ret;
  612. ret = regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
  613. DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(1));
  614. if (ret < 0)
  615. goto stop_dfsdm;
  616. ret = stm32_dfsdm_start_conv(adc, chan, false);
  617. if (ret < 0) {
  618. regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
  619. DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(0));
  620. goto stop_dfsdm;
  621. }
  622. timeout = wait_for_completion_interruptible_timeout(&adc->completion,
  623. DFSDM_TIMEOUT);
  624. /* Mask IRQ for regular conversion achievement*/
  625. regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
  626. DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(0));
  627. if (timeout == 0)
  628. ret = -ETIMEDOUT;
  629. else if (timeout < 0)
  630. ret = timeout;
  631. else
  632. ret = IIO_VAL_INT;
  633. stm32_dfsdm_stop_conv(adc, chan);
  634. stop_dfsdm:
  635. stm32_dfsdm_stop_dfsdm(adc->dfsdm);
  636. return ret;
  637. }
  638. static int stm32_dfsdm_write_raw(struct iio_dev *indio_dev,
  639. struct iio_chan_spec const *chan,
  640. int val, int val2, long mask)
  641. {
  642. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  643. struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
  644. struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel];
  645. unsigned int spi_freq;
  646. int ret = -EINVAL;
  647. switch (mask) {
  648. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  649. ret = stm32_dfsdm_set_osrs(fl, 0, val);
  650. if (!ret)
  651. adc->oversamp = val;
  652. return ret;
  653. case IIO_CHAN_INFO_SAMP_FREQ:
  654. if (!val)
  655. return -EINVAL;
  656. switch (ch->src) {
  657. case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL:
  658. spi_freq = adc->dfsdm->spi_master_freq;
  659. break;
  660. case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING:
  661. case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING:
  662. spi_freq = adc->dfsdm->spi_master_freq / 2;
  663. break;
  664. default:
  665. spi_freq = adc->spi_freq;
  666. }
  667. if (spi_freq % val)
  668. dev_warn(&indio_dev->dev,
  669. "Sampling rate not accurate (%d)\n",
  670. spi_freq / (spi_freq / val));
  671. ret = stm32_dfsdm_set_osrs(fl, 0, (spi_freq / val));
  672. if (ret < 0) {
  673. dev_err(&indio_dev->dev,
  674. "Not able to find parameter that match!\n");
  675. return ret;
  676. }
  677. adc->sample_freq = val;
  678. return 0;
  679. }
  680. return -EINVAL;
  681. }
  682. static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev,
  683. struct iio_chan_spec const *chan, int *val,
  684. int *val2, long mask)
  685. {
  686. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  687. int ret;
  688. switch (mask) {
  689. case IIO_CHAN_INFO_RAW:
  690. ret = iio_hw_consumer_enable(adc->hwc);
  691. if (ret < 0) {
  692. dev_err(&indio_dev->dev,
  693. "%s: IIO enable failed (channel %d)\n",
  694. __func__, chan->channel);
  695. return ret;
  696. }
  697. ret = stm32_dfsdm_single_conv(indio_dev, chan, val);
  698. iio_hw_consumer_disable(adc->hwc);
  699. if (ret < 0) {
  700. dev_err(&indio_dev->dev,
  701. "%s: Conversion failed (channel %d)\n",
  702. __func__, chan->channel);
  703. return ret;
  704. }
  705. return IIO_VAL_INT;
  706. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  707. *val = adc->oversamp;
  708. return IIO_VAL_INT;
  709. case IIO_CHAN_INFO_SAMP_FREQ:
  710. *val = adc->sample_freq;
  711. return IIO_VAL_INT;
  712. }
  713. return -EINVAL;
  714. }
  715. static const struct iio_info stm32_dfsdm_info_audio = {
  716. .hwfifo_set_watermark = stm32_dfsdm_set_watermark,
  717. .read_raw = stm32_dfsdm_read_raw,
  718. .write_raw = stm32_dfsdm_write_raw,
  719. };
  720. static const struct iio_info stm32_dfsdm_info_adc = {
  721. .read_raw = stm32_dfsdm_read_raw,
  722. .write_raw = stm32_dfsdm_write_raw,
  723. };
  724. static irqreturn_t stm32_dfsdm_irq(int irq, void *arg)
  725. {
  726. struct stm32_dfsdm_adc *adc = arg;
  727. struct iio_dev *indio_dev = iio_priv_to_dev(adc);
  728. struct regmap *regmap = adc->dfsdm->regmap;
  729. unsigned int status, int_en;
  730. regmap_read(regmap, DFSDM_ISR(adc->fl_id), &status);
  731. regmap_read(regmap, DFSDM_CR2(adc->fl_id), &int_en);
  732. if (status & DFSDM_ISR_REOCF_MASK) {
  733. /* Read the data register clean the IRQ status */
  734. regmap_read(regmap, DFSDM_RDATAR(adc->fl_id), adc->buffer);
  735. complete(&adc->completion);
  736. }
  737. if (status & DFSDM_ISR_ROVRF_MASK) {
  738. if (int_en & DFSDM_CR2_ROVRIE_MASK)
  739. dev_warn(&indio_dev->dev, "Overrun detected\n");
  740. regmap_update_bits(regmap, DFSDM_ICR(adc->fl_id),
  741. DFSDM_ICR_CLRROVRF_MASK,
  742. DFSDM_ICR_CLRROVRF_MASK);
  743. }
  744. return IRQ_HANDLED;
  745. }
  746. /*
  747. * Define external info for SPI Frequency and audio sampling rate that can be
  748. * configured by ASoC driver through consumer.h API
  749. */
  750. static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info[] = {
  751. /* spi_clk_freq : clock freq on SPI/manchester bus used by channel */
  752. {
  753. .name = "spi_clk_freq",
  754. .shared = IIO_SHARED_BY_TYPE,
  755. .read = dfsdm_adc_audio_get_spiclk,
  756. .write = dfsdm_adc_audio_set_spiclk,
  757. },
  758. {},
  759. };
  760. static void stm32_dfsdm_dma_release(struct iio_dev *indio_dev)
  761. {
  762. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  763. if (adc->dma_chan) {
  764. dma_free_coherent(adc->dma_chan->device->dev,
  765. DFSDM_DMA_BUFFER_SIZE,
  766. adc->rx_buf, adc->dma_buf);
  767. dma_release_channel(adc->dma_chan);
  768. }
  769. }
  770. static int stm32_dfsdm_dma_request(struct device *dev,
  771. struct iio_dev *indio_dev)
  772. {
  773. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  774. struct dma_slave_config config = {
  775. .src_addr = (dma_addr_t)adc->dfsdm->phys_base +
  776. DFSDM_RDATAR(adc->fl_id),
  777. .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  778. };
  779. int ret;
  780. adc->dma_chan = dma_request_chan(dev, "rx");
  781. if (IS_ERR(adc->dma_chan)) {
  782. int ret = PTR_ERR(adc->dma_chan);
  783. adc->dma_chan = NULL;
  784. return ret;
  785. }
  786. adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev,
  787. DFSDM_DMA_BUFFER_SIZE,
  788. &adc->dma_buf, GFP_KERNEL);
  789. if (!adc->rx_buf) {
  790. ret = -ENOMEM;
  791. goto err_release;
  792. }
  793. ret = dmaengine_slave_config(adc->dma_chan, &config);
  794. if (ret)
  795. goto err_free;
  796. return 0;
  797. err_free:
  798. dma_free_coherent(adc->dma_chan->device->dev, DFSDM_DMA_BUFFER_SIZE,
  799. adc->rx_buf, adc->dma_buf);
  800. err_release:
  801. dma_release_channel(adc->dma_chan);
  802. return ret;
  803. }
  804. static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev,
  805. struct iio_chan_spec *ch)
  806. {
  807. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  808. int ret;
  809. ret = stm32_dfsdm_channel_parse_of(adc->dfsdm, indio_dev, ch);
  810. if (ret < 0)
  811. return ret;
  812. ch->type = IIO_VOLTAGE;
  813. ch->indexed = 1;
  814. /*
  815. * IIO_CHAN_INFO_RAW: used to compute regular conversion
  816. * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling
  817. */
  818. ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  819. ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO);
  820. if (adc->dev_data->type == DFSDM_AUDIO) {
  821. ch->ext_info = dfsdm_adc_audio_ext_info;
  822. } else {
  823. ch->scan_type.shift = 8;
  824. }
  825. ch->scan_type.sign = 's';
  826. ch->scan_type.realbits = 24;
  827. ch->scan_type.storagebits = 32;
  828. return stm32_dfsdm_chan_configure(adc->dfsdm,
  829. &adc->dfsdm->ch_list[ch->channel]);
  830. }
  831. static int stm32_dfsdm_audio_init(struct device *dev, struct iio_dev *indio_dev)
  832. {
  833. struct iio_chan_spec *ch;
  834. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  835. struct stm32_dfsdm_channel *d_ch;
  836. int ret;
  837. indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
  838. indio_dev->setup_ops = &stm32_dfsdm_buffer_setup_ops;
  839. ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL);
  840. if (!ch)
  841. return -ENOMEM;
  842. ch->scan_index = 0;
  843. ret = stm32_dfsdm_adc_chan_init_one(indio_dev, ch);
  844. if (ret < 0) {
  845. dev_err(&indio_dev->dev, "Channels init failed\n");
  846. return ret;
  847. }
  848. ch->info_mask_separate = BIT(IIO_CHAN_INFO_SAMP_FREQ);
  849. d_ch = &adc->dfsdm->ch_list[ch->channel];
  850. if (d_ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL)
  851. adc->spi_freq = adc->dfsdm->spi_master_freq;
  852. indio_dev->num_channels = 1;
  853. indio_dev->channels = ch;
  854. return stm32_dfsdm_dma_request(dev, indio_dev);
  855. }
  856. static int stm32_dfsdm_adc_init(struct device *dev, struct iio_dev *indio_dev)
  857. {
  858. struct iio_chan_spec *ch;
  859. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  860. int num_ch;
  861. int ret, chan_idx;
  862. adc->oversamp = DFSDM_DEFAULT_OVERSAMPLING;
  863. ret = stm32_dfsdm_set_osrs(&adc->dfsdm->fl_list[adc->fl_id], 0,
  864. adc->oversamp);
  865. if (ret < 0)
  866. return ret;
  867. num_ch = of_property_count_u32_elems(indio_dev->dev.of_node,
  868. "st,adc-channels");
  869. if (num_ch < 0 || num_ch > adc->dfsdm->num_chs) {
  870. dev_err(&indio_dev->dev, "Bad st,adc-channels\n");
  871. return num_ch < 0 ? num_ch : -EINVAL;
  872. }
  873. /* Bind to SD modulator IIO device */
  874. adc->hwc = devm_iio_hw_consumer_alloc(&indio_dev->dev);
  875. if (IS_ERR(adc->hwc))
  876. return -EPROBE_DEFER;
  877. ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch),
  878. GFP_KERNEL);
  879. if (!ch)
  880. return -ENOMEM;
  881. for (chan_idx = 0; chan_idx < num_ch; chan_idx++) {
  882. ch[chan_idx].scan_index = chan_idx;
  883. ret = stm32_dfsdm_adc_chan_init_one(indio_dev, &ch[chan_idx]);
  884. if (ret < 0) {
  885. dev_err(&indio_dev->dev, "Channels init failed\n");
  886. return ret;
  887. }
  888. }
  889. indio_dev->num_channels = num_ch;
  890. indio_dev->channels = ch;
  891. init_completion(&adc->completion);
  892. return 0;
  893. }
  894. static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data = {
  895. .type = DFSDM_IIO,
  896. .init = stm32_dfsdm_adc_init,
  897. };
  898. static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data = {
  899. .type = DFSDM_AUDIO,
  900. .init = stm32_dfsdm_audio_init,
  901. };
  902. static const struct of_device_id stm32_dfsdm_adc_match[] = {
  903. {
  904. .compatible = "st,stm32-dfsdm-adc",
  905. .data = &stm32h7_dfsdm_adc_data,
  906. },
  907. {
  908. .compatible = "st,stm32-dfsdm-dmic",
  909. .data = &stm32h7_dfsdm_audio_data,
  910. },
  911. {}
  912. };
  913. static int stm32_dfsdm_adc_probe(struct platform_device *pdev)
  914. {
  915. struct device *dev = &pdev->dev;
  916. struct stm32_dfsdm_adc *adc;
  917. struct device_node *np = dev->of_node;
  918. const struct stm32_dfsdm_dev_data *dev_data;
  919. struct iio_dev *iio;
  920. char *name;
  921. int ret, irq, val;
  922. dev_data = of_device_get_match_data(dev);
  923. iio = devm_iio_device_alloc(dev, sizeof(*adc));
  924. if (!iio) {
  925. dev_err(dev, "%s: Failed to allocate IIO\n", __func__);
  926. return -ENOMEM;
  927. }
  928. adc = iio_priv(iio);
  929. adc->dfsdm = dev_get_drvdata(dev->parent);
  930. iio->dev.parent = dev;
  931. iio->dev.of_node = np;
  932. iio->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
  933. platform_set_drvdata(pdev, adc);
  934. ret = of_property_read_u32(dev->of_node, "reg", &adc->fl_id);
  935. if (ret != 0 || adc->fl_id >= adc->dfsdm->num_fls) {
  936. dev_err(dev, "Missing or bad reg property\n");
  937. return -EINVAL;
  938. }
  939. name = devm_kzalloc(dev, sizeof("dfsdm-adc0"), GFP_KERNEL);
  940. if (!name)
  941. return -ENOMEM;
  942. if (dev_data->type == DFSDM_AUDIO) {
  943. iio->info = &stm32_dfsdm_info_audio;
  944. snprintf(name, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc->fl_id);
  945. } else {
  946. iio->info = &stm32_dfsdm_info_adc;
  947. snprintf(name, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc->fl_id);
  948. }
  949. iio->name = name;
  950. /*
  951. * In a first step IRQs generated for channels are not treated.
  952. * So IRQ associated to filter instance 0 is dedicated to the Filter 0.
  953. */
  954. irq = platform_get_irq(pdev, 0);
  955. if (irq < 0) {
  956. if (irq != -EPROBE_DEFER)
  957. dev_err(dev, "Failed to get IRQ: %d\n", irq);
  958. return irq;
  959. }
  960. ret = devm_request_irq(dev, irq, stm32_dfsdm_irq,
  961. 0, pdev->name, adc);
  962. if (ret < 0) {
  963. dev_err(dev, "Failed to request IRQ\n");
  964. return ret;
  965. }
  966. ret = of_property_read_u32(dev->of_node, "st,filter-order", &val);
  967. if (ret < 0) {
  968. dev_err(dev, "Failed to set filter order\n");
  969. return ret;
  970. }
  971. adc->dfsdm->fl_list[adc->fl_id].ford = val;
  972. ret = of_property_read_u32(dev->of_node, "st,filter0-sync", &val);
  973. if (!ret)
  974. adc->dfsdm->fl_list[adc->fl_id].sync_mode = val;
  975. adc->dev_data = dev_data;
  976. ret = dev_data->init(dev, iio);
  977. if (ret < 0)
  978. return ret;
  979. ret = iio_device_register(iio);
  980. if (ret < 0)
  981. goto err_cleanup;
  982. if (dev_data->type == DFSDM_AUDIO) {
  983. ret = of_platform_populate(np, NULL, NULL, dev);
  984. if (ret < 0) {
  985. dev_err(dev, "Failed to find an audio DAI\n");
  986. goto err_unregister;
  987. }
  988. }
  989. return 0;
  990. err_unregister:
  991. iio_device_unregister(iio);
  992. err_cleanup:
  993. stm32_dfsdm_dma_release(iio);
  994. return ret;
  995. }
  996. static int stm32_dfsdm_adc_remove(struct platform_device *pdev)
  997. {
  998. struct stm32_dfsdm_adc *adc = platform_get_drvdata(pdev);
  999. struct iio_dev *indio_dev = iio_priv_to_dev(adc);
  1000. if (adc->dev_data->type == DFSDM_AUDIO)
  1001. of_platform_depopulate(&pdev->dev);
  1002. iio_device_unregister(indio_dev);
  1003. stm32_dfsdm_dma_release(indio_dev);
  1004. return 0;
  1005. }
  1006. static struct platform_driver stm32_dfsdm_adc_driver = {
  1007. .driver = {
  1008. .name = "stm32-dfsdm-adc",
  1009. .of_match_table = stm32_dfsdm_adc_match,
  1010. },
  1011. .probe = stm32_dfsdm_adc_probe,
  1012. .remove = stm32_dfsdm_adc_remove,
  1013. };
  1014. module_platform_driver(stm32_dfsdm_adc_driver);
  1015. MODULE_DESCRIPTION("STM32 sigma delta ADC");
  1016. MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
  1017. MODULE_LICENSE("GPL v2");