ti-adc084s021.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. /*
  29. * DMA (thus cache coherency maintenance) requires the
  30. * transfer buffers to live in their own cache line.
  31. */
  32. u16 tx_buf[4] ____cacheline_aligned;
  33. __be16 rx_buf[5]; /* First 16-bits are trash */
  34. };
  35. #define ADC084S021_VOLTAGE_CHANNEL(num) \
  36. { \
  37. .type = IIO_VOLTAGE, \
  38. .channel = (num), \
  39. .indexed = 1, \
  40. .scan_index = (num), \
  41. .scan_type = { \
  42. .sign = 'u', \
  43. .realbits = 8, \
  44. .storagebits = 16, \
  45. .shift = 4, \
  46. .endianness = IIO_BE, \
  47. }, \
  48. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  49. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
  50. }
  51. static const struct iio_chan_spec adc084s021_channels[] = {
  52. ADC084S021_VOLTAGE_CHANNEL(0),
  53. ADC084S021_VOLTAGE_CHANNEL(1),
  54. ADC084S021_VOLTAGE_CHANNEL(2),
  55. ADC084S021_VOLTAGE_CHANNEL(3),
  56. IIO_CHAN_SOFT_TIMESTAMP(4),
  57. };
  58. /**
  59. * Read an ADC channel and return its value.
  60. *
  61. * @adc: The ADC SPI data.
  62. * @data: Buffer for converted data.
  63. */
  64. static int adc084s021_adc_conversion(struct adc084s021 *adc, void *data)
  65. {
  66. int n_words = (adc->spi_trans.len >> 1) - 1; /* Discard first word */
  67. int ret, i = 0;
  68. u16 *p = data;
  69. /* Do the transfer */
  70. ret = spi_sync(adc->spi, &adc->message);
  71. if (ret < 0)
  72. return ret;
  73. for (; i < n_words; i++)
  74. *(p + i) = adc->rx_buf[i + 1];
  75. return ret;
  76. }
  77. static int adc084s021_read_raw(struct iio_dev *indio_dev,
  78. struct iio_chan_spec const *channel, int *val,
  79. int *val2, long mask)
  80. {
  81. struct adc084s021 *adc = iio_priv(indio_dev);
  82. int ret;
  83. switch (mask) {
  84. case IIO_CHAN_INFO_RAW:
  85. ret = iio_device_claim_direct_mode(indio_dev);
  86. if (ret < 0)
  87. return ret;
  88. ret = regulator_enable(adc->reg);
  89. if (ret) {
  90. iio_device_release_direct_mode(indio_dev);
  91. return ret;
  92. }
  93. adc->tx_buf[0] = channel->channel << 3;
  94. ret = adc084s021_adc_conversion(adc, val);
  95. iio_device_release_direct_mode(indio_dev);
  96. regulator_disable(adc->reg);
  97. if (ret < 0)
  98. return ret;
  99. *val = be16_to_cpu(*val);
  100. *val = (*val >> channel->scan_type.shift) & 0xff;
  101. return IIO_VAL_INT;
  102. case IIO_CHAN_INFO_SCALE:
  103. ret = regulator_enable(adc->reg);
  104. if (ret)
  105. return ret;
  106. ret = regulator_get_voltage(adc->reg);
  107. regulator_disable(adc->reg);
  108. if (ret < 0)
  109. return ret;
  110. *val = ret / 1000;
  111. return IIO_VAL_INT;
  112. default:
  113. return -EINVAL;
  114. }
  115. }
  116. /**
  117. * Read enabled ADC channels and push data to the buffer.
  118. *
  119. * @irq: The interrupt number (not used).
  120. * @pollfunc: Pointer to the poll func.
  121. */
  122. static irqreturn_t adc084s021_buffer_trigger_handler(int irq, void *pollfunc)
  123. {
  124. struct iio_poll_func *pf = pollfunc;
  125. struct iio_dev *indio_dev = pf->indio_dev;
  126. struct adc084s021 *adc = iio_priv(indio_dev);
  127. __be16 data[8] = {0}; /* 4 * 16-bit words of data + 8 bytes timestamp */
  128. mutex_lock(&adc->lock);
  129. if (adc084s021_adc_conversion(adc, &data) < 0)
  130. dev_err(&adc->spi->dev, "Failed to read data\n");
  131. iio_push_to_buffers_with_timestamp(indio_dev, data,
  132. iio_get_time_ns(indio_dev));
  133. mutex_unlock(&adc->lock);
  134. iio_trigger_notify_done(indio_dev->trig);
  135. return IRQ_HANDLED;
  136. }
  137. static int adc084s021_buffer_preenable(struct iio_dev *indio_dev)
  138. {
  139. struct adc084s021 *adc = iio_priv(indio_dev);
  140. int scan_index;
  141. int i = 0;
  142. for_each_set_bit(scan_index, indio_dev->active_scan_mask,
  143. indio_dev->masklength) {
  144. const struct iio_chan_spec *channel =
  145. &indio_dev->channels[scan_index];
  146. adc->tx_buf[i++] = channel->channel << 3;
  147. }
  148. adc->spi_trans.len = 2 + (i * sizeof(__be16)); /* Trash + channels */
  149. return regulator_enable(adc->reg);
  150. }
  151. static int adc084s021_buffer_postdisable(struct iio_dev *indio_dev)
  152. {
  153. struct adc084s021 *adc = iio_priv(indio_dev);
  154. adc->spi_trans.len = 4; /* Trash + single channel */
  155. return regulator_disable(adc->reg);
  156. }
  157. static const struct iio_info adc084s021_info = {
  158. .read_raw = adc084s021_read_raw,
  159. };
  160. static const struct iio_buffer_setup_ops adc084s021_buffer_setup_ops = {
  161. .preenable = adc084s021_buffer_preenable,
  162. .postenable = iio_triggered_buffer_postenable,
  163. .predisable = iio_triggered_buffer_predisable,
  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. /* Connect the SPI device and the iio dev */
  179. spi_set_drvdata(spi, indio_dev);
  180. /* Initiate the Industrial I/O device */
  181. indio_dev->dev.parent = &spi->dev;
  182. indio_dev->dev.of_node = spi->dev.of_node;
  183. indio_dev->name = spi_get_device_id(spi)->name;
  184. indio_dev->modes = INDIO_DIRECT_MODE;
  185. indio_dev->info = &adc084s021_info;
  186. indio_dev->channels = adc084s021_channels;
  187. indio_dev->num_channels = ARRAY_SIZE(adc084s021_channels);
  188. /* Create SPI transfer for channel reads */
  189. adc->spi_trans.tx_buf = adc->tx_buf;
  190. adc->spi_trans.rx_buf = adc->rx_buf;
  191. adc->spi_trans.len = 4; /* Trash + single channel */
  192. spi_message_init_with_transfers(&adc->message, &adc->spi_trans, 1);
  193. adc->reg = devm_regulator_get(&spi->dev, "vref");
  194. if (IS_ERR(adc->reg))
  195. return PTR_ERR(adc->reg);
  196. mutex_init(&adc->lock);
  197. /* Setup triggered buffer with pollfunction */
  198. ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
  199. adc084s021_buffer_trigger_handler,
  200. &adc084s021_buffer_setup_ops);
  201. if (ret) {
  202. dev_err(&spi->dev, "Failed to setup triggered buffer\n");
  203. return ret;
  204. }
  205. return devm_iio_device_register(&spi->dev, indio_dev);
  206. }
  207. static const struct of_device_id adc084s021_of_match[] = {
  208. { .compatible = "ti,adc084s021", },
  209. {},
  210. };
  211. MODULE_DEVICE_TABLE(of, adc084s021_of_match);
  212. static const struct spi_device_id adc084s021_id[] = {
  213. { ADC084S021_DRIVER_NAME, 0},
  214. {}
  215. };
  216. MODULE_DEVICE_TABLE(spi, adc084s021_id);
  217. static struct spi_driver adc084s021_driver = {
  218. .driver = {
  219. .name = ADC084S021_DRIVER_NAME,
  220. .of_match_table = of_match_ptr(adc084s021_of_match),
  221. },
  222. .probe = adc084s021_probe,
  223. .id_table = adc084s021_id,
  224. };
  225. module_spi_driver(adc084s021_driver);
  226. MODULE_AUTHOR("Mårten Lindahl <martenli@axis.com>");
  227. MODULE_DESCRIPTION("Texas Instruments ADC084S021");
  228. MODULE_LICENSE("GPL v2");
  229. MODULE_VERSION("1.0");