ti-adc084s021.c 7.4 KB

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