ad2s1200.c 5.0 KB

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