ltc2496.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ltc2496.c - Driver for Analog Devices/Linear Technology LTC2496 ADC
  4. *
  5. * Based on ltc2497.c which has
  6. * Copyright (C) 2017 Analog Devices Inc.
  7. *
  8. * Licensed under the GPL-2.
  9. *
  10. * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/2496fc.pdf
  11. */
  12. #include <linux/spi/spi.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/driver.h>
  15. #include <linux/module.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/property.h>
  18. #include "ltc2497.h"
  19. struct ltc2496_driverdata {
  20. /* this must be the first member */
  21. struct ltc2497core_driverdata common_ddata;
  22. struct spi_device *spi;
  23. /*
  24. * DMA (thus cache coherency maintenance) may require the
  25. * transfer buffers to live in their own cache lines.
  26. */
  27. unsigned char rxbuf[3] __aligned(IIO_DMA_MINALIGN);
  28. unsigned char txbuf[3];
  29. };
  30. static int ltc2496_result_and_measure(struct ltc2497core_driverdata *ddata,
  31. u8 address, int *val)
  32. {
  33. struct ltc2496_driverdata *st =
  34. container_of(ddata, struct ltc2496_driverdata, common_ddata);
  35. struct spi_transfer t = {
  36. .tx_buf = st->txbuf,
  37. .rx_buf = st->rxbuf,
  38. .len = sizeof(st->txbuf),
  39. };
  40. int ret;
  41. st->txbuf[0] = LTC2497_ENABLE | address;
  42. ret = spi_sync_transfer(st->spi, &t, 1);
  43. if (ret < 0) {
  44. dev_err(&st->spi->dev, "spi_sync_transfer failed: %pe\n",
  45. ERR_PTR(ret));
  46. return ret;
  47. }
  48. if (val)
  49. *val = ((st->rxbuf[0] & 0x3f) << 12 |
  50. st->rxbuf[1] << 4 | st->rxbuf[2] >> 4) -
  51. (1 << 17);
  52. return 0;
  53. }
  54. static int ltc2496_probe(struct spi_device *spi)
  55. {
  56. struct iio_dev *indio_dev;
  57. struct ltc2496_driverdata *st;
  58. struct device *dev = &spi->dev;
  59. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  60. if (!indio_dev)
  61. return -ENOMEM;
  62. st = iio_priv(indio_dev);
  63. spi_set_drvdata(spi, indio_dev);
  64. st->spi = spi;
  65. st->common_ddata.result_and_measure = ltc2496_result_and_measure;
  66. st->common_ddata.chip_info = device_get_match_data(dev);
  67. return ltc2497core_probe(dev, indio_dev);
  68. }
  69. static void ltc2496_remove(struct spi_device *spi)
  70. {
  71. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  72. ltc2497core_remove(indio_dev);
  73. }
  74. static const struct ltc2497_chip_info ltc2496_info = {
  75. .resolution = 16,
  76. .name = NULL,
  77. };
  78. static const struct of_device_id ltc2496_of_match[] = {
  79. { .compatible = "lltc,ltc2496", .data = &ltc2496_info, },
  80. {},
  81. };
  82. MODULE_DEVICE_TABLE(of, ltc2496_of_match);
  83. static struct spi_driver ltc2496_driver = {
  84. .driver = {
  85. .name = "ltc2496",
  86. .of_match_table = ltc2496_of_match,
  87. },
  88. .probe = ltc2496_probe,
  89. .remove = ltc2496_remove,
  90. };
  91. module_spi_driver(ltc2496_driver);
  92. MODULE_AUTHOR("Uwe Kleine-König <u.kleine-kö[email protected]>");
  93. MODULE_DESCRIPTION("Linear Technology LTC2496 ADC driver");
  94. MODULE_LICENSE("GPL v2");