ad7298.c 9.1 KB

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