adis16080.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. int ret;
  47. struct spi_transfer t[] = {
  48. {
  49. .tx_buf = &st->buf,
  50. .len = 2,
  51. .cs_change = 1,
  52. }, {
  53. .rx_buf = &st->buf,
  54. .len = 2,
  55. },
  56. };
  57. st->buf = cpu_to_be16(addr | ADIS16080_DIN_WRITE);
  58. ret = spi_sync_transfer(st->us, t, ARRAY_SIZE(t));
  59. if (ret == 0)
  60. *val = sign_extend32(be16_to_cpu(st->buf), 11);
  61. return ret;
  62. }
  63. static int adis16080_read_raw(struct iio_dev *indio_dev,
  64. struct iio_chan_spec const *chan,
  65. int *val,
  66. int *val2,
  67. long mask)
  68. {
  69. struct adis16080_state *st = iio_priv(indio_dev);
  70. int ret;
  71. switch (mask) {
  72. case IIO_CHAN_INFO_RAW:
  73. mutex_lock(&indio_dev->mlock);
  74. ret = adis16080_read_sample(indio_dev, chan->address, val);
  75. mutex_unlock(&indio_dev->mlock);
  76. return ret ? ret : IIO_VAL_INT;
  77. case IIO_CHAN_INFO_SCALE:
  78. switch (chan->type) {
  79. case IIO_ANGL_VEL:
  80. *val = st->info->scale_val;
  81. *val2 = st->info->scale_val2;
  82. return IIO_VAL_FRACTIONAL;
  83. case IIO_VOLTAGE:
  84. /* VREF = 5V, 12 bits */
  85. *val = 5000;
  86. *val2 = 12;
  87. return IIO_VAL_FRACTIONAL_LOG2;
  88. case IIO_TEMP:
  89. /* 85 C = 585, 25 C = 0 */
  90. *val = 85000 - 25000;
  91. *val2 = 585;
  92. return IIO_VAL_FRACTIONAL;
  93. default:
  94. return -EINVAL;
  95. }
  96. case IIO_CHAN_INFO_OFFSET:
  97. switch (chan->type) {
  98. case IIO_VOLTAGE:
  99. /* 2.5 V = 0 */
  100. *val = 2048;
  101. return IIO_VAL_INT;
  102. case IIO_TEMP:
  103. /* 85 C = 585, 25 C = 0 */
  104. *val = DIV_ROUND_CLOSEST(25 * 585, 85 - 25);
  105. return IIO_VAL_INT;
  106. default:
  107. return -EINVAL;
  108. }
  109. default:
  110. break;
  111. }
  112. return -EINVAL;
  113. }
  114. static const struct iio_chan_spec adis16080_channels[] = {
  115. {
  116. .type = IIO_ANGL_VEL,
  117. .modified = 1,
  118. .channel2 = IIO_MOD_Z,
  119. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  120. BIT(IIO_CHAN_INFO_SCALE),
  121. .address = ADIS16080_DIN_GYRO,
  122. }, {
  123. .type = IIO_VOLTAGE,
  124. .indexed = 1,
  125. .channel = 0,
  126. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  127. BIT(IIO_CHAN_INFO_SCALE) |
  128. BIT(IIO_CHAN_INFO_OFFSET),
  129. .address = ADIS16080_DIN_AIN1,
  130. }, {
  131. .type = IIO_VOLTAGE,
  132. .indexed = 1,
  133. .channel = 1,
  134. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  135. BIT(IIO_CHAN_INFO_SCALE) |
  136. BIT(IIO_CHAN_INFO_OFFSET),
  137. .address = ADIS16080_DIN_AIN2,
  138. }, {
  139. .type = IIO_TEMP,
  140. .indexed = 1,
  141. .channel = 0,
  142. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  143. BIT(IIO_CHAN_INFO_SCALE) |
  144. BIT(IIO_CHAN_INFO_OFFSET),
  145. .address = ADIS16080_DIN_TEMP,
  146. }
  147. };
  148. static const struct iio_info adis16080_info = {
  149. .read_raw = &adis16080_read_raw,
  150. .driver_module = THIS_MODULE,
  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. /* this is only used for removal purposes */
  179. spi_set_drvdata(spi, indio_dev);
  180. /* Allocate the comms buffers */
  181. st->us = spi;
  182. st->info = &adis16080_chip_info[id->driver_data];
  183. indio_dev->name = spi->dev.driver->name;
  184. indio_dev->channels = adis16080_channels;
  185. indio_dev->num_channels = ARRAY_SIZE(adis16080_channels);
  186. indio_dev->dev.parent = &spi->dev;
  187. indio_dev->info = &adis16080_info;
  188. indio_dev->modes = INDIO_DIRECT_MODE;
  189. return iio_device_register(indio_dev);
  190. }
  191. static int adis16080_remove(struct spi_device *spi)
  192. {
  193. iio_device_unregister(spi_get_drvdata(spi));
  194. return 0;
  195. }
  196. static const struct spi_device_id adis16080_ids[] = {
  197. { "adis16080", ID_ADIS16080 },
  198. { "adis16100", ID_ADIS16100 },
  199. {},
  200. };
  201. MODULE_DEVICE_TABLE(spi, adis16080_ids);
  202. static struct spi_driver adis16080_driver = {
  203. .driver = {
  204. .name = "adis16080",
  205. },
  206. .probe = adis16080_probe,
  207. .remove = adis16080_remove,
  208. .id_table = adis16080_ids,
  209. };
  210. module_spi_driver(adis16080_driver);
  211. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  212. MODULE_DESCRIPTION("Analog Devices ADIS16080/100 Yaw Rate Gyroscope Driver");
  213. MODULE_LICENSE("GPL v2");