ad7476.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Analog Devices AD7466/7/8 AD7476/5/7/8 (A) SPI ADC driver
  4. * TI ADC081S/ADC101S/ADC121S 8/10/12-bit SPI ADC driver
  5. *
  6. * Copyright 2010 Analog Devices Inc.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/err.h>
  16. #include <linux/module.h>
  17. #include <linux/bitops.h>
  18. #include <linux/delay.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/trigger_consumer.h>
  23. #include <linux/iio/triggered_buffer.h>
  24. struct ad7476_state;
  25. struct ad7476_chip_info {
  26. unsigned int int_vref_uv;
  27. struct iio_chan_spec channel[2];
  28. /* channels used when convst gpio is defined */
  29. struct iio_chan_spec convst_channel[2];
  30. void (*reset)(struct ad7476_state *);
  31. bool has_vref;
  32. bool has_vdrive;
  33. };
  34. struct ad7476_state {
  35. struct spi_device *spi;
  36. const struct ad7476_chip_info *chip_info;
  37. struct regulator *ref_reg;
  38. struct gpio_desc *convst_gpio;
  39. struct spi_transfer xfer;
  40. struct spi_message msg;
  41. /*
  42. * DMA (thus cache coherency maintenance) may require the
  43. * transfer buffers to live in their own cache lines.
  44. * Make the buffer large enough for one 16 bit sample and one 64 bit
  45. * aligned 64 bit timestamp.
  46. */
  47. unsigned char data[ALIGN(2, sizeof(s64)) + sizeof(s64)] __aligned(IIO_DMA_MINALIGN);
  48. };
  49. enum ad7476_supported_device_ids {
  50. ID_AD7091,
  51. ID_AD7091R,
  52. ID_AD7273,
  53. ID_AD7274,
  54. ID_AD7276,
  55. ID_AD7277,
  56. ID_AD7278,
  57. ID_AD7466,
  58. ID_AD7467,
  59. ID_AD7468,
  60. ID_AD7475,
  61. ID_AD7495,
  62. ID_AD7940,
  63. ID_ADC081S,
  64. ID_ADC101S,
  65. ID_ADC121S,
  66. ID_ADS7866,
  67. ID_ADS7867,
  68. ID_ADS7868,
  69. ID_LTC2314_14,
  70. };
  71. static void ad7091_convst(struct ad7476_state *st)
  72. {
  73. if (!st->convst_gpio)
  74. return;
  75. gpiod_set_value(st->convst_gpio, 0);
  76. udelay(1); /* CONVST pulse width: 10 ns min */
  77. gpiod_set_value(st->convst_gpio, 1);
  78. udelay(1); /* Conversion time: 650 ns max */
  79. }
  80. static irqreturn_t ad7476_trigger_handler(int irq, void *p)
  81. {
  82. struct iio_poll_func *pf = p;
  83. struct iio_dev *indio_dev = pf->indio_dev;
  84. struct ad7476_state *st = iio_priv(indio_dev);
  85. int b_sent;
  86. ad7091_convst(st);
  87. b_sent = spi_sync(st->spi, &st->msg);
  88. if (b_sent < 0)
  89. goto done;
  90. iio_push_to_buffers_with_timestamp(indio_dev, st->data,
  91. iio_get_time_ns(indio_dev));
  92. done:
  93. iio_trigger_notify_done(indio_dev->trig);
  94. return IRQ_HANDLED;
  95. }
  96. static void ad7091_reset(struct ad7476_state *st)
  97. {
  98. /* Any transfers with 8 scl cycles will reset the device */
  99. spi_read(st->spi, st->data, 1);
  100. }
  101. static int ad7476_scan_direct(struct ad7476_state *st)
  102. {
  103. int ret;
  104. ad7091_convst(st);
  105. ret = spi_sync(st->spi, &st->msg);
  106. if (ret)
  107. return ret;
  108. return be16_to_cpup((__be16 *)st->data);
  109. }
  110. static int ad7476_read_raw(struct iio_dev *indio_dev,
  111. struct iio_chan_spec const *chan,
  112. int *val,
  113. int *val2,
  114. long m)
  115. {
  116. int ret;
  117. struct ad7476_state *st = iio_priv(indio_dev);
  118. int scale_uv;
  119. switch (m) {
  120. case IIO_CHAN_INFO_RAW:
  121. ret = iio_device_claim_direct_mode(indio_dev);
  122. if (ret)
  123. return ret;
  124. ret = ad7476_scan_direct(st);
  125. iio_device_release_direct_mode(indio_dev);
  126. if (ret < 0)
  127. return ret;
  128. *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
  129. GENMASK(st->chip_info->channel[0].scan_type.realbits - 1, 0);
  130. return IIO_VAL_INT;
  131. case IIO_CHAN_INFO_SCALE:
  132. if (st->ref_reg) {
  133. scale_uv = regulator_get_voltage(st->ref_reg);
  134. if (scale_uv < 0)
  135. return scale_uv;
  136. } else {
  137. scale_uv = st->chip_info->int_vref_uv;
  138. }
  139. *val = scale_uv / 1000;
  140. *val2 = chan->scan_type.realbits;
  141. return IIO_VAL_FRACTIONAL_LOG2;
  142. }
  143. return -EINVAL;
  144. }
  145. #define _AD7476_CHAN(bits, _shift, _info_mask_sep) \
  146. { \
  147. .type = IIO_VOLTAGE, \
  148. .indexed = 1, \
  149. .info_mask_separate = _info_mask_sep, \
  150. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  151. .scan_type = { \
  152. .sign = 'u', \
  153. .realbits = (bits), \
  154. .storagebits = 16, \
  155. .shift = (_shift), \
  156. .endianness = IIO_BE, \
  157. }, \
  158. }
  159. #define ADC081S_CHAN(bits) _AD7476_CHAN((bits), 12 - (bits), \
  160. BIT(IIO_CHAN_INFO_RAW))
  161. #define AD7476_CHAN(bits) _AD7476_CHAN((bits), 13 - (bits), \
  162. BIT(IIO_CHAN_INFO_RAW))
  163. #define AD7940_CHAN(bits) _AD7476_CHAN((bits), 15 - (bits), \
  164. BIT(IIO_CHAN_INFO_RAW))
  165. #define AD7091R_CHAN(bits) _AD7476_CHAN((bits), 16 - (bits), 0)
  166. #define AD7091R_CONVST_CHAN(bits) _AD7476_CHAN((bits), 16 - (bits), \
  167. BIT(IIO_CHAN_INFO_RAW))
  168. #define ADS786X_CHAN(bits) _AD7476_CHAN((bits), 12 - (bits), \
  169. BIT(IIO_CHAN_INFO_RAW))
  170. static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
  171. [ID_AD7091] = {
  172. .channel[0] = AD7091R_CHAN(12),
  173. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  174. .convst_channel[0] = AD7091R_CONVST_CHAN(12),
  175. .convst_channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  176. .reset = ad7091_reset,
  177. },
  178. [ID_AD7091R] = {
  179. .channel[0] = AD7091R_CHAN(12),
  180. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  181. .convst_channel[0] = AD7091R_CONVST_CHAN(12),
  182. .convst_channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  183. .int_vref_uv = 2500000,
  184. .has_vref = true,
  185. .reset = ad7091_reset,
  186. },
  187. [ID_AD7273] = {
  188. .channel[0] = AD7940_CHAN(10),
  189. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  190. .has_vref = true,
  191. },
  192. [ID_AD7274] = {
  193. .channel[0] = AD7940_CHAN(12),
  194. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  195. .has_vref = true,
  196. },
  197. [ID_AD7276] = {
  198. .channel[0] = AD7940_CHAN(12),
  199. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  200. },
  201. [ID_AD7277] = {
  202. .channel[0] = AD7940_CHAN(10),
  203. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  204. },
  205. [ID_AD7278] = {
  206. .channel[0] = AD7940_CHAN(8),
  207. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  208. },
  209. [ID_AD7466] = {
  210. .channel[0] = AD7476_CHAN(12),
  211. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  212. },
  213. [ID_AD7467] = {
  214. .channel[0] = AD7476_CHAN(10),
  215. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  216. },
  217. [ID_AD7468] = {
  218. .channel[0] = AD7476_CHAN(8),
  219. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  220. },
  221. [ID_AD7475] = {
  222. .channel[0] = AD7476_CHAN(12),
  223. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  224. .has_vref = true,
  225. .has_vdrive = true,
  226. },
  227. [ID_AD7495] = {
  228. .channel[0] = AD7476_CHAN(12),
  229. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  230. .int_vref_uv = 2500000,
  231. .has_vdrive = true,
  232. },
  233. [ID_AD7940] = {
  234. .channel[0] = AD7940_CHAN(14),
  235. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  236. },
  237. [ID_ADC081S] = {
  238. .channel[0] = ADC081S_CHAN(8),
  239. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  240. },
  241. [ID_ADC101S] = {
  242. .channel[0] = ADC081S_CHAN(10),
  243. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  244. },
  245. [ID_ADC121S] = {
  246. .channel[0] = ADC081S_CHAN(12),
  247. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  248. },
  249. [ID_ADS7866] = {
  250. .channel[0] = ADS786X_CHAN(12),
  251. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  252. },
  253. [ID_ADS7867] = {
  254. .channel[0] = ADS786X_CHAN(10),
  255. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  256. },
  257. [ID_ADS7868] = {
  258. .channel[0] = ADS786X_CHAN(8),
  259. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  260. },
  261. [ID_LTC2314_14] = {
  262. .channel[0] = AD7940_CHAN(14),
  263. .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
  264. .has_vref = true,
  265. },
  266. };
  267. static const struct iio_info ad7476_info = {
  268. .read_raw = &ad7476_read_raw,
  269. };
  270. static void ad7476_reg_disable(void *data)
  271. {
  272. struct regulator *reg = data;
  273. regulator_disable(reg);
  274. }
  275. static int ad7476_probe(struct spi_device *spi)
  276. {
  277. struct ad7476_state *st;
  278. struct iio_dev *indio_dev;
  279. struct regulator *reg;
  280. int ret;
  281. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  282. if (!indio_dev)
  283. return -ENOMEM;
  284. st = iio_priv(indio_dev);
  285. st->chip_info =
  286. &ad7476_chip_info_tbl[spi_get_device_id(spi)->driver_data];
  287. reg = devm_regulator_get(&spi->dev, "vcc");
  288. if (IS_ERR(reg))
  289. return PTR_ERR(reg);
  290. ret = regulator_enable(reg);
  291. if (ret)
  292. return ret;
  293. ret = devm_add_action_or_reset(&spi->dev, ad7476_reg_disable, reg);
  294. if (ret)
  295. return ret;
  296. /* Either vcc or vref (below) as appropriate */
  297. if (!st->chip_info->int_vref_uv)
  298. st->ref_reg = reg;
  299. if (st->chip_info->has_vref) {
  300. /* If a device has an internal reference vref is optional */
  301. if (st->chip_info->int_vref_uv) {
  302. reg = devm_regulator_get_optional(&spi->dev, "vref");
  303. if (IS_ERR(reg) && (PTR_ERR(reg) != -ENODEV))
  304. return PTR_ERR(reg);
  305. } else {
  306. reg = devm_regulator_get(&spi->dev, "vref");
  307. if (IS_ERR(reg))
  308. return PTR_ERR(reg);
  309. }
  310. if (!IS_ERR(reg)) {
  311. ret = regulator_enable(reg);
  312. if (ret)
  313. return ret;
  314. ret = devm_add_action_or_reset(&spi->dev,
  315. ad7476_reg_disable,
  316. reg);
  317. if (ret)
  318. return ret;
  319. st->ref_reg = reg;
  320. } else {
  321. /*
  322. * Can only get here if device supports both internal
  323. * and external reference, but the regulator connected
  324. * to the external reference is not connected.
  325. * Set the reference regulator pointer to NULL to
  326. * indicate this.
  327. */
  328. st->ref_reg = NULL;
  329. }
  330. }
  331. if (st->chip_info->has_vdrive) {
  332. reg = devm_regulator_get(&spi->dev, "vdrive");
  333. if (IS_ERR(reg))
  334. return PTR_ERR(reg);
  335. ret = regulator_enable(reg);
  336. if (ret)
  337. return ret;
  338. ret = devm_add_action_or_reset(&spi->dev, ad7476_reg_disable,
  339. reg);
  340. if (ret)
  341. return ret;
  342. }
  343. st->convst_gpio = devm_gpiod_get_optional(&spi->dev,
  344. "adi,conversion-start",
  345. GPIOD_OUT_LOW);
  346. if (IS_ERR(st->convst_gpio))
  347. return PTR_ERR(st->convst_gpio);
  348. st->spi = spi;
  349. indio_dev->name = spi_get_device_id(spi)->name;
  350. indio_dev->modes = INDIO_DIRECT_MODE;
  351. indio_dev->channels = st->chip_info->channel;
  352. indio_dev->num_channels = 2;
  353. indio_dev->info = &ad7476_info;
  354. if (st->convst_gpio)
  355. indio_dev->channels = st->chip_info->convst_channel;
  356. /* Setup default message */
  357. st->xfer.rx_buf = &st->data;
  358. st->xfer.len = st->chip_info->channel[0].scan_type.storagebits / 8;
  359. spi_message_init(&st->msg);
  360. spi_message_add_tail(&st->xfer, &st->msg);
  361. ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
  362. &ad7476_trigger_handler, NULL);
  363. if (ret)
  364. return ret;
  365. if (st->chip_info->reset)
  366. st->chip_info->reset(st);
  367. return devm_iio_device_register(&spi->dev, indio_dev);
  368. }
  369. static const struct spi_device_id ad7476_id[] = {
  370. {"ad7091", ID_AD7091},
  371. {"ad7091r", ID_AD7091R},
  372. {"ad7273", ID_AD7273},
  373. {"ad7274", ID_AD7274},
  374. {"ad7276", ID_AD7276},
  375. {"ad7277", ID_AD7277},
  376. {"ad7278", ID_AD7278},
  377. {"ad7466", ID_AD7466},
  378. {"ad7467", ID_AD7467},
  379. {"ad7468", ID_AD7468},
  380. {"ad7475", ID_AD7475},
  381. {"ad7476", ID_AD7466},
  382. {"ad7476a", ID_AD7466},
  383. {"ad7477", ID_AD7467},
  384. {"ad7477a", ID_AD7467},
  385. {"ad7478", ID_AD7468},
  386. {"ad7478a", ID_AD7468},
  387. {"ad7495", ID_AD7495},
  388. {"ad7910", ID_AD7467},
  389. {"ad7920", ID_AD7466},
  390. {"ad7940", ID_AD7940},
  391. {"adc081s", ID_ADC081S},
  392. {"adc101s", ID_ADC101S},
  393. {"adc121s", ID_ADC121S},
  394. {"ads7866", ID_ADS7866},
  395. {"ads7867", ID_ADS7867},
  396. {"ads7868", ID_ADS7868},
  397. {"ltc2314-14", ID_LTC2314_14},
  398. {}
  399. };
  400. MODULE_DEVICE_TABLE(spi, ad7476_id);
  401. static struct spi_driver ad7476_driver = {
  402. .driver = {
  403. .name = "ad7476",
  404. },
  405. .probe = ad7476_probe,
  406. .id_table = ad7476_id,
  407. };
  408. module_spi_driver(ad7476_driver);
  409. MODULE_AUTHOR("Michael Hennerich <[email protected]>");
  410. MODULE_DESCRIPTION("Analog Devices AD7476 and similar 1-channel ADCs");
  411. MODULE_LICENSE("GPL v2");