adis16060_core.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * ADIS16060 Wide Bandwidth Yaw Rate Gyroscope with SPI driver
  3. *
  4. * Copyright 2010 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/delay.h>
  10. #include <linux/mutex.h>
  11. #include <linux/device.h>
  12. #include <linux/kernel.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/slab.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #define ADIS16060_GYRO 0x20 /* Measure Angular Rate (Gyro) */
  19. #define ADIS16060_TEMP_OUT 0x10 /* Measure Temperature */
  20. #define ADIS16060_AIN2 0x80 /* Measure AIN2 */
  21. #define ADIS16060_AIN1 0x40 /* Measure AIN1 */
  22. /**
  23. * struct adis16060_state - device instance specific data
  24. * @us_w: actual spi_device to write config
  25. * @us_r: actual spi_device to read back data
  26. * @buf: transmit or receive buffer
  27. * @buf_lock: mutex to protect tx and rx
  28. **/
  29. struct adis16060_state {
  30. struct spi_device *us_w;
  31. struct spi_device *us_r;
  32. struct mutex buf_lock;
  33. u8 buf[3] ____cacheline_aligned;
  34. };
  35. static struct iio_dev *adis16060_iio_dev;
  36. static int adis16060_spi_write(struct iio_dev *indio_dev, u8 val)
  37. {
  38. int ret;
  39. struct adis16060_state *st = iio_priv(indio_dev);
  40. mutex_lock(&st->buf_lock);
  41. st->buf[2] = val; /* The last 8 bits clocked in are latched */
  42. ret = spi_write(st->us_w, st->buf, 3);
  43. mutex_unlock(&st->buf_lock);
  44. return ret;
  45. }
  46. static int adis16060_spi_read(struct iio_dev *indio_dev, u16 *val)
  47. {
  48. int ret;
  49. struct adis16060_state *st = iio_priv(indio_dev);
  50. mutex_lock(&st->buf_lock);
  51. ret = spi_read(st->us_r, st->buf, 3);
  52. /* The internal successive approximation ADC begins the
  53. * conversion process on the falling edge of MSEL1 and
  54. * starts to place data MSB first on the DOUT line at
  55. * the 6th falling edge of SCLK
  56. */
  57. if (!ret)
  58. *val = ((st->buf[0] & 0x3) << 12) |
  59. (st->buf[1] << 4) |
  60. ((st->buf[2] >> 4) & 0xF);
  61. mutex_unlock(&st->buf_lock);
  62. return ret;
  63. }
  64. static int adis16060_read_raw(struct iio_dev *indio_dev,
  65. struct iio_chan_spec const *chan,
  66. int *val, int *val2,
  67. long mask)
  68. {
  69. u16 tval = 0;
  70. int ret;
  71. switch (mask) {
  72. case IIO_CHAN_INFO_RAW:
  73. /* Take the iio_dev status lock */
  74. mutex_lock(&indio_dev->mlock);
  75. ret = adis16060_spi_write(indio_dev, chan->address);
  76. if (ret < 0)
  77. goto out_unlock;
  78. ret = adis16060_spi_read(indio_dev, &tval);
  79. if (ret < 0)
  80. goto out_unlock;
  81. mutex_unlock(&indio_dev->mlock);
  82. *val = tval;
  83. return IIO_VAL_INT;
  84. case IIO_CHAN_INFO_OFFSET:
  85. *val = -7;
  86. *val2 = 461117;
  87. return IIO_VAL_INT_PLUS_MICRO;
  88. case IIO_CHAN_INFO_SCALE:
  89. *val = 0;
  90. *val2 = 34000;
  91. return IIO_VAL_INT_PLUS_MICRO;
  92. }
  93. return -EINVAL;
  94. out_unlock:
  95. mutex_unlock(&indio_dev->mlock);
  96. return ret;
  97. }
  98. static const struct iio_info adis16060_info = {
  99. .read_raw = &adis16060_read_raw,
  100. .driver_module = THIS_MODULE,
  101. };
  102. static const struct iio_chan_spec adis16060_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. .address = ADIS16060_GYRO,
  109. }, {
  110. .type = IIO_VOLTAGE,
  111. .indexed = 1,
  112. .channel = 0,
  113. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  114. .address = ADIS16060_AIN1,
  115. }, {
  116. .type = IIO_VOLTAGE,
  117. .indexed = 1,
  118. .channel = 1,
  119. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  120. .address = ADIS16060_AIN2,
  121. }, {
  122. .type = IIO_TEMP,
  123. .indexed = 1,
  124. .channel = 0,
  125. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  126. BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE),
  127. .address = ADIS16060_TEMP_OUT,
  128. }
  129. };
  130. static int adis16060_r_probe(struct spi_device *spi)
  131. {
  132. int ret;
  133. struct adis16060_state *st;
  134. struct iio_dev *indio_dev;
  135. /* setup the industrialio driver allocated elements */
  136. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  137. if (!indio_dev)
  138. return -ENOMEM;
  139. /* this is only used for removal purposes */
  140. spi_set_drvdata(spi, indio_dev);
  141. st = iio_priv(indio_dev);
  142. st->us_r = spi;
  143. mutex_init(&st->buf_lock);
  144. indio_dev->name = spi->dev.driver->name;
  145. indio_dev->dev.parent = &spi->dev;
  146. indio_dev->info = &adis16060_info;
  147. indio_dev->modes = INDIO_DIRECT_MODE;
  148. indio_dev->channels = adis16060_channels;
  149. indio_dev->num_channels = ARRAY_SIZE(adis16060_channels);
  150. ret = devm_iio_device_register(&spi->dev, indio_dev);
  151. if (ret)
  152. return ret;
  153. adis16060_iio_dev = indio_dev;
  154. return 0;
  155. }
  156. static int adis16060_w_probe(struct spi_device *spi)
  157. {
  158. int ret;
  159. struct iio_dev *indio_dev = adis16060_iio_dev;
  160. struct adis16060_state *st;
  161. if (!indio_dev) {
  162. ret = -ENODEV;
  163. goto error_ret;
  164. }
  165. st = iio_priv(indio_dev);
  166. spi_set_drvdata(spi, indio_dev);
  167. st->us_w = spi;
  168. return 0;
  169. error_ret:
  170. return ret;
  171. }
  172. static int adis16060_w_remove(struct spi_device *spi)
  173. {
  174. return 0;
  175. }
  176. static struct spi_driver adis16060_r_driver = {
  177. .driver = {
  178. .name = "adis16060_r",
  179. },
  180. .probe = adis16060_r_probe,
  181. };
  182. static struct spi_driver adis16060_w_driver = {
  183. .driver = {
  184. .name = "adis16060_w",
  185. },
  186. .probe = adis16060_w_probe,
  187. .remove = adis16060_w_remove,
  188. };
  189. static __init int adis16060_init(void)
  190. {
  191. int ret;
  192. ret = spi_register_driver(&adis16060_r_driver);
  193. if (ret < 0)
  194. return ret;
  195. ret = spi_register_driver(&adis16060_w_driver);
  196. if (ret < 0) {
  197. spi_unregister_driver(&adis16060_r_driver);
  198. return ret;
  199. }
  200. return 0;
  201. }
  202. module_init(adis16060_init);
  203. static __exit void adis16060_exit(void)
  204. {
  205. spi_unregister_driver(&adis16060_w_driver);
  206. spi_unregister_driver(&adis16060_r_driver);
  207. }
  208. module_exit(adis16060_exit);
  209. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  210. MODULE_DESCRIPTION("Analog Devices ADIS16060 Yaw Rate Gyroscope Driver");
  211. MODULE_LICENSE("GPL v2");