adis.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Common library for ADIS16XXX devices
  3. *
  4. * Copyright 2012 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  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/module.h>
  17. #include <asm/unaligned.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/buffer.h>
  21. #include <linux/iio/imu/adis.h>
  22. #define ADIS_MSC_CTRL_DATA_RDY_EN BIT(2)
  23. #define ADIS_MSC_CTRL_DATA_RDY_POL_HIGH BIT(1)
  24. #define ADIS_MSC_CTRL_DATA_RDY_DIO2 BIT(0)
  25. #define ADIS_GLOB_CMD_SW_RESET BIT(7)
  26. /**
  27. * adis_write_reg_8() - Write single byte to a register
  28. * @adis: The adis device
  29. * @reg: The address of the register to be written
  30. * @val: The value to write
  31. */
  32. int adis_write_reg_8(struct adis *adis, unsigned int reg, uint8_t val)
  33. {
  34. int ret;
  35. mutex_lock(&adis->txrx_lock);
  36. adis->tx[0] = ADIS_WRITE_REG(reg);
  37. adis->tx[1] = val;
  38. ret = spi_write(adis->spi, adis->tx, 2);
  39. mutex_unlock(&adis->txrx_lock);
  40. return ret;
  41. }
  42. EXPORT_SYMBOL_GPL(adis_write_reg_8);
  43. /**
  44. * adis_write_reg_16() - Write 2 bytes to a pair of registers
  45. * @adis: The adis device
  46. * @reg: The address of the lower of the two registers
  47. * @val: Value to be written
  48. */
  49. int adis_write_reg_16(struct adis *adis, unsigned int reg, uint16_t value)
  50. {
  51. int ret;
  52. struct spi_message msg;
  53. struct spi_transfer xfers[] = {
  54. {
  55. .tx_buf = adis->tx,
  56. .bits_per_word = 8,
  57. .len = 2,
  58. .cs_change = 1,
  59. .delay_usecs = adis->data->write_delay,
  60. }, {
  61. .tx_buf = adis->tx + 2,
  62. .bits_per_word = 8,
  63. .len = 2,
  64. .delay_usecs = adis->data->write_delay,
  65. },
  66. };
  67. mutex_lock(&adis->txrx_lock);
  68. adis->tx[0] = ADIS_WRITE_REG(reg);
  69. adis->tx[1] = value & 0xff;
  70. adis->tx[2] = ADIS_WRITE_REG(reg + 1);
  71. adis->tx[3] = (value >> 8) & 0xff;
  72. spi_message_init(&msg);
  73. spi_message_add_tail(&xfers[0], &msg);
  74. spi_message_add_tail(&xfers[1], &msg);
  75. ret = spi_sync(adis->spi, &msg);
  76. mutex_unlock(&adis->txrx_lock);
  77. return ret;
  78. }
  79. EXPORT_SYMBOL_GPL(adis_write_reg_16);
  80. /**
  81. * adis_read_reg_16() - read 2 bytes from a 16-bit register
  82. * @adis: The adis device
  83. * @reg: The address of the lower of the two registers
  84. * @val: The value read back from the device
  85. */
  86. int adis_read_reg_16(struct adis *adis, unsigned int reg, uint16_t *val)
  87. {
  88. struct spi_message msg;
  89. int ret;
  90. struct spi_transfer xfers[] = {
  91. {
  92. .tx_buf = adis->tx,
  93. .bits_per_word = 8,
  94. .len = 2,
  95. .cs_change = 1,
  96. .delay_usecs = adis->data->read_delay,
  97. }, {
  98. .rx_buf = adis->rx,
  99. .bits_per_word = 8,
  100. .len = 2,
  101. .delay_usecs = adis->data->read_delay,
  102. },
  103. };
  104. mutex_lock(&adis->txrx_lock);
  105. adis->tx[0] = ADIS_READ_REG(reg);
  106. adis->tx[1] = 0;
  107. spi_message_init(&msg);
  108. spi_message_add_tail(&xfers[0], &msg);
  109. spi_message_add_tail(&xfers[1], &msg);
  110. ret = spi_sync(adis->spi, &msg);
  111. if (ret) {
  112. dev_err(&adis->spi->dev, "Failed to read 16 bit register 0x%02X: %d\n",
  113. reg, ret);
  114. goto error_ret;
  115. }
  116. *val = get_unaligned_be16(adis->rx);
  117. error_ret:
  118. mutex_unlock(&adis->txrx_lock);
  119. return ret;
  120. }
  121. EXPORT_SYMBOL_GPL(adis_read_reg_16);
  122. /**
  123. * adis_enable_irq() - Enable or disable data ready IRQ
  124. * @adis: The adis device
  125. * @enable: Whether to enable the IRQ
  126. *
  127. * Returns 0 on success, negative error code otherwise
  128. */
  129. int adis_enable_irq(struct adis *adis, bool enable)
  130. {
  131. int ret = 0;
  132. uint16_t msc;
  133. ret = adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc);
  134. if (ret)
  135. goto error_ret;
  136. msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH;
  137. msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2;
  138. if (enable)
  139. msc |= ADIS_MSC_CTRL_DATA_RDY_EN;
  140. else
  141. msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN;
  142. ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc);
  143. error_ret:
  144. return ret;
  145. }
  146. EXPORT_SYMBOL(adis_enable_irq);
  147. /**
  148. * adis_check_status() - Check the device for error conditions
  149. * @adis: The adis device
  150. *
  151. * Returns 0 on success, a negative error code otherwise
  152. */
  153. int adis_check_status(struct adis *adis)
  154. {
  155. uint16_t status;
  156. int ret;
  157. int i;
  158. ret = adis_read_reg_16(adis, adis->data->diag_stat_reg, &status);
  159. if (ret < 0)
  160. return ret;
  161. status &= adis->data->status_error_mask;
  162. if (status == 0)
  163. return 0;
  164. for (i = 0; i < 16; ++i) {
  165. if (status & BIT(i)) {
  166. dev_err(&adis->spi->dev, "%s.\n",
  167. adis->data->status_error_msgs[i]);
  168. }
  169. }
  170. return -EIO;
  171. }
  172. EXPORT_SYMBOL_GPL(adis_check_status);
  173. /**
  174. * adis_reset() - Reset the device
  175. * @adis: The adis device
  176. *
  177. * Returns 0 on success, a negative error code otherwise
  178. */
  179. int adis_reset(struct adis *adis)
  180. {
  181. int ret;
  182. ret = adis_write_reg_8(adis, adis->data->glob_cmd_reg,
  183. ADIS_GLOB_CMD_SW_RESET);
  184. if (ret)
  185. dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret);
  186. return ret;
  187. }
  188. EXPORT_SYMBOL_GPL(adis_reset);
  189. static int adis_self_test(struct adis *adis)
  190. {
  191. int ret;
  192. ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg,
  193. adis->data->self_test_mask);
  194. if (ret) {
  195. dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n",
  196. ret);
  197. return ret;
  198. }
  199. msleep(adis->data->startup_delay);
  200. return adis_check_status(adis);
  201. }
  202. /**
  203. * adis_inital_startup() - Performs device self-test
  204. * @adis: The adis device
  205. *
  206. * Returns 0 if the device is operational, a negative error code otherwise.
  207. *
  208. * This function should be called early on in the device initialization sequence
  209. * to ensure that the device is in a sane and known state and that it is usable.
  210. */
  211. int adis_initial_startup(struct adis *adis)
  212. {
  213. int ret;
  214. ret = adis_self_test(adis);
  215. if (ret) {
  216. dev_err(&adis->spi->dev, "Self-test failed, trying reset.\n");
  217. adis_reset(adis);
  218. msleep(adis->data->startup_delay);
  219. ret = adis_self_test(adis);
  220. if (ret) {
  221. dev_err(&adis->spi->dev, "Second self-test failed, giving up.\n");
  222. return ret;
  223. }
  224. }
  225. return 0;
  226. }
  227. EXPORT_SYMBOL_GPL(adis_initial_startup);
  228. /**
  229. * adis_single_conversion() - Performs a single sample conversion
  230. * @indio_dev: The IIO device
  231. * @chan: The IIO channel
  232. * @error_mask: Mask for the error bit
  233. * @val: Result of the conversion
  234. *
  235. * Returns IIO_VAL_INT on success, a negative error code otherwise.
  236. *
  237. * The function performs a single conversion on a given channel and post
  238. * processes the value accordingly to the channel spec. If a error_mask is given
  239. * the function will check if the mask is set in the returned raw value. If it
  240. * is set the function will perform a self-check. If the device does not report
  241. * a error bit in the channels raw value set error_mask to 0.
  242. */
  243. int adis_single_conversion(struct iio_dev *indio_dev,
  244. const struct iio_chan_spec *chan, unsigned int error_mask, int *val)
  245. {
  246. struct adis *adis = iio_device_get_drvdata(indio_dev);
  247. uint16_t val16;
  248. int ret;
  249. mutex_lock(&indio_dev->mlock);
  250. ret = adis_read_reg_16(adis, chan->address, &val16);
  251. if (ret)
  252. goto err_unlock;
  253. if (val16 & error_mask) {
  254. ret = adis_check_status(adis);
  255. if (ret)
  256. goto err_unlock;
  257. }
  258. if (chan->scan_type.sign == 's')
  259. *val = sign_extend32(val16, chan->scan_type.realbits - 1);
  260. else
  261. *val = val16 & ((1 << chan->scan_type.realbits) - 1);
  262. ret = IIO_VAL_INT;
  263. err_unlock:
  264. mutex_unlock(&indio_dev->mlock);
  265. return ret;
  266. }
  267. EXPORT_SYMBOL_GPL(adis_single_conversion);
  268. /**
  269. * adis_init() - Initialize adis device structure
  270. * @adis: The adis device
  271. * @indio_dev: The iio device
  272. * @spi: The spi device
  273. * @data: Chip specific data
  274. *
  275. * Returns 0 on success, a negative error code otherwise.
  276. *
  277. * This function must be called, before any other adis helper function may be
  278. * called.
  279. */
  280. int adis_init(struct adis *adis, struct iio_dev *indio_dev,
  281. struct spi_device *spi, const struct adis_data *data)
  282. {
  283. mutex_init(&adis->txrx_lock);
  284. adis->spi = spi;
  285. adis->data = data;
  286. iio_device_set_drvdata(indio_dev, adis);
  287. return adis_enable_irq(adis, false);
  288. }
  289. EXPORT_SYMBOL_GPL(adis_init);
  290. MODULE_LICENSE("GPL");
  291. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  292. MODULE_DESCRIPTION("Common library code for ADIS16XXX devices");