ad7944.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Analog Devices AD7944/85/86 PulSAR ADC family driver.
  4. *
  5. * Copyright 2024 Analog Devices, Inc.
  6. * Copyright 2024 BayLibre, SAS
  7. */
  8. #include <linux/align.h>
  9. #include <linux/bitfield.h>
  10. #include <linux/bitops.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/module.h>
  16. #include <linux/property.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/spi/offload/consumer.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/string_helpers.h>
  21. #include <linux/units.h>
  22. #include <linux/iio/iio.h>
  23. #include <linux/iio/sysfs.h>
  24. #include <linux/iio/buffer-dmaengine.h>
  25. #include <linux/iio/trigger_consumer.h>
  26. #include <linux/iio/triggered_buffer.h>
  27. #define AD7944_INTERNAL_REF_MV 4096
  28. struct ad7944_timing_spec {
  29. /* Normal mode max conversion time (t_{CONV}). */
  30. unsigned int conv_ns;
  31. /* TURBO mode max conversion time (t_{CONV}). */
  32. unsigned int turbo_conv_ns;
  33. };
  34. enum ad7944_spi_mode {
  35. /* datasheet calls this "4-wire mode" */
  36. AD7944_SPI_MODE_DEFAULT,
  37. /* datasheet calls this "3-wire mode" (not related to SPI_3WIRE!) */
  38. AD7944_SPI_MODE_SINGLE,
  39. /* datasheet calls this "chain mode" */
  40. AD7944_SPI_MODE_CHAIN,
  41. };
  42. /* maps adi,spi-mode property value to enum */
  43. static const char * const ad7944_spi_modes[] = {
  44. [AD7944_SPI_MODE_DEFAULT] = "",
  45. [AD7944_SPI_MODE_SINGLE] = "single",
  46. [AD7944_SPI_MODE_CHAIN] = "chain",
  47. };
  48. struct ad7944_adc {
  49. struct spi_device *spi;
  50. enum ad7944_spi_mode spi_mode;
  51. struct spi_transfer xfers[3];
  52. struct spi_message msg;
  53. struct spi_transfer offload_xfers[2];
  54. struct spi_message offload_msg;
  55. struct spi_offload *offload;
  56. struct spi_offload_trigger *offload_trigger;
  57. unsigned long offload_trigger_hz;
  58. int sample_freq_range[3];
  59. void *chain_mode_buf;
  60. /* Chip-specific timing specifications. */
  61. const struct ad7944_timing_spec *timing_spec;
  62. /* GPIO connected to CNV pin. */
  63. struct gpio_desc *cnv;
  64. /* Optional GPIO to enable turbo mode. */
  65. struct gpio_desc *turbo;
  66. /* Indicates TURBO is hard-wired to be always enabled. */
  67. bool always_turbo;
  68. /* Reference voltage (millivolts). */
  69. unsigned int ref_mv;
  70. /*
  71. * DMA (thus cache coherency maintenance) requires the
  72. * transfer buffers to live in their own cache lines.
  73. */
  74. struct {
  75. union {
  76. u16 u16;
  77. u32 u32;
  78. } raw;
  79. aligned_s64 timestamp;
  80. } sample __aligned(IIO_DMA_MINALIGN);
  81. };
  82. /* quite time before CNV rising edge */
  83. #define AD7944_T_QUIET_NS 20
  84. /* minimum CNV high time to trigger conversion */
  85. #define AD7944_T_CNVH_NS 10
  86. static const struct ad7944_timing_spec ad7944_timing_spec = {
  87. .conv_ns = 420,
  88. .turbo_conv_ns = 320,
  89. };
  90. static const struct ad7944_timing_spec ad7986_timing_spec = {
  91. .conv_ns = 500,
  92. .turbo_conv_ns = 400,
  93. };
  94. struct ad7944_chip_info {
  95. const char *name;
  96. const struct ad7944_timing_spec *timing_spec;
  97. u32 max_sample_rate_hz;
  98. const struct iio_chan_spec channels[2];
  99. const struct iio_chan_spec offload_channels[1];
  100. };
  101. /* get number of bytes for SPI xfer */
  102. #define AD7944_SPI_BYTES(scan_type) ((scan_type).realbits > 16 ? 4 : 2)
  103. /*
  104. * AD7944_DEFINE_CHIP_INFO - Define a chip info structure for a specific chip
  105. * @_name: The name of the chip
  106. * @_ts: The timing specification for the chip
  107. * @_max: The maximum sample rate in Hz
  108. * @_bits: The number of bits in the conversion result
  109. * @_diff: Whether the chip is true differential or not
  110. */
  111. #define AD7944_DEFINE_CHIP_INFO(_name, _ts, _max, _bits, _diff) \
  112. static const struct ad7944_chip_info _name##_chip_info = { \
  113. .name = #_name, \
  114. .timing_spec = &_ts##_timing_spec, \
  115. .max_sample_rate_hz = _max, \
  116. .channels = { \
  117. { \
  118. .type = IIO_VOLTAGE, \
  119. .indexed = 1, \
  120. .differential = _diff, \
  121. .channel = 0, \
  122. .channel2 = _diff ? 1 : 0, \
  123. .scan_index = 0, \
  124. .scan_type.sign = _diff ? 's' : 'u', \
  125. .scan_type.realbits = _bits, \
  126. .scan_type.storagebits = _bits > 16 ? 32 : 16, \
  127. .scan_type.endianness = IIO_CPU, \
  128. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
  129. | BIT(IIO_CHAN_INFO_SCALE), \
  130. }, \
  131. IIO_CHAN_SOFT_TIMESTAMP(1), \
  132. }, \
  133. .offload_channels = { \
  134. { \
  135. .type = IIO_VOLTAGE, \
  136. .indexed = 1, \
  137. .differential = _diff, \
  138. .channel = 0, \
  139. .channel2 = _diff ? 1 : 0, \
  140. .scan_index = 0, \
  141. .scan_type.sign = _diff ? 's' : 'u', \
  142. .scan_type.realbits = _bits, \
  143. .scan_type.storagebits = 32, \
  144. .scan_type.endianness = IIO_CPU, \
  145. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
  146. | BIT(IIO_CHAN_INFO_SCALE) \
  147. | BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  148. .info_mask_separate_available = \
  149. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  150. }, \
  151. }, \
  152. }
  153. /*
  154. * Notes on the offload channels:
  155. * - There is no soft timestamp since everything is done in hardware.
  156. * - There is a sampling frequency attribute added. This controls the SPI
  157. * offload trigger.
  158. * - The storagebits value depends on the SPI offload provider. Currently there
  159. * is only one supported provider, namely the ADI PULSAR ADC HDL project,
  160. * which always uses 32-bit words for data values, even for <= 16-bit ADCs.
  161. * So the value is just hardcoded to 32 for now.
  162. */
  163. /* pseudo-differential with ground sense */
  164. AD7944_DEFINE_CHIP_INFO(ad7944, ad7944, 2.5 * MEGA, 14, 0);
  165. AD7944_DEFINE_CHIP_INFO(ad7985, ad7944, 2.5 * MEGA, 16, 0);
  166. /* fully differential */
  167. AD7944_DEFINE_CHIP_INFO(ad7986, ad7986, 2 * MEGA, 18, 1);
  168. static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
  169. const struct iio_chan_spec *chan)
  170. {
  171. unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns
  172. : adc->timing_spec->conv_ns;
  173. struct spi_transfer *xfers = adc->xfers;
  174. /*
  175. * CS is tied to CNV and we need a low to high transition to start the
  176. * conversion, so place CNV low for t_QUIET to prepare for this.
  177. */
  178. xfers[0].delay.value = AD7944_T_QUIET_NS;
  179. xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
  180. /*
  181. * CS has to be high for full conversion time to avoid triggering the
  182. * busy indication.
  183. */
  184. xfers[1].cs_off = 1;
  185. xfers[1].delay.value = t_conv_ns;
  186. xfers[1].delay.unit = SPI_DELAY_UNIT_NSECS;
  187. /* Then we can read the data during the acquisition phase */
  188. xfers[2].rx_buf = &adc->sample.raw;
  189. xfers[2].len = AD7944_SPI_BYTES(chan->scan_type);
  190. xfers[2].bits_per_word = chan->scan_type.realbits;
  191. spi_message_init_with_transfers(&adc->msg, xfers, 3);
  192. return devm_spi_optimize_message(dev, adc->spi, &adc->msg);
  193. }
  194. static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
  195. const struct iio_chan_spec *chan)
  196. {
  197. unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns
  198. : adc->timing_spec->conv_ns;
  199. struct spi_transfer *xfers = adc->xfers;
  200. /*
  201. * CS has to be high for full conversion time to avoid triggering the
  202. * busy indication.
  203. */
  204. xfers[0].cs_off = 1;
  205. xfers[0].delay.value = t_conv_ns;
  206. xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
  207. xfers[1].rx_buf = &adc->sample.raw;
  208. xfers[1].len = AD7944_SPI_BYTES(chan->scan_type);
  209. xfers[1].bits_per_word = chan->scan_type.realbits;
  210. spi_message_init_with_transfers(&adc->msg, xfers, 2);
  211. return devm_spi_optimize_message(dev, adc->spi, &adc->msg);
  212. }
  213. static int ad7944_chain_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
  214. const struct iio_chan_spec *chan,
  215. u32 n_chain_dev)
  216. {
  217. struct spi_transfer *xfers = adc->xfers;
  218. /*
  219. * NB: SCLK has to be low before we toggle CS to avoid triggering the
  220. * busy indication.
  221. */
  222. if (adc->spi->mode & SPI_CPOL)
  223. return dev_err_probe(dev, -EINVAL,
  224. "chain mode requires ~SPI_CPOL\n");
  225. /*
  226. * We only support CNV connected to CS in chain mode and we need CNV
  227. * to be high during the transfer to trigger the conversion.
  228. */
  229. if (!(adc->spi->mode & SPI_CS_HIGH))
  230. return dev_err_probe(dev, -EINVAL,
  231. "chain mode requires SPI_CS_HIGH\n");
  232. /* CNV has to be high for full conversion time before reading data. */
  233. xfers[0].delay.value = adc->timing_spec->conv_ns;
  234. xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
  235. xfers[1].rx_buf = adc->chain_mode_buf;
  236. xfers[1].len = AD7944_SPI_BYTES(chan->scan_type) * n_chain_dev;
  237. xfers[1].bits_per_word = chan->scan_type.realbits;
  238. spi_message_init_with_transfers(&adc->msg, xfers, 2);
  239. return devm_spi_optimize_message(dev, adc->spi, &adc->msg);
  240. }
  241. /*
  242. * Unlike ad7944_3wire_cs_mode_init_msg(), this creates a message that reads
  243. * during the conversion phase instead of the acquisition phase when reading
  244. * a sample from the ADC. This is needed to be able to read at the maximum
  245. * sample rate. It requires the SPI controller to have offload support and a
  246. * high enough SCLK rate to read the sample during the conversion phase.
  247. */
  248. static int ad7944_3wire_cs_mode_init_offload_msg(struct device *dev,
  249. struct ad7944_adc *adc,
  250. const struct iio_chan_spec *chan)
  251. {
  252. struct spi_transfer *xfers = adc->offload_xfers;
  253. int ret;
  254. /*
  255. * CS is tied to CNV and we need a low to high transition to start the
  256. * conversion, so place CNV low for t_QUIET to prepare for this.
  257. */
  258. xfers[0].delay.value = AD7944_T_QUIET_NS;
  259. xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
  260. /* CNV has to be high for a minimum time to trigger conversion. */
  261. xfers[0].cs_change = 1;
  262. xfers[0].cs_change_delay.value = AD7944_T_CNVH_NS;
  263. xfers[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
  264. /* Then we can read the previous sample during the conversion phase */
  265. xfers[1].offload_flags = SPI_OFFLOAD_XFER_RX_STREAM;
  266. xfers[1].len = AD7944_SPI_BYTES(chan->scan_type);
  267. xfers[1].bits_per_word = chan->scan_type.realbits;
  268. spi_message_init_with_transfers(&adc->offload_msg, xfers,
  269. ARRAY_SIZE(adc->offload_xfers));
  270. adc->offload_msg.offload = adc->offload;
  271. ret = devm_spi_optimize_message(dev, adc->spi, &adc->offload_msg);
  272. if (ret)
  273. return dev_err_probe(dev, ret, "failed to prepare offload msg\n");
  274. return 0;
  275. }
  276. /**
  277. * ad7944_convert_and_acquire - Perform a single conversion and acquisition
  278. * @adc: The ADC device structure
  279. * Return: 0 on success, a negative error code on failure
  280. *
  281. * Perform a conversion and acquisition of a single sample using the
  282. * pre-optimized adc->msg.
  283. *
  284. * Upon successful return adc->sample.raw will contain the conversion result
  285. * (or adc->chain_mode_buf if the device is using chain mode).
  286. */
  287. static int ad7944_convert_and_acquire(struct ad7944_adc *adc)
  288. {
  289. int ret;
  290. /*
  291. * In 4-wire mode, the CNV line is held high for the entire conversion
  292. * and acquisition process. In other modes adc->cnv is NULL and is
  293. * ignored (CS is wired to CNV in those cases).
  294. */
  295. gpiod_set_value_cansleep(adc->cnv, 1);
  296. ret = spi_sync(adc->spi, &adc->msg);
  297. gpiod_set_value_cansleep(adc->cnv, 0);
  298. return ret;
  299. }
  300. static int ad7944_single_conversion(struct ad7944_adc *adc,
  301. const struct iio_chan_spec *chan,
  302. int *val)
  303. {
  304. int ret;
  305. ret = ad7944_convert_and_acquire(adc);
  306. if (ret)
  307. return ret;
  308. if (adc->spi_mode == AD7944_SPI_MODE_CHAIN) {
  309. if (chan->scan_type.realbits > 16)
  310. *val = ((u32 *)adc->chain_mode_buf)[chan->scan_index];
  311. else
  312. *val = ((u16 *)adc->chain_mode_buf)[chan->scan_index];
  313. } else {
  314. if (chan->scan_type.realbits > 16)
  315. *val = adc->sample.raw.u32;
  316. else
  317. *val = adc->sample.raw.u16;
  318. }
  319. if (chan->scan_type.sign == 's')
  320. *val = sign_extend32(*val, chan->scan_type.realbits - 1);
  321. else
  322. *val &= GENMASK(chan->scan_type.realbits - 1, 0);
  323. return IIO_VAL_INT;
  324. }
  325. static int ad7944_read_avail(struct iio_dev *indio_dev,
  326. struct iio_chan_spec const *chan,
  327. const int **vals, int *type, int *length,
  328. long mask)
  329. {
  330. struct ad7944_adc *adc = iio_priv(indio_dev);
  331. switch (mask) {
  332. case IIO_CHAN_INFO_SAMP_FREQ:
  333. *vals = adc->sample_freq_range;
  334. *type = IIO_VAL_INT;
  335. return IIO_AVAIL_RANGE;
  336. default:
  337. return -EINVAL;
  338. }
  339. }
  340. static int ad7944_read_raw(struct iio_dev *indio_dev,
  341. const struct iio_chan_spec *chan,
  342. int *val, int *val2, long info)
  343. {
  344. struct ad7944_adc *adc = iio_priv(indio_dev);
  345. int ret;
  346. switch (info) {
  347. case IIO_CHAN_INFO_RAW:
  348. if (!iio_device_claim_direct(indio_dev))
  349. return -EBUSY;
  350. ret = ad7944_single_conversion(adc, chan, val);
  351. iio_device_release_direct(indio_dev);
  352. return ret;
  353. case IIO_CHAN_INFO_SCALE:
  354. switch (chan->type) {
  355. case IIO_VOLTAGE:
  356. *val = adc->ref_mv;
  357. if (chan->scan_type.sign == 's')
  358. *val2 = chan->scan_type.realbits - 1;
  359. else
  360. *val2 = chan->scan_type.realbits;
  361. return IIO_VAL_FRACTIONAL_LOG2;
  362. default:
  363. return -EINVAL;
  364. }
  365. case IIO_CHAN_INFO_SAMP_FREQ:
  366. *val = adc->offload_trigger_hz;
  367. return IIO_VAL_INT;
  368. default:
  369. return -EINVAL;
  370. }
  371. }
  372. static int ad7944_set_sample_freq(struct ad7944_adc *adc, int val)
  373. {
  374. struct spi_offload_trigger_config config = {
  375. .type = SPI_OFFLOAD_TRIGGER_PERIODIC,
  376. .periodic = {
  377. .frequency_hz = val,
  378. },
  379. };
  380. int ret;
  381. ret = spi_offload_trigger_validate(adc->offload_trigger, &config);
  382. if (ret)
  383. return ret;
  384. adc->offload_trigger_hz = config.periodic.frequency_hz;
  385. return 0;
  386. }
  387. static int ad7944_write_raw(struct iio_dev *indio_dev,
  388. const struct iio_chan_spec *chan,
  389. int val, int val2, long info)
  390. {
  391. struct ad7944_adc *adc = iio_priv(indio_dev);
  392. switch (info) {
  393. case IIO_CHAN_INFO_SAMP_FREQ:
  394. if (val < 1 || val > adc->sample_freq_range[2])
  395. return -EINVAL;
  396. return ad7944_set_sample_freq(adc, val);
  397. default:
  398. return -EINVAL;
  399. }
  400. }
  401. static int ad7944_write_raw_get_fmt(struct iio_dev *indio_dev,
  402. const struct iio_chan_spec *chan,
  403. long mask)
  404. {
  405. switch (mask) {
  406. case IIO_CHAN_INFO_SAMP_FREQ:
  407. return IIO_VAL_INT;
  408. default:
  409. return IIO_VAL_INT_PLUS_MICRO;
  410. }
  411. }
  412. static const struct iio_info ad7944_iio_info = {
  413. .read_avail = &ad7944_read_avail,
  414. .read_raw = &ad7944_read_raw,
  415. .write_raw = &ad7944_write_raw,
  416. .write_raw_get_fmt = &ad7944_write_raw_get_fmt,
  417. };
  418. static int ad7944_offload_buffer_postenable(struct iio_dev *indio_dev)
  419. {
  420. struct ad7944_adc *adc = iio_priv(indio_dev);
  421. struct spi_offload_trigger_config config = {
  422. .type = SPI_OFFLOAD_TRIGGER_PERIODIC,
  423. .periodic = {
  424. .frequency_hz = adc->offload_trigger_hz,
  425. },
  426. };
  427. int ret;
  428. gpiod_set_value_cansleep(adc->turbo, 1);
  429. ret = spi_offload_trigger_enable(adc->offload, adc->offload_trigger,
  430. &config);
  431. if (ret)
  432. gpiod_set_value_cansleep(adc->turbo, 0);
  433. return ret;
  434. }
  435. static int ad7944_offload_buffer_predisable(struct iio_dev *indio_dev)
  436. {
  437. struct ad7944_adc *adc = iio_priv(indio_dev);
  438. spi_offload_trigger_disable(adc->offload, adc->offload_trigger);
  439. gpiod_set_value_cansleep(adc->turbo, 0);
  440. return 0;
  441. }
  442. static const struct iio_buffer_setup_ops ad7944_offload_buffer_setup_ops = {
  443. .postenable = &ad7944_offload_buffer_postenable,
  444. .predisable = &ad7944_offload_buffer_predisable,
  445. };
  446. static irqreturn_t ad7944_trigger_handler(int irq, void *p)
  447. {
  448. struct iio_poll_func *pf = p;
  449. struct iio_dev *indio_dev = pf->indio_dev;
  450. struct ad7944_adc *adc = iio_priv(indio_dev);
  451. int ret;
  452. ret = ad7944_convert_and_acquire(adc);
  453. if (ret)
  454. goto out;
  455. if (adc->spi_mode == AD7944_SPI_MODE_CHAIN)
  456. iio_push_to_buffers_with_timestamp(indio_dev, adc->chain_mode_buf,
  457. pf->timestamp);
  458. else
  459. iio_push_to_buffers_with_timestamp(indio_dev, &adc->sample.raw,
  460. pf->timestamp);
  461. out:
  462. iio_trigger_notify_done(indio_dev->trig);
  463. return IRQ_HANDLED;
  464. }
  465. /**
  466. * ad7944_chain_mode_alloc - allocate and initialize channel specs and buffers
  467. * for daisy-chained devices
  468. * @dev: The device for devm_ functions
  469. * @chan_template: The channel template for the devices (array of 2 channels
  470. * voltage and timestamp)
  471. * @n_chain_dev: The number of devices in the chain
  472. * @chain_chan: Pointer to receive the allocated channel specs
  473. * @chain_mode_buf: Pointer to receive the allocated rx buffer
  474. * @chain_scan_masks: Pointer to receive the allocated scan masks
  475. * Return: 0 on success, a negative error code on failure
  476. */
  477. static int ad7944_chain_mode_alloc(struct device *dev,
  478. const struct iio_chan_spec *chan_template,
  479. u32 n_chain_dev,
  480. struct iio_chan_spec **chain_chan,
  481. void **chain_mode_buf,
  482. unsigned long **chain_scan_masks)
  483. {
  484. struct iio_chan_spec *chan;
  485. size_t chain_mode_buf_size;
  486. unsigned long *scan_masks;
  487. void *buf;
  488. int i;
  489. /* 1 channel for each device in chain plus 1 for soft timestamp */
  490. chan = devm_kcalloc(dev, n_chain_dev + 1, sizeof(*chan), GFP_KERNEL);
  491. if (!chan)
  492. return -ENOMEM;
  493. for (i = 0; i < n_chain_dev; i++) {
  494. chan[i] = chan_template[0];
  495. if (chan_template[0].differential) {
  496. chan[i].channel = 2 * i;
  497. chan[i].channel2 = 2 * i + 1;
  498. } else {
  499. chan[i].channel = i;
  500. }
  501. chan[i].scan_index = i;
  502. }
  503. /* soft timestamp */
  504. chan[i] = chan_template[1];
  505. chan[i].scan_index = i;
  506. *chain_chan = chan;
  507. /* 1 word for each voltage channel + aligned u64 for timestamp */
  508. chain_mode_buf_size = ALIGN(n_chain_dev *
  509. AD7944_SPI_BYTES(chan[0].scan_type), sizeof(u64)) + sizeof(u64);
  510. buf = devm_kzalloc(dev, chain_mode_buf_size, GFP_KERNEL);
  511. if (!buf)
  512. return -ENOMEM;
  513. *chain_mode_buf = buf;
  514. /*
  515. * Have to limit n_chain_dev due to current implementation of
  516. * available_scan_masks.
  517. */
  518. if (n_chain_dev > BITS_PER_LONG)
  519. return dev_err_probe(dev, -EINVAL,
  520. "chain is limited to 32 devices\n");
  521. scan_masks = devm_kcalloc(dev, 2, sizeof(*scan_masks), GFP_KERNEL);
  522. if (!scan_masks)
  523. return -ENOMEM;
  524. /*
  525. * Scan mask is needed since we always have to read all devices in the
  526. * chain in one SPI transfer.
  527. */
  528. scan_masks[0] = GENMASK(n_chain_dev - 1, 0);
  529. *chain_scan_masks = scan_masks;
  530. return 0;
  531. }
  532. static const char * const ad7944_power_supplies[] = {
  533. "avdd", "dvdd", "bvdd", "vio"
  534. };
  535. static const struct spi_offload_config ad7944_offload_config = {
  536. .capability_flags = SPI_OFFLOAD_CAP_TRIGGER |
  537. SPI_OFFLOAD_CAP_RX_STREAM_DMA,
  538. };
  539. static int ad7944_probe(struct spi_device *spi)
  540. {
  541. const struct ad7944_chip_info *chip_info;
  542. struct device *dev = &spi->dev;
  543. struct iio_dev *indio_dev;
  544. struct ad7944_adc *adc;
  545. bool have_refin;
  546. struct iio_chan_spec *chain_chan;
  547. unsigned long *chain_scan_masks;
  548. u32 n_chain_dev;
  549. int ret, ref_mv;
  550. indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
  551. if (!indio_dev)
  552. return -ENOMEM;
  553. adc = iio_priv(indio_dev);
  554. adc->spi = spi;
  555. chip_info = spi_get_device_match_data(spi);
  556. if (!chip_info)
  557. return dev_err_probe(dev, -EINVAL, "no chip info\n");
  558. adc->timing_spec = chip_info->timing_spec;
  559. adc->sample_freq_range[0] = 1; /* min */
  560. adc->sample_freq_range[1] = 1; /* step */
  561. adc->sample_freq_range[2] = chip_info->max_sample_rate_hz; /* max */
  562. ret = device_property_match_property_string(dev, "adi,spi-mode",
  563. ad7944_spi_modes,
  564. ARRAY_SIZE(ad7944_spi_modes));
  565. /* absence of adi,spi-mode property means default mode */
  566. if (ret == -EINVAL)
  567. adc->spi_mode = AD7944_SPI_MODE_DEFAULT;
  568. else if (ret < 0)
  569. return dev_err_probe(dev, ret,
  570. "getting adi,spi-mode property failed\n");
  571. else
  572. adc->spi_mode = ret;
  573. /*
  574. * Some chips use unusual word sizes, so check now instead of waiting
  575. * for the first xfer.
  576. */
  577. if (!spi_is_bpw_supported(spi, chip_info->channels[0].scan_type.realbits))
  578. return dev_err_probe(dev, -EINVAL,
  579. "SPI host does not support %d bits per word\n",
  580. chip_info->channels[0].scan_type.realbits);
  581. ret = devm_regulator_bulk_get_enable(dev,
  582. ARRAY_SIZE(ad7944_power_supplies),
  583. ad7944_power_supplies);
  584. if (ret)
  585. return dev_err_probe(dev, ret,
  586. "failed to get and enable supplies\n");
  587. /*
  588. * Sort out what is being used for the reference voltage. Options are:
  589. * - internal reference: neither REF or REFIN is connected
  590. * - internal reference with external buffer: REF not connected, REFIN
  591. * is connected
  592. * - external reference: REF is connected, REFIN is not connected
  593. */
  594. ret = devm_regulator_get_enable_read_voltage(dev, "ref");
  595. if (ret < 0 && ret != -ENODEV)
  596. return dev_err_probe(dev, ret, "failed to get REF voltage\n");
  597. ref_mv = ret == -ENODEV ? 0 : ret / 1000;
  598. ret = devm_regulator_get_enable_optional(dev, "refin");
  599. if (ret < 0 && ret != -ENODEV)
  600. return dev_err_probe(dev, ret, "failed to get REFIN voltage\n");
  601. have_refin = ret != -ENODEV;
  602. if (have_refin && ref_mv)
  603. return dev_err_probe(dev, -EINVAL,
  604. "cannot have both refin and ref supplies\n");
  605. adc->ref_mv = ref_mv ?: AD7944_INTERNAL_REF_MV;
  606. adc->cnv = devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_LOW);
  607. if (IS_ERR(adc->cnv))
  608. return dev_err_probe(dev, PTR_ERR(adc->cnv),
  609. "failed to get CNV GPIO\n");
  610. if (!adc->cnv && adc->spi_mode == AD7944_SPI_MODE_DEFAULT)
  611. return dev_err_probe(&spi->dev, -EINVAL, "CNV GPIO is required\n");
  612. if (adc->cnv && adc->spi_mode != AD7944_SPI_MODE_DEFAULT)
  613. return dev_err_probe(&spi->dev, -EINVAL,
  614. "CNV GPIO in single and chain mode is not currently supported\n");
  615. adc->turbo = devm_gpiod_get_optional(dev, "turbo", GPIOD_OUT_LOW);
  616. if (IS_ERR(adc->turbo))
  617. return dev_err_probe(dev, PTR_ERR(adc->turbo),
  618. "failed to get TURBO GPIO\n");
  619. adc->always_turbo = device_property_present(dev, "adi,always-turbo");
  620. if (adc->turbo && adc->always_turbo)
  621. return dev_err_probe(dev, -EINVAL,
  622. "cannot have both turbo-gpios and adi,always-turbo\n");
  623. if (adc->spi_mode == AD7944_SPI_MODE_CHAIN && adc->always_turbo)
  624. return dev_err_probe(dev, -EINVAL,
  625. "cannot have both chain mode and always turbo\n");
  626. switch (adc->spi_mode) {
  627. case AD7944_SPI_MODE_DEFAULT:
  628. ret = ad7944_4wire_mode_init_msg(dev, adc, &chip_info->channels[0]);
  629. if (ret)
  630. return ret;
  631. break;
  632. case AD7944_SPI_MODE_SINGLE:
  633. ret = ad7944_3wire_cs_mode_init_msg(dev, adc, &chip_info->channels[0]);
  634. if (ret)
  635. return ret;
  636. break;
  637. case AD7944_SPI_MODE_CHAIN:
  638. ret = device_property_read_u32(dev, "#daisy-chained-devices",
  639. &n_chain_dev);
  640. if (ret)
  641. return dev_err_probe(dev, ret,
  642. "failed to get #daisy-chained-devices\n");
  643. ret = ad7944_chain_mode_alloc(dev, chip_info->channels,
  644. n_chain_dev, &chain_chan,
  645. &adc->chain_mode_buf,
  646. &chain_scan_masks);
  647. if (ret)
  648. return ret;
  649. ret = ad7944_chain_mode_init_msg(dev, adc, &chain_chan[0],
  650. n_chain_dev);
  651. if (ret)
  652. return ret;
  653. break;
  654. }
  655. indio_dev->name = chip_info->name;
  656. indio_dev->modes = INDIO_DIRECT_MODE;
  657. indio_dev->info = &ad7944_iio_info;
  658. adc->offload = devm_spi_offload_get(dev, spi, &ad7944_offload_config);
  659. ret = PTR_ERR_OR_ZERO(adc->offload);
  660. if (ret && ret != -ENODEV)
  661. return dev_err_probe(dev, ret, "failed to get offload\n");
  662. /* Fall back to low speed usage when no SPI offload available. */
  663. if (ret == -ENODEV) {
  664. if (adc->spi_mode == AD7944_SPI_MODE_CHAIN) {
  665. indio_dev->available_scan_masks = chain_scan_masks;
  666. indio_dev->channels = chain_chan;
  667. indio_dev->num_channels = n_chain_dev + 1;
  668. } else {
  669. indio_dev->channels = chip_info->channels;
  670. indio_dev->num_channels = ARRAY_SIZE(chip_info->channels);
  671. }
  672. ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
  673. iio_pollfunc_store_time,
  674. ad7944_trigger_handler,
  675. NULL);
  676. if (ret)
  677. return ret;
  678. } else {
  679. struct dma_chan *rx_dma;
  680. if (adc->spi_mode != AD7944_SPI_MODE_SINGLE)
  681. return dev_err_probe(dev, -EINVAL,
  682. "offload only supported in single mode\n");
  683. indio_dev->setup_ops = &ad7944_offload_buffer_setup_ops;
  684. indio_dev->channels = chip_info->offload_channels;
  685. indio_dev->num_channels = ARRAY_SIZE(chip_info->offload_channels);
  686. adc->offload_trigger = devm_spi_offload_trigger_get(dev,
  687. adc->offload, SPI_OFFLOAD_TRIGGER_PERIODIC);
  688. if (IS_ERR(adc->offload_trigger))
  689. return dev_err_probe(dev, PTR_ERR(adc->offload_trigger),
  690. "failed to get offload trigger\n");
  691. ret = ad7944_set_sample_freq(adc, 2 * MEGA);
  692. if (ret)
  693. return dev_err_probe(dev, ret,
  694. "failed to init sample rate\n");
  695. rx_dma = devm_spi_offload_rx_stream_request_dma_chan(dev,
  696. adc->offload);
  697. if (IS_ERR(rx_dma))
  698. return dev_err_probe(dev, PTR_ERR(rx_dma),
  699. "failed to get offload RX DMA\n");
  700. /*
  701. * REVISIT: ideally, we would confirm that the offload RX DMA
  702. * buffer layout is the same as what is hard-coded in
  703. * offload_channels. Right now, the only supported offload
  704. * is the pulsar_adc project which always uses 32-bit word
  705. * size for data values, regardless of the SPI bits per word.
  706. */
  707. ret = devm_iio_dmaengine_buffer_setup_with_handle(dev,
  708. indio_dev, rx_dma, IIO_BUFFER_DIRECTION_IN);
  709. if (ret)
  710. return ret;
  711. ret = ad7944_3wire_cs_mode_init_offload_msg(dev, adc,
  712. &chip_info->offload_channels[0]);
  713. if (ret)
  714. return ret;
  715. }
  716. return devm_iio_device_register(dev, indio_dev);
  717. }
  718. static const struct of_device_id ad7944_of_match[] = {
  719. { .compatible = "adi,ad7944", .data = &ad7944_chip_info },
  720. { .compatible = "adi,ad7985", .data = &ad7985_chip_info },
  721. { .compatible = "adi,ad7986", .data = &ad7986_chip_info },
  722. { }
  723. };
  724. MODULE_DEVICE_TABLE(of, ad7944_of_match);
  725. static const struct spi_device_id ad7944_spi_id[] = {
  726. { "ad7944", (kernel_ulong_t)&ad7944_chip_info },
  727. { "ad7985", (kernel_ulong_t)&ad7985_chip_info },
  728. { "ad7986", (kernel_ulong_t)&ad7986_chip_info },
  729. { }
  730. };
  731. MODULE_DEVICE_TABLE(spi, ad7944_spi_id);
  732. static struct spi_driver ad7944_driver = {
  733. .driver = {
  734. .name = "ad7944",
  735. .of_match_table = ad7944_of_match,
  736. },
  737. .probe = ad7944_probe,
  738. .id_table = ad7944_spi_id,
  739. };
  740. module_spi_driver(ad7944_driver);
  741. MODULE_AUTHOR("David Lechner <dlechner@baylibre.com>");
  742. MODULE_DESCRIPTION("Analog Devices AD7944 PulSAR ADC family driver");
  743. MODULE_LICENSE("GPL");
  744. MODULE_IMPORT_NS("IIO_DMAENGINE_BUFFER");