ti-ads7950.c 14 KB

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