ad7292.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Analog Devices AD7292 SPI ADC driver
  4. *
  5. * Copyright 2019 Analog Devices Inc.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/property.h>
  12. #include <linux/regulator/consumer.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/iio/iio.h>
  15. #define ADI_VENDOR_ID 0x0018
  16. #define AD7292_INTERNAL_REF_MV 1250
  17. /* AD7292 registers definition */
  18. #define AD7292_REG_VENDOR_ID 0x00
  19. #define AD7292_REG_CONF_BANK 0x05
  20. #define AD7292_REG_CONV_COMM 0x0E
  21. #define AD7292_REG_ADC_CH(x) (0x10 + (x))
  22. /* AD7292 configuration bank subregisters definition */
  23. #define AD7292_BANK_REG_VIN_RNG0 0x10
  24. #define AD7292_BANK_REG_VIN_RNG1 0x11
  25. #define AD7292_BANK_REG_SAMP_MODE 0x12
  26. #define AD7292_RD_FLAG_MSK(x) (BIT(7) | ((x) & 0x3F))
  27. /* AD7292_REG_ADC_CONVERSION */
  28. #define AD7292_ADC_DATA_MASK GENMASK(15, 6)
  29. #define AD7292_ADC_DATA(x) FIELD_GET(AD7292_ADC_DATA_MASK, x)
  30. /* AD7292_CHANNEL_SAMPLING_MODE */
  31. #define AD7292_CH_SAMP_MODE(reg, ch) (((reg) >> 8) & BIT(ch))
  32. /* AD7292_CHANNEL_VIN_RANGE */
  33. #define AD7292_CH_VIN_RANGE(reg, ch) ((reg) & BIT(ch))
  34. #define AD7292_VOLTAGE_CHAN(_chan) \
  35. { \
  36. .type = IIO_VOLTAGE, \
  37. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  38. BIT(IIO_CHAN_INFO_SCALE), \
  39. .indexed = 1, \
  40. .channel = _chan, \
  41. }
  42. static const struct iio_chan_spec ad7292_channels[] = {
  43. AD7292_VOLTAGE_CHAN(0),
  44. AD7292_VOLTAGE_CHAN(1),
  45. AD7292_VOLTAGE_CHAN(2),
  46. AD7292_VOLTAGE_CHAN(3),
  47. AD7292_VOLTAGE_CHAN(4),
  48. AD7292_VOLTAGE_CHAN(5),
  49. AD7292_VOLTAGE_CHAN(6),
  50. AD7292_VOLTAGE_CHAN(7)
  51. };
  52. static const struct iio_chan_spec ad7292_channels_diff[] = {
  53. {
  54. .type = IIO_VOLTAGE,
  55. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  56. .indexed = 1,
  57. .differential = 1,
  58. .channel = 0,
  59. .channel2 = 1,
  60. },
  61. AD7292_VOLTAGE_CHAN(2),
  62. AD7292_VOLTAGE_CHAN(3),
  63. AD7292_VOLTAGE_CHAN(4),
  64. AD7292_VOLTAGE_CHAN(5),
  65. AD7292_VOLTAGE_CHAN(6),
  66. AD7292_VOLTAGE_CHAN(7)
  67. };
  68. struct ad7292_state {
  69. struct spi_device *spi;
  70. unsigned short vref_mv;
  71. __be16 d16 __aligned(IIO_DMA_MINALIGN);
  72. u8 d8[2];
  73. };
  74. static int ad7292_spi_reg_read(struct ad7292_state *st, unsigned int addr)
  75. {
  76. int ret;
  77. st->d8[0] = AD7292_RD_FLAG_MSK(addr);
  78. ret = spi_write_then_read(st->spi, st->d8, 1, &st->d16, 2);
  79. if (ret < 0)
  80. return ret;
  81. return be16_to_cpu(st->d16);
  82. }
  83. static int ad7292_spi_subreg_read(struct ad7292_state *st, unsigned int addr,
  84. unsigned int sub_addr, unsigned int len)
  85. {
  86. unsigned int shift = 16 - (8 * len);
  87. int ret;
  88. st->d8[0] = AD7292_RD_FLAG_MSK(addr);
  89. st->d8[1] = sub_addr;
  90. ret = spi_write_then_read(st->spi, st->d8, 2, &st->d16, len);
  91. if (ret < 0)
  92. return ret;
  93. return (be16_to_cpu(st->d16) >> shift);
  94. }
  95. static int ad7292_single_conversion(struct ad7292_state *st,
  96. unsigned int chan_addr)
  97. {
  98. int ret;
  99. struct spi_transfer t[] = {
  100. {
  101. .tx_buf = &st->d8,
  102. .len = 4,
  103. .delay = {
  104. .value = 6,
  105. .unit = SPI_DELAY_UNIT_USECS
  106. },
  107. }, {
  108. .rx_buf = &st->d16,
  109. .len = 2,
  110. },
  111. };
  112. st->d8[0] = chan_addr;
  113. st->d8[1] = AD7292_RD_FLAG_MSK(AD7292_REG_CONV_COMM);
  114. ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
  115. if (ret < 0)
  116. return ret;
  117. return be16_to_cpu(st->d16);
  118. }
  119. static int ad7292_vin_range_multiplier(struct ad7292_state *st, int channel)
  120. {
  121. int samp_mode, range0, range1, factor = 1;
  122. /*
  123. * Every AD7292 ADC channel may have its input range adjusted according
  124. * to the settings at the ADC sampling mode and VIN range subregisters.
  125. * For a given channel, the minimum input range is equal to Vref, and it
  126. * may be increased by a multiplier factor of 2 or 4 according to the
  127. * following rule:
  128. * If channel is being sampled with respect to AGND:
  129. * factor = 4 if VIN range0 and VIN range1 equal 0
  130. * factor = 2 if only one of VIN ranges equal 1
  131. * factor = 1 if both VIN range0 and VIN range1 equal 1
  132. * If channel is being sampled with respect to AVDD:
  133. * factor = 4 if VIN range0 and VIN range1 equal 0
  134. * Behavior is undefined if any of VIN range doesn't equal 0
  135. */
  136. samp_mode = ad7292_spi_subreg_read(st, AD7292_REG_CONF_BANK,
  137. AD7292_BANK_REG_SAMP_MODE, 2);
  138. if (samp_mode < 0)
  139. return samp_mode;
  140. range0 = ad7292_spi_subreg_read(st, AD7292_REG_CONF_BANK,
  141. AD7292_BANK_REG_VIN_RNG0, 2);
  142. if (range0 < 0)
  143. return range0;
  144. range1 = ad7292_spi_subreg_read(st, AD7292_REG_CONF_BANK,
  145. AD7292_BANK_REG_VIN_RNG1, 2);
  146. if (range1 < 0)
  147. return range1;
  148. if (AD7292_CH_SAMP_MODE(samp_mode, channel)) {
  149. /* Sampling with respect to AGND */
  150. if (!AD7292_CH_VIN_RANGE(range0, channel))
  151. factor *= 2;
  152. if (!AD7292_CH_VIN_RANGE(range1, channel))
  153. factor *= 2;
  154. } else {
  155. /* Sampling with respect to AVDD */
  156. if (AD7292_CH_VIN_RANGE(range0, channel) ||
  157. AD7292_CH_VIN_RANGE(range1, channel))
  158. return -EPERM;
  159. factor = 4;
  160. }
  161. return factor;
  162. }
  163. static int ad7292_read_raw(struct iio_dev *indio_dev,
  164. const struct iio_chan_spec *chan,
  165. int *val, int *val2, long info)
  166. {
  167. struct ad7292_state *st = iio_priv(indio_dev);
  168. unsigned int ch_addr;
  169. int ret;
  170. switch (info) {
  171. case IIO_CHAN_INFO_RAW:
  172. ch_addr = AD7292_REG_ADC_CH(chan->channel);
  173. ret = ad7292_single_conversion(st, ch_addr);
  174. if (ret < 0)
  175. return ret;
  176. *val = AD7292_ADC_DATA(ret);
  177. return IIO_VAL_INT;
  178. case IIO_CHAN_INFO_SCALE:
  179. /*
  180. * To convert a raw value to standard units, the IIO defines
  181. * this formula: Scaled value = (raw + offset) * scale.
  182. * For the scale to be a correct multiplier for (raw + offset),
  183. * it must be calculated as the input range divided by the
  184. * number of possible distinct input values. Given the ADC data
  185. * is 10 bit long, it may assume 2^10 distinct values.
  186. * Hence, scale = range / 2^10. The IIO_VAL_FRACTIONAL_LOG2
  187. * return type indicates to the IIO API to divide *val by 2 to
  188. * the power of *val2 when returning from read_raw.
  189. */
  190. ret = ad7292_vin_range_multiplier(st, chan->channel);
  191. if (ret < 0)
  192. return ret;
  193. *val = st->vref_mv * ret;
  194. *val2 = 10;
  195. return IIO_VAL_FRACTIONAL_LOG2;
  196. default:
  197. break;
  198. }
  199. return -EINVAL;
  200. }
  201. static const struct iio_info ad7292_info = {
  202. .read_raw = ad7292_read_raw,
  203. };
  204. static int ad7292_probe(struct spi_device *spi)
  205. {
  206. struct ad7292_state *st;
  207. struct iio_dev *indio_dev;
  208. bool diff_channels = false;
  209. int ret;
  210. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  211. if (!indio_dev)
  212. return -ENOMEM;
  213. st = iio_priv(indio_dev);
  214. st->spi = spi;
  215. ret = ad7292_spi_reg_read(st, AD7292_REG_VENDOR_ID);
  216. if (ret != ADI_VENDOR_ID) {
  217. dev_err(&spi->dev, "Wrong vendor id 0x%x\n", ret);
  218. return -EINVAL;
  219. }
  220. ret = devm_regulator_get_enable_read_voltage(&spi->dev, "vref");
  221. if (ret < 0 && ret != -ENODEV)
  222. return ret;
  223. st->vref_mv = ret == -ENODEV ? AD7292_INTERNAL_REF_MV : ret / 1000;
  224. indio_dev->name = spi_get_device_id(spi)->name;
  225. indio_dev->modes = INDIO_DIRECT_MODE;
  226. indio_dev->info = &ad7292_info;
  227. device_for_each_child_node_scoped(&spi->dev, child) {
  228. diff_channels = fwnode_property_read_bool(child,
  229. "diff-channels");
  230. if (diff_channels)
  231. break;
  232. }
  233. if (diff_channels) {
  234. indio_dev->num_channels = ARRAY_SIZE(ad7292_channels_diff);
  235. indio_dev->channels = ad7292_channels_diff;
  236. } else {
  237. indio_dev->num_channels = ARRAY_SIZE(ad7292_channels);
  238. indio_dev->channels = ad7292_channels;
  239. }
  240. return devm_iio_device_register(&spi->dev, indio_dev);
  241. }
  242. static const struct spi_device_id ad7292_id_table[] = {
  243. { "ad7292", 0 },
  244. { }
  245. };
  246. MODULE_DEVICE_TABLE(spi, ad7292_id_table);
  247. static const struct of_device_id ad7292_of_match[] = {
  248. { .compatible = "adi,ad7292" },
  249. { }
  250. };
  251. MODULE_DEVICE_TABLE(of, ad7292_of_match);
  252. static struct spi_driver ad7292_driver = {
  253. .driver = {
  254. .name = "ad7292",
  255. .of_match_table = ad7292_of_match,
  256. },
  257. .probe = ad7292_probe,
  258. .id_table = ad7292_id_table,
  259. };
  260. module_spi_driver(ad7292_driver);
  261. MODULE_AUTHOR("Marcelo Schmitt <marcelo.schmitt1@gmail.com>");
  262. MODULE_DESCRIPTION("Analog Devices AD7292 ADC driver");
  263. MODULE_LICENSE("GPL v2");