ti-ads7950.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Texas Instruments ADS7950 SPI ADC driver
  3. *
  4. * Copyright 2016 David Lechner <david@lechnology.com>
  5. *
  6. * Based on iio/ad7923.c:
  7. * Copyright 2011 Analog Devices Inc
  8. * Copyright 2012 CS Systemes d'Information
  9. *
  10. * And also on hwmon/ads79xx.c
  11. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
  12. * Nishanth Menon
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation version 2.
  17. *
  18. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  19. * kind, whether express or implied; without even the implied warranty
  20. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include <linux/bitops.h>
  24. #include <linux/device.h>
  25. #include <linux/err.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <linux/slab.h>
  31. #include <linux/spi/spi.h>
  32. #include <linux/iio/buffer.h>
  33. #include <linux/iio/iio.h>
  34. #include <linux/iio/sysfs.h>
  35. #include <linux/iio/trigger_consumer.h>
  36. #include <linux/iio/triggered_buffer.h>
  37. #define TI_ADS7950_CR_MANUAL BIT(12)
  38. #define TI_ADS7950_CR_WRITE BIT(11)
  39. #define TI_ADS7950_CR_CHAN(ch) ((ch) << 7)
  40. #define TI_ADS7950_CR_RANGE_5V BIT(6)
  41. #define TI_ADS7950_MAX_CHAN 16
  42. #define TI_ADS7950_TIMESTAMP_SIZE (sizeof(int64_t) / sizeof(__be16))
  43. /* val = value, dec = left shift, bits = number of bits of the mask */
  44. #define TI_ADS7950_EXTRACT(val, dec, bits) \
  45. (((val) >> (dec)) & ((1 << (bits)) - 1))
  46. struct ti_ads7950_state {
  47. struct spi_device *spi;
  48. struct spi_transfer ring_xfer[TI_ADS7950_MAX_CHAN + 2];
  49. struct spi_transfer scan_single_xfer[3];
  50. struct spi_message ring_msg;
  51. struct spi_message scan_single_msg;
  52. struct regulator *reg;
  53. unsigned int settings;
  54. /*
  55. * DMA (thus cache coherency maintenance) requires the
  56. * transfer buffers to live in their own cache lines.
  57. */
  58. __be16 rx_buf[TI_ADS7950_MAX_CHAN + TI_ADS7950_TIMESTAMP_SIZE]
  59. ____cacheline_aligned;
  60. __be16 tx_buf[TI_ADS7950_MAX_CHAN];
  61. };
  62. struct ti_ads7950_chip_info {
  63. const struct iio_chan_spec *channels;
  64. unsigned int num_channels;
  65. };
  66. enum ti_ads7950_id {
  67. TI_ADS7950,
  68. TI_ADS7951,
  69. TI_ADS7952,
  70. TI_ADS7953,
  71. TI_ADS7954,
  72. TI_ADS7955,
  73. TI_ADS7956,
  74. TI_ADS7957,
  75. TI_ADS7958,
  76. TI_ADS7959,
  77. TI_ADS7960,
  78. TI_ADS7961,
  79. };
  80. #define TI_ADS7950_V_CHAN(index, bits) \
  81. { \
  82. .type = IIO_VOLTAGE, \
  83. .indexed = 1, \
  84. .channel = index, \
  85. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  86. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  87. .address = index, \
  88. .datasheet_name = "CH##index", \
  89. .scan_index = index, \
  90. .scan_type = { \
  91. .sign = 'u', \
  92. .realbits = bits, \
  93. .storagebits = 16, \
  94. .shift = 12 - (bits), \
  95. .endianness = IIO_BE, \
  96. }, \
  97. }
  98. #define DECLARE_TI_ADS7950_4_CHANNELS(name, bits) \
  99. const struct iio_chan_spec name ## _channels[] = { \
  100. TI_ADS7950_V_CHAN(0, bits), \
  101. TI_ADS7950_V_CHAN(1, bits), \
  102. TI_ADS7950_V_CHAN(2, bits), \
  103. TI_ADS7950_V_CHAN(3, bits), \
  104. IIO_CHAN_SOFT_TIMESTAMP(4), \
  105. }
  106. #define DECLARE_TI_ADS7950_8_CHANNELS(name, bits) \
  107. const struct iio_chan_spec name ## _channels[] = { \
  108. TI_ADS7950_V_CHAN(0, bits), \
  109. TI_ADS7950_V_CHAN(1, bits), \
  110. TI_ADS7950_V_CHAN(2, bits), \
  111. TI_ADS7950_V_CHAN(3, bits), \
  112. TI_ADS7950_V_CHAN(4, bits), \
  113. TI_ADS7950_V_CHAN(5, bits), \
  114. TI_ADS7950_V_CHAN(6, bits), \
  115. TI_ADS7950_V_CHAN(7, bits), \
  116. IIO_CHAN_SOFT_TIMESTAMP(8), \
  117. }
  118. #define DECLARE_TI_ADS7950_12_CHANNELS(name, bits) \
  119. const struct iio_chan_spec name ## _channels[] = { \
  120. TI_ADS7950_V_CHAN(0, bits), \
  121. TI_ADS7950_V_CHAN(1, bits), \
  122. TI_ADS7950_V_CHAN(2, bits), \
  123. TI_ADS7950_V_CHAN(3, bits), \
  124. TI_ADS7950_V_CHAN(4, bits), \
  125. TI_ADS7950_V_CHAN(5, bits), \
  126. TI_ADS7950_V_CHAN(6, bits), \
  127. TI_ADS7950_V_CHAN(7, bits), \
  128. TI_ADS7950_V_CHAN(8, bits), \
  129. TI_ADS7950_V_CHAN(9, bits), \
  130. TI_ADS7950_V_CHAN(10, bits), \
  131. TI_ADS7950_V_CHAN(11, bits), \
  132. IIO_CHAN_SOFT_TIMESTAMP(12), \
  133. }
  134. #define DECLARE_TI_ADS7950_16_CHANNELS(name, bits) \
  135. const struct iio_chan_spec name ## _channels[] = { \
  136. TI_ADS7950_V_CHAN(0, bits), \
  137. TI_ADS7950_V_CHAN(1, bits), \
  138. TI_ADS7950_V_CHAN(2, bits), \
  139. TI_ADS7950_V_CHAN(3, bits), \
  140. TI_ADS7950_V_CHAN(4, bits), \
  141. TI_ADS7950_V_CHAN(5, bits), \
  142. TI_ADS7950_V_CHAN(6, bits), \
  143. TI_ADS7950_V_CHAN(7, bits), \
  144. TI_ADS7950_V_CHAN(8, bits), \
  145. TI_ADS7950_V_CHAN(9, bits), \
  146. TI_ADS7950_V_CHAN(10, bits), \
  147. TI_ADS7950_V_CHAN(11, bits), \
  148. TI_ADS7950_V_CHAN(12, bits), \
  149. TI_ADS7950_V_CHAN(13, bits), \
  150. TI_ADS7950_V_CHAN(14, bits), \
  151. TI_ADS7950_V_CHAN(15, bits), \
  152. IIO_CHAN_SOFT_TIMESTAMP(16), \
  153. }
  154. static DECLARE_TI_ADS7950_4_CHANNELS(ti_ads7950, 12);
  155. static DECLARE_TI_ADS7950_8_CHANNELS(ti_ads7951, 12);
  156. static DECLARE_TI_ADS7950_12_CHANNELS(ti_ads7952, 12);
  157. static DECLARE_TI_ADS7950_16_CHANNELS(ti_ads7953, 12);
  158. static DECLARE_TI_ADS7950_4_CHANNELS(ti_ads7954, 10);
  159. static DECLARE_TI_ADS7950_8_CHANNELS(ti_ads7955, 10);
  160. static DECLARE_TI_ADS7950_12_CHANNELS(ti_ads7956, 10);
  161. static DECLARE_TI_ADS7950_16_CHANNELS(ti_ads7957, 10);
  162. static DECLARE_TI_ADS7950_4_CHANNELS(ti_ads7958, 8);
  163. static DECLARE_TI_ADS7950_8_CHANNELS(ti_ads7959, 8);
  164. static DECLARE_TI_ADS7950_12_CHANNELS(ti_ads7960, 8);
  165. static DECLARE_TI_ADS7950_16_CHANNELS(ti_ads7961, 8);
  166. static const struct ti_ads7950_chip_info ti_ads7950_chip_info[] = {
  167. [TI_ADS7950] = {
  168. .channels = ti_ads7950_channels,
  169. .num_channels = ARRAY_SIZE(ti_ads7950_channels),
  170. },
  171. [TI_ADS7951] = {
  172. .channels = ti_ads7951_channels,
  173. .num_channels = ARRAY_SIZE(ti_ads7951_channels),
  174. },
  175. [TI_ADS7952] = {
  176. .channels = ti_ads7952_channels,
  177. .num_channels = ARRAY_SIZE(ti_ads7952_channels),
  178. },
  179. [TI_ADS7953] = {
  180. .channels = ti_ads7953_channels,
  181. .num_channels = ARRAY_SIZE(ti_ads7953_channels),
  182. },
  183. [TI_ADS7954] = {
  184. .channels = ti_ads7954_channels,
  185. .num_channels = ARRAY_SIZE(ti_ads7954_channels),
  186. },
  187. [TI_ADS7955] = {
  188. .channels = ti_ads7955_channels,
  189. .num_channels = ARRAY_SIZE(ti_ads7955_channels),
  190. },
  191. [TI_ADS7956] = {
  192. .channels = ti_ads7956_channels,
  193. .num_channels = ARRAY_SIZE(ti_ads7956_channels),
  194. },
  195. [TI_ADS7957] = {
  196. .channels = ti_ads7957_channels,
  197. .num_channels = ARRAY_SIZE(ti_ads7957_channels),
  198. },
  199. [TI_ADS7958] = {
  200. .channels = ti_ads7958_channels,
  201. .num_channels = ARRAY_SIZE(ti_ads7958_channels),
  202. },
  203. [TI_ADS7959] = {
  204. .channels = ti_ads7959_channels,
  205. .num_channels = ARRAY_SIZE(ti_ads7959_channels),
  206. },
  207. [TI_ADS7960] = {
  208. .channels = ti_ads7960_channels,
  209. .num_channels = ARRAY_SIZE(ti_ads7960_channels),
  210. },
  211. [TI_ADS7961] = {
  212. .channels = ti_ads7961_channels,
  213. .num_channels = ARRAY_SIZE(ti_ads7961_channels),
  214. },
  215. };
  216. /*
  217. * ti_ads7950_update_scan_mode() setup the spi transfer buffer for the new
  218. * scan mask
  219. */
  220. static int ti_ads7950_update_scan_mode(struct iio_dev *indio_dev,
  221. const unsigned long *active_scan_mask)
  222. {
  223. struct ti_ads7950_state *st = iio_priv(indio_dev);
  224. int i, cmd, len;
  225. len = 0;
  226. for_each_set_bit(i, active_scan_mask, indio_dev->num_channels) {
  227. cmd = TI_ADS7950_CR_WRITE | TI_ADS7950_CR_CHAN(i) | st->settings;
  228. st->tx_buf[len++] = cpu_to_be16(cmd);
  229. }
  230. /* Data for the 1st channel is not returned until the 3rd transfer */
  231. len += 2;
  232. for (i = 0; i < len; i++) {
  233. if ((i + 2) < len)
  234. st->ring_xfer[i].tx_buf = &st->tx_buf[i];
  235. if (i >= 2)
  236. st->ring_xfer[i].rx_buf = &st->rx_buf[i - 2];
  237. st->ring_xfer[i].len = 2;
  238. st->ring_xfer[i].cs_change = 1;
  239. }
  240. /* make sure last transfer's cs_change is not set */
  241. st->ring_xfer[len - 1].cs_change = 0;
  242. spi_message_init_with_transfers(&st->ring_msg, st->ring_xfer, len);
  243. return 0;
  244. }
  245. static irqreturn_t ti_ads7950_trigger_handler(int irq, void *p)
  246. {
  247. struct iio_poll_func *pf = p;
  248. struct iio_dev *indio_dev = pf->indio_dev;
  249. struct ti_ads7950_state *st = iio_priv(indio_dev);
  250. int ret;
  251. ret = spi_sync(st->spi, &st->ring_msg);
  252. if (ret < 0)
  253. goto out;
  254. iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
  255. iio_get_time_ns(indio_dev));
  256. out:
  257. iio_trigger_notify_done(indio_dev->trig);
  258. return IRQ_HANDLED;
  259. }
  260. static int ti_ads7950_scan_direct(struct ti_ads7950_state *st, unsigned int ch)
  261. {
  262. int ret, cmd;
  263. cmd = TI_ADS7950_CR_WRITE | TI_ADS7950_CR_CHAN(ch) | st->settings;
  264. st->tx_buf[0] = cpu_to_be16(cmd);
  265. ret = spi_sync(st->spi, &st->scan_single_msg);
  266. if (ret)
  267. return ret;
  268. return be16_to_cpu(st->rx_buf[0]);
  269. }
  270. static int ti_ads7950_get_range(struct ti_ads7950_state *st)
  271. {
  272. int vref;
  273. vref = regulator_get_voltage(st->reg);
  274. if (vref < 0)
  275. return vref;
  276. vref /= 1000;
  277. if (st->settings & TI_ADS7950_CR_RANGE_5V)
  278. vref *= 2;
  279. return vref;
  280. }
  281. static int ti_ads7950_read_raw(struct iio_dev *indio_dev,
  282. struct iio_chan_spec const *chan,
  283. int *val, int *val2, long m)
  284. {
  285. struct ti_ads7950_state *st = iio_priv(indio_dev);
  286. int ret;
  287. switch (m) {
  288. case IIO_CHAN_INFO_RAW:
  289. ret = iio_device_claim_direct_mode(indio_dev);
  290. if (ret < 0)
  291. return ret;
  292. ret = ti_ads7950_scan_direct(st, chan->address);
  293. iio_device_release_direct_mode(indio_dev);
  294. if (ret < 0)
  295. return ret;
  296. if (chan->address != TI_ADS7950_EXTRACT(ret, 12, 4))
  297. return -EIO;
  298. *val = TI_ADS7950_EXTRACT(ret, chan->scan_type.shift,
  299. chan->scan_type.realbits);
  300. return IIO_VAL_INT;
  301. case IIO_CHAN_INFO_SCALE:
  302. ret = ti_ads7950_get_range(st);
  303. if (ret < 0)
  304. return ret;
  305. *val = ret;
  306. *val2 = (1 << chan->scan_type.realbits) - 1;
  307. return IIO_VAL_FRACTIONAL;
  308. }
  309. return -EINVAL;
  310. }
  311. static const struct iio_info ti_ads7950_info = {
  312. .read_raw = &ti_ads7950_read_raw,
  313. .update_scan_mode = ti_ads7950_update_scan_mode,
  314. .driver_module = THIS_MODULE,
  315. };
  316. static int ti_ads7950_probe(struct spi_device *spi)
  317. {
  318. struct ti_ads7950_state *st;
  319. struct iio_dev *indio_dev;
  320. const struct ti_ads7950_chip_info *info;
  321. int ret;
  322. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  323. if (!indio_dev)
  324. return -ENOMEM;
  325. st = iio_priv(indio_dev);
  326. spi_set_drvdata(spi, indio_dev);
  327. st->spi = spi;
  328. st->settings = TI_ADS7950_CR_MANUAL | TI_ADS7950_CR_RANGE_5V;
  329. info = &ti_ads7950_chip_info[spi_get_device_id(spi)->driver_data];
  330. indio_dev->name = spi_get_device_id(spi)->name;
  331. indio_dev->dev.parent = &spi->dev;
  332. indio_dev->modes = INDIO_DIRECT_MODE;
  333. indio_dev->channels = info->channels;
  334. indio_dev->num_channels = info->num_channels;
  335. indio_dev->info = &ti_ads7950_info;
  336. /*
  337. * Setup default message. The sample is read at the end of the first
  338. * transfer, then it takes one full cycle to convert the sample and one
  339. * more cycle to send the value. The conversion process is driven by
  340. * the SPI clock, which is why we have 3 transfers. The middle one is
  341. * just dummy data sent while the chip is converting the sample that
  342. * was read at the end of the first transfer.
  343. */
  344. st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
  345. st->scan_single_xfer[0].len = 2;
  346. st->scan_single_xfer[0].cs_change = 1;
  347. st->scan_single_xfer[1].tx_buf = &st->tx_buf[0];
  348. st->scan_single_xfer[1].len = 2;
  349. st->scan_single_xfer[1].cs_change = 1;
  350. st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
  351. st->scan_single_xfer[2].len = 2;
  352. spi_message_init_with_transfers(&st->scan_single_msg,
  353. st->scan_single_xfer, 3);
  354. st->reg = devm_regulator_get(&spi->dev, "vref");
  355. if (IS_ERR(st->reg)) {
  356. dev_err(&spi->dev, "Failed get get regulator \"vref\"\n");
  357. return PTR_ERR(st->reg);
  358. }
  359. ret = regulator_enable(st->reg);
  360. if (ret) {
  361. dev_err(&spi->dev, "Failed to enable regulator \"vref\"\n");
  362. return ret;
  363. }
  364. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  365. &ti_ads7950_trigger_handler, NULL);
  366. if (ret) {
  367. dev_err(&spi->dev, "Failed to setup triggered buffer\n");
  368. goto error_disable_reg;
  369. }
  370. ret = iio_device_register(indio_dev);
  371. if (ret) {
  372. dev_err(&spi->dev, "Failed to register iio device\n");
  373. goto error_cleanup_ring;
  374. }
  375. return 0;
  376. error_cleanup_ring:
  377. iio_triggered_buffer_cleanup(indio_dev);
  378. error_disable_reg:
  379. regulator_disable(st->reg);
  380. return ret;
  381. }
  382. static int ti_ads7950_remove(struct spi_device *spi)
  383. {
  384. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  385. struct ti_ads7950_state *st = iio_priv(indio_dev);
  386. iio_device_unregister(indio_dev);
  387. iio_triggered_buffer_cleanup(indio_dev);
  388. regulator_disable(st->reg);
  389. return 0;
  390. }
  391. static const struct spi_device_id ti_ads7950_id[] = {
  392. { "ads7950", TI_ADS7950 },
  393. { "ads7951", TI_ADS7951 },
  394. { "ads7952", TI_ADS7952 },
  395. { "ads7953", TI_ADS7953 },
  396. { "ads7954", TI_ADS7954 },
  397. { "ads7955", TI_ADS7955 },
  398. { "ads7956", TI_ADS7956 },
  399. { "ads7957", TI_ADS7957 },
  400. { "ads7958", TI_ADS7958 },
  401. { "ads7959", TI_ADS7959 },
  402. { "ads7960", TI_ADS7960 },
  403. { "ads7961", TI_ADS7961 },
  404. { }
  405. };
  406. MODULE_DEVICE_TABLE(spi, ti_ads7950_id);
  407. static struct spi_driver ti_ads7950_driver = {
  408. .driver = {
  409. .name = "ads7950",
  410. },
  411. .probe = ti_ads7950_probe,
  412. .remove = ti_ads7950_remove,
  413. .id_table = ti_ads7950_id,
  414. };
  415. module_spi_driver(ti_ads7950_driver);
  416. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  417. MODULE_DESCRIPTION("TI TI_ADS7950 ADC");
  418. MODULE_LICENSE("GPL v2");