ti-adc0832.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. };
  173. static irqreturn_t adc0832_trigger_handler(int irq, void *p)
  174. {
  175. struct iio_poll_func *pf = p;
  176. struct iio_dev *indio_dev = pf->indio_dev;
  177. struct adc0832 *adc = iio_priv(indio_dev);
  178. u8 data[24] = { }; /* 16x 1 byte ADC data + 8 bytes timestamp */
  179. int scan_index;
  180. int i = 0;
  181. mutex_lock(&adc->lock);
  182. for_each_set_bit(scan_index, indio_dev->active_scan_mask,
  183. indio_dev->masklength) {
  184. const struct iio_chan_spec *scan_chan =
  185. &indio_dev->channels[scan_index];
  186. int ret = adc0832_adc_conversion(adc, scan_chan->channel,
  187. scan_chan->differential);
  188. if (ret < 0) {
  189. dev_warn(&adc->spi->dev,
  190. "failed to get conversion data\n");
  191. goto out;
  192. }
  193. data[i] = ret;
  194. i++;
  195. }
  196. iio_push_to_buffers_with_timestamp(indio_dev, data,
  197. iio_get_time_ns(indio_dev));
  198. out:
  199. mutex_unlock(&adc->lock);
  200. iio_trigger_notify_done(indio_dev->trig);
  201. return IRQ_HANDLED;
  202. }
  203. static int adc0832_probe(struct spi_device *spi)
  204. {
  205. struct iio_dev *indio_dev;
  206. struct adc0832 *adc;
  207. int ret;
  208. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  209. if (!indio_dev)
  210. return -ENOMEM;
  211. adc = iio_priv(indio_dev);
  212. adc->spi = spi;
  213. mutex_init(&adc->lock);
  214. indio_dev->name = spi_get_device_id(spi)->name;
  215. indio_dev->dev.parent = &spi->dev;
  216. indio_dev->dev.of_node = spi->dev.of_node;
  217. indio_dev->info = &adc0832_info;
  218. indio_dev->modes = INDIO_DIRECT_MODE;
  219. switch (spi_get_device_id(spi)->driver_data) {
  220. case adc0831:
  221. adc->mux_bits = 0;
  222. indio_dev->channels = adc0831_channels;
  223. indio_dev->num_channels = ARRAY_SIZE(adc0831_channels);
  224. break;
  225. case adc0832:
  226. adc->mux_bits = 1;
  227. indio_dev->channels = adc0832_channels;
  228. indio_dev->num_channels = ARRAY_SIZE(adc0832_channels);
  229. break;
  230. case adc0834:
  231. adc->mux_bits = 2;
  232. indio_dev->channels = adc0834_channels;
  233. indio_dev->num_channels = ARRAY_SIZE(adc0834_channels);
  234. break;
  235. case adc0838:
  236. adc->mux_bits = 3;
  237. indio_dev->channels = adc0838_channels;
  238. indio_dev->num_channels = ARRAY_SIZE(adc0838_channels);
  239. break;
  240. default:
  241. return -EINVAL;
  242. }
  243. adc->reg = devm_regulator_get(&spi->dev, "vref");
  244. if (IS_ERR(adc->reg))
  245. return PTR_ERR(adc->reg);
  246. ret = regulator_enable(adc->reg);
  247. if (ret)
  248. return ret;
  249. spi_set_drvdata(spi, indio_dev);
  250. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  251. adc0832_trigger_handler, NULL);
  252. if (ret)
  253. goto err_reg_disable;
  254. ret = iio_device_register(indio_dev);
  255. if (ret)
  256. goto err_buffer_cleanup;
  257. return 0;
  258. err_buffer_cleanup:
  259. iio_triggered_buffer_cleanup(indio_dev);
  260. err_reg_disable:
  261. regulator_disable(adc->reg);
  262. return ret;
  263. }
  264. static int adc0832_remove(struct spi_device *spi)
  265. {
  266. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  267. struct adc0832 *adc = iio_priv(indio_dev);
  268. iio_device_unregister(indio_dev);
  269. iio_triggered_buffer_cleanup(indio_dev);
  270. regulator_disable(adc->reg);
  271. return 0;
  272. }
  273. #ifdef CONFIG_OF
  274. static const struct of_device_id adc0832_dt_ids[] = {
  275. { .compatible = "ti,adc0831", },
  276. { .compatible = "ti,adc0832", },
  277. { .compatible = "ti,adc0834", },
  278. { .compatible = "ti,adc0838", },
  279. {}
  280. };
  281. MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
  282. #endif
  283. static const struct spi_device_id adc0832_id[] = {
  284. { "adc0831", adc0831 },
  285. { "adc0832", adc0832 },
  286. { "adc0834", adc0834 },
  287. { "adc0838", adc0838 },
  288. {}
  289. };
  290. MODULE_DEVICE_TABLE(spi, adc0832_id);
  291. static struct spi_driver adc0832_driver = {
  292. .driver = {
  293. .name = "adc0832",
  294. .of_match_table = of_match_ptr(adc0832_dt_ids),
  295. },
  296. .probe = adc0832_probe,
  297. .remove = adc0832_remove,
  298. .id_table = adc0832_id,
  299. };
  300. module_spi_driver(adc0832_driver);
  301. MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
  302. MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
  303. MODULE_LICENSE("GPL v2");