ti-adc161s626.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * ti-adc161s626.c - Texas Instruments ADC161S626 1-channel differential ADC
  3. *
  4. * ADC Devices Supported:
  5. * adc141s626 - 14-bit ADC
  6. * adc161s626 - 16-bit ADC
  7. *
  8. * Copyright (C) 2016 Matt Ranostay <mranostay@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/trigger.h>
  26. #include <linux/iio/buffer.h>
  27. #include <linux/iio/trigger_consumer.h>
  28. #include <linux/iio/triggered_buffer.h>
  29. #define TI_ADC_DRV_NAME "ti-adc161s626"
  30. enum {
  31. TI_ADC141S626,
  32. TI_ADC161S626,
  33. };
  34. static const struct iio_chan_spec ti_adc141s626_channels[] = {
  35. {
  36. .type = IIO_VOLTAGE,
  37. .channel = 0,
  38. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  39. .scan_index = 0,
  40. .scan_type = {
  41. .sign = 's',
  42. .realbits = 14,
  43. .storagebits = 16,
  44. },
  45. },
  46. IIO_CHAN_SOFT_TIMESTAMP(1),
  47. };
  48. static const struct iio_chan_spec ti_adc161s626_channels[] = {
  49. {
  50. .type = IIO_VOLTAGE,
  51. .channel = 0,
  52. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  53. .scan_index = 0,
  54. .scan_type = {
  55. .sign = 's',
  56. .realbits = 16,
  57. .storagebits = 16,
  58. },
  59. },
  60. IIO_CHAN_SOFT_TIMESTAMP(1),
  61. };
  62. struct ti_adc_data {
  63. struct iio_dev *indio_dev;
  64. struct spi_device *spi;
  65. u8 read_size;
  66. u8 shift;
  67. u8 buffer[16] ____cacheline_aligned;
  68. };
  69. static int ti_adc_read_measurement(struct ti_adc_data *data,
  70. struct iio_chan_spec const *chan, int *val)
  71. {
  72. int ret;
  73. switch (data->read_size) {
  74. case 2: {
  75. __be16 buf;
  76. ret = spi_read(data->spi, (void *) &buf, 2);
  77. if (ret)
  78. return ret;
  79. *val = be16_to_cpu(buf);
  80. break;
  81. }
  82. case 3: {
  83. __be32 buf;
  84. ret = spi_read(data->spi, (void *) &buf, 3);
  85. if (ret)
  86. return ret;
  87. *val = be32_to_cpu(buf) >> 8;
  88. break;
  89. }
  90. default:
  91. return -EINVAL;
  92. }
  93. *val = sign_extend32(*val >> data->shift, chan->scan_type.realbits - 1);
  94. return 0;
  95. }
  96. static irqreturn_t ti_adc_trigger_handler(int irq, void *private)
  97. {
  98. struct iio_poll_func *pf = private;
  99. struct iio_dev *indio_dev = pf->indio_dev;
  100. struct ti_adc_data *data = iio_priv(indio_dev);
  101. int ret;
  102. ret = ti_adc_read_measurement(data, &indio_dev->channels[0],
  103. (int *) &data->buffer);
  104. if (!ret)
  105. iio_push_to_buffers_with_timestamp(indio_dev,
  106. data->buffer,
  107. iio_get_time_ns(indio_dev));
  108. iio_trigger_notify_done(indio_dev->trig);
  109. return IRQ_HANDLED;
  110. }
  111. static int ti_adc_read_raw(struct iio_dev *indio_dev,
  112. struct iio_chan_spec const *chan,
  113. int *val, int *val2, long mask)
  114. {
  115. struct ti_adc_data *data = iio_priv(indio_dev);
  116. int ret;
  117. if (mask != IIO_CHAN_INFO_RAW)
  118. return -EINVAL;
  119. ret = iio_device_claim_direct_mode(indio_dev);
  120. if (ret)
  121. return ret;
  122. ret = ti_adc_read_measurement(data, chan, val);
  123. iio_device_release_direct_mode(indio_dev);
  124. if (!ret)
  125. return IIO_VAL_INT;
  126. return 0;
  127. }
  128. static const struct iio_info ti_adc_info = {
  129. .driver_module = THIS_MODULE,
  130. .read_raw = ti_adc_read_raw,
  131. };
  132. static int ti_adc_probe(struct spi_device *spi)
  133. {
  134. struct iio_dev *indio_dev;
  135. struct ti_adc_data *data;
  136. int ret;
  137. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
  138. if (!indio_dev)
  139. return -ENOMEM;
  140. indio_dev->info = &ti_adc_info;
  141. indio_dev->dev.parent = &spi->dev;
  142. indio_dev->dev.of_node = spi->dev.of_node;
  143. indio_dev->name = TI_ADC_DRV_NAME;
  144. indio_dev->modes = INDIO_DIRECT_MODE;
  145. spi_set_drvdata(spi, indio_dev);
  146. data = iio_priv(indio_dev);
  147. data->spi = spi;
  148. switch (spi_get_device_id(spi)->driver_data) {
  149. case TI_ADC141S626:
  150. indio_dev->channels = ti_adc141s626_channels;
  151. indio_dev->num_channels = ARRAY_SIZE(ti_adc141s626_channels);
  152. data->shift = 0;
  153. data->read_size = 2;
  154. break;
  155. case TI_ADC161S626:
  156. indio_dev->channels = ti_adc161s626_channels;
  157. indio_dev->num_channels = ARRAY_SIZE(ti_adc161s626_channels);
  158. data->shift = 6;
  159. data->read_size = 3;
  160. break;
  161. }
  162. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  163. ti_adc_trigger_handler, NULL);
  164. if (ret)
  165. return ret;
  166. ret = iio_device_register(indio_dev);
  167. if (ret)
  168. goto error_unreg_buffer;
  169. return 0;
  170. error_unreg_buffer:
  171. iio_triggered_buffer_cleanup(indio_dev);
  172. return ret;
  173. }
  174. static int ti_adc_remove(struct spi_device *spi)
  175. {
  176. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  177. iio_device_unregister(indio_dev);
  178. iio_triggered_buffer_cleanup(indio_dev);
  179. return 0;
  180. }
  181. static const struct of_device_id ti_adc_dt_ids[] = {
  182. { .compatible = "ti,adc141s626", },
  183. { .compatible = "ti,adc161s626", },
  184. {}
  185. };
  186. MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
  187. static const struct spi_device_id ti_adc_id[] = {
  188. {"adc141s626", TI_ADC141S626},
  189. {"adc161s626", TI_ADC161S626},
  190. {},
  191. };
  192. MODULE_DEVICE_TABLE(spi, ti_adc_id);
  193. static struct spi_driver ti_adc_driver = {
  194. .driver = {
  195. .name = TI_ADC_DRV_NAME,
  196. .of_match_table = of_match_ptr(ti_adc_dt_ids),
  197. },
  198. .probe = ti_adc_probe,
  199. .remove = ti_adc_remove,
  200. .id_table = ti_adc_id,
  201. };
  202. module_spi_driver(ti_adc_driver);
  203. MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
  204. MODULE_DESCRIPTION("Texas Instruments ADC1x1S 1-channel differential ADC");
  205. MODULE_LICENSE("GPL");