ti-adc0832.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. #include <linux/iio/buffer.h>
  17. #include <linux/iio/trigger.h>
  18. #include <linux/iio/triggered_buffer.h>
  19. #include <linux/iio/trigger_consumer.h>
  20. enum {
  21. adc0831,
  22. adc0832,
  23. adc0834,
  24. adc0838,
  25. };
  26. struct adc0832 {
  27. struct spi_device *spi;
  28. struct regulator *reg;
  29. struct mutex lock;
  30. u8 mux_bits;
  31. u8 tx_buf[2] ____cacheline_aligned;
  32. u8 rx_buf[2];
  33. };
  34. #define ADC0832_VOLTAGE_CHANNEL(chan) \
  35. { \
  36. .type = IIO_VOLTAGE, \
  37. .indexed = 1, \
  38. .channel = chan, \
  39. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  40. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  41. .scan_index = chan, \
  42. .scan_type = { \
  43. .sign = 'u', \
  44. .realbits = 8, \
  45. .storagebits = 8, \
  46. }, \
  47. }
  48. #define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si) \
  49. { \
  50. .type = IIO_VOLTAGE, \
  51. .indexed = 1, \
  52. .channel = (chan1), \
  53. .channel2 = (chan2), \
  54. .differential = 1, \
  55. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  56. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  57. .scan_index = si, \
  58. .scan_type = { \
  59. .sign = 'u', \
  60. .realbits = 8, \
  61. .storagebits = 8, \
  62. }, \
  63. }
  64. static const struct iio_chan_spec adc0831_channels[] = {
  65. ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 0),
  66. IIO_CHAN_SOFT_TIMESTAMP(1),
  67. };
  68. static const struct iio_chan_spec adc0832_channels[] = {
  69. ADC0832_VOLTAGE_CHANNEL(0),
  70. ADC0832_VOLTAGE_CHANNEL(1),
  71. ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
  72. ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
  73. IIO_CHAN_SOFT_TIMESTAMP(4),
  74. };
  75. static const struct iio_chan_spec adc0834_channels[] = {
  76. ADC0832_VOLTAGE_CHANNEL(0),
  77. ADC0832_VOLTAGE_CHANNEL(1),
  78. ADC0832_VOLTAGE_CHANNEL(2),
  79. ADC0832_VOLTAGE_CHANNEL(3),
  80. ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 4),
  81. ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 5),
  82. ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 6),
  83. ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 7),
  84. IIO_CHAN_SOFT_TIMESTAMP(8),
  85. };
  86. static const struct iio_chan_spec adc0838_channels[] = {
  87. ADC0832_VOLTAGE_CHANNEL(0),
  88. ADC0832_VOLTAGE_CHANNEL(1),
  89. ADC0832_VOLTAGE_CHANNEL(2),
  90. ADC0832_VOLTAGE_CHANNEL(3),
  91. ADC0832_VOLTAGE_CHANNEL(4),
  92. ADC0832_VOLTAGE_CHANNEL(5),
  93. ADC0832_VOLTAGE_CHANNEL(6),
  94. ADC0832_VOLTAGE_CHANNEL(7),
  95. ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
  96. ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
  97. ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
  98. ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
  99. ADC0832_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
  100. ADC0832_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
  101. ADC0832_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
  102. ADC0832_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
  103. IIO_CHAN_SOFT_TIMESTAMP(16),
  104. };
  105. static int adc0831_adc_conversion(struct adc0832 *adc)
  106. {
  107. struct spi_device *spi = adc->spi;
  108. int ret;
  109. ret = spi_read(spi, &adc->rx_buf, 2);
  110. if (ret)
  111. return ret;
  112. /*
  113. * Skip TRI-STATE and a leading zero
  114. */
  115. return (adc->rx_buf[0] << 2 & 0xff) | (adc->rx_buf[1] >> 6);
  116. }
  117. static int adc0832_adc_conversion(struct adc0832 *adc, int channel,
  118. bool differential)
  119. {
  120. struct spi_device *spi = adc->spi;
  121. struct spi_transfer xfer = {
  122. .tx_buf = adc->tx_buf,
  123. .rx_buf = adc->rx_buf,
  124. .len = 2,
  125. };
  126. int ret;
  127. if (!adc->mux_bits)
  128. return adc0831_adc_conversion(adc);
  129. /* start bit */
  130. adc->tx_buf[0] = 1 << (adc->mux_bits + 1);
  131. /* single-ended or differential */
  132. adc->tx_buf[0] |= differential ? 0 : (1 << adc->mux_bits);
  133. /* odd / sign */
  134. adc->tx_buf[0] |= (channel % 2) << (adc->mux_bits - 1);
  135. /* select */
  136. if (adc->mux_bits > 1)
  137. adc->tx_buf[0] |= channel / 2;
  138. /* align Data output BIT7 (MSB) to 8-bit boundary */
  139. adc->tx_buf[0] <<= 1;
  140. ret = spi_sync_transfer(spi, &xfer, 1);
  141. if (ret)
  142. return ret;
  143. return adc->rx_buf[1];
  144. }
  145. static int adc0832_read_raw(struct iio_dev *iio,
  146. struct iio_chan_spec const *channel, int *value,
  147. int *shift, long mask)
  148. {
  149. struct adc0832 *adc = iio_priv(iio);
  150. switch (mask) {
  151. case IIO_CHAN_INFO_RAW:
  152. mutex_lock(&adc->lock);
  153. *value = adc0832_adc_conversion(adc, channel->channel,
  154. channel->differential);
  155. mutex_unlock(&adc->lock);
  156. if (*value < 0)
  157. return *value;
  158. return IIO_VAL_INT;
  159. case IIO_CHAN_INFO_SCALE:
  160. *value = regulator_get_voltage(adc->reg);
  161. if (*value < 0)
  162. return *value;
  163. /* convert regulator output voltage to mV */
  164. *value /= 1000;
  165. *shift = 8;
  166. return IIO_VAL_FRACTIONAL_LOG2;
  167. }
  168. return -EINVAL;
  169. }
  170. static const struct iio_info adc0832_info = {
  171. .read_raw = adc0832_read_raw,
  172. .driver_module = THIS_MODULE,
  173. };
  174. static irqreturn_t adc0832_trigger_handler(int irq, void *p)
  175. {
  176. struct iio_poll_func *pf = p;
  177. struct iio_dev *indio_dev = pf->indio_dev;
  178. struct adc0832 *adc = iio_priv(indio_dev);
  179. u8 data[24] = { }; /* 16x 1 byte ADC data + 8 bytes timestamp */
  180. int scan_index;
  181. int i = 0;
  182. mutex_lock(&adc->lock);
  183. for_each_set_bit(scan_index, indio_dev->active_scan_mask,
  184. indio_dev->masklength) {
  185. const struct iio_chan_spec *scan_chan =
  186. &indio_dev->channels[scan_index];
  187. int ret = adc0832_adc_conversion(adc, scan_chan->channel,
  188. scan_chan->differential);
  189. if (ret < 0) {
  190. dev_warn(&adc->spi->dev,
  191. "failed to get conversion data\n");
  192. goto out;
  193. }
  194. data[i] = ret;
  195. i++;
  196. }
  197. iio_push_to_buffers_with_timestamp(indio_dev, data,
  198. iio_get_time_ns(indio_dev));
  199. out:
  200. mutex_unlock(&adc->lock);
  201. iio_trigger_notify_done(indio_dev->trig);
  202. return IRQ_HANDLED;
  203. }
  204. static int adc0832_probe(struct spi_device *spi)
  205. {
  206. struct iio_dev *indio_dev;
  207. struct adc0832 *adc;
  208. int ret;
  209. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  210. if (!indio_dev)
  211. return -ENOMEM;
  212. adc = iio_priv(indio_dev);
  213. adc->spi = spi;
  214. mutex_init(&adc->lock);
  215. indio_dev->name = spi_get_device_id(spi)->name;
  216. indio_dev->dev.parent = &spi->dev;
  217. indio_dev->dev.of_node = spi->dev.of_node;
  218. indio_dev->info = &adc0832_info;
  219. indio_dev->modes = INDIO_DIRECT_MODE;
  220. switch (spi_get_device_id(spi)->driver_data) {
  221. case adc0831:
  222. adc->mux_bits = 0;
  223. indio_dev->channels = adc0831_channels;
  224. indio_dev->num_channels = ARRAY_SIZE(adc0831_channels);
  225. break;
  226. case adc0832:
  227. adc->mux_bits = 1;
  228. indio_dev->channels = adc0832_channels;
  229. indio_dev->num_channels = ARRAY_SIZE(adc0832_channels);
  230. break;
  231. case adc0834:
  232. adc->mux_bits = 2;
  233. indio_dev->channels = adc0834_channels;
  234. indio_dev->num_channels = ARRAY_SIZE(adc0834_channels);
  235. break;
  236. case adc0838:
  237. adc->mux_bits = 3;
  238. indio_dev->channels = adc0838_channels;
  239. indio_dev->num_channels = ARRAY_SIZE(adc0838_channels);
  240. break;
  241. default:
  242. return -EINVAL;
  243. }
  244. adc->reg = devm_regulator_get(&spi->dev, "vref");
  245. if (IS_ERR(adc->reg))
  246. return PTR_ERR(adc->reg);
  247. ret = regulator_enable(adc->reg);
  248. if (ret)
  249. return ret;
  250. spi_set_drvdata(spi, indio_dev);
  251. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  252. adc0832_trigger_handler, NULL);
  253. if (ret)
  254. goto err_reg_disable;
  255. ret = iio_device_register(indio_dev);
  256. if (ret)
  257. goto err_buffer_cleanup;
  258. return 0;
  259. err_buffer_cleanup:
  260. iio_triggered_buffer_cleanup(indio_dev);
  261. err_reg_disable:
  262. regulator_disable(adc->reg);
  263. return ret;
  264. }
  265. static int adc0832_remove(struct spi_device *spi)
  266. {
  267. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  268. struct adc0832 *adc = iio_priv(indio_dev);
  269. iio_device_unregister(indio_dev);
  270. iio_triggered_buffer_cleanup(indio_dev);
  271. regulator_disable(adc->reg);
  272. return 0;
  273. }
  274. #ifdef CONFIG_OF
  275. static const struct of_device_id adc0832_dt_ids[] = {
  276. { .compatible = "ti,adc0831", },
  277. { .compatible = "ti,adc0832", },
  278. { .compatible = "ti,adc0834", },
  279. { .compatible = "ti,adc0838", },
  280. {}
  281. };
  282. MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
  283. #endif
  284. static const struct spi_device_id adc0832_id[] = {
  285. { "adc0831", adc0831 },
  286. { "adc0832", adc0832 },
  287. { "adc0834", adc0834 },
  288. { "adc0838", adc0838 },
  289. {}
  290. };
  291. MODULE_DEVICE_TABLE(spi, adc0832_id);
  292. static struct spi_driver adc0832_driver = {
  293. .driver = {
  294. .name = "adc0832",
  295. .of_match_table = of_match_ptr(adc0832_dt_ids),
  296. },
  297. .probe = adc0832_probe,
  298. .remove = adc0832_remove,
  299. .id_table = adc0832_id,
  300. };
  301. module_spi_driver(adc0832_driver);
  302. MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
  303. MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
  304. MODULE_LICENSE("GPL v2");