ti-tlc4541.c 6.7 KB

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