stm32-dfsdm-adc.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  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 <[email protected]>.
  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/iio/timer/stm32-lptim-trigger.h>
  15. #include <linux/iio/timer/stm32-timer-trigger.h>
  16. #include <linux/iio/trigger.h>
  17. #include <linux/iio/trigger_consumer.h>
  18. #include <linux/iio/triggered_buffer.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/slab.h>
  25. #include "stm32-dfsdm.h"
  26. #define DFSDM_DMA_BUFFER_SIZE (4 * PAGE_SIZE)
  27. /* Conversion timeout */
  28. #define DFSDM_TIMEOUT_US 100000
  29. #define DFSDM_TIMEOUT (msecs_to_jiffies(DFSDM_TIMEOUT_US / 1000))
  30. /* Oversampling attribute default */
  31. #define DFSDM_DEFAULT_OVERSAMPLING 100
  32. /* Oversampling max values */
  33. #define DFSDM_MAX_INT_OVERSAMPLING 256
  34. #define DFSDM_MAX_FL_OVERSAMPLING 1024
  35. /* Limit filter output resolution to 31 bits. (i.e. sample range is +/-2^30) */
  36. #define DFSDM_DATA_MAX BIT(30)
  37. /*
  38. * Data are output as two's complement data in a 24 bit field.
  39. * Data from filters are in the range +/-2^(n-1)
  40. * 2^(n-1) maximum positive value cannot be coded in 2's complement n bits
  41. * An extra bit is required to avoid wrap-around of the binary code for 2^(n-1)
  42. * So, the resolution of samples from filter is actually limited to 23 bits
  43. */
  44. #define DFSDM_DATA_RES 24
  45. /* Filter configuration */
  46. #define DFSDM_CR1_CFG_MASK (DFSDM_CR1_RCH_MASK | DFSDM_CR1_RCONT_MASK | \
  47. DFSDM_CR1_RSYNC_MASK | DFSDM_CR1_JSYNC_MASK | \
  48. DFSDM_CR1_JSCAN_MASK)
  49. enum sd_converter_type {
  50. DFSDM_AUDIO,
  51. DFSDM_IIO,
  52. };
  53. struct stm32_dfsdm_dev_data {
  54. int type;
  55. int (*init)(struct device *dev, struct iio_dev *indio_dev);
  56. unsigned int num_channels;
  57. const struct regmap_config *regmap_cfg;
  58. };
  59. struct stm32_dfsdm_adc {
  60. struct stm32_dfsdm *dfsdm;
  61. const struct stm32_dfsdm_dev_data *dev_data;
  62. unsigned int fl_id;
  63. unsigned int nconv;
  64. unsigned long smask;
  65. /* ADC specific */
  66. unsigned int oversamp;
  67. struct iio_hw_consumer *hwc;
  68. struct completion completion;
  69. u32 *buffer;
  70. /* Audio specific */
  71. unsigned int spi_freq; /* SPI bus clock frequency */
  72. unsigned int sample_freq; /* Sample frequency after filter decimation */
  73. int (*cb)(const void *data, size_t size, void *cb_priv);
  74. void *cb_priv;
  75. /* DMA */
  76. u8 *rx_buf;
  77. unsigned int bufi; /* Buffer current position */
  78. unsigned int buf_sz; /* Buffer size */
  79. struct dma_chan *dma_chan;
  80. dma_addr_t dma_buf;
  81. };
  82. struct stm32_dfsdm_str2field {
  83. const char *name;
  84. unsigned int val;
  85. };
  86. /* DFSDM channel serial interface type */
  87. static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_type[] = {
  88. { "SPI_R", 0 }, /* SPI with data on rising edge */
  89. { "SPI_F", 1 }, /* SPI with data on falling edge */
  90. { "MANCH_R", 2 }, /* Manchester codec, rising edge = logic 0 */
  91. { "MANCH_F", 3 }, /* Manchester codec, falling edge = logic 1 */
  92. {},
  93. };
  94. /* DFSDM channel clock source */
  95. static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_src[] = {
  96. /* External SPI clock (CLKIN x) */
  97. { "CLKIN", DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL },
  98. /* Internal SPI clock (CLKOUT) */
  99. { "CLKOUT", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL },
  100. /* Internal SPI clock divided by 2 (falling edge) */
  101. { "CLKOUT_F", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING },
  102. /* Internal SPI clock divided by 2 (falling edge) */
  103. { "CLKOUT_R", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING },
  104. {},
  105. };
  106. static int stm32_dfsdm_str2val(const char *str,
  107. const struct stm32_dfsdm_str2field *list)
  108. {
  109. const struct stm32_dfsdm_str2field *p = list;
  110. for (p = list; p && p->name; p++)
  111. if (!strcmp(p->name, str))
  112. return p->val;
  113. return -EINVAL;
  114. }
  115. /**
  116. * struct stm32_dfsdm_trig_info - DFSDM trigger info
  117. * @name: name of the trigger, corresponding to its source
  118. * @jextsel: trigger signal selection
  119. */
  120. struct stm32_dfsdm_trig_info {
  121. const char *name;
  122. unsigned int jextsel;
  123. };
  124. /* hardware injected trigger enable, edge selection */
  125. enum stm32_dfsdm_jexten {
  126. STM32_DFSDM_JEXTEN_DISABLED,
  127. STM32_DFSDM_JEXTEN_RISING_EDGE,
  128. STM32_DFSDM_JEXTEN_FALLING_EDGE,
  129. STM32_DFSDM_EXTEN_BOTH_EDGES,
  130. };
  131. static const struct stm32_dfsdm_trig_info stm32_dfsdm_trigs[] = {
  132. { TIM1_TRGO, 0 },
  133. { TIM1_TRGO2, 1 },
  134. { TIM8_TRGO, 2 },
  135. { TIM8_TRGO2, 3 },
  136. { TIM3_TRGO, 4 },
  137. { TIM4_TRGO, 5 },
  138. { TIM16_OC1, 6 },
  139. { TIM6_TRGO, 7 },
  140. { TIM7_TRGO, 8 },
  141. { LPTIM1_OUT, 26 },
  142. { LPTIM2_OUT, 27 },
  143. { LPTIM3_OUT, 28 },
  144. {},
  145. };
  146. static int stm32_dfsdm_get_jextsel(struct iio_dev *indio_dev,
  147. struct iio_trigger *trig)
  148. {
  149. int i;
  150. /* lookup triggers registered by stm32 timer trigger driver */
  151. for (i = 0; stm32_dfsdm_trigs[i].name; i++) {
  152. /**
  153. * Checking both stm32 timer trigger type and trig name
  154. * should be safe against arbitrary trigger names.
  155. */
  156. if ((is_stm32_timer_trigger(trig) ||
  157. is_stm32_lptim_trigger(trig)) &&
  158. !strcmp(stm32_dfsdm_trigs[i].name, trig->name)) {
  159. return stm32_dfsdm_trigs[i].jextsel;
  160. }
  161. }
  162. return -EINVAL;
  163. }
  164. static int stm32_dfsdm_compute_osrs(struct stm32_dfsdm_filter *fl,
  165. unsigned int fast, unsigned int oversamp)
  166. {
  167. unsigned int i, d, fosr, iosr;
  168. u64 res, max;
  169. int bits, shift;
  170. unsigned int m = 1; /* multiplication factor */
  171. unsigned int p = fl->ford; /* filter order (ford) */
  172. struct stm32_dfsdm_filter_osr *flo = &fl->flo[fast];
  173. pr_debug("Requested oversampling: %d\n", oversamp);
  174. /*
  175. * This function tries to compute filter oversampling and integrator
  176. * oversampling, base on oversampling ratio requested by user.
  177. *
  178. * Decimation d depends on the filter order and the oversampling ratios.
  179. * ford: filter order
  180. * fosr: filter over sampling ratio
  181. * iosr: integrator over sampling ratio
  182. */
  183. if (fl->ford == DFSDM_FASTSINC_ORDER) {
  184. m = 2;
  185. p = 2;
  186. }
  187. /*
  188. * Look for filter and integrator oversampling ratios which allows
  189. * to maximize data output resolution.
  190. */
  191. for (fosr = 1; fosr <= DFSDM_MAX_FL_OVERSAMPLING; fosr++) {
  192. for (iosr = 1; iosr <= DFSDM_MAX_INT_OVERSAMPLING; iosr++) {
  193. if (fast)
  194. d = fosr * iosr;
  195. else if (fl->ford == DFSDM_FASTSINC_ORDER)
  196. d = fosr * (iosr + 3) + 2;
  197. else
  198. d = fosr * (iosr - 1 + p) + p;
  199. if (d > oversamp)
  200. break;
  201. else if (d != oversamp)
  202. continue;
  203. /*
  204. * Check resolution (limited to signed 32 bits)
  205. * res <= 2^31
  206. * Sincx filters:
  207. * res = m * fosr^p x iosr (with m=1, p=ford)
  208. * FastSinc filter
  209. * res = m * fosr^p x iosr (with m=2, p=2)
  210. */
  211. res = fosr;
  212. for (i = p - 1; i > 0; i--) {
  213. res = res * (u64)fosr;
  214. if (res > DFSDM_DATA_MAX)
  215. break;
  216. }
  217. if (res > DFSDM_DATA_MAX)
  218. continue;
  219. res = res * (u64)m * (u64)iosr;
  220. if (res > DFSDM_DATA_MAX)
  221. continue;
  222. if (res >= flo->res) {
  223. flo->res = res;
  224. flo->fosr = fosr;
  225. flo->iosr = iosr;
  226. bits = fls(flo->res);
  227. /* 8 LBSs in data register contain chan info */
  228. max = flo->res << 8;
  229. /* if resolution is not a power of two */
  230. if (flo->res > BIT(bits - 1))
  231. bits++;
  232. else
  233. max--;
  234. shift = DFSDM_DATA_RES - bits;
  235. /*
  236. * Compute right/left shift
  237. * Right shift is performed by hardware
  238. * when transferring samples to data register.
  239. * Left shift is done by software on buffer
  240. */
  241. if (shift > 0) {
  242. /* Resolution is lower than 24 bits */
  243. flo->rshift = 0;
  244. flo->lshift = shift;
  245. } else {
  246. /*
  247. * If resolution is 24 bits or more,
  248. * max positive value may be ambiguous
  249. * (equal to max negative value as sign
  250. * bit is dropped).
  251. * Reduce resolution to 23 bits (rshift)
  252. * to keep the sign on bit 23 and treat
  253. * saturation before rescaling on 24
  254. * bits (lshift).
  255. */
  256. flo->rshift = 1 - shift;
  257. flo->lshift = 1;
  258. max >>= flo->rshift;
  259. }
  260. flo->max = (s32)max;
  261. flo->bits = bits;
  262. pr_debug("fast %d, fosr %d, iosr %d, res 0x%llx/%d bits, rshift %d, lshift %d\n",
  263. fast, flo->fosr, flo->iosr,
  264. flo->res, bits, flo->rshift,
  265. flo->lshift);
  266. }
  267. }
  268. }
  269. if (!flo->res)
  270. return -EINVAL;
  271. return 0;
  272. }
  273. static int stm32_dfsdm_compute_all_osrs(struct iio_dev *indio_dev,
  274. unsigned int oversamp)
  275. {
  276. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  277. struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
  278. int ret0, ret1;
  279. memset(&fl->flo[0], 0, sizeof(fl->flo[0]));
  280. memset(&fl->flo[1], 0, sizeof(fl->flo[1]));
  281. ret0 = stm32_dfsdm_compute_osrs(fl, 0, oversamp);
  282. ret1 = stm32_dfsdm_compute_osrs(fl, 1, oversamp);
  283. if (ret0 < 0 && ret1 < 0) {
  284. dev_err(&indio_dev->dev,
  285. "Filter parameters not found: errors %d/%d\n",
  286. ret0, ret1);
  287. return -EINVAL;
  288. }
  289. return 0;
  290. }
  291. static int stm32_dfsdm_start_channel(struct iio_dev *indio_dev)
  292. {
  293. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  294. struct regmap *regmap = adc->dfsdm->regmap;
  295. const struct iio_chan_spec *chan;
  296. unsigned int bit;
  297. int ret;
  298. for_each_set_bit(bit, &adc->smask, sizeof(adc->smask) * BITS_PER_BYTE) {
  299. chan = indio_dev->channels + bit;
  300. ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(chan->channel),
  301. DFSDM_CHCFGR1_CHEN_MASK,
  302. DFSDM_CHCFGR1_CHEN(1));
  303. if (ret < 0)
  304. return ret;
  305. }
  306. return 0;
  307. }
  308. static void stm32_dfsdm_stop_channel(struct iio_dev *indio_dev)
  309. {
  310. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  311. struct regmap *regmap = adc->dfsdm->regmap;
  312. const struct iio_chan_spec *chan;
  313. unsigned int bit;
  314. for_each_set_bit(bit, &adc->smask, sizeof(adc->smask) * BITS_PER_BYTE) {
  315. chan = indio_dev->channels + bit;
  316. regmap_update_bits(regmap, DFSDM_CHCFGR1(chan->channel),
  317. DFSDM_CHCFGR1_CHEN_MASK,
  318. DFSDM_CHCFGR1_CHEN(0));
  319. }
  320. }
  321. static int stm32_dfsdm_chan_configure(struct stm32_dfsdm *dfsdm,
  322. struct stm32_dfsdm_channel *ch)
  323. {
  324. unsigned int id = ch->id;
  325. struct regmap *regmap = dfsdm->regmap;
  326. int ret;
  327. ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
  328. DFSDM_CHCFGR1_SITP_MASK,
  329. DFSDM_CHCFGR1_SITP(ch->type));
  330. if (ret < 0)
  331. return ret;
  332. ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
  333. DFSDM_CHCFGR1_SPICKSEL_MASK,
  334. DFSDM_CHCFGR1_SPICKSEL(ch->src));
  335. if (ret < 0)
  336. return ret;
  337. return regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
  338. DFSDM_CHCFGR1_CHINSEL_MASK,
  339. DFSDM_CHCFGR1_CHINSEL(ch->alt_si));
  340. }
  341. static int stm32_dfsdm_start_filter(struct stm32_dfsdm_adc *adc,
  342. unsigned int fl_id,
  343. struct iio_trigger *trig)
  344. {
  345. struct stm32_dfsdm *dfsdm = adc->dfsdm;
  346. int ret;
  347. /* Enable filter */
  348. ret = regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
  349. DFSDM_CR1_DFEN_MASK, DFSDM_CR1_DFEN(1));
  350. if (ret < 0)
  351. return ret;
  352. /* Nothing more to do for injected (scan mode/triggered) conversions */
  353. if (adc->nconv > 1 || trig)
  354. return 0;
  355. /* Software start (single or continuous) regular conversion */
  356. return regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
  357. DFSDM_CR1_RSWSTART_MASK,
  358. DFSDM_CR1_RSWSTART(1));
  359. }
  360. static void stm32_dfsdm_stop_filter(struct stm32_dfsdm *dfsdm,
  361. unsigned int fl_id)
  362. {
  363. /* Disable conversion */
  364. regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
  365. DFSDM_CR1_DFEN_MASK, DFSDM_CR1_DFEN(0));
  366. }
  367. static int stm32_dfsdm_filter_set_trig(struct iio_dev *indio_dev,
  368. unsigned int fl_id,
  369. struct iio_trigger *trig)
  370. {
  371. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  372. struct regmap *regmap = adc->dfsdm->regmap;
  373. u32 jextsel = 0, jexten = STM32_DFSDM_JEXTEN_DISABLED;
  374. int ret;
  375. if (trig) {
  376. ret = stm32_dfsdm_get_jextsel(indio_dev, trig);
  377. if (ret < 0)
  378. return ret;
  379. /* set trigger source and polarity (default to rising edge) */
  380. jextsel = ret;
  381. jexten = STM32_DFSDM_JEXTEN_RISING_EDGE;
  382. }
  383. ret = regmap_update_bits(regmap, DFSDM_CR1(fl_id),
  384. DFSDM_CR1_JEXTSEL_MASK | DFSDM_CR1_JEXTEN_MASK,
  385. DFSDM_CR1_JEXTSEL(jextsel) |
  386. DFSDM_CR1_JEXTEN(jexten));
  387. if (ret < 0)
  388. return ret;
  389. return 0;
  390. }
  391. static int stm32_dfsdm_channels_configure(struct iio_dev *indio_dev,
  392. unsigned int fl_id,
  393. struct iio_trigger *trig)
  394. {
  395. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  396. struct regmap *regmap = adc->dfsdm->regmap;
  397. struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[fl_id];
  398. struct stm32_dfsdm_filter_osr *flo = &fl->flo[0];
  399. const struct iio_chan_spec *chan;
  400. unsigned int bit;
  401. int ret;
  402. fl->fast = 0;
  403. /*
  404. * In continuous mode, use fast mode configuration,
  405. * if it provides a better resolution.
  406. */
  407. if (adc->nconv == 1 && !trig && iio_buffer_enabled(indio_dev)) {
  408. if (fl->flo[1].res >= fl->flo[0].res) {
  409. fl->fast = 1;
  410. flo = &fl->flo[1];
  411. }
  412. }
  413. if (!flo->res)
  414. return -EINVAL;
  415. dev_dbg(&indio_dev->dev, "Samples actual resolution: %d bits",
  416. min(flo->bits, (u32)DFSDM_DATA_RES - 1));
  417. for_each_set_bit(bit, &adc->smask,
  418. sizeof(adc->smask) * BITS_PER_BYTE) {
  419. chan = indio_dev->channels + bit;
  420. ret = regmap_update_bits(regmap,
  421. DFSDM_CHCFGR2(chan->channel),
  422. DFSDM_CHCFGR2_DTRBS_MASK,
  423. DFSDM_CHCFGR2_DTRBS(flo->rshift));
  424. if (ret)
  425. return ret;
  426. }
  427. return 0;
  428. }
  429. static int stm32_dfsdm_filter_configure(struct iio_dev *indio_dev,
  430. unsigned int fl_id,
  431. struct iio_trigger *trig)
  432. {
  433. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  434. struct regmap *regmap = adc->dfsdm->regmap;
  435. struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[fl_id];
  436. struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast];
  437. u32 cr1;
  438. const struct iio_chan_spec *chan;
  439. unsigned int bit, jchg = 0;
  440. int ret;
  441. /* Average integrator oversampling */
  442. ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_IOSR_MASK,
  443. DFSDM_FCR_IOSR(flo->iosr - 1));
  444. if (ret)
  445. return ret;
  446. /* Filter order and Oversampling */
  447. ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_FOSR_MASK,
  448. DFSDM_FCR_FOSR(flo->fosr - 1));
  449. if (ret)
  450. return ret;
  451. ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_FORD_MASK,
  452. DFSDM_FCR_FORD(fl->ford));
  453. if (ret)
  454. return ret;
  455. ret = stm32_dfsdm_filter_set_trig(indio_dev, fl_id, trig);
  456. if (ret)
  457. return ret;
  458. ret = regmap_update_bits(regmap, DFSDM_CR1(fl_id),
  459. DFSDM_CR1_FAST_MASK,
  460. DFSDM_CR1_FAST(fl->fast));
  461. if (ret)
  462. return ret;
  463. /*
  464. * DFSDM modes configuration W.R.T audio/iio type modes
  465. * ----------------------------------------------------------------
  466. * Modes | regular | regular | injected | injected |
  467. * | | continuous | | + scan |
  468. * --------------|---------|--------------|----------|------------|
  469. * single conv | x | | | |
  470. * (1 chan) | | | | |
  471. * --------------|---------|--------------|----------|------------|
  472. * 1 Audio chan | | sample freq | | |
  473. * | | or sync_mode | | |
  474. * --------------|---------|--------------|----------|------------|
  475. * 1 IIO chan | | sample freq | trigger | |
  476. * | | or sync_mode | | |
  477. * --------------|---------|--------------|----------|------------|
  478. * 2+ IIO chans | | | | trigger or |
  479. * | | | | sync_mode |
  480. * ----------------------------------------------------------------
  481. */
  482. if (adc->nconv == 1 && !trig) {
  483. bit = __ffs(adc->smask);
  484. chan = indio_dev->channels + bit;
  485. /* Use regular conversion for single channel without trigger */
  486. cr1 = DFSDM_CR1_RCH(chan->channel);
  487. /* Continuous conversions triggered by SPI clk in buffer mode */
  488. if (iio_buffer_enabled(indio_dev))
  489. cr1 |= DFSDM_CR1_RCONT(1);
  490. cr1 |= DFSDM_CR1_RSYNC(fl->sync_mode);
  491. } else {
  492. /* Use injected conversion for multiple channels */
  493. for_each_set_bit(bit, &adc->smask,
  494. sizeof(adc->smask) * BITS_PER_BYTE) {
  495. chan = indio_dev->channels + bit;
  496. jchg |= BIT(chan->channel);
  497. }
  498. ret = regmap_write(regmap, DFSDM_JCHGR(fl_id), jchg);
  499. if (ret < 0)
  500. return ret;
  501. /* Use scan mode for multiple channels */
  502. cr1 = DFSDM_CR1_JSCAN((adc->nconv > 1) ? 1 : 0);
  503. /*
  504. * Continuous conversions not supported in injected mode,
  505. * either use:
  506. * - conversions in sync with filter 0
  507. * - triggered conversions
  508. */
  509. if (!fl->sync_mode && !trig)
  510. return -EINVAL;
  511. cr1 |= DFSDM_CR1_JSYNC(fl->sync_mode);
  512. }
  513. return regmap_update_bits(regmap, DFSDM_CR1(fl_id), DFSDM_CR1_CFG_MASK,
  514. cr1);
  515. }
  516. static int stm32_dfsdm_channel_parse_of(struct stm32_dfsdm *dfsdm,
  517. struct iio_dev *indio_dev,
  518. struct iio_chan_spec *ch)
  519. {
  520. struct stm32_dfsdm_channel *df_ch;
  521. const char *of_str;
  522. int chan_idx = ch->scan_index;
  523. int ret, val;
  524. ret = of_property_read_u32_index(indio_dev->dev.of_node,
  525. "st,adc-channels", chan_idx,
  526. &ch->channel);
  527. if (ret < 0) {
  528. dev_err(&indio_dev->dev,
  529. " Error parsing 'st,adc-channels' for idx %d\n",
  530. chan_idx);
  531. return ret;
  532. }
  533. if (ch->channel >= dfsdm->num_chs) {
  534. dev_err(&indio_dev->dev,
  535. " Error bad channel number %d (max = %d)\n",
  536. ch->channel, dfsdm->num_chs);
  537. return -EINVAL;
  538. }
  539. ret = of_property_read_string_index(indio_dev->dev.of_node,
  540. "st,adc-channel-names", chan_idx,
  541. &ch->datasheet_name);
  542. if (ret < 0) {
  543. dev_err(&indio_dev->dev,
  544. " Error parsing 'st,adc-channel-names' for idx %d\n",
  545. chan_idx);
  546. return ret;
  547. }
  548. df_ch = &dfsdm->ch_list[ch->channel];
  549. df_ch->id = ch->channel;
  550. ret = of_property_read_string_index(indio_dev->dev.of_node,
  551. "st,adc-channel-types", chan_idx,
  552. &of_str);
  553. if (!ret) {
  554. val = stm32_dfsdm_str2val(of_str, stm32_dfsdm_chan_type);
  555. if (val < 0)
  556. return val;
  557. } else {
  558. val = 0;
  559. }
  560. df_ch->type = val;
  561. ret = of_property_read_string_index(indio_dev->dev.of_node,
  562. "st,adc-channel-clk-src", chan_idx,
  563. &of_str);
  564. if (!ret) {
  565. val = stm32_dfsdm_str2val(of_str, stm32_dfsdm_chan_src);
  566. if (val < 0)
  567. return val;
  568. } else {
  569. val = 0;
  570. }
  571. df_ch->src = val;
  572. ret = of_property_read_u32_index(indio_dev->dev.of_node,
  573. "st,adc-alt-channel", chan_idx,
  574. &df_ch->alt_si);
  575. if (ret < 0)
  576. df_ch->alt_si = 0;
  577. return 0;
  578. }
  579. static ssize_t dfsdm_adc_audio_get_spiclk(struct iio_dev *indio_dev,
  580. uintptr_t priv,
  581. const struct iio_chan_spec *chan,
  582. char *buf)
  583. {
  584. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  585. return snprintf(buf, PAGE_SIZE, "%d\n", adc->spi_freq);
  586. }
  587. static int dfsdm_adc_set_samp_freq(struct iio_dev *indio_dev,
  588. unsigned int sample_freq,
  589. unsigned int spi_freq)
  590. {
  591. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  592. unsigned int oversamp;
  593. int ret;
  594. oversamp = DIV_ROUND_CLOSEST(spi_freq, sample_freq);
  595. if (spi_freq % sample_freq)
  596. dev_dbg(&indio_dev->dev,
  597. "Rate not accurate. requested (%u), actual (%u)\n",
  598. sample_freq, spi_freq / oversamp);
  599. ret = stm32_dfsdm_compute_all_osrs(indio_dev, oversamp);
  600. if (ret < 0)
  601. return ret;
  602. adc->sample_freq = spi_freq / oversamp;
  603. adc->oversamp = oversamp;
  604. return 0;
  605. }
  606. static ssize_t dfsdm_adc_audio_set_spiclk(struct iio_dev *indio_dev,
  607. uintptr_t priv,
  608. const struct iio_chan_spec *chan,
  609. const char *buf, size_t len)
  610. {
  611. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  612. struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel];
  613. unsigned int sample_freq = adc->sample_freq;
  614. unsigned int spi_freq;
  615. int ret;
  616. dev_err(&indio_dev->dev, "enter %s\n", __func__);
  617. /* If DFSDM is master on SPI, SPI freq can not be updated */
  618. if (ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL)
  619. return -EPERM;
  620. ret = kstrtoint(buf, 0, &spi_freq);
  621. if (ret)
  622. return ret;
  623. if (!spi_freq)
  624. return -EINVAL;
  625. if (sample_freq) {
  626. ret = dfsdm_adc_set_samp_freq(indio_dev, sample_freq, spi_freq);
  627. if (ret < 0)
  628. return ret;
  629. }
  630. adc->spi_freq = spi_freq;
  631. return len;
  632. }
  633. static int stm32_dfsdm_start_conv(struct iio_dev *indio_dev,
  634. struct iio_trigger *trig)
  635. {
  636. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  637. struct regmap *regmap = adc->dfsdm->regmap;
  638. int ret;
  639. ret = stm32_dfsdm_channels_configure(indio_dev, adc->fl_id, trig);
  640. if (ret < 0)
  641. return ret;
  642. ret = stm32_dfsdm_start_channel(indio_dev);
  643. if (ret < 0)
  644. return ret;
  645. ret = stm32_dfsdm_filter_configure(indio_dev, adc->fl_id, trig);
  646. if (ret < 0)
  647. goto stop_channels;
  648. ret = stm32_dfsdm_start_filter(adc, adc->fl_id, trig);
  649. if (ret < 0)
  650. goto filter_unconfigure;
  651. return 0;
  652. filter_unconfigure:
  653. regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
  654. DFSDM_CR1_CFG_MASK, 0);
  655. stop_channels:
  656. stm32_dfsdm_stop_channel(indio_dev);
  657. return ret;
  658. }
  659. static void stm32_dfsdm_stop_conv(struct iio_dev *indio_dev)
  660. {
  661. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  662. struct regmap *regmap = adc->dfsdm->regmap;
  663. stm32_dfsdm_stop_filter(adc->dfsdm, adc->fl_id);
  664. regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
  665. DFSDM_CR1_CFG_MASK, 0);
  666. stm32_dfsdm_stop_channel(indio_dev);
  667. }
  668. static int stm32_dfsdm_set_watermark(struct iio_dev *indio_dev,
  669. unsigned int val)
  670. {
  671. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  672. unsigned int watermark = DFSDM_DMA_BUFFER_SIZE / 2;
  673. unsigned int rx_buf_sz = DFSDM_DMA_BUFFER_SIZE;
  674. /*
  675. * DMA cyclic transfers are used, buffer is split into two periods.
  676. * There should be :
  677. * - always one buffer (period) DMA is working on
  678. * - one buffer (period) driver pushed to ASoC side.
  679. */
  680. watermark = min(watermark, val * (unsigned int)(sizeof(u32)));
  681. adc->buf_sz = min(rx_buf_sz, watermark * 2 * adc->nconv);
  682. return 0;
  683. }
  684. static unsigned int stm32_dfsdm_adc_dma_residue(struct stm32_dfsdm_adc *adc)
  685. {
  686. struct dma_tx_state state;
  687. enum dma_status status;
  688. status = dmaengine_tx_status(adc->dma_chan,
  689. adc->dma_chan->cookie,
  690. &state);
  691. if (status == DMA_IN_PROGRESS) {
  692. /* Residue is size in bytes from end of buffer */
  693. unsigned int i = adc->buf_sz - state.residue;
  694. unsigned int size;
  695. /* Return available bytes */
  696. if (i >= adc->bufi)
  697. size = i - adc->bufi;
  698. else
  699. size = adc->buf_sz + i - adc->bufi;
  700. return size;
  701. }
  702. return 0;
  703. }
  704. static inline void stm32_dfsdm_process_data(struct stm32_dfsdm_adc *adc,
  705. s32 *buffer)
  706. {
  707. struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
  708. struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast];
  709. unsigned int i = adc->nconv;
  710. s32 *ptr = buffer;
  711. while (i--) {
  712. /* Mask 8 LSB that contains the channel ID */
  713. *ptr &= 0xFFFFFF00;
  714. /* Convert 2^(n-1) sample to 2^(n-1)-1 to avoid wrap-around */
  715. if (*ptr > flo->max)
  716. *ptr -= 1;
  717. /*
  718. * Samples from filter are retrieved with 23 bits resolution
  719. * or less. Shift left to align MSB on 24 bits.
  720. */
  721. *ptr <<= flo->lshift;
  722. ptr++;
  723. }
  724. }
  725. static void stm32_dfsdm_dma_buffer_done(void *data)
  726. {
  727. struct iio_dev *indio_dev = data;
  728. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  729. int available = stm32_dfsdm_adc_dma_residue(adc);
  730. size_t old_pos;
  731. /*
  732. * FIXME: In Kernel interface does not support cyclic DMA buffer,and
  733. * offers only an interface to push data samples per samples.
  734. * For this reason IIO buffer interface is not used and interface is
  735. * bypassed using a private callback registered by ASoC.
  736. * This should be a temporary solution waiting a cyclic DMA engine
  737. * support in IIO.
  738. */
  739. dev_dbg(&indio_dev->dev, "pos = %d, available = %d\n",
  740. adc->bufi, available);
  741. old_pos = adc->bufi;
  742. while (available >= indio_dev->scan_bytes) {
  743. s32 *buffer = (s32 *)&adc->rx_buf[adc->bufi];
  744. stm32_dfsdm_process_data(adc, buffer);
  745. available -= indio_dev->scan_bytes;
  746. adc->bufi += indio_dev->scan_bytes;
  747. if (adc->bufi >= adc->buf_sz) {
  748. if (adc->cb)
  749. adc->cb(&adc->rx_buf[old_pos],
  750. adc->buf_sz - old_pos, adc->cb_priv);
  751. adc->bufi = 0;
  752. old_pos = 0;
  753. }
  754. /*
  755. * In DMA mode the trigger services of IIO are not used
  756. * (e.g. no call to iio_trigger_poll).
  757. * Calling irq handler associated to the hardware trigger is not
  758. * relevant as the conversions have already been done. Data
  759. * transfers are performed directly in DMA callback instead.
  760. * This implementation avoids to call trigger irq handler that
  761. * may sleep, in an atomic context (DMA irq handler context).
  762. */
  763. if (adc->dev_data->type == DFSDM_IIO)
  764. iio_push_to_buffers(indio_dev, buffer);
  765. }
  766. if (adc->cb)
  767. adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos,
  768. adc->cb_priv);
  769. }
  770. static int stm32_dfsdm_adc_dma_start(struct iio_dev *indio_dev)
  771. {
  772. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  773. /*
  774. * The DFSDM supports half-word transfers. However, for 16 bits record,
  775. * 4 bytes buswidth is kept, to avoid losing samples LSBs when left
  776. * shift is required.
  777. */
  778. struct dma_slave_config config = {
  779. .src_addr = (dma_addr_t)adc->dfsdm->phys_base,
  780. .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  781. };
  782. struct dma_async_tx_descriptor *desc;
  783. dma_cookie_t cookie;
  784. int ret;
  785. if (!adc->dma_chan)
  786. return -EINVAL;
  787. dev_dbg(&indio_dev->dev, "size=%d watermark=%d\n",
  788. adc->buf_sz, adc->buf_sz / 2);
  789. if (adc->nconv == 1 && !indio_dev->trig)
  790. config.src_addr += DFSDM_RDATAR(adc->fl_id);
  791. else
  792. config.src_addr += DFSDM_JDATAR(adc->fl_id);
  793. ret = dmaengine_slave_config(adc->dma_chan, &config);
  794. if (ret)
  795. return ret;
  796. /* Prepare a DMA cyclic transaction */
  797. desc = dmaengine_prep_dma_cyclic(adc->dma_chan,
  798. adc->dma_buf,
  799. adc->buf_sz, adc->buf_sz / 2,
  800. DMA_DEV_TO_MEM,
  801. DMA_PREP_INTERRUPT);
  802. if (!desc)
  803. return -EBUSY;
  804. desc->callback = stm32_dfsdm_dma_buffer_done;
  805. desc->callback_param = indio_dev;
  806. cookie = dmaengine_submit(desc);
  807. ret = dma_submit_error(cookie);
  808. if (ret)
  809. goto err_stop_dma;
  810. /* Issue pending DMA requests */
  811. dma_async_issue_pending(adc->dma_chan);
  812. if (adc->nconv == 1 && !indio_dev->trig) {
  813. /* Enable regular DMA transfer*/
  814. ret = regmap_update_bits(adc->dfsdm->regmap,
  815. DFSDM_CR1(adc->fl_id),
  816. DFSDM_CR1_RDMAEN_MASK,
  817. DFSDM_CR1_RDMAEN_MASK);
  818. } else {
  819. /* Enable injected DMA transfer*/
  820. ret = regmap_update_bits(adc->dfsdm->regmap,
  821. DFSDM_CR1(adc->fl_id),
  822. DFSDM_CR1_JDMAEN_MASK,
  823. DFSDM_CR1_JDMAEN_MASK);
  824. }
  825. if (ret < 0)
  826. goto err_stop_dma;
  827. return 0;
  828. err_stop_dma:
  829. dmaengine_terminate_all(adc->dma_chan);
  830. return ret;
  831. }
  832. static void stm32_dfsdm_adc_dma_stop(struct iio_dev *indio_dev)
  833. {
  834. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  835. if (!adc->dma_chan)
  836. return;
  837. regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR1(adc->fl_id),
  838. DFSDM_CR1_RDMAEN_MASK | DFSDM_CR1_JDMAEN_MASK, 0);
  839. dmaengine_terminate_all(adc->dma_chan);
  840. }
  841. static int stm32_dfsdm_update_scan_mode(struct iio_dev *indio_dev,
  842. const unsigned long *scan_mask)
  843. {
  844. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  845. adc->nconv = bitmap_weight(scan_mask, indio_dev->masklength);
  846. adc->smask = *scan_mask;
  847. dev_dbg(&indio_dev->dev, "nconv=%d mask=%lx\n", adc->nconv, *scan_mask);
  848. return 0;
  849. }
  850. static int stm32_dfsdm_postenable(struct iio_dev *indio_dev)
  851. {
  852. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  853. int ret;
  854. /* Reset adc buffer index */
  855. adc->bufi = 0;
  856. if (adc->hwc) {
  857. ret = iio_hw_consumer_enable(adc->hwc);
  858. if (ret < 0)
  859. return ret;
  860. }
  861. ret = stm32_dfsdm_start_dfsdm(adc->dfsdm);
  862. if (ret < 0)
  863. goto err_stop_hwc;
  864. ret = stm32_dfsdm_adc_dma_start(indio_dev);
  865. if (ret) {
  866. dev_err(&indio_dev->dev, "Can't start DMA\n");
  867. goto stop_dfsdm;
  868. }
  869. ret = stm32_dfsdm_start_conv(indio_dev, indio_dev->trig);
  870. if (ret) {
  871. dev_err(&indio_dev->dev, "Can't start conversion\n");
  872. goto err_stop_dma;
  873. }
  874. return 0;
  875. err_stop_dma:
  876. stm32_dfsdm_adc_dma_stop(indio_dev);
  877. stop_dfsdm:
  878. stm32_dfsdm_stop_dfsdm(adc->dfsdm);
  879. err_stop_hwc:
  880. if (adc->hwc)
  881. iio_hw_consumer_disable(adc->hwc);
  882. return ret;
  883. }
  884. static int stm32_dfsdm_predisable(struct iio_dev *indio_dev)
  885. {
  886. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  887. stm32_dfsdm_stop_conv(indio_dev);
  888. stm32_dfsdm_adc_dma_stop(indio_dev);
  889. stm32_dfsdm_stop_dfsdm(adc->dfsdm);
  890. if (adc->hwc)
  891. iio_hw_consumer_disable(adc->hwc);
  892. return 0;
  893. }
  894. static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops = {
  895. .postenable = &stm32_dfsdm_postenable,
  896. .predisable = &stm32_dfsdm_predisable,
  897. };
  898. /**
  899. * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
  900. * DMA transfer period is achieved.
  901. *
  902. * @iio_dev: Handle to IIO device.
  903. * @cb: Pointer to callback function:
  904. * - data: pointer to data buffer
  905. * - size: size in byte of the data buffer
  906. * - private: pointer to consumer private structure.
  907. * @private: Pointer to consumer private structure.
  908. */
  909. int stm32_dfsdm_get_buff_cb(struct iio_dev *iio_dev,
  910. int (*cb)(const void *data, size_t size,
  911. void *private),
  912. void *private)
  913. {
  914. struct stm32_dfsdm_adc *adc;
  915. if (!iio_dev)
  916. return -EINVAL;
  917. adc = iio_priv(iio_dev);
  918. adc->cb = cb;
  919. adc->cb_priv = private;
  920. return 0;
  921. }
  922. EXPORT_SYMBOL_GPL(stm32_dfsdm_get_buff_cb);
  923. /**
  924. * stm32_dfsdm_release_buff_cb - unregister buffer callback
  925. *
  926. * @iio_dev: Handle to IIO device.
  927. */
  928. int stm32_dfsdm_release_buff_cb(struct iio_dev *iio_dev)
  929. {
  930. struct stm32_dfsdm_adc *adc;
  931. if (!iio_dev)
  932. return -EINVAL;
  933. adc = iio_priv(iio_dev);
  934. adc->cb = NULL;
  935. adc->cb_priv = NULL;
  936. return 0;
  937. }
  938. EXPORT_SYMBOL_GPL(stm32_dfsdm_release_buff_cb);
  939. static int stm32_dfsdm_single_conv(struct iio_dev *indio_dev,
  940. const struct iio_chan_spec *chan, int *res)
  941. {
  942. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  943. long timeout;
  944. int ret;
  945. reinit_completion(&adc->completion);
  946. adc->buffer = res;
  947. ret = stm32_dfsdm_start_dfsdm(adc->dfsdm);
  948. if (ret < 0)
  949. return ret;
  950. ret = regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
  951. DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(1));
  952. if (ret < 0)
  953. goto stop_dfsdm;
  954. adc->nconv = 1;
  955. adc->smask = BIT(chan->scan_index);
  956. ret = stm32_dfsdm_start_conv(indio_dev, NULL);
  957. if (ret < 0) {
  958. regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
  959. DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(0));
  960. goto stop_dfsdm;
  961. }
  962. timeout = wait_for_completion_interruptible_timeout(&adc->completion,
  963. DFSDM_TIMEOUT);
  964. /* Mask IRQ for regular conversion achievement*/
  965. regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
  966. DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(0));
  967. if (timeout == 0)
  968. ret = -ETIMEDOUT;
  969. else if (timeout < 0)
  970. ret = timeout;
  971. else
  972. ret = IIO_VAL_INT;
  973. stm32_dfsdm_stop_conv(indio_dev);
  974. stm32_dfsdm_process_data(adc, res);
  975. stop_dfsdm:
  976. stm32_dfsdm_stop_dfsdm(adc->dfsdm);
  977. return ret;
  978. }
  979. static int stm32_dfsdm_write_raw(struct iio_dev *indio_dev,
  980. struct iio_chan_spec const *chan,
  981. int val, int val2, long mask)
  982. {
  983. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  984. struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel];
  985. unsigned int spi_freq;
  986. int ret = -EINVAL;
  987. switch (ch->src) {
  988. case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL:
  989. spi_freq = adc->dfsdm->spi_master_freq;
  990. break;
  991. case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING:
  992. case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING:
  993. spi_freq = adc->dfsdm->spi_master_freq / 2;
  994. break;
  995. default:
  996. spi_freq = adc->spi_freq;
  997. }
  998. switch (mask) {
  999. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  1000. ret = iio_device_claim_direct_mode(indio_dev);
  1001. if (ret)
  1002. return ret;
  1003. ret = stm32_dfsdm_compute_all_osrs(indio_dev, val);
  1004. if (!ret) {
  1005. dev_dbg(&indio_dev->dev,
  1006. "Sampling rate changed from (%u) to (%u)\n",
  1007. adc->sample_freq, spi_freq / val);
  1008. adc->oversamp = val;
  1009. adc->sample_freq = spi_freq / val;
  1010. }
  1011. iio_device_release_direct_mode(indio_dev);
  1012. return ret;
  1013. case IIO_CHAN_INFO_SAMP_FREQ:
  1014. if (!val)
  1015. return -EINVAL;
  1016. ret = iio_device_claim_direct_mode(indio_dev);
  1017. if (ret)
  1018. return ret;
  1019. ret = dfsdm_adc_set_samp_freq(indio_dev, val, spi_freq);
  1020. iio_device_release_direct_mode(indio_dev);
  1021. return ret;
  1022. }
  1023. return -EINVAL;
  1024. }
  1025. static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev,
  1026. struct iio_chan_spec const *chan, int *val,
  1027. int *val2, long mask)
  1028. {
  1029. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  1030. int ret;
  1031. switch (mask) {
  1032. case IIO_CHAN_INFO_RAW:
  1033. ret = iio_device_claim_direct_mode(indio_dev);
  1034. if (ret)
  1035. return ret;
  1036. ret = iio_hw_consumer_enable(adc->hwc);
  1037. if (ret < 0) {
  1038. dev_err(&indio_dev->dev,
  1039. "%s: IIO enable failed (channel %d)\n",
  1040. __func__, chan->channel);
  1041. iio_device_release_direct_mode(indio_dev);
  1042. return ret;
  1043. }
  1044. ret = stm32_dfsdm_single_conv(indio_dev, chan, val);
  1045. iio_hw_consumer_disable(adc->hwc);
  1046. if (ret < 0) {
  1047. dev_err(&indio_dev->dev,
  1048. "%s: Conversion failed (channel %d)\n",
  1049. __func__, chan->channel);
  1050. iio_device_release_direct_mode(indio_dev);
  1051. return ret;
  1052. }
  1053. iio_device_release_direct_mode(indio_dev);
  1054. return IIO_VAL_INT;
  1055. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  1056. *val = adc->oversamp;
  1057. return IIO_VAL_INT;
  1058. case IIO_CHAN_INFO_SAMP_FREQ:
  1059. *val = adc->sample_freq;
  1060. return IIO_VAL_INT;
  1061. }
  1062. return -EINVAL;
  1063. }
  1064. static int stm32_dfsdm_validate_trigger(struct iio_dev *indio_dev,
  1065. struct iio_trigger *trig)
  1066. {
  1067. return stm32_dfsdm_get_jextsel(indio_dev, trig) < 0 ? -EINVAL : 0;
  1068. }
  1069. static const struct iio_info stm32_dfsdm_info_audio = {
  1070. .hwfifo_set_watermark = stm32_dfsdm_set_watermark,
  1071. .read_raw = stm32_dfsdm_read_raw,
  1072. .write_raw = stm32_dfsdm_write_raw,
  1073. .update_scan_mode = stm32_dfsdm_update_scan_mode,
  1074. };
  1075. static const struct iio_info stm32_dfsdm_info_adc = {
  1076. .hwfifo_set_watermark = stm32_dfsdm_set_watermark,
  1077. .read_raw = stm32_dfsdm_read_raw,
  1078. .write_raw = stm32_dfsdm_write_raw,
  1079. .update_scan_mode = stm32_dfsdm_update_scan_mode,
  1080. .validate_trigger = stm32_dfsdm_validate_trigger,
  1081. };
  1082. static irqreturn_t stm32_dfsdm_irq(int irq, void *arg)
  1083. {
  1084. struct iio_dev *indio_dev = arg;
  1085. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  1086. struct regmap *regmap = adc->dfsdm->regmap;
  1087. unsigned int status, int_en;
  1088. regmap_read(regmap, DFSDM_ISR(adc->fl_id), &status);
  1089. regmap_read(regmap, DFSDM_CR2(adc->fl_id), &int_en);
  1090. if (status & DFSDM_ISR_REOCF_MASK) {
  1091. /* Read the data register clean the IRQ status */
  1092. regmap_read(regmap, DFSDM_RDATAR(adc->fl_id), adc->buffer);
  1093. complete(&adc->completion);
  1094. }
  1095. if (status & DFSDM_ISR_ROVRF_MASK) {
  1096. if (int_en & DFSDM_CR2_ROVRIE_MASK)
  1097. dev_warn(&indio_dev->dev, "Overrun detected\n");
  1098. regmap_update_bits(regmap, DFSDM_ICR(adc->fl_id),
  1099. DFSDM_ICR_CLRROVRF_MASK,
  1100. DFSDM_ICR_CLRROVRF_MASK);
  1101. }
  1102. return IRQ_HANDLED;
  1103. }
  1104. /*
  1105. * Define external info for SPI Frequency and audio sampling rate that can be
  1106. * configured by ASoC driver through consumer.h API
  1107. */
  1108. static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info[] = {
  1109. /* spi_clk_freq : clock freq on SPI/manchester bus used by channel */
  1110. {
  1111. .name = "spi_clk_freq",
  1112. .shared = IIO_SHARED_BY_TYPE,
  1113. .read = dfsdm_adc_audio_get_spiclk,
  1114. .write = dfsdm_adc_audio_set_spiclk,
  1115. },
  1116. {},
  1117. };
  1118. static void stm32_dfsdm_dma_release(struct iio_dev *indio_dev)
  1119. {
  1120. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  1121. if (adc->dma_chan) {
  1122. dma_free_coherent(adc->dma_chan->device->dev,
  1123. DFSDM_DMA_BUFFER_SIZE,
  1124. adc->rx_buf, adc->dma_buf);
  1125. dma_release_channel(adc->dma_chan);
  1126. }
  1127. }
  1128. static int stm32_dfsdm_dma_request(struct device *dev,
  1129. struct iio_dev *indio_dev)
  1130. {
  1131. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  1132. adc->dma_chan = dma_request_chan(dev, "rx");
  1133. if (IS_ERR(adc->dma_chan)) {
  1134. int ret = PTR_ERR(adc->dma_chan);
  1135. adc->dma_chan = NULL;
  1136. return ret;
  1137. }
  1138. adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev,
  1139. DFSDM_DMA_BUFFER_SIZE,
  1140. &adc->dma_buf, GFP_KERNEL);
  1141. if (!adc->rx_buf) {
  1142. dma_release_channel(adc->dma_chan);
  1143. return -ENOMEM;
  1144. }
  1145. indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
  1146. indio_dev->setup_ops = &stm32_dfsdm_buffer_setup_ops;
  1147. return 0;
  1148. }
  1149. static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev,
  1150. struct iio_chan_spec *ch)
  1151. {
  1152. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  1153. int ret;
  1154. ret = stm32_dfsdm_channel_parse_of(adc->dfsdm, indio_dev, ch);
  1155. if (ret < 0)
  1156. return ret;
  1157. ch->type = IIO_VOLTAGE;
  1158. ch->indexed = 1;
  1159. /*
  1160. * IIO_CHAN_INFO_RAW: used to compute regular conversion
  1161. * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling
  1162. */
  1163. ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  1164. ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
  1165. BIT(IIO_CHAN_INFO_SAMP_FREQ);
  1166. if (adc->dev_data->type == DFSDM_AUDIO) {
  1167. ch->ext_info = dfsdm_adc_audio_ext_info;
  1168. } else {
  1169. ch->scan_type.shift = 8;
  1170. }
  1171. ch->scan_type.sign = 's';
  1172. ch->scan_type.realbits = 24;
  1173. ch->scan_type.storagebits = 32;
  1174. return stm32_dfsdm_chan_configure(adc->dfsdm,
  1175. &adc->dfsdm->ch_list[ch->channel]);
  1176. }
  1177. static int stm32_dfsdm_audio_init(struct device *dev, struct iio_dev *indio_dev)
  1178. {
  1179. struct iio_chan_spec *ch;
  1180. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  1181. struct stm32_dfsdm_channel *d_ch;
  1182. int ret;
  1183. ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL);
  1184. if (!ch)
  1185. return -ENOMEM;
  1186. ch->scan_index = 0;
  1187. ret = stm32_dfsdm_adc_chan_init_one(indio_dev, ch);
  1188. if (ret < 0) {
  1189. dev_err(&indio_dev->dev, "Channels init failed\n");
  1190. return ret;
  1191. }
  1192. ch->info_mask_separate = BIT(IIO_CHAN_INFO_SAMP_FREQ);
  1193. d_ch = &adc->dfsdm->ch_list[ch->channel];
  1194. if (d_ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL)
  1195. adc->spi_freq = adc->dfsdm->spi_master_freq;
  1196. indio_dev->num_channels = 1;
  1197. indio_dev->channels = ch;
  1198. return stm32_dfsdm_dma_request(dev, indio_dev);
  1199. }
  1200. static int stm32_dfsdm_adc_init(struct device *dev, struct iio_dev *indio_dev)
  1201. {
  1202. struct iio_chan_spec *ch;
  1203. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  1204. int num_ch;
  1205. int ret, chan_idx;
  1206. adc->oversamp = DFSDM_DEFAULT_OVERSAMPLING;
  1207. ret = stm32_dfsdm_compute_all_osrs(indio_dev, adc->oversamp);
  1208. if (ret < 0)
  1209. return ret;
  1210. num_ch = of_property_count_u32_elems(indio_dev->dev.of_node,
  1211. "st,adc-channels");
  1212. if (num_ch < 0 || num_ch > adc->dfsdm->num_chs) {
  1213. dev_err(&indio_dev->dev, "Bad st,adc-channels\n");
  1214. return num_ch < 0 ? num_ch : -EINVAL;
  1215. }
  1216. /* Bind to SD modulator IIO device */
  1217. adc->hwc = devm_iio_hw_consumer_alloc(&indio_dev->dev);
  1218. if (IS_ERR(adc->hwc))
  1219. return -EPROBE_DEFER;
  1220. ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch),
  1221. GFP_KERNEL);
  1222. if (!ch)
  1223. return -ENOMEM;
  1224. for (chan_idx = 0; chan_idx < num_ch; chan_idx++) {
  1225. ch[chan_idx].scan_index = chan_idx;
  1226. ret = stm32_dfsdm_adc_chan_init_one(indio_dev, &ch[chan_idx]);
  1227. if (ret < 0) {
  1228. dev_err(&indio_dev->dev, "Channels init failed\n");
  1229. return ret;
  1230. }
  1231. }
  1232. indio_dev->num_channels = num_ch;
  1233. indio_dev->channels = ch;
  1234. init_completion(&adc->completion);
  1235. /* Optionally request DMA */
  1236. ret = stm32_dfsdm_dma_request(dev, indio_dev);
  1237. if (ret) {
  1238. if (ret != -ENODEV)
  1239. return dev_err_probe(dev, ret,
  1240. "DMA channel request failed with\n");
  1241. dev_dbg(dev, "No DMA support\n");
  1242. return 0;
  1243. }
  1244. ret = iio_triggered_buffer_setup(indio_dev,
  1245. &iio_pollfunc_store_time, NULL,
  1246. &stm32_dfsdm_buffer_setup_ops);
  1247. if (ret) {
  1248. stm32_dfsdm_dma_release(indio_dev);
  1249. dev_err(&indio_dev->dev, "buffer setup failed\n");
  1250. return ret;
  1251. }
  1252. /* lptimer/timer hardware triggers */
  1253. indio_dev->modes |= INDIO_HARDWARE_TRIGGERED;
  1254. return 0;
  1255. }
  1256. static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data = {
  1257. .type = DFSDM_IIO,
  1258. .init = stm32_dfsdm_adc_init,
  1259. };
  1260. static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data = {
  1261. .type = DFSDM_AUDIO,
  1262. .init = stm32_dfsdm_audio_init,
  1263. };
  1264. static const struct of_device_id stm32_dfsdm_adc_match[] = {
  1265. {
  1266. .compatible = "st,stm32-dfsdm-adc",
  1267. .data = &stm32h7_dfsdm_adc_data,
  1268. },
  1269. {
  1270. .compatible = "st,stm32-dfsdm-dmic",
  1271. .data = &stm32h7_dfsdm_audio_data,
  1272. },
  1273. {}
  1274. };
  1275. MODULE_DEVICE_TABLE(of, stm32_dfsdm_adc_match);
  1276. static int stm32_dfsdm_adc_probe(struct platform_device *pdev)
  1277. {
  1278. struct device *dev = &pdev->dev;
  1279. struct stm32_dfsdm_adc *adc;
  1280. struct device_node *np = dev->of_node;
  1281. const struct stm32_dfsdm_dev_data *dev_data;
  1282. struct iio_dev *iio;
  1283. char *name;
  1284. int ret, irq, val;
  1285. dev_data = of_device_get_match_data(dev);
  1286. iio = devm_iio_device_alloc(dev, sizeof(*adc));
  1287. if (!iio) {
  1288. dev_err(dev, "%s: Failed to allocate IIO\n", __func__);
  1289. return -ENOMEM;
  1290. }
  1291. adc = iio_priv(iio);
  1292. adc->dfsdm = dev_get_drvdata(dev->parent);
  1293. iio->dev.of_node = np;
  1294. iio->modes = INDIO_DIRECT_MODE;
  1295. platform_set_drvdata(pdev, iio);
  1296. ret = of_property_read_u32(dev->of_node, "reg", &adc->fl_id);
  1297. if (ret != 0 || adc->fl_id >= adc->dfsdm->num_fls) {
  1298. dev_err(dev, "Missing or bad reg property\n");
  1299. return -EINVAL;
  1300. }
  1301. name = devm_kzalloc(dev, sizeof("dfsdm-adc0"), GFP_KERNEL);
  1302. if (!name)
  1303. return -ENOMEM;
  1304. if (dev_data->type == DFSDM_AUDIO) {
  1305. iio->info = &stm32_dfsdm_info_audio;
  1306. snprintf(name, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc->fl_id);
  1307. } else {
  1308. iio->info = &stm32_dfsdm_info_adc;
  1309. snprintf(name, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc->fl_id);
  1310. }
  1311. iio->name = name;
  1312. /*
  1313. * In a first step IRQs generated for channels are not treated.
  1314. * So IRQ associated to filter instance 0 is dedicated to the Filter 0.
  1315. */
  1316. irq = platform_get_irq(pdev, 0);
  1317. if (irq < 0)
  1318. return irq;
  1319. ret = devm_request_irq(dev, irq, stm32_dfsdm_irq,
  1320. 0, pdev->name, iio);
  1321. if (ret < 0) {
  1322. dev_err(dev, "Failed to request IRQ\n");
  1323. return ret;
  1324. }
  1325. ret = of_property_read_u32(dev->of_node, "st,filter-order", &val);
  1326. if (ret < 0) {
  1327. dev_err(dev, "Failed to set filter order\n");
  1328. return ret;
  1329. }
  1330. adc->dfsdm->fl_list[adc->fl_id].ford = val;
  1331. ret = of_property_read_u32(dev->of_node, "st,filter0-sync", &val);
  1332. if (!ret)
  1333. adc->dfsdm->fl_list[adc->fl_id].sync_mode = val;
  1334. adc->dev_data = dev_data;
  1335. ret = dev_data->init(dev, iio);
  1336. if (ret < 0)
  1337. return ret;
  1338. ret = iio_device_register(iio);
  1339. if (ret < 0)
  1340. goto err_cleanup;
  1341. if (dev_data->type == DFSDM_AUDIO) {
  1342. ret = of_platform_populate(np, NULL, NULL, dev);
  1343. if (ret < 0) {
  1344. dev_err(dev, "Failed to find an audio DAI\n");
  1345. goto err_unregister;
  1346. }
  1347. }
  1348. return 0;
  1349. err_unregister:
  1350. iio_device_unregister(iio);
  1351. err_cleanup:
  1352. stm32_dfsdm_dma_release(iio);
  1353. return ret;
  1354. }
  1355. static int stm32_dfsdm_adc_remove(struct platform_device *pdev)
  1356. {
  1357. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  1358. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  1359. if (adc->dev_data->type == DFSDM_AUDIO)
  1360. of_platform_depopulate(&pdev->dev);
  1361. iio_device_unregister(indio_dev);
  1362. stm32_dfsdm_dma_release(indio_dev);
  1363. return 0;
  1364. }
  1365. static int stm32_dfsdm_adc_suspend(struct device *dev)
  1366. {
  1367. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1368. if (iio_buffer_enabled(indio_dev))
  1369. stm32_dfsdm_predisable(indio_dev);
  1370. return 0;
  1371. }
  1372. static int stm32_dfsdm_adc_resume(struct device *dev)
  1373. {
  1374. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1375. struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
  1376. const struct iio_chan_spec *chan;
  1377. struct stm32_dfsdm_channel *ch;
  1378. int i, ret;
  1379. /* restore channels configuration */
  1380. for (i = 0; i < indio_dev->num_channels; i++) {
  1381. chan = indio_dev->channels + i;
  1382. ch = &adc->dfsdm->ch_list[chan->channel];
  1383. ret = stm32_dfsdm_chan_configure(adc->dfsdm, ch);
  1384. if (ret)
  1385. return ret;
  1386. }
  1387. if (iio_buffer_enabled(indio_dev))
  1388. stm32_dfsdm_postenable(indio_dev);
  1389. return 0;
  1390. }
  1391. static DEFINE_SIMPLE_DEV_PM_OPS(stm32_dfsdm_adc_pm_ops,
  1392. stm32_dfsdm_adc_suspend,
  1393. stm32_dfsdm_adc_resume);
  1394. static struct platform_driver stm32_dfsdm_adc_driver = {
  1395. .driver = {
  1396. .name = "stm32-dfsdm-adc",
  1397. .of_match_table = stm32_dfsdm_adc_match,
  1398. .pm = pm_sleep_ptr(&stm32_dfsdm_adc_pm_ops),
  1399. },
  1400. .probe = stm32_dfsdm_adc_probe,
  1401. .remove = stm32_dfsdm_adc_remove,
  1402. };
  1403. module_platform_driver(stm32_dfsdm_adc_driver);
  1404. MODULE_DESCRIPTION("STM32 sigma delta ADC");
  1405. MODULE_AUTHOR("Arnaud Pouliquen <[email protected]>");
  1406. MODULE_LICENSE("GPL v2");