adis16130.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_message msg;
  45. struct spi_transfer xfer = {
  46. .tx_buf = st->buf,
  47. .rx_buf = st->buf,
  48. .len = 4,
  49. };
  50. mutex_lock(&st->buf_lock);
  51. st->buf[0] = ADIS16130_CON_RD | reg_addr;
  52. st->buf[1] = st->buf[2] = st->buf[3] = 0;
  53. spi_message_init(&msg);
  54. spi_message_add_tail(&xfer, &msg);
  55. ret = spi_sync(st->us, &msg);
  56. if (ret == 0)
  57. *val = (st->buf[1] << 16) | (st->buf[2] << 8) | st->buf[3];
  58. mutex_unlock(&st->buf_lock);
  59. return ret;
  60. }
  61. static int adis16130_read_raw(struct iio_dev *indio_dev,
  62. struct iio_chan_spec const *chan,
  63. int *val, int *val2,
  64. long mask)
  65. {
  66. int ret;
  67. u32 temp;
  68. switch (mask) {
  69. case IIO_CHAN_INFO_RAW:
  70. /* Take the iio_dev status lock */
  71. mutex_lock(&indio_dev->mlock);
  72. ret = adis16130_spi_read(indio_dev, chan->address, &temp);
  73. mutex_unlock(&indio_dev->mlock);
  74. if (ret)
  75. return ret;
  76. *val = temp;
  77. return IIO_VAL_INT;
  78. case IIO_CHAN_INFO_SCALE:
  79. switch (chan->type) {
  80. case IIO_ANGL_VEL:
  81. /* 0 degree = 838860, 250 degree = 14260608 */
  82. *val = 250;
  83. *val2 = 336440817; /* RAD_TO_DEGREE(14260608 - 8388608) */
  84. return IIO_VAL_FRACTIONAL;
  85. case IIO_TEMP:
  86. /* 0C = 8036283, 105C = 9516048 */
  87. *val = 105000;
  88. *val2 = 9516048 - 8036283;
  89. return IIO_VAL_FRACTIONAL;
  90. default:
  91. return -EINVAL;
  92. }
  93. case IIO_CHAN_INFO_OFFSET:
  94. switch (chan->type) {
  95. case IIO_ANGL_VEL:
  96. *val = -8388608;
  97. return IIO_VAL_INT;
  98. case IIO_TEMP:
  99. *val = -8036283;
  100. return IIO_VAL_INT;
  101. default:
  102. return -EINVAL;
  103. }
  104. }
  105. return -EINVAL;
  106. }
  107. static const struct iio_chan_spec adis16130_channels[] = {
  108. {
  109. .type = IIO_ANGL_VEL,
  110. .modified = 1,
  111. .channel2 = IIO_MOD_Z,
  112. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  113. BIT(IIO_CHAN_INFO_SCALE) |
  114. BIT(IIO_CHAN_INFO_OFFSET),
  115. .address = ADIS16130_RATEDATA,
  116. }, {
  117. .type = IIO_TEMP,
  118. .indexed = 1,
  119. .channel = 0,
  120. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  121. BIT(IIO_CHAN_INFO_SCALE) |
  122. BIT(IIO_CHAN_INFO_OFFSET),
  123. .address = ADIS16130_TEMPDATA,
  124. }
  125. };
  126. static const struct iio_info adis16130_info = {
  127. .read_raw = &adis16130_read_raw,
  128. .driver_module = THIS_MODULE,
  129. };
  130. static int adis16130_probe(struct spi_device *spi)
  131. {
  132. struct adis16130_state *st;
  133. struct iio_dev *indio_dev;
  134. /* setup the industrialio driver allocated elements */
  135. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  136. if (!indio_dev)
  137. return -ENOMEM;
  138. st = iio_priv(indio_dev);
  139. /* this is only used for removal purposes */
  140. spi_set_drvdata(spi, indio_dev);
  141. st->us = spi;
  142. mutex_init(&st->buf_lock);
  143. indio_dev->name = spi->dev.driver->name;
  144. indio_dev->channels = adis16130_channels;
  145. indio_dev->num_channels = ARRAY_SIZE(adis16130_channels);
  146. indio_dev->dev.parent = &spi->dev;
  147. indio_dev->info = &adis16130_info;
  148. indio_dev->modes = INDIO_DIRECT_MODE;
  149. return iio_device_register(indio_dev);
  150. }
  151. static int adis16130_remove(struct spi_device *spi)
  152. {
  153. iio_device_unregister(spi_get_drvdata(spi));
  154. return 0;
  155. }
  156. static struct spi_driver adis16130_driver = {
  157. .driver = {
  158. .name = "adis16130",
  159. .owner = THIS_MODULE,
  160. },
  161. .probe = adis16130_probe,
  162. .remove = adis16130_remove,
  163. };
  164. module_spi_driver(adis16130_driver);
  165. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  166. MODULE_DESCRIPTION("Analog Devices ADIS16130 High Precision Angular Rate");
  167. MODULE_LICENSE("GPL v2");
  168. MODULE_ALIAS("spi:adis16130");