ad4000.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * AD4000 SPI ADC driver
  4. *
  5. * Copyright 2024 Analog Devices Inc.
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/byteorder/generic.h>
  10. #include <linux/cleanup.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/math.h>
  14. #include <linux/module.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/units.h>
  20. #include <linux/util_macros.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/buffer.h>
  23. #include <linux/iio/triggered_buffer.h>
  24. #include <linux/iio/trigger_consumer.h>
  25. #define AD4000_READ_COMMAND 0x54
  26. #define AD4000_WRITE_COMMAND 0x14
  27. #define AD4000_CONFIG_REG_DEFAULT 0xE1
  28. /* AD4000 Configuration Register programmable bits */
  29. #define AD4000_CFG_SPAN_COMP BIT(3) /* Input span compression */
  30. #define AD4000_CFG_HIGHZ BIT(2) /* High impedance mode */
  31. #define AD4000_SCALE_OPTIONS 2
  32. #define AD4000_TQUIET1_NS 190
  33. #define AD4000_TQUIET2_NS 60
  34. #define AD4000_TCONV_NS 320
  35. #define __AD4000_DIFF_CHANNEL(_sign, _real_bits, _storage_bits, _reg_access) \
  36. { \
  37. .type = IIO_VOLTAGE, \
  38. .indexed = 1, \
  39. .differential = 1, \
  40. .channel = 0, \
  41. .channel2 = 1, \
  42. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  43. BIT(IIO_CHAN_INFO_SCALE), \
  44. .info_mask_separate_available = _reg_access ? BIT(IIO_CHAN_INFO_SCALE) : 0,\
  45. .scan_type = { \
  46. .sign = _sign, \
  47. .realbits = _real_bits, \
  48. .storagebits = _storage_bits, \
  49. .shift = _storage_bits - _real_bits, \
  50. .endianness = IIO_BE, \
  51. }, \
  52. }
  53. #define AD4000_DIFF_CHANNEL(_sign, _real_bits, _reg_access) \
  54. __AD4000_DIFF_CHANNEL((_sign), (_real_bits), \
  55. ((_real_bits) > 16 ? 32 : 16), (_reg_access))
  56. #define __AD4000_PSEUDO_DIFF_CHANNEL(_sign, _real_bits, _storage_bits, _reg_access)\
  57. { \
  58. .type = IIO_VOLTAGE, \
  59. .indexed = 1, \
  60. .channel = 0, \
  61. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  62. BIT(IIO_CHAN_INFO_SCALE) | \
  63. BIT(IIO_CHAN_INFO_OFFSET), \
  64. .info_mask_separate_available = _reg_access ? BIT(IIO_CHAN_INFO_SCALE) : 0,\
  65. .scan_type = { \
  66. .sign = _sign, \
  67. .realbits = _real_bits, \
  68. .storagebits = _storage_bits, \
  69. .shift = _storage_bits - _real_bits, \
  70. .endianness = IIO_BE, \
  71. }, \
  72. }
  73. #define AD4000_PSEUDO_DIFF_CHANNEL(_sign, _real_bits, _reg_access) \
  74. __AD4000_PSEUDO_DIFF_CHANNEL((_sign), (_real_bits), \
  75. ((_real_bits) > 16 ? 32 : 16), (_reg_access))
  76. static const char * const ad4000_power_supplies[] = {
  77. "vdd", "vio"
  78. };
  79. enum ad4000_sdi {
  80. AD4000_SDI_MOSI,
  81. AD4000_SDI_VIO,
  82. AD4000_SDI_CS,
  83. AD4000_SDI_GND,
  84. };
  85. /* maps adi,sdi-pin property value to enum */
  86. static const char * const ad4000_sdi_pin[] = {
  87. [AD4000_SDI_MOSI] = "sdi",
  88. [AD4000_SDI_VIO] = "high",
  89. [AD4000_SDI_CS] = "cs",
  90. [AD4000_SDI_GND] = "low",
  91. };
  92. /* Gains stored as fractions of 1000 so they can be expressed by integers. */
  93. static const int ad4000_gains[] = {
  94. 454, 909, 1000, 1900,
  95. };
  96. struct ad4000_chip_info {
  97. const char *dev_name;
  98. struct iio_chan_spec chan_spec;
  99. struct iio_chan_spec reg_access_chan_spec;
  100. bool has_hardware_gain;
  101. };
  102. static const struct ad4000_chip_info ad4000_chip_info = {
  103. .dev_name = "ad4000",
  104. .chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0),
  105. .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 1),
  106. };
  107. static const struct ad4000_chip_info ad4001_chip_info = {
  108. .dev_name = "ad4001",
  109. .chan_spec = AD4000_DIFF_CHANNEL('s', 16, 0),
  110. .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 1),
  111. };
  112. static const struct ad4000_chip_info ad4002_chip_info = {
  113. .dev_name = "ad4002",
  114. .chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 0),
  115. .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 1),
  116. };
  117. static const struct ad4000_chip_info ad4003_chip_info = {
  118. .dev_name = "ad4003",
  119. .chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0),
  120. .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 1),
  121. };
  122. static const struct ad4000_chip_info ad4004_chip_info = {
  123. .dev_name = "ad4004",
  124. .chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0),
  125. .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 1),
  126. };
  127. static const struct ad4000_chip_info ad4005_chip_info = {
  128. .dev_name = "ad4005",
  129. .chan_spec = AD4000_DIFF_CHANNEL('s', 16, 0),
  130. .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 1),
  131. };
  132. static const struct ad4000_chip_info ad4006_chip_info = {
  133. .dev_name = "ad4006",
  134. .chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 0),
  135. .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 1),
  136. };
  137. static const struct ad4000_chip_info ad4007_chip_info = {
  138. .dev_name = "ad4007",
  139. .chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0),
  140. .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 1),
  141. };
  142. static const struct ad4000_chip_info ad4008_chip_info = {
  143. .dev_name = "ad4008",
  144. .chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0),
  145. .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 1),
  146. };
  147. static const struct ad4000_chip_info ad4010_chip_info = {
  148. .dev_name = "ad4010",
  149. .chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 0),
  150. .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 1),
  151. };
  152. static const struct ad4000_chip_info ad4011_chip_info = {
  153. .dev_name = "ad4011",
  154. .chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0),
  155. .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 1),
  156. };
  157. static const struct ad4000_chip_info ad4020_chip_info = {
  158. .dev_name = "ad4020",
  159. .chan_spec = AD4000_DIFF_CHANNEL('s', 20, 0),
  160. .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 20, 1),
  161. };
  162. static const struct ad4000_chip_info ad4021_chip_info = {
  163. .dev_name = "ad4021",
  164. .chan_spec = AD4000_DIFF_CHANNEL('s', 20, 0),
  165. .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 20, 1),
  166. };
  167. static const struct ad4000_chip_info ad4022_chip_info = {
  168. .dev_name = "ad4022",
  169. .chan_spec = AD4000_DIFF_CHANNEL('s', 20, 0),
  170. .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 20, 1),
  171. };
  172. static const struct ad4000_chip_info adaq4001_chip_info = {
  173. .dev_name = "adaq4001",
  174. .chan_spec = AD4000_DIFF_CHANNEL('s', 16, 0),
  175. .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 1),
  176. .has_hardware_gain = true,
  177. };
  178. static const struct ad4000_chip_info adaq4003_chip_info = {
  179. .dev_name = "adaq4003",
  180. .chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0),
  181. .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 1),
  182. .has_hardware_gain = true,
  183. };
  184. struct ad4000_state {
  185. struct spi_device *spi;
  186. struct gpio_desc *cnv_gpio;
  187. struct spi_transfer xfers[2];
  188. struct spi_message msg;
  189. struct mutex lock; /* Protect read modify write cycle */
  190. int vref_mv;
  191. enum ad4000_sdi sdi_pin;
  192. bool span_comp;
  193. u16 gain_milli;
  194. int scale_tbl[AD4000_SCALE_OPTIONS][2];
  195. /*
  196. * DMA (thus cache coherency maintenance) requires the transfer buffers
  197. * to live in their own cache lines.
  198. */
  199. struct {
  200. union {
  201. __be16 sample_buf16;
  202. __be32 sample_buf32;
  203. } data;
  204. s64 timestamp __aligned(8);
  205. } scan __aligned(IIO_DMA_MINALIGN);
  206. u8 tx_buf[2];
  207. u8 rx_buf[2];
  208. };
  209. static void ad4000_fill_scale_tbl(struct ad4000_state *st,
  210. struct iio_chan_spec const *chan)
  211. {
  212. int val, tmp0, tmp1;
  213. int scale_bits;
  214. u64 tmp2;
  215. /*
  216. * ADCs that output two's complement code have one less bit to express
  217. * voltage magnitude.
  218. */
  219. if (chan->scan_type.sign == 's')
  220. scale_bits = chan->scan_type.realbits - 1;
  221. else
  222. scale_bits = chan->scan_type.realbits;
  223. /*
  224. * The gain is stored as a fraction of 1000 and, as we need to
  225. * divide vref_mv by the gain, we invert the gain/1000 fraction.
  226. * Also multiply by an extra MILLI to preserve precision.
  227. * Thus, we have MILLI * MILLI equals MICRO as fraction numerator.
  228. */
  229. val = mult_frac(st->vref_mv, MICRO, st->gain_milli);
  230. /* Would multiply by NANO here but we multiplied by extra MILLI */
  231. tmp2 = shift_right((u64)val * MICRO, scale_bits);
  232. tmp0 = div_s64_rem(tmp2, NANO, &tmp1);
  233. /* Store scale for when span compression is disabled */
  234. st->scale_tbl[0][0] = tmp0; /* Integer part */
  235. st->scale_tbl[0][1] = abs(tmp1); /* Fractional part */
  236. /* Store scale for when span compression is enabled */
  237. st->scale_tbl[1][0] = tmp0;
  238. /* The integer part is always zero so don't bother to divide it. */
  239. if (chan->differential)
  240. st->scale_tbl[1][1] = DIV_ROUND_CLOSEST(abs(tmp1) * 4, 5);
  241. else
  242. st->scale_tbl[1][1] = DIV_ROUND_CLOSEST(abs(tmp1) * 9, 10);
  243. }
  244. static int ad4000_write_reg(struct ad4000_state *st, uint8_t val)
  245. {
  246. st->tx_buf[0] = AD4000_WRITE_COMMAND;
  247. st->tx_buf[1] = val;
  248. return spi_write(st->spi, st->tx_buf, ARRAY_SIZE(st->tx_buf));
  249. }
  250. static int ad4000_read_reg(struct ad4000_state *st, unsigned int *val)
  251. {
  252. struct spi_transfer t = {
  253. .tx_buf = st->tx_buf,
  254. .rx_buf = st->rx_buf,
  255. .len = 2,
  256. };
  257. int ret;
  258. st->tx_buf[0] = AD4000_READ_COMMAND;
  259. ret = spi_sync_transfer(st->spi, &t, 1);
  260. if (ret < 0)
  261. return ret;
  262. *val = st->rx_buf[1];
  263. return ret;
  264. }
  265. static int ad4000_convert_and_acquire(struct ad4000_state *st)
  266. {
  267. int ret;
  268. /*
  269. * In 4-wire mode, the CNV line is held high for the entire conversion
  270. * and acquisition process. In other modes, the CNV GPIO is optional
  271. * and, if provided, replaces controller CS. If CNV GPIO is not defined
  272. * gpiod_set_value_cansleep() has no effect.
  273. */
  274. gpiod_set_value_cansleep(st->cnv_gpio, 1);
  275. ret = spi_sync(st->spi, &st->msg);
  276. gpiod_set_value_cansleep(st->cnv_gpio, 0);
  277. return ret;
  278. }
  279. static int ad4000_single_conversion(struct iio_dev *indio_dev,
  280. const struct iio_chan_spec *chan, int *val)
  281. {
  282. struct ad4000_state *st = iio_priv(indio_dev);
  283. u32 sample;
  284. int ret;
  285. ret = ad4000_convert_and_acquire(st);
  286. if (ret < 0)
  287. return ret;
  288. if (chan->scan_type.storagebits > 16)
  289. sample = be32_to_cpu(st->scan.data.sample_buf32);
  290. else
  291. sample = be16_to_cpu(st->scan.data.sample_buf16);
  292. sample >>= chan->scan_type.shift;
  293. if (chan->scan_type.sign == 's')
  294. *val = sign_extend32(sample, chan->scan_type.realbits - 1);
  295. else
  296. *val = sample;
  297. return IIO_VAL_INT;
  298. }
  299. static int ad4000_read_raw(struct iio_dev *indio_dev,
  300. struct iio_chan_spec const *chan, int *val,
  301. int *val2, long info)
  302. {
  303. struct ad4000_state *st = iio_priv(indio_dev);
  304. switch (info) {
  305. case IIO_CHAN_INFO_RAW:
  306. iio_device_claim_direct_scoped(return -EBUSY, indio_dev)
  307. return ad4000_single_conversion(indio_dev, chan, val);
  308. unreachable();
  309. case IIO_CHAN_INFO_SCALE:
  310. *val = st->scale_tbl[st->span_comp][0];
  311. *val2 = st->scale_tbl[st->span_comp][1];
  312. return IIO_VAL_INT_PLUS_NANO;
  313. case IIO_CHAN_INFO_OFFSET:
  314. *val = 0;
  315. if (st->span_comp)
  316. *val = mult_frac(st->vref_mv, 1, 10);
  317. return IIO_VAL_INT;
  318. default:
  319. return -EINVAL;
  320. }
  321. }
  322. static int ad4000_read_avail(struct iio_dev *indio_dev,
  323. struct iio_chan_spec const *chan,
  324. const int **vals, int *type, int *length,
  325. long info)
  326. {
  327. struct ad4000_state *st = iio_priv(indio_dev);
  328. switch (info) {
  329. case IIO_CHAN_INFO_SCALE:
  330. *vals = (int *)st->scale_tbl;
  331. *length = AD4000_SCALE_OPTIONS * 2;
  332. *type = IIO_VAL_INT_PLUS_NANO;
  333. return IIO_AVAIL_LIST;
  334. default:
  335. return -EINVAL;
  336. }
  337. }
  338. static int ad4000_write_raw_get_fmt(struct iio_dev *indio_dev,
  339. struct iio_chan_spec const *chan, long mask)
  340. {
  341. switch (mask) {
  342. case IIO_CHAN_INFO_SCALE:
  343. return IIO_VAL_INT_PLUS_NANO;
  344. default:
  345. return IIO_VAL_INT_PLUS_MICRO;
  346. }
  347. }
  348. static int ad4000_write_raw(struct iio_dev *indio_dev,
  349. struct iio_chan_spec const *chan, int val, int val2,
  350. long mask)
  351. {
  352. struct ad4000_state *st = iio_priv(indio_dev);
  353. unsigned int reg_val;
  354. bool span_comp_en;
  355. int ret;
  356. switch (mask) {
  357. case IIO_CHAN_INFO_SCALE:
  358. iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
  359. guard(mutex)(&st->lock);
  360. ret = ad4000_read_reg(st, &reg_val);
  361. if (ret < 0)
  362. return ret;
  363. span_comp_en = val2 == st->scale_tbl[1][1];
  364. reg_val &= ~AD4000_CFG_SPAN_COMP;
  365. reg_val |= FIELD_PREP(AD4000_CFG_SPAN_COMP, span_comp_en);
  366. ret = ad4000_write_reg(st, reg_val);
  367. if (ret < 0)
  368. return ret;
  369. st->span_comp = span_comp_en;
  370. return 0;
  371. }
  372. unreachable();
  373. default:
  374. return -EINVAL;
  375. }
  376. }
  377. static irqreturn_t ad4000_trigger_handler(int irq, void *p)
  378. {
  379. struct iio_poll_func *pf = p;
  380. struct iio_dev *indio_dev = pf->indio_dev;
  381. struct ad4000_state *st = iio_priv(indio_dev);
  382. int ret;
  383. ret = ad4000_convert_and_acquire(st);
  384. if (ret < 0)
  385. goto err_out;
  386. iio_push_to_buffers_with_timestamp(indio_dev, &st->scan, pf->timestamp);
  387. err_out:
  388. iio_trigger_notify_done(indio_dev->trig);
  389. return IRQ_HANDLED;
  390. }
  391. static const struct iio_info ad4000_reg_access_info = {
  392. .read_raw = &ad4000_read_raw,
  393. .read_avail = &ad4000_read_avail,
  394. .write_raw = &ad4000_write_raw,
  395. .write_raw_get_fmt = &ad4000_write_raw_get_fmt,
  396. };
  397. static const struct iio_info ad4000_info = {
  398. .read_raw = &ad4000_read_raw,
  399. };
  400. /*
  401. * This executes a data sample transfer for when the device connections are
  402. * in "3-wire" mode, selected when the adi,sdi-pin device tree property is
  403. * absent or set to "high". In this connection mode, the ADC SDI pin is
  404. * connected to MOSI or to VIO and ADC CNV pin is connected either to a SPI
  405. * controller CS or to a GPIO.
  406. * AD4000 series of devices initiate conversions on the rising edge of CNV pin.
  407. *
  408. * If the CNV pin is connected to an SPI controller CS line (which is by default
  409. * active low), the ADC readings would have a latency (delay) of one read.
  410. * Moreover, since we also do ADC sampling for filling the buffer on triggered
  411. * buffer mode, the timestamps of buffer readings would be disarranged.
  412. * To prevent the read latency and reduce the time discrepancy between the
  413. * sample read request and the time of actual sampling by the ADC, do a
  414. * preparatory transfer to pulse the CS/CNV line.
  415. */
  416. static int ad4000_prepare_3wire_mode_message(struct ad4000_state *st,
  417. const struct iio_chan_spec *chan)
  418. {
  419. unsigned int cnv_pulse_time = AD4000_TCONV_NS;
  420. struct spi_transfer *xfers = st->xfers;
  421. xfers[0].cs_change = 1;
  422. xfers[0].cs_change_delay.value = cnv_pulse_time;
  423. xfers[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
  424. xfers[1].rx_buf = &st->scan.data;
  425. xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits);
  426. xfers[1].delay.value = AD4000_TQUIET2_NS;
  427. xfers[1].delay.unit = SPI_DELAY_UNIT_NSECS;
  428. spi_message_init_with_transfers(&st->msg, st->xfers, 2);
  429. return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->msg);
  430. }
  431. /*
  432. * This executes a data sample transfer for when the device connections are
  433. * in "4-wire" mode, selected when the adi,sdi-pin device tree property is
  434. * set to "cs". In this connection mode, the controller CS pin is connected to
  435. * ADC SDI pin and a GPIO is connected to ADC CNV pin.
  436. * The GPIO connected to ADC CNV pin is set outside of the SPI transfer.
  437. */
  438. static int ad4000_prepare_4wire_mode_message(struct ad4000_state *st,
  439. const struct iio_chan_spec *chan)
  440. {
  441. unsigned int cnv_to_sdi_time = AD4000_TCONV_NS;
  442. struct spi_transfer *xfers = st->xfers;
  443. /*
  444. * Dummy transfer to cause enough delay between CNV going high and SDI
  445. * going low.
  446. */
  447. xfers[0].cs_off = 1;
  448. xfers[0].delay.value = cnv_to_sdi_time;
  449. xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
  450. xfers[1].rx_buf = &st->scan.data;
  451. xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits);
  452. spi_message_init_with_transfers(&st->msg, st->xfers, 2);
  453. return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->msg);
  454. }
  455. static int ad4000_config(struct ad4000_state *st)
  456. {
  457. unsigned int reg_val = AD4000_CONFIG_REG_DEFAULT;
  458. if (device_property_present(&st->spi->dev, "adi,high-z-input"))
  459. reg_val |= FIELD_PREP(AD4000_CFG_HIGHZ, 1);
  460. return ad4000_write_reg(st, reg_val);
  461. }
  462. static int ad4000_probe(struct spi_device *spi)
  463. {
  464. const struct ad4000_chip_info *chip;
  465. struct device *dev = &spi->dev;
  466. struct iio_dev *indio_dev;
  467. struct ad4000_state *st;
  468. int gain_idx, ret;
  469. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  470. if (!indio_dev)
  471. return -ENOMEM;
  472. chip = spi_get_device_match_data(spi);
  473. if (!chip)
  474. return -EINVAL;
  475. st = iio_priv(indio_dev);
  476. st->spi = spi;
  477. ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(ad4000_power_supplies),
  478. ad4000_power_supplies);
  479. if (ret)
  480. return dev_err_probe(dev, ret, "Failed to enable power supplies\n");
  481. ret = devm_regulator_get_enable_read_voltage(dev, "ref");
  482. if (ret < 0)
  483. return dev_err_probe(dev, ret,
  484. "Failed to get ref regulator reference\n");
  485. st->vref_mv = ret / 1000;
  486. st->cnv_gpio = devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_HIGH);
  487. if (IS_ERR(st->cnv_gpio))
  488. return dev_err_probe(dev, PTR_ERR(st->cnv_gpio),
  489. "Failed to get CNV GPIO");
  490. ret = device_property_match_property_string(dev, "adi,sdi-pin",
  491. ad4000_sdi_pin,
  492. ARRAY_SIZE(ad4000_sdi_pin));
  493. if (ret < 0 && ret != -EINVAL)
  494. return dev_err_probe(dev, ret,
  495. "getting adi,sdi-pin property failed\n");
  496. /* Default to usual SPI connections if pin properties are not present */
  497. st->sdi_pin = ret == -EINVAL ? AD4000_SDI_MOSI : ret;
  498. switch (st->sdi_pin) {
  499. case AD4000_SDI_MOSI:
  500. indio_dev->info = &ad4000_reg_access_info;
  501. indio_dev->channels = &chip->reg_access_chan_spec;
  502. /*
  503. * In "3-wire mode", the ADC SDI line must be kept high when
  504. * data is not being clocked out of the controller.
  505. * Request the SPI controller to make MOSI idle high.
  506. */
  507. spi->mode |= SPI_MOSI_IDLE_HIGH;
  508. ret = spi_setup(spi);
  509. if (ret < 0)
  510. return ret;
  511. ret = ad4000_prepare_3wire_mode_message(st, indio_dev->channels);
  512. if (ret)
  513. return ret;
  514. ret = ad4000_config(st);
  515. if (ret < 0)
  516. return dev_err_probe(dev, ret, "Failed to config device\n");
  517. break;
  518. case AD4000_SDI_VIO:
  519. indio_dev->info = &ad4000_info;
  520. indio_dev->channels = &chip->chan_spec;
  521. ret = ad4000_prepare_3wire_mode_message(st, indio_dev->channels);
  522. if (ret)
  523. return ret;
  524. break;
  525. case AD4000_SDI_CS:
  526. indio_dev->info = &ad4000_info;
  527. indio_dev->channels = &chip->chan_spec;
  528. ret = ad4000_prepare_4wire_mode_message(st, indio_dev->channels);
  529. if (ret)
  530. return ret;
  531. break;
  532. case AD4000_SDI_GND:
  533. return dev_err_probe(dev, -EPROTONOSUPPORT,
  534. "Unsupported connection mode\n");
  535. default:
  536. return dev_err_probe(dev, -EINVAL, "Unrecognized connection mode\n");
  537. }
  538. indio_dev->name = chip->dev_name;
  539. indio_dev->num_channels = 1;
  540. ret = devm_mutex_init(dev, &st->lock);
  541. if (ret)
  542. return ret;
  543. st->gain_milli = 1000;
  544. if (chip->has_hardware_gain) {
  545. ret = device_property_read_u16(dev, "adi,gain-milli",
  546. &st->gain_milli);
  547. if (!ret) {
  548. /* Match gain value from dt to one of supported gains */
  549. gain_idx = find_closest(st->gain_milli, ad4000_gains,
  550. ARRAY_SIZE(ad4000_gains));
  551. st->gain_milli = ad4000_gains[gain_idx];
  552. } else {
  553. return dev_err_probe(dev, ret,
  554. "Failed to read gain property\n");
  555. }
  556. }
  557. ad4000_fill_scale_tbl(st, indio_dev->channels);
  558. ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
  559. &iio_pollfunc_store_time,
  560. &ad4000_trigger_handler, NULL);
  561. if (ret)
  562. return ret;
  563. return devm_iio_device_register(dev, indio_dev);
  564. }
  565. static const struct spi_device_id ad4000_id[] = {
  566. { "ad4000", (kernel_ulong_t)&ad4000_chip_info },
  567. { "ad4001", (kernel_ulong_t)&ad4001_chip_info },
  568. { "ad4002", (kernel_ulong_t)&ad4002_chip_info },
  569. { "ad4003", (kernel_ulong_t)&ad4003_chip_info },
  570. { "ad4004", (kernel_ulong_t)&ad4004_chip_info },
  571. { "ad4005", (kernel_ulong_t)&ad4005_chip_info },
  572. { "ad4006", (kernel_ulong_t)&ad4006_chip_info },
  573. { "ad4007", (kernel_ulong_t)&ad4007_chip_info },
  574. { "ad4008", (kernel_ulong_t)&ad4008_chip_info },
  575. { "ad4010", (kernel_ulong_t)&ad4010_chip_info },
  576. { "ad4011", (kernel_ulong_t)&ad4011_chip_info },
  577. { "ad4020", (kernel_ulong_t)&ad4020_chip_info },
  578. { "ad4021", (kernel_ulong_t)&ad4021_chip_info },
  579. { "ad4022", (kernel_ulong_t)&ad4022_chip_info },
  580. { "adaq4001", (kernel_ulong_t)&adaq4001_chip_info },
  581. { "adaq4003", (kernel_ulong_t)&adaq4003_chip_info },
  582. { }
  583. };
  584. MODULE_DEVICE_TABLE(spi, ad4000_id);
  585. static const struct of_device_id ad4000_of_match[] = {
  586. { .compatible = "adi,ad4000", .data = &ad4000_chip_info },
  587. { .compatible = "adi,ad4001", .data = &ad4001_chip_info },
  588. { .compatible = "adi,ad4002", .data = &ad4002_chip_info },
  589. { .compatible = "adi,ad4003", .data = &ad4003_chip_info },
  590. { .compatible = "adi,ad4004", .data = &ad4004_chip_info },
  591. { .compatible = "adi,ad4005", .data = &ad4005_chip_info },
  592. { .compatible = "adi,ad4006", .data = &ad4006_chip_info },
  593. { .compatible = "adi,ad4007", .data = &ad4007_chip_info },
  594. { .compatible = "adi,ad4008", .data = &ad4008_chip_info },
  595. { .compatible = "adi,ad4010", .data = &ad4010_chip_info },
  596. { .compatible = "adi,ad4011", .data = &ad4011_chip_info },
  597. { .compatible = "adi,ad4020", .data = &ad4020_chip_info },
  598. { .compatible = "adi,ad4021", .data = &ad4021_chip_info },
  599. { .compatible = "adi,ad4022", .data = &ad4022_chip_info },
  600. { .compatible = "adi,adaq4001", .data = &adaq4001_chip_info },
  601. { .compatible = "adi,adaq4003", .data = &adaq4003_chip_info },
  602. { }
  603. };
  604. MODULE_DEVICE_TABLE(of, ad4000_of_match);
  605. static struct spi_driver ad4000_driver = {
  606. .driver = {
  607. .name = "ad4000",
  608. .of_match_table = ad4000_of_match,
  609. },
  610. .probe = ad4000_probe,
  611. .id_table = ad4000_id,
  612. };
  613. module_spi_driver(ad4000_driver);
  614. MODULE_AUTHOR("Marcelo Schmitt <marcelo.schmitt@analog.com>");
  615. MODULE_DESCRIPTION("Analog Devices AD4000 ADC driver");
  616. MODULE_LICENSE("GPL");