adis16080.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ADIS16080/100 Yaw Rate Gyroscope with SPI driver
  4. *
  5. * Copyright 2010 Analog Devices Inc.
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/mutex.h>
  9. #include <linux/device.h>
  10. #include <linux/kernel.h>
  11. #include <linux/spi/spi.h>
  12. #include <linux/slab.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/module.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/sysfs.h>
  17. #define ADIS16080_DIN_GYRO (0 << 10) /* Gyroscope output */
  18. #define ADIS16080_DIN_TEMP (1 << 10) /* Temperature output */
  19. #define ADIS16080_DIN_AIN1 (2 << 10)
  20. #define ADIS16080_DIN_AIN2 (3 << 10)
  21. /*
  22. * 1: Write contents on DIN to control register.
  23. * 0: No changes to control register.
  24. */
  25. #define ADIS16080_DIN_WRITE (1 << 15)
  26. struct adis16080_chip_info {
  27. int scale_val;
  28. int scale_val2;
  29. };
  30. /**
  31. * struct adis16080_state - device instance specific data
  32. * @us: actual spi_device to write data
  33. * @info: chip specific parameters
  34. * @buf: transmit or receive buffer
  35. * @lock: lock to protect buffer during reads
  36. **/
  37. struct adis16080_state {
  38. struct spi_device *us;
  39. const struct adis16080_chip_info *info;
  40. struct mutex lock;
  41. __be16 buf __aligned(IIO_DMA_MINALIGN);
  42. };
  43. static int adis16080_read_sample(struct iio_dev *indio_dev,
  44. u16 addr, int *val)
  45. {
  46. struct adis16080_state *st = iio_priv(indio_dev);
  47. int ret;
  48. struct spi_transfer t[] = {
  49. {
  50. .tx_buf = &st->buf,
  51. .len = 2,
  52. .cs_change = 1,
  53. }, {
  54. .rx_buf = &st->buf,
  55. .len = 2,
  56. },
  57. };
  58. st->buf = cpu_to_be16(addr | ADIS16080_DIN_WRITE);
  59. ret = spi_sync_transfer(st->us, t, ARRAY_SIZE(t));
  60. if (ret == 0)
  61. *val = sign_extend32(be16_to_cpu(st->buf), 11);
  62. return ret;
  63. }
  64. static int adis16080_read_raw(struct iio_dev *indio_dev,
  65. struct iio_chan_spec const *chan,
  66. int *val,
  67. int *val2,
  68. long mask)
  69. {
  70. struct adis16080_state *st = iio_priv(indio_dev);
  71. int ret;
  72. switch (mask) {
  73. case IIO_CHAN_INFO_RAW:
  74. mutex_lock(&st->lock);
  75. ret = adis16080_read_sample(indio_dev, chan->address, val);
  76. mutex_unlock(&st->lock);
  77. return ret ? ret : IIO_VAL_INT;
  78. case IIO_CHAN_INFO_SCALE:
  79. switch (chan->type) {
  80. case IIO_ANGL_VEL:
  81. *val = st->info->scale_val;
  82. *val2 = st->info->scale_val2;
  83. return IIO_VAL_FRACTIONAL;
  84. case IIO_VOLTAGE:
  85. /* VREF = 5V, 12 bits */
  86. *val = 5000;
  87. *val2 = 12;
  88. return IIO_VAL_FRACTIONAL_LOG2;
  89. case IIO_TEMP:
  90. /* 85 C = 585, 25 C = 0 */
  91. *val = 85000 - 25000;
  92. *val2 = 585;
  93. return IIO_VAL_FRACTIONAL;
  94. default:
  95. return -EINVAL;
  96. }
  97. case IIO_CHAN_INFO_OFFSET:
  98. switch (chan->type) {
  99. case IIO_VOLTAGE:
  100. /* 2.5 V = 0 */
  101. *val = 2048;
  102. return IIO_VAL_INT;
  103. case IIO_TEMP:
  104. /* 85 C = 585, 25 C = 0 */
  105. *val = DIV_ROUND_CLOSEST(25 * 585, 85 - 25);
  106. return IIO_VAL_INT;
  107. default:
  108. return -EINVAL;
  109. }
  110. default:
  111. break;
  112. }
  113. return -EINVAL;
  114. }
  115. static const struct iio_chan_spec adis16080_channels[] = {
  116. {
  117. .type = IIO_ANGL_VEL,
  118. .modified = 1,
  119. .channel2 = IIO_MOD_Z,
  120. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  121. BIT(IIO_CHAN_INFO_SCALE),
  122. .address = ADIS16080_DIN_GYRO,
  123. }, {
  124. .type = IIO_VOLTAGE,
  125. .indexed = 1,
  126. .channel = 0,
  127. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  128. BIT(IIO_CHAN_INFO_SCALE) |
  129. BIT(IIO_CHAN_INFO_OFFSET),
  130. .address = ADIS16080_DIN_AIN1,
  131. }, {
  132. .type = IIO_VOLTAGE,
  133. .indexed = 1,
  134. .channel = 1,
  135. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  136. BIT(IIO_CHAN_INFO_SCALE) |
  137. BIT(IIO_CHAN_INFO_OFFSET),
  138. .address = ADIS16080_DIN_AIN2,
  139. }, {
  140. .type = IIO_TEMP,
  141. .indexed = 1,
  142. .channel = 0,
  143. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  144. BIT(IIO_CHAN_INFO_SCALE) |
  145. BIT(IIO_CHAN_INFO_OFFSET),
  146. .address = ADIS16080_DIN_TEMP,
  147. }
  148. };
  149. static const struct iio_info adis16080_info = {
  150. .read_raw = &adis16080_read_raw,
  151. };
  152. enum {
  153. ID_ADIS16080,
  154. ID_ADIS16100,
  155. };
  156. static const struct adis16080_chip_info adis16080_chip_info[] = {
  157. [ID_ADIS16080] = {
  158. /* 80 degree = 819, 819 rad = 46925 degree */
  159. .scale_val = 80,
  160. .scale_val2 = 46925,
  161. },
  162. [ID_ADIS16100] = {
  163. /* 300 degree = 1230, 1230 rad = 70474 degree */
  164. .scale_val = 300,
  165. .scale_val2 = 70474,
  166. },
  167. };
  168. static int adis16080_probe(struct spi_device *spi)
  169. {
  170. const struct spi_device_id *id = spi_get_device_id(spi);
  171. struct adis16080_state *st;
  172. struct iio_dev *indio_dev;
  173. /* setup the industrialio driver allocated elements */
  174. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  175. if (!indio_dev)
  176. return -ENOMEM;
  177. st = iio_priv(indio_dev);
  178. mutex_init(&st->lock);
  179. /* Allocate the comms buffers */
  180. st->us = spi;
  181. st->info = &adis16080_chip_info[id->driver_data];
  182. indio_dev->name = spi->dev.driver->name;
  183. indio_dev->channels = adis16080_channels;
  184. indio_dev->num_channels = ARRAY_SIZE(adis16080_channels);
  185. indio_dev->info = &adis16080_info;
  186. indio_dev->modes = INDIO_DIRECT_MODE;
  187. return devm_iio_device_register(&spi->dev, indio_dev);
  188. }
  189. static const struct spi_device_id adis16080_ids[] = {
  190. { "adis16080", ID_ADIS16080 },
  191. { "adis16100", ID_ADIS16100 },
  192. {},
  193. };
  194. MODULE_DEVICE_TABLE(spi, adis16080_ids);
  195. static struct spi_driver adis16080_driver = {
  196. .driver = {
  197. .name = "adis16080",
  198. },
  199. .probe = adis16080_probe,
  200. .id_table = adis16080_ids,
  201. };
  202. module_spi_driver(adis16080_driver);
  203. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  204. MODULE_DESCRIPTION("Analog Devices ADIS16080/100 Yaw Rate Gyroscope Driver");
  205. MODULE_LICENSE("GPL v2");