ad7298.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * AD7298 SPI ADC driver
  3. *
  4. * Copyright 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/delay.h>
  16. #include <linux/module.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/buffer.h>
  21. #include <linux/iio/trigger_consumer.h>
  22. #include <linux/iio/triggered_buffer.h>
  23. #include <linux/platform_data/ad7298.h>
  24. #define AD7298_WRITE (1 << 15) /* write to the control register */
  25. #define AD7298_REPEAT (1 << 14) /* repeated conversion enable */
  26. #define AD7298_CH(x) (1 << (13 - (x))) /* channel select */
  27. #define AD7298_TSENSE (1 << 5) /* temperature conversion enable */
  28. #define AD7298_EXTREF (1 << 2) /* external reference enable */
  29. #define AD7298_TAVG (1 << 1) /* temperature sensor averaging enable */
  30. #define AD7298_PDD (1 << 0) /* partial power down enable */
  31. #define AD7298_MAX_CHAN 8
  32. #define AD7298_BITS 12
  33. #define AD7298_STORAGE_BITS 16
  34. #define AD7298_INTREF_mV 2500
  35. #define AD7298_CH_TEMP 9
  36. #define RES_MASK(bits) ((1 << (bits)) - 1)
  37. struct ad7298_state {
  38. struct spi_device *spi;
  39. struct regulator *reg;
  40. unsigned ext_ref;
  41. struct spi_transfer ring_xfer[10];
  42. struct spi_transfer scan_single_xfer[3];
  43. struct spi_message ring_msg;
  44. struct spi_message scan_single_msg;
  45. /*
  46. * DMA (thus cache coherency maintenance) requires the
  47. * transfer buffers to live in their own cache lines.
  48. */
  49. __be16 rx_buf[12] ____cacheline_aligned;
  50. __be16 tx_buf[2];
  51. };
  52. #define AD7298_V_CHAN(index) \
  53. { \
  54. .type = IIO_VOLTAGE, \
  55. .indexed = 1, \
  56. .channel = index, \
  57. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  58. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  59. .address = index, \
  60. .scan_index = index, \
  61. .scan_type = { \
  62. .sign = 'u', \
  63. .realbits = 12, \
  64. .storagebits = 16, \
  65. .endianness = IIO_BE, \
  66. }, \
  67. }
  68. static const struct iio_chan_spec ad7298_channels[] = {
  69. {
  70. .type = IIO_TEMP,
  71. .indexed = 1,
  72. .channel = 0,
  73. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  74. BIT(IIO_CHAN_INFO_SCALE) |
  75. BIT(IIO_CHAN_INFO_OFFSET),
  76. .address = AD7298_CH_TEMP,
  77. .scan_index = -1,
  78. .scan_type = {
  79. .sign = 's',
  80. .realbits = 32,
  81. .storagebits = 32,
  82. },
  83. },
  84. AD7298_V_CHAN(0),
  85. AD7298_V_CHAN(1),
  86. AD7298_V_CHAN(2),
  87. AD7298_V_CHAN(3),
  88. AD7298_V_CHAN(4),
  89. AD7298_V_CHAN(5),
  90. AD7298_V_CHAN(6),
  91. AD7298_V_CHAN(7),
  92. IIO_CHAN_SOFT_TIMESTAMP(8),
  93. };
  94. /**
  95. * ad7298_update_scan_mode() setup the spi transfer buffer for the new scan mask
  96. **/
  97. static int ad7298_update_scan_mode(struct iio_dev *indio_dev,
  98. const unsigned long *active_scan_mask)
  99. {
  100. struct ad7298_state *st = iio_priv(indio_dev);
  101. int i, m;
  102. unsigned short command;
  103. int scan_count;
  104. /* Now compute overall size */
  105. scan_count = bitmap_weight(active_scan_mask, indio_dev->masklength);
  106. command = AD7298_WRITE | st->ext_ref;
  107. for (i = 0, m = AD7298_CH(0); i < AD7298_MAX_CHAN; i++, m >>= 1)
  108. if (test_bit(i, active_scan_mask))
  109. command |= m;
  110. st->tx_buf[0] = cpu_to_be16(command);
  111. /* build spi ring message */
  112. st->ring_xfer[0].tx_buf = &st->tx_buf[0];
  113. st->ring_xfer[0].len = 2;
  114. st->ring_xfer[0].cs_change = 1;
  115. st->ring_xfer[1].tx_buf = &st->tx_buf[1];
  116. st->ring_xfer[1].len = 2;
  117. st->ring_xfer[1].cs_change = 1;
  118. spi_message_init(&st->ring_msg);
  119. spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
  120. spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
  121. for (i = 0; i < scan_count; i++) {
  122. st->ring_xfer[i + 2].rx_buf = &st->rx_buf[i];
  123. st->ring_xfer[i + 2].len = 2;
  124. st->ring_xfer[i + 2].cs_change = 1;
  125. spi_message_add_tail(&st->ring_xfer[i + 2], &st->ring_msg);
  126. }
  127. /* make sure last transfer cs_change is not set */
  128. st->ring_xfer[i + 1].cs_change = 0;
  129. return 0;
  130. }
  131. /**
  132. * ad7298_trigger_handler() bh of trigger launched polling to ring buffer
  133. *
  134. * Currently there is no option in this driver to disable the saving of
  135. * timestamps within the ring.
  136. **/
  137. static irqreturn_t ad7298_trigger_handler(int irq, void *p)
  138. {
  139. struct iio_poll_func *pf = p;
  140. struct iio_dev *indio_dev = pf->indio_dev;
  141. struct ad7298_state *st = iio_priv(indio_dev);
  142. int b_sent;
  143. b_sent = spi_sync(st->spi, &st->ring_msg);
  144. if (b_sent)
  145. goto done;
  146. iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
  147. iio_get_time_ns());
  148. done:
  149. iio_trigger_notify_done(indio_dev->trig);
  150. return IRQ_HANDLED;
  151. }
  152. static int ad7298_scan_direct(struct ad7298_state *st, unsigned ch)
  153. {
  154. int ret;
  155. st->tx_buf[0] = cpu_to_be16(AD7298_WRITE | st->ext_ref |
  156. (AD7298_CH(0) >> ch));
  157. ret = spi_sync(st->spi, &st->scan_single_msg);
  158. if (ret)
  159. return ret;
  160. return be16_to_cpu(st->rx_buf[0]);
  161. }
  162. static int ad7298_scan_temp(struct ad7298_state *st, int *val)
  163. {
  164. int ret;
  165. __be16 buf;
  166. buf = cpu_to_be16(AD7298_WRITE | AD7298_TSENSE |
  167. AD7298_TAVG | st->ext_ref);
  168. ret = spi_write(st->spi, (u8 *)&buf, 2);
  169. if (ret)
  170. return ret;
  171. buf = cpu_to_be16(0);
  172. ret = spi_write(st->spi, (u8 *)&buf, 2);
  173. if (ret)
  174. return ret;
  175. usleep_range(101, 1000); /* sleep > 100us */
  176. ret = spi_read(st->spi, (u8 *)&buf, 2);
  177. if (ret)
  178. return ret;
  179. *val = sign_extend32(be16_to_cpu(buf), 11);
  180. return 0;
  181. }
  182. static int ad7298_get_ref_voltage(struct ad7298_state *st)
  183. {
  184. int vref;
  185. if (st->ext_ref) {
  186. vref = regulator_get_voltage(st->reg);
  187. if (vref < 0)
  188. return vref;
  189. return vref / 1000;
  190. } else {
  191. return AD7298_INTREF_mV;
  192. }
  193. }
  194. static int ad7298_read_raw(struct iio_dev *indio_dev,
  195. struct iio_chan_spec const *chan,
  196. int *val,
  197. int *val2,
  198. long m)
  199. {
  200. int ret;
  201. struct ad7298_state *st = iio_priv(indio_dev);
  202. switch (m) {
  203. case IIO_CHAN_INFO_RAW:
  204. mutex_lock(&indio_dev->mlock);
  205. if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
  206. ret = -EBUSY;
  207. } else {
  208. if (chan->address == AD7298_CH_TEMP)
  209. ret = ad7298_scan_temp(st, val);
  210. else
  211. ret = ad7298_scan_direct(st, chan->address);
  212. }
  213. mutex_unlock(&indio_dev->mlock);
  214. if (ret < 0)
  215. return ret;
  216. if (chan->address != AD7298_CH_TEMP)
  217. *val = ret & RES_MASK(AD7298_BITS);
  218. return IIO_VAL_INT;
  219. case IIO_CHAN_INFO_SCALE:
  220. switch (chan->type) {
  221. case IIO_VOLTAGE:
  222. *val = ad7298_get_ref_voltage(st);
  223. *val2 = chan->scan_type.realbits;
  224. return IIO_VAL_FRACTIONAL_LOG2;
  225. case IIO_TEMP:
  226. *val = ad7298_get_ref_voltage(st);
  227. *val2 = 10;
  228. return IIO_VAL_FRACTIONAL;
  229. default:
  230. return -EINVAL;
  231. }
  232. case IIO_CHAN_INFO_OFFSET:
  233. *val = 1093 - 2732500 / ad7298_get_ref_voltage(st);
  234. return IIO_VAL_INT;
  235. }
  236. return -EINVAL;
  237. }
  238. static const struct iio_info ad7298_info = {
  239. .read_raw = &ad7298_read_raw,
  240. .update_scan_mode = ad7298_update_scan_mode,
  241. .driver_module = THIS_MODULE,
  242. };
  243. static int ad7298_probe(struct spi_device *spi)
  244. {
  245. struct ad7298_platform_data *pdata = spi->dev.platform_data;
  246. struct ad7298_state *st;
  247. struct iio_dev *indio_dev;
  248. int ret;
  249. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  250. if (indio_dev == NULL)
  251. return -ENOMEM;
  252. st = iio_priv(indio_dev);
  253. if (pdata && pdata->ext_ref)
  254. st->ext_ref = AD7298_EXTREF;
  255. if (st->ext_ref) {
  256. st->reg = devm_regulator_get(&spi->dev, "vref");
  257. if (IS_ERR(st->reg))
  258. return PTR_ERR(st->reg);
  259. ret = regulator_enable(st->reg);
  260. if (ret)
  261. return ret;
  262. }
  263. spi_set_drvdata(spi, indio_dev);
  264. st->spi = spi;
  265. indio_dev->name = spi_get_device_id(spi)->name;
  266. indio_dev->dev.parent = &spi->dev;
  267. indio_dev->modes = INDIO_DIRECT_MODE;
  268. indio_dev->channels = ad7298_channels;
  269. indio_dev->num_channels = ARRAY_SIZE(ad7298_channels);
  270. indio_dev->info = &ad7298_info;
  271. /* Setup default message */
  272. st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
  273. st->scan_single_xfer[0].len = 2;
  274. st->scan_single_xfer[0].cs_change = 1;
  275. st->scan_single_xfer[1].tx_buf = &st->tx_buf[1];
  276. st->scan_single_xfer[1].len = 2;
  277. st->scan_single_xfer[1].cs_change = 1;
  278. st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
  279. st->scan_single_xfer[2].len = 2;
  280. spi_message_init(&st->scan_single_msg);
  281. spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
  282. spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
  283. spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg);
  284. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  285. &ad7298_trigger_handler, NULL);
  286. if (ret)
  287. goto error_disable_reg;
  288. ret = iio_device_register(indio_dev);
  289. if (ret)
  290. goto error_cleanup_ring;
  291. return 0;
  292. error_cleanup_ring:
  293. iio_triggered_buffer_cleanup(indio_dev);
  294. error_disable_reg:
  295. if (st->ext_ref)
  296. regulator_disable(st->reg);
  297. return ret;
  298. }
  299. static int ad7298_remove(struct spi_device *spi)
  300. {
  301. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  302. struct ad7298_state *st = iio_priv(indio_dev);
  303. iio_device_unregister(indio_dev);
  304. iio_triggered_buffer_cleanup(indio_dev);
  305. if (st->ext_ref)
  306. regulator_disable(st->reg);
  307. return 0;
  308. }
  309. static const struct spi_device_id ad7298_id[] = {
  310. {"ad7298", 0},
  311. {}
  312. };
  313. MODULE_DEVICE_TABLE(spi, ad7298_id);
  314. static struct spi_driver ad7298_driver = {
  315. .driver = {
  316. .name = "ad7298",
  317. .owner = THIS_MODULE,
  318. },
  319. .probe = ad7298_probe,
  320. .remove = ad7298_remove,
  321. .id_table = ad7298_id,
  322. };
  323. module_spi_driver(ad7298_driver);
  324. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  325. MODULE_DESCRIPTION("Analog Devices AD7298 ADC");
  326. MODULE_LICENSE("GPL v2");