ad2s90.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ad2s90.c simple support for the ADI Resolver to Digital Converters: AD2S90
  4. *
  5. * Copyright (c) 2010-2010 Analog Devices Inc.
  6. */
  7. #include <linux/types.h>
  8. #include <linux/mutex.h>
  9. #include <linux/device.h>
  10. #include <linux/spi/spi.h>
  11. #include <linux/slab.h>
  12. #include <linux/sysfs.h>
  13. #include <linux/module.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/sysfs.h>
  16. /*
  17. * Although chip's max frequency is 2Mhz, it needs 600ns between CS and the
  18. * first falling edge of SCLK, so frequency should be at most 1 / (2 * 6e-7)
  19. */
  20. #define AD2S90_MAX_SPI_FREQ_HZ 830000
  21. struct ad2s90_state {
  22. struct mutex lock; /* lock to protect rx buffer */
  23. struct spi_device *sdev;
  24. u8 rx[2] __aligned(IIO_DMA_MINALIGN);
  25. };
  26. static int ad2s90_read_raw(struct iio_dev *indio_dev,
  27. struct iio_chan_spec const *chan,
  28. int *val,
  29. int *val2,
  30. long m)
  31. {
  32. int ret;
  33. struct ad2s90_state *st = iio_priv(indio_dev);
  34. if (chan->type != IIO_ANGL)
  35. return -EINVAL;
  36. switch (m) {
  37. case IIO_CHAN_INFO_SCALE:
  38. /* 2 * Pi / 2^12 */
  39. *val = 6283; /* mV */
  40. *val2 = 12;
  41. return IIO_VAL_FRACTIONAL_LOG2;
  42. case IIO_CHAN_INFO_RAW:
  43. mutex_lock(&st->lock);
  44. ret = spi_read(st->sdev, st->rx, 2);
  45. if (ret < 0) {
  46. mutex_unlock(&st->lock);
  47. return ret;
  48. }
  49. *val = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
  50. mutex_unlock(&st->lock);
  51. return IIO_VAL_INT;
  52. default:
  53. break;
  54. }
  55. return -EINVAL;
  56. }
  57. static const struct iio_info ad2s90_info = {
  58. .read_raw = ad2s90_read_raw,
  59. };
  60. static const struct iio_chan_spec ad2s90_chan = {
  61. .type = IIO_ANGL,
  62. .indexed = 1,
  63. .channel = 0,
  64. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  65. };
  66. static int ad2s90_probe(struct spi_device *spi)
  67. {
  68. struct iio_dev *indio_dev;
  69. struct ad2s90_state *st;
  70. if (spi->max_speed_hz > AD2S90_MAX_SPI_FREQ_HZ) {
  71. dev_err(&spi->dev, "SPI CLK, %d Hz exceeds %d Hz\n",
  72. spi->max_speed_hz, AD2S90_MAX_SPI_FREQ_HZ);
  73. return -EINVAL;
  74. }
  75. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  76. if (!indio_dev)
  77. return -ENOMEM;
  78. st = iio_priv(indio_dev);
  79. spi_set_drvdata(spi, indio_dev);
  80. mutex_init(&st->lock);
  81. st->sdev = spi;
  82. indio_dev->info = &ad2s90_info;
  83. indio_dev->modes = INDIO_DIRECT_MODE;
  84. indio_dev->channels = &ad2s90_chan;
  85. indio_dev->num_channels = 1;
  86. indio_dev->name = spi_get_device_id(spi)->name;
  87. return devm_iio_device_register(indio_dev->dev.parent, indio_dev);
  88. }
  89. static const struct of_device_id ad2s90_of_match[] = {
  90. { .compatible = "adi,ad2s90", },
  91. {}
  92. };
  93. MODULE_DEVICE_TABLE(of, ad2s90_of_match);
  94. static const struct spi_device_id ad2s90_id[] = {
  95. { "ad2s90" },
  96. {}
  97. };
  98. MODULE_DEVICE_TABLE(spi, ad2s90_id);
  99. static struct spi_driver ad2s90_driver = {
  100. .driver = {
  101. .name = "ad2s90",
  102. .of_match_table = ad2s90_of_match,
  103. },
  104. .probe = ad2s90_probe,
  105. .id_table = ad2s90_id,
  106. };
  107. module_spi_driver(ad2s90_driver);
  108. MODULE_AUTHOR("Graff Yang <[email protected]>");
  109. MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver");
  110. MODULE_LICENSE("GPL v2");