adis16130.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ADIS16130 Digital Output, High Precision Angular Rate Sensor driver
  4. *
  5. * Copyright 2010 Analog Devices Inc.
  6. */
  7. #include <linux/mutex.h>
  8. #include <linux/kernel.h>
  9. #include <linux/spi/spi.h>
  10. #include <linux/module.h>
  11. #include <linux/iio/iio.h>
  12. #include <asm/unaligned.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 = get_unaligned_be24(&st->buf[1]);
  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. ret = adis16130_spi_read(indio_dev, chan->address, &temp);
  69. if (ret)
  70. return ret;
  71. *val = temp;
  72. return IIO_VAL_INT;
  73. case IIO_CHAN_INFO_SCALE:
  74. switch (chan->type) {
  75. case IIO_ANGL_VEL:
  76. /* 0 degree = 838860, 250 degree = 14260608 */
  77. *val = 250;
  78. *val2 = 336440817; /* RAD_TO_DEGREE(14260608 - 8388608) */
  79. return IIO_VAL_FRACTIONAL;
  80. case IIO_TEMP:
  81. /* 0C = 8036283, 105C = 9516048 */
  82. *val = 105000;
  83. *val2 = 9516048 - 8036283;
  84. return IIO_VAL_FRACTIONAL;
  85. default:
  86. return -EINVAL;
  87. }
  88. case IIO_CHAN_INFO_OFFSET:
  89. switch (chan->type) {
  90. case IIO_ANGL_VEL:
  91. *val = -8388608;
  92. return IIO_VAL_INT;
  93. case IIO_TEMP:
  94. *val = -8036283;
  95. return IIO_VAL_INT;
  96. default:
  97. return -EINVAL;
  98. }
  99. }
  100. return -EINVAL;
  101. }
  102. static const struct iio_chan_spec adis16130_channels[] = {
  103. {
  104. .type = IIO_ANGL_VEL,
  105. .modified = 1,
  106. .channel2 = IIO_MOD_Z,
  107. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  108. BIT(IIO_CHAN_INFO_SCALE) |
  109. BIT(IIO_CHAN_INFO_OFFSET),
  110. .address = ADIS16130_RATEDATA,
  111. }, {
  112. .type = IIO_TEMP,
  113. .indexed = 1,
  114. .channel = 0,
  115. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  116. BIT(IIO_CHAN_INFO_SCALE) |
  117. BIT(IIO_CHAN_INFO_OFFSET),
  118. .address = ADIS16130_TEMPDATA,
  119. }
  120. };
  121. static const struct iio_info adis16130_info = {
  122. .read_raw = &adis16130_read_raw,
  123. };
  124. static int adis16130_probe(struct spi_device *spi)
  125. {
  126. struct adis16130_state *st;
  127. struct iio_dev *indio_dev;
  128. /* setup the industrialio driver allocated elements */
  129. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  130. if (!indio_dev)
  131. return -ENOMEM;
  132. st = iio_priv(indio_dev);
  133. /* this is only used for removal purposes */
  134. spi_set_drvdata(spi, indio_dev);
  135. st->us = spi;
  136. mutex_init(&st->buf_lock);
  137. indio_dev->name = spi->dev.driver->name;
  138. indio_dev->channels = adis16130_channels;
  139. indio_dev->num_channels = ARRAY_SIZE(adis16130_channels);
  140. indio_dev->info = &adis16130_info;
  141. indio_dev->modes = INDIO_DIRECT_MODE;
  142. return devm_iio_device_register(&spi->dev, indio_dev);
  143. }
  144. static struct spi_driver adis16130_driver = {
  145. .driver = {
  146. .name = "adis16130",
  147. },
  148. .probe = adis16130_probe,
  149. };
  150. module_spi_driver(adis16130_driver);
  151. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  152. MODULE_DESCRIPTION("Analog Devices ADIS16130 High Precision Angular Rate");
  153. MODULE_LICENSE("GPL v2");
  154. MODULE_ALIAS("spi:adis16130");