ad7887.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * AD7887 SPI ADC driver
  3. *
  4. * Copyright 2010-2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/sysfs.h>
  19. #include <linux/iio/buffer.h>
  20. #include <linux/iio/trigger_consumer.h>
  21. #include <linux/iio/triggered_buffer.h>
  22. #include <linux/platform_data/ad7887.h>
  23. #define AD7887_REF_DIS (1 << 5) /* on-chip reference disable */
  24. #define AD7887_DUAL (1 << 4) /* dual-channel mode */
  25. #define AD7887_CH_AIN1 (1 << 3) /* convert on channel 1, DUAL=1 */
  26. #define AD7887_CH_AIN0 (0 << 3) /* convert on channel 0, DUAL=0,1 */
  27. #define AD7887_PM_MODE1 (0) /* CS based shutdown */
  28. #define AD7887_PM_MODE2 (1) /* full on */
  29. #define AD7887_PM_MODE3 (2) /* auto shutdown after conversion */
  30. #define AD7887_PM_MODE4 (3) /* standby mode */
  31. enum ad7887_channels {
  32. AD7887_CH0,
  33. AD7887_CH0_CH1,
  34. AD7887_CH1,
  35. };
  36. #define RES_MASK(bits) ((1 << (bits)) - 1)
  37. /**
  38. * struct ad7887_chip_info - chip specifc information
  39. * @int_vref_mv: the internal reference voltage
  40. * @channel: channel specification
  41. */
  42. struct ad7887_chip_info {
  43. u16 int_vref_mv;
  44. struct iio_chan_spec channel[3];
  45. };
  46. struct ad7887_state {
  47. struct spi_device *spi;
  48. const struct ad7887_chip_info *chip_info;
  49. struct regulator *reg;
  50. struct spi_transfer xfer[4];
  51. struct spi_message msg[3];
  52. struct spi_message *ring_msg;
  53. unsigned char tx_cmd_buf[4];
  54. /*
  55. * DMA (thus cache coherency maintenance) requires the
  56. * transfer buffers to live in their own cache lines.
  57. * Buffer needs to be large enough to hold two 16 bit samples and a
  58. * 64 bit aligned 64 bit timestamp.
  59. */
  60. unsigned char data[ALIGN(4, sizeof(s64)) + sizeof(s64)]
  61. ____cacheline_aligned;
  62. };
  63. enum ad7887_supported_device_ids {
  64. ID_AD7887
  65. };
  66. static int ad7887_ring_preenable(struct iio_dev *indio_dev)
  67. {
  68. struct ad7887_state *st = iio_priv(indio_dev);
  69. int ret;
  70. ret = iio_sw_buffer_preenable(indio_dev);
  71. if (ret < 0)
  72. return ret;
  73. /* We know this is a single long so can 'cheat' */
  74. switch (*indio_dev->active_scan_mask) {
  75. case (1 << 0):
  76. st->ring_msg = &st->msg[AD7887_CH0];
  77. break;
  78. case (1 << 1):
  79. st->ring_msg = &st->msg[AD7887_CH1];
  80. /* Dummy read: push CH1 setting down to hardware */
  81. spi_sync(st->spi, st->ring_msg);
  82. break;
  83. case ((1 << 1) | (1 << 0)):
  84. st->ring_msg = &st->msg[AD7887_CH0_CH1];
  85. break;
  86. }
  87. return 0;
  88. }
  89. static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
  90. {
  91. struct ad7887_state *st = iio_priv(indio_dev);
  92. /* dummy read: restore default CH0 settin */
  93. return spi_sync(st->spi, &st->msg[AD7887_CH0]);
  94. }
  95. /**
  96. * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
  97. *
  98. * Currently there is no option in this driver to disable the saving of
  99. * timestamps within the ring.
  100. **/
  101. static irqreturn_t ad7887_trigger_handler(int irq, void *p)
  102. {
  103. struct iio_poll_func *pf = p;
  104. struct iio_dev *indio_dev = pf->indio_dev;
  105. struct ad7887_state *st = iio_priv(indio_dev);
  106. s64 time_ns;
  107. int b_sent;
  108. b_sent = spi_sync(st->spi, st->ring_msg);
  109. if (b_sent)
  110. goto done;
  111. time_ns = iio_get_time_ns();
  112. if (indio_dev->scan_timestamp)
  113. memcpy(st->data + indio_dev->scan_bytes - sizeof(s64),
  114. &time_ns, sizeof(time_ns));
  115. iio_push_to_buffers(indio_dev, st->data);
  116. done:
  117. iio_trigger_notify_done(indio_dev->trig);
  118. return IRQ_HANDLED;
  119. }
  120. static const struct iio_buffer_setup_ops ad7887_ring_setup_ops = {
  121. .preenable = &ad7887_ring_preenable,
  122. .postenable = &iio_triggered_buffer_postenable,
  123. .predisable = &iio_triggered_buffer_predisable,
  124. .postdisable = &ad7887_ring_postdisable,
  125. };
  126. static int ad7887_scan_direct(struct ad7887_state *st, unsigned ch)
  127. {
  128. int ret = spi_sync(st->spi, &st->msg[ch]);
  129. if (ret)
  130. return ret;
  131. return (st->data[(ch * 2)] << 8) | st->data[(ch * 2) + 1];
  132. }
  133. static int ad7887_read_raw(struct iio_dev *indio_dev,
  134. struct iio_chan_spec const *chan,
  135. int *val,
  136. int *val2,
  137. long m)
  138. {
  139. int ret;
  140. struct ad7887_state *st = iio_priv(indio_dev);
  141. switch (m) {
  142. case IIO_CHAN_INFO_RAW:
  143. mutex_lock(&indio_dev->mlock);
  144. if (iio_buffer_enabled(indio_dev))
  145. ret = -EBUSY;
  146. else
  147. ret = ad7887_scan_direct(st, chan->address);
  148. mutex_unlock(&indio_dev->mlock);
  149. if (ret < 0)
  150. return ret;
  151. *val = ret >> chan->scan_type.shift;
  152. *val &= RES_MASK(chan->scan_type.realbits);
  153. return IIO_VAL_INT;
  154. case IIO_CHAN_INFO_SCALE:
  155. if (st->reg) {
  156. *val = regulator_get_voltage(st->reg);
  157. if (*val < 0)
  158. return *val;
  159. *val /= 1000;
  160. } else {
  161. *val = st->chip_info->int_vref_mv;
  162. }
  163. *val2 = chan->scan_type.realbits;
  164. return IIO_VAL_FRACTIONAL_LOG2;
  165. }
  166. return -EINVAL;
  167. }
  168. static const struct ad7887_chip_info ad7887_chip_info_tbl[] = {
  169. /*
  170. * More devices added in future
  171. */
  172. [ID_AD7887] = {
  173. .channel[0] = {
  174. .type = IIO_VOLTAGE,
  175. .indexed = 1,
  176. .channel = 1,
  177. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
  178. IIO_CHAN_INFO_SCALE_SHARED_BIT,
  179. .address = 1,
  180. .scan_index = 1,
  181. .scan_type = IIO_ST('u', 12, 16, 0),
  182. },
  183. .channel[1] = {
  184. .type = IIO_VOLTAGE,
  185. .indexed = 1,
  186. .channel = 0,
  187. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
  188. IIO_CHAN_INFO_SCALE_SHARED_BIT,
  189. .address = 0,
  190. .scan_index = 0,
  191. .scan_type = IIO_ST('u', 12, 16, 0),
  192. },
  193. .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
  194. .int_vref_mv = 2500,
  195. },
  196. };
  197. static const struct iio_info ad7887_info = {
  198. .read_raw = &ad7887_read_raw,
  199. .driver_module = THIS_MODULE,
  200. };
  201. static int ad7887_probe(struct spi_device *spi)
  202. {
  203. struct ad7887_platform_data *pdata = spi->dev.platform_data;
  204. struct ad7887_state *st;
  205. struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st));
  206. uint8_t mode;
  207. int ret;
  208. if (indio_dev == NULL)
  209. return -ENOMEM;
  210. st = iio_priv(indio_dev);
  211. if (!pdata || !pdata->use_onchip_ref) {
  212. st->reg = regulator_get(&spi->dev, "vref");
  213. if (IS_ERR(st->reg)) {
  214. ret = PTR_ERR(st->reg);
  215. goto error_free;
  216. }
  217. ret = regulator_enable(st->reg);
  218. if (ret)
  219. goto error_put_reg;
  220. }
  221. st->chip_info =
  222. &ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data];
  223. spi_set_drvdata(spi, indio_dev);
  224. st->spi = spi;
  225. /* Estabilish that the iio_dev is a child of the spi device */
  226. indio_dev->dev.parent = &spi->dev;
  227. indio_dev->name = spi_get_device_id(spi)->name;
  228. indio_dev->info = &ad7887_info;
  229. indio_dev->modes = INDIO_DIRECT_MODE;
  230. /* Setup default message */
  231. mode = AD7887_PM_MODE4;
  232. if (!pdata || !pdata->use_onchip_ref)
  233. mode |= AD7887_REF_DIS;
  234. if (pdata && pdata->en_dual)
  235. mode |= AD7887_DUAL;
  236. st->tx_cmd_buf[0] = AD7887_CH_AIN0 | mode;
  237. st->xfer[0].rx_buf = &st->data[0];
  238. st->xfer[0].tx_buf = &st->tx_cmd_buf[0];
  239. st->xfer[0].len = 2;
  240. spi_message_init(&st->msg[AD7887_CH0]);
  241. spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]);
  242. if (pdata && pdata->en_dual) {
  243. st->tx_cmd_buf[2] = AD7887_CH_AIN1 | mode;
  244. st->xfer[1].rx_buf = &st->data[0];
  245. st->xfer[1].tx_buf = &st->tx_cmd_buf[2];
  246. st->xfer[1].len = 2;
  247. st->xfer[2].rx_buf = &st->data[2];
  248. st->xfer[2].tx_buf = &st->tx_cmd_buf[0];
  249. st->xfer[2].len = 2;
  250. spi_message_init(&st->msg[AD7887_CH0_CH1]);
  251. spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]);
  252. spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]);
  253. st->xfer[3].rx_buf = &st->data[2];
  254. st->xfer[3].tx_buf = &st->tx_cmd_buf[2];
  255. st->xfer[3].len = 2;
  256. spi_message_init(&st->msg[AD7887_CH1]);
  257. spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]);
  258. indio_dev->channels = st->chip_info->channel;
  259. indio_dev->num_channels = 3;
  260. } else {
  261. indio_dev->channels = &st->chip_info->channel[1];
  262. indio_dev->num_channels = 2;
  263. }
  264. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  265. &ad7887_trigger_handler, &ad7887_ring_setup_ops);
  266. if (ret)
  267. goto error_disable_reg;
  268. ret = iio_device_register(indio_dev);
  269. if (ret)
  270. goto error_unregister_ring;
  271. return 0;
  272. error_unregister_ring:
  273. iio_triggered_buffer_cleanup(indio_dev);
  274. error_disable_reg:
  275. if (st->reg)
  276. regulator_disable(st->reg);
  277. error_put_reg:
  278. if (st->reg)
  279. regulator_put(st->reg);
  280. error_free:
  281. iio_device_free(indio_dev);
  282. return ret;
  283. }
  284. static int ad7887_remove(struct spi_device *spi)
  285. {
  286. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  287. struct ad7887_state *st = iio_priv(indio_dev);
  288. iio_device_unregister(indio_dev);
  289. iio_triggered_buffer_cleanup(indio_dev);
  290. if (st->reg) {
  291. regulator_disable(st->reg);
  292. regulator_put(st->reg);
  293. }
  294. iio_device_free(indio_dev);
  295. return 0;
  296. }
  297. static const struct spi_device_id ad7887_id[] = {
  298. {"ad7887", ID_AD7887},
  299. {}
  300. };
  301. MODULE_DEVICE_TABLE(spi, ad7887_id);
  302. static struct spi_driver ad7887_driver = {
  303. .driver = {
  304. .name = "ad7887",
  305. .owner = THIS_MODULE,
  306. },
  307. .probe = ad7887_probe,
  308. .remove = ad7887_remove,
  309. .id_table = ad7887_id,
  310. };
  311. module_spi_driver(ad7887_driver);
  312. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  313. MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
  314. MODULE_LICENSE("GPL v2");