adis16130.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. break;
  94. case IIO_CHAN_INFO_OFFSET:
  95. switch (chan->type) {
  96. case IIO_ANGL_VEL:
  97. *val = -8388608;
  98. return IIO_VAL_INT;
  99. case IIO_TEMP:
  100. *val = -8036283;
  101. return IIO_VAL_INT;
  102. default:
  103. return -EINVAL;
  104. }
  105. break;
  106. }
  107. return -EINVAL;
  108. }
  109. static const struct iio_chan_spec adis16130_channels[] = {
  110. {
  111. .type = IIO_ANGL_VEL,
  112. .modified = 1,
  113. .channel2 = IIO_MOD_Z,
  114. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  115. BIT(IIO_CHAN_INFO_SCALE) |
  116. BIT(IIO_CHAN_INFO_OFFSET),
  117. .address = ADIS16130_RATEDATA,
  118. }, {
  119. .type = IIO_TEMP,
  120. .indexed = 1,
  121. .channel = 0,
  122. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  123. BIT(IIO_CHAN_INFO_SCALE) |
  124. BIT(IIO_CHAN_INFO_OFFSET),
  125. .address = ADIS16130_TEMPDATA,
  126. }
  127. };
  128. static const struct iio_info adis16130_info = {
  129. .read_raw = &adis16130_read_raw,
  130. .driver_module = THIS_MODULE,
  131. };
  132. static int adis16130_probe(struct spi_device *spi)
  133. {
  134. struct adis16130_state *st;
  135. struct iio_dev *indio_dev;
  136. /* setup the industrialio driver allocated elements */
  137. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  138. if (!indio_dev)
  139. return -ENOMEM;
  140. st = iio_priv(indio_dev);
  141. /* this is only used for removal purposes */
  142. spi_set_drvdata(spi, indio_dev);
  143. st->us = spi;
  144. mutex_init(&st->buf_lock);
  145. indio_dev->name = spi->dev.driver->name;
  146. indio_dev->channels = adis16130_channels;
  147. indio_dev->num_channels = ARRAY_SIZE(adis16130_channels);
  148. indio_dev->dev.parent = &spi->dev;
  149. indio_dev->info = &adis16130_info;
  150. indio_dev->modes = INDIO_DIRECT_MODE;
  151. return iio_device_register(indio_dev);
  152. }
  153. static int adis16130_remove(struct spi_device *spi)
  154. {
  155. iio_device_unregister(spi_get_drvdata(spi));
  156. return 0;
  157. }
  158. static struct spi_driver adis16130_driver = {
  159. .driver = {
  160. .name = "adis16130",
  161. .owner = THIS_MODULE,
  162. },
  163. .probe = adis16130_probe,
  164. .remove = adis16130_remove,
  165. };
  166. module_spi_driver(adis16130_driver);
  167. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  168. MODULE_DESCRIPTION("Analog Devices ADIS16130 High Precision Angular Rate");
  169. MODULE_LICENSE("GPL v2");
  170. MODULE_ALIAS("spi:adis16130");