ad2s1200.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ad2s1200.c simple support for the ADI Resolver to Digital Converters:
  4. * AD2S1200/1205
  5. *
  6. * Copyright (c) 2018-2018 David Veenstra <[email protected]>
  7. * Copyright (c) 2010-2010 Analog Devices Inc.
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/module.h>
  14. #include <linux/mod_devicetable.h>
  15. #include <linux/mutex.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/types.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #define DRV_NAME "ad2s1200"
  22. /* input clock on serial interface */
  23. #define AD2S1200_HZ 8192000
  24. /* clock period in nano second */
  25. #define AD2S1200_TSCLK (1000000000 / AD2S1200_HZ)
  26. /**
  27. * struct ad2s1200_state - driver instance specific data.
  28. * @lock: protects both the GPIO pins and the rx buffer.
  29. * @sdev: spi device.
  30. * @sample: GPIO pin SAMPLE.
  31. * @rdvel: GPIO pin RDVEL.
  32. * @rx: buffer for spi transfers.
  33. */
  34. struct ad2s1200_state {
  35. struct mutex lock;
  36. struct spi_device *sdev;
  37. struct gpio_desc *sample;
  38. struct gpio_desc *rdvel;
  39. __be16 rx __aligned(IIO_DMA_MINALIGN);
  40. };
  41. static int ad2s1200_read_raw(struct iio_dev *indio_dev,
  42. struct iio_chan_spec const *chan,
  43. int *val,
  44. int *val2,
  45. long m)
  46. {
  47. struct ad2s1200_state *st = iio_priv(indio_dev);
  48. int ret;
  49. switch (m) {
  50. case IIO_CHAN_INFO_SCALE:
  51. switch (chan->type) {
  52. case IIO_ANGL:
  53. /* 2 * Pi / (2^12 - 1) ~= 0.001534355 */
  54. *val = 0;
  55. *val2 = 1534355;
  56. return IIO_VAL_INT_PLUS_NANO;
  57. case IIO_ANGL_VEL:
  58. /* 2 * Pi ~= 6.283185 */
  59. *val = 6;
  60. *val2 = 283185;
  61. return IIO_VAL_INT_PLUS_MICRO;
  62. default:
  63. return -EINVAL;
  64. }
  65. break;
  66. case IIO_CHAN_INFO_RAW:
  67. mutex_lock(&st->lock);
  68. gpiod_set_value(st->sample, 0);
  69. /* delay (6 * AD2S1200_TSCLK + 20) nano seconds */
  70. udelay(1);
  71. gpiod_set_value(st->sample, 1);
  72. gpiod_set_value(st->rdvel, !!(chan->type == IIO_ANGL));
  73. ret = spi_read(st->sdev, &st->rx, 2);
  74. if (ret < 0) {
  75. mutex_unlock(&st->lock);
  76. return ret;
  77. }
  78. switch (chan->type) {
  79. case IIO_ANGL:
  80. *val = be16_to_cpup(&st->rx) >> 4;
  81. break;
  82. case IIO_ANGL_VEL:
  83. *val = sign_extend32(be16_to_cpup(&st->rx) >> 4, 11);
  84. break;
  85. default:
  86. mutex_unlock(&st->lock);
  87. return -EINVAL;
  88. }
  89. /* delay (2 * AD2S1200_TSCLK + 20) ns for sample pulse */
  90. udelay(1);
  91. mutex_unlock(&st->lock);
  92. return IIO_VAL_INT;
  93. default:
  94. break;
  95. }
  96. return -EINVAL;
  97. }
  98. static const struct iio_chan_spec ad2s1200_channels[] = {
  99. {
  100. .type = IIO_ANGL,
  101. .indexed = 1,
  102. .channel = 0,
  103. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  104. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  105. }, {
  106. .type = IIO_ANGL_VEL,
  107. .indexed = 1,
  108. .channel = 0,
  109. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  110. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  111. }
  112. };
  113. static const struct iio_info ad2s1200_info = {
  114. .read_raw = ad2s1200_read_raw,
  115. };
  116. static int ad2s1200_probe(struct spi_device *spi)
  117. {
  118. struct ad2s1200_state *st;
  119. struct iio_dev *indio_dev;
  120. int ret;
  121. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  122. if (!indio_dev)
  123. return -ENOMEM;
  124. spi_set_drvdata(spi, indio_dev);
  125. st = iio_priv(indio_dev);
  126. mutex_init(&st->lock);
  127. st->sdev = spi;
  128. st->sample = devm_gpiod_get(&spi->dev, "adi,sample", GPIOD_OUT_LOW);
  129. if (IS_ERR(st->sample)) {
  130. dev_err(&spi->dev, "Failed to claim SAMPLE gpio: err=%ld\n",
  131. PTR_ERR(st->sample));
  132. return PTR_ERR(st->sample);
  133. }
  134. st->rdvel = devm_gpiod_get(&spi->dev, "adi,rdvel", GPIOD_OUT_LOW);
  135. if (IS_ERR(st->rdvel)) {
  136. dev_err(&spi->dev, "Failed to claim RDVEL gpio: err=%ld\n",
  137. PTR_ERR(st->rdvel));
  138. return PTR_ERR(st->rdvel);
  139. }
  140. indio_dev->info = &ad2s1200_info;
  141. indio_dev->modes = INDIO_DIRECT_MODE;
  142. indio_dev->channels = ad2s1200_channels;
  143. indio_dev->num_channels = ARRAY_SIZE(ad2s1200_channels);
  144. indio_dev->name = spi_get_device_id(spi)->name;
  145. spi->max_speed_hz = AD2S1200_HZ;
  146. spi->mode = SPI_MODE_3;
  147. ret = spi_setup(spi);
  148. if (ret < 0) {
  149. dev_err(&spi->dev, "spi_setup failed!\n");
  150. return ret;
  151. }
  152. return devm_iio_device_register(&spi->dev, indio_dev);
  153. }
  154. static const struct of_device_id ad2s1200_of_match[] = {
  155. { .compatible = "adi,ad2s1200", },
  156. { .compatible = "adi,ad2s1205", },
  157. { }
  158. };
  159. MODULE_DEVICE_TABLE(of, ad2s1200_of_match);
  160. static const struct spi_device_id ad2s1200_id[] = {
  161. { "ad2s1200" },
  162. { "ad2s1205" },
  163. {}
  164. };
  165. MODULE_DEVICE_TABLE(spi, ad2s1200_id);
  166. static struct spi_driver ad2s1200_driver = {
  167. .driver = {
  168. .name = DRV_NAME,
  169. .of_match_table = ad2s1200_of_match,
  170. },
  171. .probe = ad2s1200_probe,
  172. .id_table = ad2s1200_id,
  173. };
  174. module_spi_driver(ad2s1200_driver);
  175. MODULE_AUTHOR("David Veenstra <[email protected]>");
  176. MODULE_AUTHOR("Graff Yang <[email protected]>");
  177. MODULE_DESCRIPTION("Analog Devices AD2S1200/1205 Resolver to Digital SPI driver");
  178. MODULE_LICENSE("GPL v2");