ad7949.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* ad7949.c - Analog Devices ADC driver 14/16 bits 4/8 channels
  3. *
  4. * Copyright (C) 2018 CMC NV
  5. *
  6. * https://www.analog.com/media/en/technical-documentation/data-sheets/AD7949.pdf
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/iio/iio.h>
  10. #include <linux/module.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/bitfield.h>
  14. #define AD7949_CFG_MASK_TOTAL GENMASK(13, 0)
  15. /* CFG: Configuration Update */
  16. #define AD7949_CFG_MASK_OVERWRITE BIT(13)
  17. /* INCC: Input Channel Configuration */
  18. #define AD7949_CFG_MASK_INCC GENMASK(12, 10)
  19. #define AD7949_CFG_VAL_INCC_UNIPOLAR_GND 7
  20. #define AD7949_CFG_VAL_INCC_UNIPOLAR_COMM 6
  21. #define AD7949_CFG_VAL_INCC_UNIPOLAR_DIFF 4
  22. #define AD7949_CFG_VAL_INCC_TEMP 3
  23. #define AD7949_CFG_VAL_INCC_BIPOLAR 2
  24. #define AD7949_CFG_VAL_INCC_BIPOLAR_DIFF 0
  25. /* INX: Input channel Selection in a binary fashion */
  26. #define AD7949_CFG_MASK_INX GENMASK(9, 7)
  27. /* BW: select bandwidth for low-pass filter. Full or Quarter */
  28. #define AD7949_CFG_MASK_BW_FULL BIT(6)
  29. /* REF: reference/buffer selection */
  30. #define AD7949_CFG_MASK_REF GENMASK(5, 3)
  31. #define AD7949_CFG_VAL_REF_EXT_TEMP_BUF 3
  32. #define AD7949_CFG_VAL_REF_EXT_TEMP 2
  33. #define AD7949_CFG_VAL_REF_INT_4096 1
  34. #define AD7949_CFG_VAL_REF_INT_2500 0
  35. #define AD7949_CFG_VAL_REF_EXTERNAL BIT(1)
  36. /* SEQ: channel sequencer. Allows for scanning channels */
  37. #define AD7949_CFG_MASK_SEQ GENMASK(2, 1)
  38. /* RB: Read back the CFG register */
  39. #define AD7949_CFG_MASK_RBN BIT(0)
  40. enum {
  41. ID_AD7949 = 0,
  42. ID_AD7682,
  43. ID_AD7689,
  44. };
  45. struct ad7949_adc_spec {
  46. u8 num_channels;
  47. u8 resolution;
  48. };
  49. static const struct ad7949_adc_spec ad7949_adc_spec[] = {
  50. [ID_AD7949] = { .num_channels = 8, .resolution = 14 },
  51. [ID_AD7682] = { .num_channels = 4, .resolution = 16 },
  52. [ID_AD7689] = { .num_channels = 8, .resolution = 16 },
  53. };
  54. /**
  55. * struct ad7949_adc_chip - AD ADC chip
  56. * @lock: protects write sequences
  57. * @vref: regulator generating Vref
  58. * @indio_dev: reference to iio structure
  59. * @spi: reference to spi structure
  60. * @refsel: reference selection
  61. * @resolution: resolution of the chip
  62. * @cfg: copy of the configuration register
  63. * @current_channel: current channel in use
  64. * @buffer: buffer to send / receive data to / from device
  65. * @buf8b: be16 buffer to exchange data with the device in 8-bit transfers
  66. */
  67. struct ad7949_adc_chip {
  68. struct mutex lock;
  69. struct regulator *vref;
  70. struct iio_dev *indio_dev;
  71. struct spi_device *spi;
  72. u32 refsel;
  73. u8 resolution;
  74. u16 cfg;
  75. unsigned int current_channel;
  76. u16 buffer __aligned(IIO_DMA_MINALIGN);
  77. __be16 buf8b;
  78. };
  79. static int ad7949_spi_write_cfg(struct ad7949_adc_chip *ad7949_adc, u16 val,
  80. u16 mask)
  81. {
  82. int ret;
  83. ad7949_adc->cfg = (val & mask) | (ad7949_adc->cfg & ~mask);
  84. switch (ad7949_adc->spi->bits_per_word) {
  85. case 16:
  86. ad7949_adc->buffer = ad7949_adc->cfg << 2;
  87. ret = spi_write(ad7949_adc->spi, &ad7949_adc->buffer, 2);
  88. break;
  89. case 14:
  90. ad7949_adc->buffer = ad7949_adc->cfg;
  91. ret = spi_write(ad7949_adc->spi, &ad7949_adc->buffer, 2);
  92. break;
  93. case 8:
  94. /* Here, type is big endian as it must be sent in two transfers */
  95. ad7949_adc->buf8b = cpu_to_be16(ad7949_adc->cfg << 2);
  96. ret = spi_write(ad7949_adc->spi, &ad7949_adc->buf8b, 2);
  97. break;
  98. default:
  99. dev_err(&ad7949_adc->indio_dev->dev, "unsupported BPW\n");
  100. return -EINVAL;
  101. }
  102. /*
  103. * This delay is to avoid a new request before the required time to
  104. * send a new command to the device
  105. */
  106. udelay(2);
  107. return ret;
  108. }
  109. static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val,
  110. unsigned int channel)
  111. {
  112. int ret;
  113. int i;
  114. /*
  115. * 1: write CFG for sample N and read old data (sample N-2)
  116. * 2: if CFG was not changed since sample N-1 then we'll get good data
  117. * at the next xfer, so we bail out now, otherwise we write something
  118. * and we read garbage (sample N-1 configuration).
  119. */
  120. for (i = 0; i < 2; i++) {
  121. ret = ad7949_spi_write_cfg(ad7949_adc,
  122. FIELD_PREP(AD7949_CFG_MASK_INX, channel),
  123. AD7949_CFG_MASK_INX);
  124. if (ret)
  125. return ret;
  126. if (channel == ad7949_adc->current_channel)
  127. break;
  128. }
  129. /* 3: write something and read actual data */
  130. if (ad7949_adc->spi->bits_per_word == 8)
  131. ret = spi_read(ad7949_adc->spi, &ad7949_adc->buf8b, 2);
  132. else
  133. ret = spi_read(ad7949_adc->spi, &ad7949_adc->buffer, 2);
  134. if (ret)
  135. return ret;
  136. /*
  137. * This delay is to avoid a new request before the required time to
  138. * send a new command to the device
  139. */
  140. udelay(2);
  141. ad7949_adc->current_channel = channel;
  142. switch (ad7949_adc->spi->bits_per_word) {
  143. case 16:
  144. *val = ad7949_adc->buffer;
  145. /* Shift-out padding bits */
  146. *val >>= 16 - ad7949_adc->resolution;
  147. break;
  148. case 14:
  149. *val = ad7949_adc->buffer & GENMASK(13, 0);
  150. break;
  151. case 8:
  152. /* Here, type is big endian as data was sent in two transfers */
  153. *val = be16_to_cpu(ad7949_adc->buf8b);
  154. /* Shift-out padding bits */
  155. *val >>= 16 - ad7949_adc->resolution;
  156. break;
  157. default:
  158. dev_err(&ad7949_adc->indio_dev->dev, "unsupported BPW\n");
  159. return -EINVAL;
  160. }
  161. return 0;
  162. }
  163. #define AD7949_ADC_CHANNEL(chan) { \
  164. .type = IIO_VOLTAGE, \
  165. .indexed = 1, \
  166. .channel = (chan), \
  167. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  168. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  169. }
  170. static const struct iio_chan_spec ad7949_adc_channels[] = {
  171. AD7949_ADC_CHANNEL(0),
  172. AD7949_ADC_CHANNEL(1),
  173. AD7949_ADC_CHANNEL(2),
  174. AD7949_ADC_CHANNEL(3),
  175. AD7949_ADC_CHANNEL(4),
  176. AD7949_ADC_CHANNEL(5),
  177. AD7949_ADC_CHANNEL(6),
  178. AD7949_ADC_CHANNEL(7),
  179. };
  180. static int ad7949_spi_read_raw(struct iio_dev *indio_dev,
  181. struct iio_chan_spec const *chan,
  182. int *val, int *val2, long mask)
  183. {
  184. struct ad7949_adc_chip *ad7949_adc = iio_priv(indio_dev);
  185. int ret;
  186. if (!val)
  187. return -EINVAL;
  188. switch (mask) {
  189. case IIO_CHAN_INFO_RAW:
  190. mutex_lock(&ad7949_adc->lock);
  191. ret = ad7949_spi_read_channel(ad7949_adc, val, chan->channel);
  192. mutex_unlock(&ad7949_adc->lock);
  193. if (ret < 0)
  194. return ret;
  195. return IIO_VAL_INT;
  196. case IIO_CHAN_INFO_SCALE:
  197. switch (ad7949_adc->refsel) {
  198. case AD7949_CFG_VAL_REF_INT_2500:
  199. *val = 2500;
  200. break;
  201. case AD7949_CFG_VAL_REF_INT_4096:
  202. *val = 4096;
  203. break;
  204. case AD7949_CFG_VAL_REF_EXT_TEMP:
  205. case AD7949_CFG_VAL_REF_EXT_TEMP_BUF:
  206. ret = regulator_get_voltage(ad7949_adc->vref);
  207. if (ret < 0)
  208. return ret;
  209. /* convert value back to mV */
  210. *val = ret / 1000;
  211. break;
  212. }
  213. *val2 = (1 << ad7949_adc->resolution) - 1;
  214. return IIO_VAL_FRACTIONAL;
  215. }
  216. return -EINVAL;
  217. }
  218. static int ad7949_spi_reg_access(struct iio_dev *indio_dev,
  219. unsigned int reg, unsigned int writeval,
  220. unsigned int *readval)
  221. {
  222. struct ad7949_adc_chip *ad7949_adc = iio_priv(indio_dev);
  223. int ret = 0;
  224. if (readval)
  225. *readval = ad7949_adc->cfg;
  226. else
  227. ret = ad7949_spi_write_cfg(ad7949_adc, writeval,
  228. AD7949_CFG_MASK_TOTAL);
  229. return ret;
  230. }
  231. static const struct iio_info ad7949_spi_info = {
  232. .read_raw = ad7949_spi_read_raw,
  233. .debugfs_reg_access = ad7949_spi_reg_access,
  234. };
  235. static int ad7949_spi_init(struct ad7949_adc_chip *ad7949_adc)
  236. {
  237. int ret;
  238. int val;
  239. u16 cfg;
  240. ad7949_adc->current_channel = 0;
  241. cfg = FIELD_PREP(AD7949_CFG_MASK_OVERWRITE, 1) |
  242. FIELD_PREP(AD7949_CFG_MASK_INCC, AD7949_CFG_VAL_INCC_UNIPOLAR_GND) |
  243. FIELD_PREP(AD7949_CFG_MASK_INX, ad7949_adc->current_channel) |
  244. FIELD_PREP(AD7949_CFG_MASK_BW_FULL, 1) |
  245. FIELD_PREP(AD7949_CFG_MASK_REF, ad7949_adc->refsel) |
  246. FIELD_PREP(AD7949_CFG_MASK_SEQ, 0x0) |
  247. FIELD_PREP(AD7949_CFG_MASK_RBN, 1);
  248. ret = ad7949_spi_write_cfg(ad7949_adc, cfg, AD7949_CFG_MASK_TOTAL);
  249. /*
  250. * Do two dummy conversions to apply the first configuration setting.
  251. * Required only after the start up of the device.
  252. */
  253. ad7949_spi_read_channel(ad7949_adc, &val, ad7949_adc->current_channel);
  254. ad7949_spi_read_channel(ad7949_adc, &val, ad7949_adc->current_channel);
  255. return ret;
  256. }
  257. static void ad7949_disable_reg(void *reg)
  258. {
  259. regulator_disable(reg);
  260. }
  261. static int ad7949_spi_probe(struct spi_device *spi)
  262. {
  263. u32 spi_ctrl_mask = spi->controller->bits_per_word_mask;
  264. struct device *dev = &spi->dev;
  265. const struct ad7949_adc_spec *spec;
  266. struct ad7949_adc_chip *ad7949_adc;
  267. struct iio_dev *indio_dev;
  268. u32 tmp;
  269. int ret;
  270. indio_dev = devm_iio_device_alloc(dev, sizeof(*ad7949_adc));
  271. if (!indio_dev) {
  272. dev_err(dev, "can not allocate iio device\n");
  273. return -ENOMEM;
  274. }
  275. indio_dev->info = &ad7949_spi_info;
  276. indio_dev->name = spi_get_device_id(spi)->name;
  277. indio_dev->modes = INDIO_DIRECT_MODE;
  278. indio_dev->channels = ad7949_adc_channels;
  279. spi_set_drvdata(spi, indio_dev);
  280. ad7949_adc = iio_priv(indio_dev);
  281. ad7949_adc->indio_dev = indio_dev;
  282. ad7949_adc->spi = spi;
  283. spec = &ad7949_adc_spec[spi_get_device_id(spi)->driver_data];
  284. indio_dev->num_channels = spec->num_channels;
  285. ad7949_adc->resolution = spec->resolution;
  286. /* Set SPI bits per word */
  287. if (spi_ctrl_mask & SPI_BPW_MASK(ad7949_adc->resolution)) {
  288. spi->bits_per_word = ad7949_adc->resolution;
  289. } else if (spi_ctrl_mask == SPI_BPW_MASK(16)) {
  290. spi->bits_per_word = 16;
  291. } else if (spi_ctrl_mask == SPI_BPW_MASK(8)) {
  292. spi->bits_per_word = 8;
  293. } else {
  294. dev_err(dev, "unable to find common BPW with spi controller\n");
  295. return -EINVAL;
  296. }
  297. /* Setup internal voltage reference */
  298. tmp = 4096000;
  299. device_property_read_u32(dev, "adi,internal-ref-microvolt", &tmp);
  300. switch (tmp) {
  301. case 2500000:
  302. ad7949_adc->refsel = AD7949_CFG_VAL_REF_INT_2500;
  303. break;
  304. case 4096000:
  305. ad7949_adc->refsel = AD7949_CFG_VAL_REF_INT_4096;
  306. break;
  307. default:
  308. dev_err(dev, "unsupported internal voltage reference\n");
  309. return -EINVAL;
  310. }
  311. /* Setup external voltage reference, buffered? */
  312. ad7949_adc->vref = devm_regulator_get_optional(dev, "vrefin");
  313. if (IS_ERR(ad7949_adc->vref)) {
  314. ret = PTR_ERR(ad7949_adc->vref);
  315. if (ret != -ENODEV)
  316. return ret;
  317. /* unbuffered? */
  318. ad7949_adc->vref = devm_regulator_get_optional(dev, "vref");
  319. if (IS_ERR(ad7949_adc->vref)) {
  320. ret = PTR_ERR(ad7949_adc->vref);
  321. if (ret != -ENODEV)
  322. return ret;
  323. } else {
  324. ad7949_adc->refsel = AD7949_CFG_VAL_REF_EXT_TEMP;
  325. }
  326. } else {
  327. ad7949_adc->refsel = AD7949_CFG_VAL_REF_EXT_TEMP_BUF;
  328. }
  329. if (ad7949_adc->refsel & AD7949_CFG_VAL_REF_EXTERNAL) {
  330. ret = regulator_enable(ad7949_adc->vref);
  331. if (ret < 0) {
  332. dev_err(dev, "fail to enable regulator\n");
  333. return ret;
  334. }
  335. ret = devm_add_action_or_reset(dev, ad7949_disable_reg,
  336. ad7949_adc->vref);
  337. if (ret)
  338. return ret;
  339. }
  340. mutex_init(&ad7949_adc->lock);
  341. ret = ad7949_spi_init(ad7949_adc);
  342. if (ret) {
  343. dev_err(dev, "fail to init this device: %d\n", ret);
  344. return ret;
  345. }
  346. ret = devm_iio_device_register(dev, indio_dev);
  347. if (ret)
  348. dev_err(dev, "fail to register iio device: %d\n", ret);
  349. return ret;
  350. }
  351. static const struct of_device_id ad7949_spi_of_id[] = {
  352. { .compatible = "adi,ad7949" },
  353. { .compatible = "adi,ad7682" },
  354. { .compatible = "adi,ad7689" },
  355. { }
  356. };
  357. MODULE_DEVICE_TABLE(of, ad7949_spi_of_id);
  358. static const struct spi_device_id ad7949_spi_id[] = {
  359. { "ad7949", ID_AD7949 },
  360. { "ad7682", ID_AD7682 },
  361. { "ad7689", ID_AD7689 },
  362. { }
  363. };
  364. MODULE_DEVICE_TABLE(spi, ad7949_spi_id);
  365. static struct spi_driver ad7949_spi_driver = {
  366. .driver = {
  367. .name = "ad7949",
  368. .of_match_table = ad7949_spi_of_id,
  369. },
  370. .probe = ad7949_spi_probe,
  371. .id_table = ad7949_spi_id,
  372. };
  373. module_spi_driver(ad7949_spi_driver);
  374. MODULE_AUTHOR("Charles-Antoine Couret <[email protected]>");
  375. MODULE_DESCRIPTION("Analog Devices 14/16-bit 8-channel ADC driver");
  376. MODULE_LICENSE("GPL v2");