adis16080.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * ADIS16080/100 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/delay.h>
  9. #include <linux/mutex.h>
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/slab.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/module.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #define ADIS16080_DIN_GYRO (0 << 10) /* Gyroscope output */
  19. #define ADIS16080_DIN_TEMP (1 << 10) /* Temperature output */
  20. #define ADIS16080_DIN_AIN1 (2 << 10)
  21. #define ADIS16080_DIN_AIN2 (3 << 10)
  22. /*
  23. * 1: Write contents on DIN to control register.
  24. * 0: No changes to control register.
  25. */
  26. #define ADIS16080_DIN_WRITE (1 << 15)
  27. struct adis16080_chip_info {
  28. int scale_val;
  29. int scale_val2;
  30. };
  31. /**
  32. * struct adis16080_state - device instance specific data
  33. * @us: actual spi_device to write data
  34. * @info: chip specific parameters
  35. * @buf: transmit or receive buffer
  36. **/
  37. struct adis16080_state {
  38. struct spi_device *us;
  39. const struct adis16080_chip_info *info;
  40. __be16 buf ____cacheline_aligned;
  41. };
  42. static int adis16080_read_sample(struct iio_dev *indio_dev,
  43. u16 addr, int *val)
  44. {
  45. struct adis16080_state *st = iio_priv(indio_dev);
  46. struct spi_message m;
  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. spi_message_init(&m);
  60. spi_message_add_tail(&t[0], &m);
  61. spi_message_add_tail(&t[1], &m);
  62. ret = spi_sync(st->us, &m);
  63. if (ret == 0)
  64. *val = sign_extend32(be16_to_cpu(st->buf), 11);
  65. return ret;
  66. }
  67. static int adis16080_read_raw(struct iio_dev *indio_dev,
  68. struct iio_chan_spec const *chan,
  69. int *val,
  70. int *val2,
  71. long mask)
  72. {
  73. struct adis16080_state *st = iio_priv(indio_dev);
  74. int ret;
  75. switch (mask) {
  76. case IIO_CHAN_INFO_RAW:
  77. mutex_lock(&indio_dev->mlock);
  78. ret = adis16080_read_sample(indio_dev, chan->address, val);
  79. mutex_unlock(&indio_dev->mlock);
  80. return ret ? ret : IIO_VAL_INT;
  81. case IIO_CHAN_INFO_SCALE:
  82. switch (chan->type) {
  83. case IIO_ANGL_VEL:
  84. *val = st->info->scale_val;
  85. *val2 = st->info->scale_val2;
  86. return IIO_VAL_FRACTIONAL;
  87. case IIO_VOLTAGE:
  88. /* VREF = 5V, 12 bits */
  89. *val = 5000;
  90. *val2 = 12;
  91. return IIO_VAL_FRACTIONAL_LOG2;
  92. case IIO_TEMP:
  93. /* 85 C = 585, 25 C = 0 */
  94. *val = 85000 - 25000;
  95. *val2 = 585;
  96. return IIO_VAL_FRACTIONAL;
  97. default:
  98. return -EINVAL;
  99. }
  100. case IIO_CHAN_INFO_OFFSET:
  101. switch (chan->type) {
  102. case IIO_VOLTAGE:
  103. /* 2.5 V = 0 */
  104. *val = 2048;
  105. return IIO_VAL_INT;
  106. case IIO_TEMP:
  107. /* 85 C = 585, 25 C = 0 */
  108. *val = DIV_ROUND_CLOSEST(25 * 585, 85 - 25);
  109. return IIO_VAL_INT;
  110. default:
  111. return -EINVAL;
  112. }
  113. default:
  114. break;
  115. }
  116. return -EINVAL;
  117. }
  118. static const struct iio_chan_spec adis16080_channels[] = {
  119. {
  120. .type = IIO_ANGL_VEL,
  121. .modified = 1,
  122. .channel2 = IIO_MOD_Z,
  123. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
  124. IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
  125. .address = ADIS16080_DIN_GYRO,
  126. }, {
  127. .type = IIO_VOLTAGE,
  128. .indexed = 1,
  129. .channel = 0,
  130. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
  131. IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
  132. IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
  133. .address = ADIS16080_DIN_AIN1,
  134. }, {
  135. .type = IIO_VOLTAGE,
  136. .indexed = 1,
  137. .channel = 1,
  138. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
  139. IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
  140. IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
  141. .address = ADIS16080_DIN_AIN2,
  142. }, {
  143. .type = IIO_TEMP,
  144. .indexed = 1,
  145. .channel = 0,
  146. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
  147. IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
  148. IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
  149. .address = ADIS16080_DIN_TEMP,
  150. }
  151. };
  152. static const struct iio_info adis16080_info = {
  153. .read_raw = &adis16080_read_raw,
  154. .driver_module = THIS_MODULE,
  155. };
  156. enum {
  157. ID_ADIS16080,
  158. ID_ADIS16100,
  159. };
  160. static const struct adis16080_chip_info adis16080_chip_info[] = {
  161. [ID_ADIS16080] = {
  162. /* 80 degree = 819, 819 rad = 46925 degree */
  163. .scale_val = 80,
  164. .scale_val2 = 46925,
  165. },
  166. [ID_ADIS16100] = {
  167. /* 300 degree = 1230, 1230 rad = 70474 degree */
  168. .scale_val = 300,
  169. .scale_val2 = 70474,
  170. },
  171. };
  172. static int adis16080_probe(struct spi_device *spi)
  173. {
  174. const struct spi_device_id *id = spi_get_device_id(spi);
  175. int ret;
  176. struct adis16080_state *st;
  177. struct iio_dev *indio_dev;
  178. /* setup the industrialio driver allocated elements */
  179. indio_dev = iio_device_alloc(sizeof(*st));
  180. if (indio_dev == NULL) {
  181. ret = -ENOMEM;
  182. goto error_ret;
  183. }
  184. st = iio_priv(indio_dev);
  185. /* this is only used for removal purposes */
  186. spi_set_drvdata(spi, indio_dev);
  187. /* Allocate the comms buffers */
  188. st->us = spi;
  189. st->info = &adis16080_chip_info[id->driver_data];
  190. indio_dev->name = spi->dev.driver->name;
  191. indio_dev->channels = adis16080_channels;
  192. indio_dev->num_channels = ARRAY_SIZE(adis16080_channels);
  193. indio_dev->dev.parent = &spi->dev;
  194. indio_dev->info = &adis16080_info;
  195. indio_dev->modes = INDIO_DIRECT_MODE;
  196. ret = iio_device_register(indio_dev);
  197. if (ret)
  198. goto error_free_dev;
  199. return 0;
  200. error_free_dev:
  201. iio_device_free(indio_dev);
  202. error_ret:
  203. return ret;
  204. }
  205. static int adis16080_remove(struct spi_device *spi)
  206. {
  207. iio_device_unregister(spi_get_drvdata(spi));
  208. iio_device_free(spi_get_drvdata(spi));
  209. return 0;
  210. }
  211. static const struct spi_device_id adis16080_ids[] = {
  212. { "adis16080", ID_ADIS16080 },
  213. { "adis16100", ID_ADIS16100 },
  214. {},
  215. };
  216. MODULE_DEVICE_TABLE(spi, adis16080_ids);
  217. static struct spi_driver adis16080_driver = {
  218. .driver = {
  219. .name = "adis16080",
  220. .owner = THIS_MODULE,
  221. },
  222. .probe = adis16080_probe,
  223. .remove = adis16080_remove,
  224. .id_table = adis16080_ids,
  225. };
  226. module_spi_driver(adis16080_driver);
  227. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  228. MODULE_DESCRIPTION("Analog Devices ADIS16080/100 Yaw Rate Gyroscope Driver");
  229. MODULE_LICENSE("GPL v2");