ti-ads7950.c 14 KB

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