ti-adc0832.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * ADC0831/ADC0832/ADC0834/ADC0838 8-bit ADC driver
  3. *
  4. * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * Datasheet: http://www.ti.com/lit/ds/symlink/adc0832-n.pdf
  11. */
  12. #include <linux/module.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/regulator/consumer.h>
  16. enum {
  17. adc0831,
  18. adc0832,
  19. adc0834,
  20. adc0838,
  21. };
  22. struct adc0832 {
  23. struct spi_device *spi;
  24. struct regulator *reg;
  25. struct mutex lock;
  26. u8 mux_bits;
  27. u8 tx_buf[2] ____cacheline_aligned;
  28. u8 rx_buf[2];
  29. };
  30. #define ADC0832_VOLTAGE_CHANNEL(chan) \
  31. { \
  32. .type = IIO_VOLTAGE, \
  33. .indexed = 1, \
  34. .channel = chan, \
  35. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  36. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
  37. }
  38. #define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2) \
  39. { \
  40. .type = IIO_VOLTAGE, \
  41. .indexed = 1, \
  42. .channel = (chan1), \
  43. .channel2 = (chan2), \
  44. .differential = 1, \
  45. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  46. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
  47. }
  48. static const struct iio_chan_spec adc0831_channels[] = {
  49. ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
  50. };
  51. static const struct iio_chan_spec adc0832_channels[] = {
  52. ADC0832_VOLTAGE_CHANNEL(0),
  53. ADC0832_VOLTAGE_CHANNEL(1),
  54. ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
  55. ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0),
  56. };
  57. static const struct iio_chan_spec adc0834_channels[] = {
  58. ADC0832_VOLTAGE_CHANNEL(0),
  59. ADC0832_VOLTAGE_CHANNEL(1),
  60. ADC0832_VOLTAGE_CHANNEL(2),
  61. ADC0832_VOLTAGE_CHANNEL(3),
  62. ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
  63. ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0),
  64. ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3),
  65. ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2),
  66. };
  67. static const struct iio_chan_spec adc0838_channels[] = {
  68. ADC0832_VOLTAGE_CHANNEL(0),
  69. ADC0832_VOLTAGE_CHANNEL(1),
  70. ADC0832_VOLTAGE_CHANNEL(2),
  71. ADC0832_VOLTAGE_CHANNEL(3),
  72. ADC0832_VOLTAGE_CHANNEL(4),
  73. ADC0832_VOLTAGE_CHANNEL(5),
  74. ADC0832_VOLTAGE_CHANNEL(6),
  75. ADC0832_VOLTAGE_CHANNEL(7),
  76. ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
  77. ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0),
  78. ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3),
  79. ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2),
  80. ADC0832_VOLTAGE_CHANNEL_DIFF(4, 5),
  81. ADC0832_VOLTAGE_CHANNEL_DIFF(5, 4),
  82. ADC0832_VOLTAGE_CHANNEL_DIFF(6, 7),
  83. ADC0832_VOLTAGE_CHANNEL_DIFF(7, 6),
  84. };
  85. static int adc0831_adc_conversion(struct adc0832 *adc)
  86. {
  87. struct spi_device *spi = adc->spi;
  88. int ret;
  89. ret = spi_read(spi, &adc->rx_buf, 2);
  90. if (ret)
  91. return ret;
  92. /*
  93. * Skip TRI-STATE and a leading zero
  94. */
  95. return (adc->rx_buf[0] << 2 & 0xff) | (adc->rx_buf[1] >> 6);
  96. }
  97. static int adc0832_adc_conversion(struct adc0832 *adc, int channel,
  98. bool differential)
  99. {
  100. struct spi_device *spi = adc->spi;
  101. struct spi_transfer xfer = {
  102. .tx_buf = adc->tx_buf,
  103. .rx_buf = adc->rx_buf,
  104. .len = 2,
  105. };
  106. int ret;
  107. if (!adc->mux_bits)
  108. return adc0831_adc_conversion(adc);
  109. /* start bit */
  110. adc->tx_buf[0] = 1 << (adc->mux_bits + 1);
  111. /* single-ended or differential */
  112. adc->tx_buf[0] |= differential ? 0 : (1 << adc->mux_bits);
  113. /* odd / sign */
  114. adc->tx_buf[0] |= (channel % 2) << (adc->mux_bits - 1);
  115. /* select */
  116. if (adc->mux_bits > 1)
  117. adc->tx_buf[0] |= channel / 2;
  118. /* align Data output BIT7 (MSB) to 8-bit boundary */
  119. adc->tx_buf[0] <<= 1;
  120. ret = spi_sync_transfer(spi, &xfer, 1);
  121. if (ret)
  122. return ret;
  123. return adc->rx_buf[1];
  124. }
  125. static int adc0832_read_raw(struct iio_dev *iio,
  126. struct iio_chan_spec const *channel, int *value,
  127. int *shift, long mask)
  128. {
  129. struct adc0832 *adc = iio_priv(iio);
  130. switch (mask) {
  131. case IIO_CHAN_INFO_RAW:
  132. mutex_lock(&adc->lock);
  133. *value = adc0832_adc_conversion(adc, channel->channel,
  134. channel->differential);
  135. mutex_unlock(&adc->lock);
  136. if (*value < 0)
  137. return *value;
  138. return IIO_VAL_INT;
  139. case IIO_CHAN_INFO_SCALE:
  140. *value = regulator_get_voltage(adc->reg);
  141. if (*value < 0)
  142. return *value;
  143. /* convert regulator output voltage to mV */
  144. *value /= 1000;
  145. *shift = 8;
  146. return IIO_VAL_FRACTIONAL_LOG2;
  147. }
  148. return -EINVAL;
  149. }
  150. static const struct iio_info adc0832_info = {
  151. .read_raw = adc0832_read_raw,
  152. .driver_module = THIS_MODULE,
  153. };
  154. static int adc0832_probe(struct spi_device *spi)
  155. {
  156. struct iio_dev *indio_dev;
  157. struct adc0832 *adc;
  158. int ret;
  159. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  160. if (!indio_dev)
  161. return -ENOMEM;
  162. adc = iio_priv(indio_dev);
  163. adc->spi = spi;
  164. mutex_init(&adc->lock);
  165. indio_dev->name = spi_get_device_id(spi)->name;
  166. indio_dev->dev.parent = &spi->dev;
  167. indio_dev->dev.of_node = spi->dev.of_node;
  168. indio_dev->info = &adc0832_info;
  169. indio_dev->modes = INDIO_DIRECT_MODE;
  170. switch (spi_get_device_id(spi)->driver_data) {
  171. case adc0831:
  172. adc->mux_bits = 0;
  173. indio_dev->channels = adc0831_channels;
  174. indio_dev->num_channels = ARRAY_SIZE(adc0831_channels);
  175. break;
  176. case adc0832:
  177. adc->mux_bits = 1;
  178. indio_dev->channels = adc0832_channels;
  179. indio_dev->num_channels = ARRAY_SIZE(adc0832_channels);
  180. break;
  181. case adc0834:
  182. adc->mux_bits = 2;
  183. indio_dev->channels = adc0834_channels;
  184. indio_dev->num_channels = ARRAY_SIZE(adc0834_channels);
  185. break;
  186. case adc0838:
  187. adc->mux_bits = 3;
  188. indio_dev->channels = adc0838_channels;
  189. indio_dev->num_channels = ARRAY_SIZE(adc0838_channels);
  190. break;
  191. default:
  192. return -EINVAL;
  193. }
  194. adc->reg = devm_regulator_get(&spi->dev, "vref");
  195. if (IS_ERR(adc->reg))
  196. return PTR_ERR(adc->reg);
  197. ret = regulator_enable(adc->reg);
  198. if (ret)
  199. return ret;
  200. spi_set_drvdata(spi, indio_dev);
  201. ret = iio_device_register(indio_dev);
  202. if (ret)
  203. regulator_disable(adc->reg);
  204. return ret;
  205. }
  206. static int adc0832_remove(struct spi_device *spi)
  207. {
  208. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  209. struct adc0832 *adc = iio_priv(indio_dev);
  210. iio_device_unregister(indio_dev);
  211. regulator_disable(adc->reg);
  212. return 0;
  213. }
  214. #ifdef CONFIG_OF
  215. static const struct of_device_id adc0832_dt_ids[] = {
  216. { .compatible = "ti,adc0831", },
  217. { .compatible = "ti,adc0832", },
  218. { .compatible = "ti,adc0834", },
  219. { .compatible = "ti,adc0838", },
  220. {}
  221. };
  222. MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
  223. #endif
  224. static const struct spi_device_id adc0832_id[] = {
  225. { "adc0831", adc0831 },
  226. { "adc0832", adc0832 },
  227. { "adc0834", adc0834 },
  228. { "adc0838", adc0838 },
  229. {}
  230. };
  231. MODULE_DEVICE_TABLE(spi, adc0832_id);
  232. static struct spi_driver adc0832_driver = {
  233. .driver = {
  234. .name = "adc0832",
  235. .of_match_table = of_match_ptr(adc0832_dt_ids),
  236. },
  237. .probe = adc0832_probe,
  238. .remove = adc0832_remove,
  239. .id_table = adc0832_id,
  240. };
  241. module_spi_driver(adc0832_driver);
  242. MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
  243. MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
  244. MODULE_LICENSE("GPL v2");