adis16130.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * ADIS16130 Digital Output, High Precision Angular Rate Sensor driver
  3. *
  4. * Copyright 2010 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/mutex.h>
  9. #include <linux/kernel.h>
  10. #include <linux/spi/spi.h>
  11. #include <linux/module.h>
  12. #include <linux/iio/iio.h>
  13. #define ADIS16130_CON 0x0
  14. #define ADIS16130_CON_RD (1 << 6)
  15. #define ADIS16130_IOP 0x1
  16. /* 1 = data-ready signal low when unread data on all channels; */
  17. #define ADIS16130_IOP_ALL_RDY (1 << 3)
  18. #define ADIS16130_IOP_SYNC (1 << 0) /* 1 = synchronization enabled */
  19. #define ADIS16130_RATEDATA 0x8 /* Gyroscope output, rate of rotation */
  20. #define ADIS16130_TEMPDATA 0xA /* Temperature output */
  21. #define ADIS16130_RATECS 0x28 /* Gyroscope channel setup */
  22. #define ADIS16130_RATECS_EN (1 << 3) /* 1 = channel enable; */
  23. #define ADIS16130_TEMPCS 0x2A /* Temperature channel setup */
  24. #define ADIS16130_TEMPCS_EN (1 << 3)
  25. #define ADIS16130_RATECONV 0x30
  26. #define ADIS16130_TEMPCONV 0x32
  27. #define ADIS16130_MODE 0x38
  28. #define ADIS16130_MODE_24BIT (1 << 1) /* 1 = 24-bit resolution; */
  29. /**
  30. * struct adis16130_state - device instance specific data
  31. * @us: actual spi_device to write data
  32. * @buf_lock: mutex to protect tx and rx
  33. * @buf: unified tx/rx buffer
  34. **/
  35. struct adis16130_state {
  36. struct spi_device *us;
  37. struct mutex buf_lock;
  38. u8 buf[4] ____cacheline_aligned;
  39. };
  40. static int adis16130_spi_read(struct iio_dev *indio_dev, u8 reg_addr, u32 *val)
  41. {
  42. int ret;
  43. struct adis16130_state *st = iio_priv(indio_dev);
  44. struct spi_transfer xfer = {
  45. .tx_buf = st->buf,
  46. .rx_buf = st->buf,
  47. .len = 4,
  48. };
  49. mutex_lock(&st->buf_lock);
  50. st->buf[0] = ADIS16130_CON_RD | reg_addr;
  51. st->buf[1] = st->buf[2] = st->buf[3] = 0;
  52. ret = spi_sync_transfer(st->us, &xfer, 1);
  53. if (ret == 0)
  54. *val = (st->buf[1] << 16) | (st->buf[2] << 8) | st->buf[3];
  55. mutex_unlock(&st->buf_lock);
  56. return ret;
  57. }
  58. static int adis16130_read_raw(struct iio_dev *indio_dev,
  59. struct iio_chan_spec const *chan,
  60. int *val, int *val2,
  61. long mask)
  62. {
  63. int ret;
  64. u32 temp;
  65. switch (mask) {
  66. case IIO_CHAN_INFO_RAW:
  67. /* Take the iio_dev status lock */
  68. mutex_lock(&indio_dev->mlock);
  69. ret = adis16130_spi_read(indio_dev, chan->address, &temp);
  70. mutex_unlock(&indio_dev->mlock);
  71. if (ret)
  72. return ret;
  73. *val = temp;
  74. return IIO_VAL_INT;
  75. case IIO_CHAN_INFO_SCALE:
  76. switch (chan->type) {
  77. case IIO_ANGL_VEL:
  78. /* 0 degree = 838860, 250 degree = 14260608 */
  79. *val = 250;
  80. *val2 = 336440817; /* RAD_TO_DEGREE(14260608 - 8388608) */
  81. return IIO_VAL_FRACTIONAL;
  82. case IIO_TEMP:
  83. /* 0C = 8036283, 105C = 9516048 */
  84. *val = 105000;
  85. *val2 = 9516048 - 8036283;
  86. return IIO_VAL_FRACTIONAL;
  87. default:
  88. return -EINVAL;
  89. }
  90. case IIO_CHAN_INFO_OFFSET:
  91. switch (chan->type) {
  92. case IIO_ANGL_VEL:
  93. *val = -8388608;
  94. return IIO_VAL_INT;
  95. case IIO_TEMP:
  96. *val = -8036283;
  97. return IIO_VAL_INT;
  98. default:
  99. return -EINVAL;
  100. }
  101. }
  102. return -EINVAL;
  103. }
  104. static const struct iio_chan_spec adis16130_channels[] = {
  105. {
  106. .type = IIO_ANGL_VEL,
  107. .modified = 1,
  108. .channel2 = IIO_MOD_Z,
  109. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  110. BIT(IIO_CHAN_INFO_SCALE) |
  111. BIT(IIO_CHAN_INFO_OFFSET),
  112. .address = ADIS16130_RATEDATA,
  113. }, {
  114. .type = IIO_TEMP,
  115. .indexed = 1,
  116. .channel = 0,
  117. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  118. BIT(IIO_CHAN_INFO_SCALE) |
  119. BIT(IIO_CHAN_INFO_OFFSET),
  120. .address = ADIS16130_TEMPDATA,
  121. }
  122. };
  123. static const struct iio_info adis16130_info = {
  124. .read_raw = &adis16130_read_raw,
  125. .driver_module = THIS_MODULE,
  126. };
  127. static int adis16130_probe(struct spi_device *spi)
  128. {
  129. struct adis16130_state *st;
  130. struct iio_dev *indio_dev;
  131. /* setup the industrialio driver allocated elements */
  132. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  133. if (!indio_dev)
  134. return -ENOMEM;
  135. st = iio_priv(indio_dev);
  136. /* this is only used for removal purposes */
  137. spi_set_drvdata(spi, indio_dev);
  138. st->us = spi;
  139. mutex_init(&st->buf_lock);
  140. indio_dev->name = spi->dev.driver->name;
  141. indio_dev->channels = adis16130_channels;
  142. indio_dev->num_channels = ARRAY_SIZE(adis16130_channels);
  143. indio_dev->dev.parent = &spi->dev;
  144. indio_dev->info = &adis16130_info;
  145. indio_dev->modes = INDIO_DIRECT_MODE;
  146. return devm_iio_device_register(&spi->dev, indio_dev);
  147. }
  148. static struct spi_driver adis16130_driver = {
  149. .driver = {
  150. .name = "adis16130",
  151. },
  152. .probe = adis16130_probe,
  153. };
  154. module_spi_driver(adis16130_driver);
  155. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  156. MODULE_DESCRIPTION("Analog Devices ADIS16130 High Precision Angular Rate");
  157. MODULE_LICENSE("GPL v2");
  158. MODULE_ALIAS("spi:adis16130");