ti-adc084s021.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 Axis Communications AB
  4. *
  5. * Driver for Texas Instruments' ADC084S021 ADC chip.
  6. * Datasheets can be found here:
  7. * https://www.ti.com/lit/ds/symlink/adc084s021.pdf
  8. */
  9. #include <linux/err.h>
  10. #include <linux/spi/spi.h>
  11. #include <linux/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/buffer.h>
  16. #include <linux/iio/triggered_buffer.h>
  17. #include <linux/iio/trigger_consumer.h>
  18. #include <linux/regulator/consumer.h>
  19. #define ADC084S021_DRIVER_NAME "adc084s021"
  20. struct adc084s021 {
  21. struct spi_device *spi;
  22. struct spi_message message;
  23. struct spi_transfer spi_trans;
  24. struct regulator *reg;
  25. struct mutex lock;
  26. /* Buffer used to align data */
  27. struct {
  28. __be16 channels[4];
  29. s64 ts __aligned(8);
  30. } scan;
  31. /*
  32. * DMA (thus cache coherency maintenance) may require the
  33. * transfer buffers to live in their own cache line.
  34. */
  35. u16 tx_buf[4] __aligned(IIO_DMA_MINALIGN);
  36. __be16 rx_buf[5]; /* First 16-bits are trash */
  37. };
  38. #define ADC084S021_VOLTAGE_CHANNEL(num) \
  39. { \
  40. .type = IIO_VOLTAGE, \
  41. .channel = (num), \
  42. .indexed = 1, \
  43. .scan_index = (num), \
  44. .scan_type = { \
  45. .sign = 'u', \
  46. .realbits = 8, \
  47. .storagebits = 16, \
  48. .shift = 4, \
  49. .endianness = IIO_BE, \
  50. }, \
  51. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  52. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
  53. }
  54. static const struct iio_chan_spec adc084s021_channels[] = {
  55. ADC084S021_VOLTAGE_CHANNEL(0),
  56. ADC084S021_VOLTAGE_CHANNEL(1),
  57. ADC084S021_VOLTAGE_CHANNEL(2),
  58. ADC084S021_VOLTAGE_CHANNEL(3),
  59. IIO_CHAN_SOFT_TIMESTAMP(4),
  60. };
  61. /**
  62. * adc084s021_adc_conversion() - Read an ADC channel and return its value.
  63. *
  64. * @adc: The ADC SPI data.
  65. * @data: Buffer for converted data.
  66. */
  67. static int adc084s021_adc_conversion(struct adc084s021 *adc, __be16 *data)
  68. {
  69. int n_words = (adc->spi_trans.len >> 1) - 1; /* Discard first word */
  70. int ret, i = 0;
  71. /* Do the transfer */
  72. ret = spi_sync(adc->spi, &adc->message);
  73. if (ret < 0)
  74. return ret;
  75. for (; i < n_words; i++)
  76. *(data + i) = adc->rx_buf[i + 1];
  77. return ret;
  78. }
  79. static int adc084s021_read_raw(struct iio_dev *indio_dev,
  80. struct iio_chan_spec const *channel, int *val,
  81. int *val2, long mask)
  82. {
  83. struct adc084s021 *adc = iio_priv(indio_dev);
  84. int ret;
  85. __be16 be_val;
  86. switch (mask) {
  87. case IIO_CHAN_INFO_RAW:
  88. ret = iio_device_claim_direct_mode(indio_dev);
  89. if (ret < 0)
  90. return ret;
  91. ret = regulator_enable(adc->reg);
  92. if (ret) {
  93. iio_device_release_direct_mode(indio_dev);
  94. return ret;
  95. }
  96. adc->tx_buf[0] = channel->channel << 3;
  97. ret = adc084s021_adc_conversion(adc, &be_val);
  98. iio_device_release_direct_mode(indio_dev);
  99. regulator_disable(adc->reg);
  100. if (ret < 0)
  101. return ret;
  102. *val = be16_to_cpu(be_val);
  103. *val = (*val >> channel->scan_type.shift) & 0xff;
  104. return IIO_VAL_INT;
  105. case IIO_CHAN_INFO_SCALE:
  106. ret = regulator_enable(adc->reg);
  107. if (ret)
  108. return ret;
  109. ret = regulator_get_voltage(adc->reg);
  110. regulator_disable(adc->reg);
  111. if (ret < 0)
  112. return ret;
  113. *val = ret / 1000;
  114. return IIO_VAL_INT;
  115. default:
  116. return -EINVAL;
  117. }
  118. }
  119. /**
  120. * adc084s021_buffer_trigger_handler() - Read ADC channels and push to buffer.
  121. *
  122. * @irq: The interrupt number (not used).
  123. * @pollfunc: Pointer to the poll func.
  124. */
  125. static irqreturn_t adc084s021_buffer_trigger_handler(int irq, void *pollfunc)
  126. {
  127. struct iio_poll_func *pf = pollfunc;
  128. struct iio_dev *indio_dev = pf->indio_dev;
  129. struct adc084s021 *adc = iio_priv(indio_dev);
  130. mutex_lock(&adc->lock);
  131. if (adc084s021_adc_conversion(adc, adc->scan.channels) < 0)
  132. dev_err(&adc->spi->dev, "Failed to read data\n");
  133. iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan,
  134. iio_get_time_ns(indio_dev));
  135. mutex_unlock(&adc->lock);
  136. iio_trigger_notify_done(indio_dev->trig);
  137. return IRQ_HANDLED;
  138. }
  139. static int adc084s021_buffer_preenable(struct iio_dev *indio_dev)
  140. {
  141. struct adc084s021 *adc = iio_priv(indio_dev);
  142. int scan_index;
  143. int i = 0;
  144. for_each_set_bit(scan_index, indio_dev->active_scan_mask,
  145. indio_dev->masklength) {
  146. const struct iio_chan_spec *channel =
  147. &indio_dev->channels[scan_index];
  148. adc->tx_buf[i++] = channel->channel << 3;
  149. }
  150. adc->spi_trans.len = 2 + (i * sizeof(__be16)); /* Trash + channels */
  151. return regulator_enable(adc->reg);
  152. }
  153. static int adc084s021_buffer_postdisable(struct iio_dev *indio_dev)
  154. {
  155. struct adc084s021 *adc = iio_priv(indio_dev);
  156. adc->spi_trans.len = 4; /* Trash + single channel */
  157. return regulator_disable(adc->reg);
  158. }
  159. static const struct iio_info adc084s021_info = {
  160. .read_raw = adc084s021_read_raw,
  161. };
  162. static const struct iio_buffer_setup_ops adc084s021_buffer_setup_ops = {
  163. .preenable = adc084s021_buffer_preenable,
  164. .postdisable = adc084s021_buffer_postdisable,
  165. };
  166. static int adc084s021_probe(struct spi_device *spi)
  167. {
  168. struct iio_dev *indio_dev;
  169. struct adc084s021 *adc;
  170. int ret;
  171. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  172. if (!indio_dev) {
  173. dev_err(&spi->dev, "Failed to allocate IIO device\n");
  174. return -ENOMEM;
  175. }
  176. adc = iio_priv(indio_dev);
  177. adc->spi = spi;
  178. /* Initiate the Industrial I/O device */
  179. indio_dev->name = spi_get_device_id(spi)->name;
  180. indio_dev->modes = INDIO_DIRECT_MODE;
  181. indio_dev->info = &adc084s021_info;
  182. indio_dev->channels = adc084s021_channels;
  183. indio_dev->num_channels = ARRAY_SIZE(adc084s021_channels);
  184. /* Create SPI transfer for channel reads */
  185. adc->spi_trans.tx_buf = adc->tx_buf;
  186. adc->spi_trans.rx_buf = adc->rx_buf;
  187. adc->spi_trans.len = 4; /* Trash + single channel */
  188. spi_message_init_with_transfers(&adc->message, &adc->spi_trans, 1);
  189. adc->reg = devm_regulator_get(&spi->dev, "vref");
  190. if (IS_ERR(adc->reg))
  191. return PTR_ERR(adc->reg);
  192. mutex_init(&adc->lock);
  193. /* Setup triggered buffer with pollfunction */
  194. ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
  195. adc084s021_buffer_trigger_handler,
  196. &adc084s021_buffer_setup_ops);
  197. if (ret) {
  198. dev_err(&spi->dev, "Failed to setup triggered buffer\n");
  199. return ret;
  200. }
  201. return devm_iio_device_register(&spi->dev, indio_dev);
  202. }
  203. static const struct of_device_id adc084s021_of_match[] = {
  204. { .compatible = "ti,adc084s021", },
  205. {},
  206. };
  207. MODULE_DEVICE_TABLE(of, adc084s021_of_match);
  208. static const struct spi_device_id adc084s021_id[] = {
  209. { ADC084S021_DRIVER_NAME, 0 },
  210. {}
  211. };
  212. MODULE_DEVICE_TABLE(spi, adc084s021_id);
  213. static struct spi_driver adc084s021_driver = {
  214. .driver = {
  215. .name = ADC084S021_DRIVER_NAME,
  216. .of_match_table = adc084s021_of_match,
  217. },
  218. .probe = adc084s021_probe,
  219. .id_table = adc084s021_id,
  220. };
  221. module_spi_driver(adc084s021_driver);
  222. MODULE_AUTHOR("Mårten Lindahl <[email protected]>");
  223. MODULE_DESCRIPTION("Texas Instruments ADC084S021");
  224. MODULE_LICENSE("GPL v2");
  225. MODULE_VERSION("1.0");