ti-adc108s102.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI ADC108S102 SPI ADC driver
  4. *
  5. * Copyright (c) 2013-2015 Intel Corporation.
  6. * Copyright (c) 2017 Siemens AG
  7. *
  8. * This IIO device driver is designed to work with the following
  9. * analog to digital converters from Texas Instruments:
  10. * ADC108S102
  11. * ADC128S102
  12. * The communication with ADC chip is via the SPI bus (mode 3).
  13. */
  14. #include <linux/acpi.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/buffer.h>
  17. #include <linux/iio/types.h>
  18. #include <linux/iio/triggered_buffer.h>
  19. #include <linux/iio/trigger_consumer.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/mod_devicetable.h>
  23. #include <linux/property.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/spi/spi.h>
  26. /*
  27. * In case of ACPI, we use the hard-wired 5000 mV of the Galileo and IOT2000
  28. * boards as default for the reference pin VA. Device tree users encode that
  29. * via the vref-supply regulator.
  30. */
  31. #define ADC108S102_VA_MV_ACPI_DEFAULT 5000
  32. /*
  33. * Defining the ADC resolution being 12 bits, we can use the same driver for
  34. * both ADC108S102 (10 bits resolution) and ADC128S102 (12 bits resolution)
  35. * chips. The ADC108S102 effectively returns a 12-bit result with the 2
  36. * least-significant bits unset.
  37. */
  38. #define ADC108S102_BITS 12
  39. #define ADC108S102_MAX_CHANNELS 8
  40. /*
  41. * 16-bit SPI command format:
  42. * [15:14] Ignored
  43. * [13:11] 3-bit channel address
  44. * [10:0] Ignored
  45. */
  46. #define ADC108S102_CMD(ch) ((u16)(ch) << 11)
  47. /*
  48. * 16-bit SPI response format:
  49. * [15:12] Zeros
  50. * [11:0] 12-bit ADC sample (for ADC108S102, [1:0] will always be 0).
  51. */
  52. #define ADC108S102_RES_DATA(res) ((u16)res & GENMASK(11, 0))
  53. struct adc108s102_state {
  54. struct spi_device *spi;
  55. struct regulator *reg;
  56. u32 va_millivolt;
  57. /* SPI transfer used by triggered buffer handler*/
  58. struct spi_transfer ring_xfer;
  59. /* SPI transfer used by direct scan */
  60. struct spi_transfer scan_single_xfer;
  61. /* SPI message used by ring_xfer SPI transfer */
  62. struct spi_message ring_msg;
  63. /* SPI message used by scan_single_xfer SPI transfer */
  64. struct spi_message scan_single_msg;
  65. /*
  66. * SPI message buffers:
  67. * tx_buf: |C0|C1|C2|C3|C4|C5|C6|C7|XX|
  68. * rx_buf: |XX|R0|R1|R2|R3|R4|R5|R6|R7|tt|tt|tt|tt|
  69. *
  70. * tx_buf: 8 channel read commands, plus 1 dummy command
  71. * rx_buf: 1 dummy response, 8 channel responses, plus 64-bit timestamp
  72. */
  73. __be16 rx_buf[13] ____cacheline_aligned;
  74. __be16 tx_buf[9] ____cacheline_aligned;
  75. };
  76. #define ADC108S102_V_CHAN(index) \
  77. { \
  78. .type = IIO_VOLTAGE, \
  79. .indexed = 1, \
  80. .channel = index, \
  81. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  82. BIT(IIO_CHAN_INFO_SCALE), \
  83. .address = index, \
  84. .scan_index = index, \
  85. .scan_type = { \
  86. .sign = 'u', \
  87. .realbits = ADC108S102_BITS, \
  88. .storagebits = 16, \
  89. .endianness = IIO_BE, \
  90. }, \
  91. }
  92. static const struct iio_chan_spec adc108s102_channels[] = {
  93. ADC108S102_V_CHAN(0),
  94. ADC108S102_V_CHAN(1),
  95. ADC108S102_V_CHAN(2),
  96. ADC108S102_V_CHAN(3),
  97. ADC108S102_V_CHAN(4),
  98. ADC108S102_V_CHAN(5),
  99. ADC108S102_V_CHAN(6),
  100. ADC108S102_V_CHAN(7),
  101. IIO_CHAN_SOFT_TIMESTAMP(8),
  102. };
  103. static int adc108s102_update_scan_mode(struct iio_dev *indio_dev,
  104. unsigned long const *active_scan_mask)
  105. {
  106. struct adc108s102_state *st = iio_priv(indio_dev);
  107. unsigned int bit, cmds;
  108. /*
  109. * Fill in the first x shorts of tx_buf with the number of channels
  110. * enabled for sampling by the triggered buffer.
  111. */
  112. cmds = 0;
  113. for_each_set_bit(bit, active_scan_mask, ADC108S102_MAX_CHANNELS)
  114. st->tx_buf[cmds++] = cpu_to_be16(ADC108S102_CMD(bit));
  115. /* One dummy command added, to clock in the last response */
  116. st->tx_buf[cmds++] = 0x00;
  117. /* build SPI ring message */
  118. st->ring_xfer.tx_buf = &st->tx_buf[0];
  119. st->ring_xfer.rx_buf = &st->rx_buf[0];
  120. st->ring_xfer.len = cmds * sizeof(st->tx_buf[0]);
  121. spi_message_init_with_transfers(&st->ring_msg, &st->ring_xfer, 1);
  122. return 0;
  123. }
  124. static irqreturn_t adc108s102_trigger_handler(int irq, void *p)
  125. {
  126. struct iio_poll_func *pf = p;
  127. struct iio_dev *indio_dev = pf->indio_dev;
  128. struct adc108s102_state *st = iio_priv(indio_dev);
  129. int ret;
  130. ret = spi_sync(st->spi, &st->ring_msg);
  131. if (ret < 0)
  132. goto out_notify;
  133. /* Skip the dummy response in the first slot */
  134. iio_push_to_buffers_with_timestamp(indio_dev,
  135. (u8 *)&st->rx_buf[1],
  136. iio_get_time_ns(indio_dev));
  137. out_notify:
  138. iio_trigger_notify_done(indio_dev->trig);
  139. return IRQ_HANDLED;
  140. }
  141. static int adc108s102_scan_direct(struct adc108s102_state *st, unsigned int ch)
  142. {
  143. int ret;
  144. st->tx_buf[0] = cpu_to_be16(ADC108S102_CMD(ch));
  145. ret = spi_sync(st->spi, &st->scan_single_msg);
  146. if (ret)
  147. return ret;
  148. /* Skip the dummy response in the first slot */
  149. return be16_to_cpu(st->rx_buf[1]);
  150. }
  151. static int adc108s102_read_raw(struct iio_dev *indio_dev,
  152. struct iio_chan_spec const *chan,
  153. int *val, int *val2, long m)
  154. {
  155. struct adc108s102_state *st = iio_priv(indio_dev);
  156. int ret;
  157. switch (m) {
  158. case IIO_CHAN_INFO_RAW:
  159. ret = iio_device_claim_direct_mode(indio_dev);
  160. if (ret)
  161. return ret;
  162. ret = adc108s102_scan_direct(st, chan->address);
  163. iio_device_release_direct_mode(indio_dev);
  164. if (ret < 0)
  165. return ret;
  166. *val = ADC108S102_RES_DATA(ret);
  167. return IIO_VAL_INT;
  168. case IIO_CHAN_INFO_SCALE:
  169. if (chan->type != IIO_VOLTAGE)
  170. break;
  171. *val = st->va_millivolt;
  172. *val2 = chan->scan_type.realbits;
  173. return IIO_VAL_FRACTIONAL_LOG2;
  174. default:
  175. break;
  176. }
  177. return -EINVAL;
  178. }
  179. static const struct iio_info adc108s102_info = {
  180. .read_raw = &adc108s102_read_raw,
  181. .update_scan_mode = &adc108s102_update_scan_mode,
  182. };
  183. static int adc108s102_probe(struct spi_device *spi)
  184. {
  185. struct adc108s102_state *st;
  186. struct iio_dev *indio_dev;
  187. int ret;
  188. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  189. if (!indio_dev)
  190. return -ENOMEM;
  191. st = iio_priv(indio_dev);
  192. if (ACPI_COMPANION(&spi->dev)) {
  193. st->va_millivolt = ADC108S102_VA_MV_ACPI_DEFAULT;
  194. } else {
  195. st->reg = devm_regulator_get(&spi->dev, "vref");
  196. if (IS_ERR(st->reg))
  197. return PTR_ERR(st->reg);
  198. ret = regulator_enable(st->reg);
  199. if (ret < 0) {
  200. dev_err(&spi->dev, "Cannot enable vref regulator\n");
  201. return ret;
  202. }
  203. ret = regulator_get_voltage(st->reg);
  204. if (ret < 0) {
  205. dev_err(&spi->dev, "vref get voltage failed\n");
  206. return ret;
  207. }
  208. st->va_millivolt = ret / 1000;
  209. }
  210. spi_set_drvdata(spi, indio_dev);
  211. st->spi = spi;
  212. indio_dev->name = spi->modalias;
  213. indio_dev->modes = INDIO_DIRECT_MODE;
  214. indio_dev->channels = adc108s102_channels;
  215. indio_dev->num_channels = ARRAY_SIZE(adc108s102_channels);
  216. indio_dev->info = &adc108s102_info;
  217. /* Setup default message */
  218. st->scan_single_xfer.tx_buf = st->tx_buf;
  219. st->scan_single_xfer.rx_buf = st->rx_buf;
  220. st->scan_single_xfer.len = 2 * sizeof(st->tx_buf[0]);
  221. spi_message_init_with_transfers(&st->scan_single_msg,
  222. &st->scan_single_xfer, 1);
  223. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  224. &adc108s102_trigger_handler, NULL);
  225. if (ret)
  226. goto error_disable_reg;
  227. ret = iio_device_register(indio_dev);
  228. if (ret) {
  229. dev_err(&spi->dev, "Failed to register IIO device\n");
  230. goto error_cleanup_triggered_buffer;
  231. }
  232. return 0;
  233. error_cleanup_triggered_buffer:
  234. iio_triggered_buffer_cleanup(indio_dev);
  235. error_disable_reg:
  236. regulator_disable(st->reg);
  237. return ret;
  238. }
  239. static int adc108s102_remove(struct spi_device *spi)
  240. {
  241. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  242. struct adc108s102_state *st = iio_priv(indio_dev);
  243. iio_device_unregister(indio_dev);
  244. iio_triggered_buffer_cleanup(indio_dev);
  245. regulator_disable(st->reg);
  246. return 0;
  247. }
  248. static const struct of_device_id adc108s102_of_match[] = {
  249. { .compatible = "ti,adc108s102" },
  250. { }
  251. };
  252. MODULE_DEVICE_TABLE(of, adc108s102_of_match);
  253. #ifdef CONFIG_ACPI
  254. static const struct acpi_device_id adc108s102_acpi_ids[] = {
  255. { "INT3495", 0 },
  256. { }
  257. };
  258. MODULE_DEVICE_TABLE(acpi, adc108s102_acpi_ids);
  259. #endif
  260. static const struct spi_device_id adc108s102_id[] = {
  261. { "adc108s102", 0 },
  262. { }
  263. };
  264. MODULE_DEVICE_TABLE(spi, adc108s102_id);
  265. static struct spi_driver adc108s102_driver = {
  266. .driver = {
  267. .name = "adc108s102",
  268. .of_match_table = adc108s102_of_match,
  269. .acpi_match_table = ACPI_PTR(adc108s102_acpi_ids),
  270. },
  271. .probe = adc108s102_probe,
  272. .remove = adc108s102_remove,
  273. .id_table = adc108s102_id,
  274. };
  275. module_spi_driver(adc108s102_driver);
  276. MODULE_AUTHOR("Bogdan Pricop <bogdan.pricop@emutex.com>");
  277. MODULE_DESCRIPTION("Texas Instruments ADC108S102 and ADC128S102 driver");
  278. MODULE_LICENSE("GPL v2");