ti-adc108s102.c 8.8 KB

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