ti-tlc4541.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI tlc4541 ADC Driver
  4. *
  5. * Copyright (C) 2017 Phil Reid
  6. *
  7. * Datasheets can be found here:
  8. * https://www.ti.com/lit/gpn/tlc3541
  9. * https://www.ti.com/lit/gpn/tlc4541
  10. *
  11. * The tlc4541 requires 24 clock cycles to start a transfer.
  12. * Conversion then takes 2.94us to complete before data is ready
  13. * Data is returned MSB first.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/err.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/trigger_consumer.h>
  23. #include <linux/iio/triggered_buffer.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/mod_devicetable.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/slab.h>
  29. #include <linux/spi/spi.h>
  30. #include <linux/sysfs.h>
  31. struct tlc4541_state {
  32. struct spi_device *spi;
  33. struct regulator *reg;
  34. struct spi_transfer scan_single_xfer[3];
  35. struct spi_message scan_single_msg;
  36. /*
  37. * DMA (thus cache coherency maintenance) requires the
  38. * transfer buffers to live in their own cache lines.
  39. * 2 bytes data + 6 bytes padding + 8 bytes timestamp when
  40. * call iio_push_to_buffers_with_timestamp.
  41. */
  42. __be16 rx_buf[8] ____cacheline_aligned;
  43. };
  44. struct tlc4541_chip_info {
  45. const struct iio_chan_spec *channels;
  46. unsigned int num_channels;
  47. };
  48. enum tlc4541_id {
  49. TLC3541,
  50. TLC4541,
  51. };
  52. #define TLC4541_V_CHAN(bits, bitshift) { \
  53. .type = IIO_VOLTAGE, \
  54. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  55. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  56. .scan_type = { \
  57. .sign = 'u', \
  58. .realbits = (bits), \
  59. .storagebits = 16, \
  60. .shift = (bitshift), \
  61. .endianness = IIO_BE, \
  62. }, \
  63. }
  64. #define DECLARE_TLC4541_CHANNELS(name, bits, bitshift) \
  65. const struct iio_chan_spec name ## _channels[] = { \
  66. TLC4541_V_CHAN(bits, bitshift), \
  67. IIO_CHAN_SOFT_TIMESTAMP(1), \
  68. }
  69. static DECLARE_TLC4541_CHANNELS(tlc3541, 14, 2);
  70. static DECLARE_TLC4541_CHANNELS(tlc4541, 16, 0);
  71. static const struct tlc4541_chip_info tlc4541_chip_info[] = {
  72. [TLC3541] = {
  73. .channels = tlc3541_channels,
  74. .num_channels = ARRAY_SIZE(tlc3541_channels),
  75. },
  76. [TLC4541] = {
  77. .channels = tlc4541_channels,
  78. .num_channels = ARRAY_SIZE(tlc4541_channels),
  79. },
  80. };
  81. static irqreturn_t tlc4541_trigger_handler(int irq, void *p)
  82. {
  83. struct iio_poll_func *pf = p;
  84. struct iio_dev *indio_dev = pf->indio_dev;
  85. struct tlc4541_state *st = iio_priv(indio_dev);
  86. int ret;
  87. ret = spi_sync(st->spi, &st->scan_single_msg);
  88. if (ret < 0)
  89. goto done;
  90. iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
  91. iio_get_time_ns(indio_dev));
  92. done:
  93. iio_trigger_notify_done(indio_dev->trig);
  94. return IRQ_HANDLED;
  95. }
  96. static int tlc4541_get_range(struct tlc4541_state *st)
  97. {
  98. int vref;
  99. vref = regulator_get_voltage(st->reg);
  100. if (vref < 0)
  101. return vref;
  102. vref /= 1000;
  103. return vref;
  104. }
  105. static int tlc4541_read_raw(struct iio_dev *indio_dev,
  106. struct iio_chan_spec const *chan,
  107. int *val,
  108. int *val2,
  109. long m)
  110. {
  111. int ret = 0;
  112. struct tlc4541_state *st = iio_priv(indio_dev);
  113. switch (m) {
  114. case IIO_CHAN_INFO_RAW:
  115. ret = iio_device_claim_direct_mode(indio_dev);
  116. if (ret)
  117. return ret;
  118. ret = spi_sync(st->spi, &st->scan_single_msg);
  119. iio_device_release_direct_mode(indio_dev);
  120. if (ret < 0)
  121. return ret;
  122. *val = be16_to_cpu(st->rx_buf[0]);
  123. *val = *val >> chan->scan_type.shift;
  124. *val &= GENMASK(chan->scan_type.realbits - 1, 0);
  125. return IIO_VAL_INT;
  126. case IIO_CHAN_INFO_SCALE:
  127. ret = tlc4541_get_range(st);
  128. if (ret < 0)
  129. return ret;
  130. *val = ret;
  131. *val2 = chan->scan_type.realbits;
  132. return IIO_VAL_FRACTIONAL_LOG2;
  133. }
  134. return -EINVAL;
  135. }
  136. static const struct iio_info tlc4541_info = {
  137. .read_raw = &tlc4541_read_raw,
  138. };
  139. static int tlc4541_probe(struct spi_device *spi)
  140. {
  141. struct tlc4541_state *st;
  142. struct iio_dev *indio_dev;
  143. const struct tlc4541_chip_info *info;
  144. int ret;
  145. int8_t device_init = 0;
  146. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  147. if (indio_dev == NULL)
  148. return -ENOMEM;
  149. st = iio_priv(indio_dev);
  150. spi_set_drvdata(spi, indio_dev);
  151. st->spi = spi;
  152. info = &tlc4541_chip_info[spi_get_device_id(spi)->driver_data];
  153. indio_dev->name = spi_get_device_id(spi)->name;
  154. indio_dev->modes = INDIO_DIRECT_MODE;
  155. indio_dev->channels = info->channels;
  156. indio_dev->num_channels = info->num_channels;
  157. indio_dev->info = &tlc4541_info;
  158. /* perform reset */
  159. spi_write(spi, &device_init, 1);
  160. /* Setup default message */
  161. st->scan_single_xfer[0].rx_buf = &st->rx_buf[0];
  162. st->scan_single_xfer[0].len = 3;
  163. st->scan_single_xfer[1].delay.value = 3;
  164. st->scan_single_xfer[1].delay.unit = SPI_DELAY_UNIT_NSECS;
  165. st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
  166. st->scan_single_xfer[2].len = 2;
  167. spi_message_init_with_transfers(&st->scan_single_msg,
  168. st->scan_single_xfer, 3);
  169. st->reg = devm_regulator_get(&spi->dev, "vref");
  170. if (IS_ERR(st->reg))
  171. return PTR_ERR(st->reg);
  172. ret = regulator_enable(st->reg);
  173. if (ret)
  174. return ret;
  175. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  176. &tlc4541_trigger_handler, NULL);
  177. if (ret)
  178. goto error_disable_reg;
  179. ret = iio_device_register(indio_dev);
  180. if (ret)
  181. goto error_cleanup_buffer;
  182. return 0;
  183. error_cleanup_buffer:
  184. iio_triggered_buffer_cleanup(indio_dev);
  185. error_disable_reg:
  186. regulator_disable(st->reg);
  187. return ret;
  188. }
  189. static int tlc4541_remove(struct spi_device *spi)
  190. {
  191. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  192. struct tlc4541_state *st = iio_priv(indio_dev);
  193. iio_device_unregister(indio_dev);
  194. iio_triggered_buffer_cleanup(indio_dev);
  195. regulator_disable(st->reg);
  196. return 0;
  197. }
  198. static const struct of_device_id tlc4541_dt_ids[] = {
  199. { .compatible = "ti,tlc3541", },
  200. { .compatible = "ti,tlc4541", },
  201. {}
  202. };
  203. MODULE_DEVICE_TABLE(of, tlc4541_dt_ids);
  204. static const struct spi_device_id tlc4541_id[] = {
  205. {"tlc3541", TLC3541},
  206. {"tlc4541", TLC4541},
  207. {}
  208. };
  209. MODULE_DEVICE_TABLE(spi, tlc4541_id);
  210. static struct spi_driver tlc4541_driver = {
  211. .driver = {
  212. .name = "tlc4541",
  213. .of_match_table = tlc4541_dt_ids,
  214. },
  215. .probe = tlc4541_probe,
  216. .remove = tlc4541_remove,
  217. .id_table = tlc4541_id,
  218. };
  219. module_spi_driver(tlc4541_driver);
  220. MODULE_AUTHOR("Phil Reid <preid@electromag.com.au>");
  221. MODULE_DESCRIPTION("Texas Instruments TLC4541 ADC");
  222. MODULE_LICENSE("GPL v2");