adis.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. int adis_write_reg(struct adis *adis, unsigned int reg,
  27. unsigned int value, unsigned int size)
  28. {
  29. unsigned int page = reg / ADIS_PAGE_SIZE;
  30. int ret, i;
  31. struct spi_message msg;
  32. struct spi_transfer xfers[] = {
  33. {
  34. .tx_buf = adis->tx,
  35. .bits_per_word = 8,
  36. .len = 2,
  37. .cs_change = 1,
  38. .delay_usecs = adis->data->write_delay,
  39. }, {
  40. .tx_buf = adis->tx + 2,
  41. .bits_per_word = 8,
  42. .len = 2,
  43. .cs_change = 1,
  44. .delay_usecs = adis->data->write_delay,
  45. }, {
  46. .tx_buf = adis->tx + 4,
  47. .bits_per_word = 8,
  48. .len = 2,
  49. .cs_change = 1,
  50. .delay_usecs = adis->data->write_delay,
  51. }, {
  52. .tx_buf = adis->tx + 6,
  53. .bits_per_word = 8,
  54. .len = 2,
  55. .delay_usecs = adis->data->write_delay,
  56. }, {
  57. .tx_buf = adis->tx + 8,
  58. .bits_per_word = 8,
  59. .len = 2,
  60. .delay_usecs = adis->data->write_delay,
  61. },
  62. };
  63. mutex_lock(&adis->txrx_lock);
  64. spi_message_init(&msg);
  65. if (adis->current_page != page) {
  66. adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
  67. adis->tx[1] = page;
  68. spi_message_add_tail(&xfers[0], &msg);
  69. }
  70. switch (size) {
  71. case 4:
  72. adis->tx[8] = ADIS_WRITE_REG(reg + 3);
  73. adis->tx[9] = (value >> 24) & 0xff;
  74. adis->tx[6] = ADIS_WRITE_REG(reg + 2);
  75. adis->tx[7] = (value >> 16) & 0xff;
  76. case 2:
  77. adis->tx[4] = ADIS_WRITE_REG(reg + 1);
  78. adis->tx[5] = (value >> 8) & 0xff;
  79. case 1:
  80. adis->tx[2] = ADIS_WRITE_REG(reg);
  81. adis->tx[3] = value & 0xff;
  82. break;
  83. default:
  84. ret = -EINVAL;
  85. goto out_unlock;
  86. }
  87. xfers[size].cs_change = 0;
  88. for (i = 1; i <= size; i++)
  89. spi_message_add_tail(&xfers[i], &msg);
  90. ret = spi_sync(adis->spi, &msg);
  91. if (ret) {
  92. dev_err(&adis->spi->dev, "Failed to write register 0x%02X: %d\n",
  93. reg, ret);
  94. } else {
  95. adis->current_page = page;
  96. }
  97. out_unlock:
  98. mutex_unlock(&adis->txrx_lock);
  99. return ret;
  100. }
  101. EXPORT_SYMBOL_GPL(adis_write_reg);
  102. /**
  103. * adis_read_reg() - read 2 bytes from a 16-bit register
  104. * @adis: The adis device
  105. * @reg: The address of the lower of the two registers
  106. * @val: The value read back from the device
  107. */
  108. int adis_read_reg(struct adis *adis, unsigned int reg,
  109. unsigned int *val, unsigned int size)
  110. {
  111. unsigned int page = reg / ADIS_PAGE_SIZE;
  112. struct spi_message msg;
  113. int ret;
  114. struct spi_transfer xfers[] = {
  115. {
  116. .tx_buf = adis->tx,
  117. .bits_per_word = 8,
  118. .len = 2,
  119. .cs_change = 1,
  120. .delay_usecs = adis->data->write_delay,
  121. }, {
  122. .tx_buf = adis->tx + 2,
  123. .bits_per_word = 8,
  124. .len = 2,
  125. .cs_change = 1,
  126. .delay_usecs = adis->data->read_delay,
  127. }, {
  128. .tx_buf = adis->tx + 4,
  129. .rx_buf = adis->rx,
  130. .bits_per_word = 8,
  131. .len = 2,
  132. .cs_change = 1,
  133. .delay_usecs = adis->data->read_delay,
  134. }, {
  135. .rx_buf = adis->rx + 2,
  136. .bits_per_word = 8,
  137. .len = 2,
  138. .delay_usecs = adis->data->read_delay,
  139. },
  140. };
  141. mutex_lock(&adis->txrx_lock);
  142. spi_message_init(&msg);
  143. if (adis->current_page != page) {
  144. adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
  145. adis->tx[1] = page;
  146. spi_message_add_tail(&xfers[0], &msg);
  147. }
  148. switch (size) {
  149. case 4:
  150. adis->tx[2] = ADIS_READ_REG(reg + 2);
  151. adis->tx[3] = 0;
  152. spi_message_add_tail(&xfers[1], &msg);
  153. case 2:
  154. adis->tx[4] = ADIS_READ_REG(reg);
  155. adis->tx[5] = 0;
  156. spi_message_add_tail(&xfers[2], &msg);
  157. spi_message_add_tail(&xfers[3], &msg);
  158. break;
  159. default:
  160. ret = -EINVAL;
  161. goto out_unlock;
  162. }
  163. ret = spi_sync(adis->spi, &msg);
  164. if (ret) {
  165. dev_err(&adis->spi->dev, "Failed to read register 0x%02X: %d\n",
  166. reg, ret);
  167. goto out_unlock;
  168. } else {
  169. adis->current_page = page;
  170. }
  171. switch (size) {
  172. case 4:
  173. *val = get_unaligned_be32(adis->rx);
  174. break;
  175. case 2:
  176. *val = get_unaligned_be16(adis->rx + 2);
  177. break;
  178. }
  179. out_unlock:
  180. mutex_unlock(&adis->txrx_lock);
  181. return ret;
  182. }
  183. EXPORT_SYMBOL_GPL(adis_read_reg);
  184. #ifdef CONFIG_DEBUG_FS
  185. int adis_debugfs_reg_access(struct iio_dev *indio_dev,
  186. unsigned int reg, unsigned int writeval, unsigned int *readval)
  187. {
  188. struct adis *adis = iio_device_get_drvdata(indio_dev);
  189. if (readval) {
  190. uint16_t val16;
  191. int ret;
  192. ret = adis_read_reg_16(adis, reg, &val16);
  193. *readval = val16;
  194. return ret;
  195. } else {
  196. return adis_write_reg_16(adis, reg, writeval);
  197. }
  198. }
  199. EXPORT_SYMBOL(adis_debugfs_reg_access);
  200. #endif
  201. /**
  202. * adis_enable_irq() - Enable or disable data ready IRQ
  203. * @adis: The adis device
  204. * @enable: Whether to enable the IRQ
  205. *
  206. * Returns 0 on success, negative error code otherwise
  207. */
  208. int adis_enable_irq(struct adis *adis, bool enable)
  209. {
  210. int ret = 0;
  211. uint16_t msc;
  212. if (adis->data->enable_irq)
  213. return adis->data->enable_irq(adis, enable);
  214. ret = adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc);
  215. if (ret)
  216. goto error_ret;
  217. msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH;
  218. msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2;
  219. if (enable)
  220. msc |= ADIS_MSC_CTRL_DATA_RDY_EN;
  221. else
  222. msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN;
  223. ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc);
  224. error_ret:
  225. return ret;
  226. }
  227. EXPORT_SYMBOL(adis_enable_irq);
  228. /**
  229. * adis_check_status() - Check the device for error conditions
  230. * @adis: The adis device
  231. *
  232. * Returns 0 on success, a negative error code otherwise
  233. */
  234. int adis_check_status(struct adis *adis)
  235. {
  236. uint16_t status;
  237. int ret;
  238. int i;
  239. ret = adis_read_reg_16(adis, adis->data->diag_stat_reg, &status);
  240. if (ret < 0)
  241. return ret;
  242. status &= adis->data->status_error_mask;
  243. if (status == 0)
  244. return 0;
  245. for (i = 0; i < 16; ++i) {
  246. if (status & BIT(i)) {
  247. dev_err(&adis->spi->dev, "%s.\n",
  248. adis->data->status_error_msgs[i]);
  249. }
  250. }
  251. return -EIO;
  252. }
  253. EXPORT_SYMBOL_GPL(adis_check_status);
  254. /**
  255. * adis_reset() - Reset the device
  256. * @adis: The adis device
  257. *
  258. * Returns 0 on success, a negative error code otherwise
  259. */
  260. int adis_reset(struct adis *adis)
  261. {
  262. int ret;
  263. ret = adis_write_reg_8(adis, adis->data->glob_cmd_reg,
  264. ADIS_GLOB_CMD_SW_RESET);
  265. if (ret)
  266. dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret);
  267. return ret;
  268. }
  269. EXPORT_SYMBOL_GPL(adis_reset);
  270. static int adis_self_test(struct adis *adis)
  271. {
  272. int ret;
  273. ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg,
  274. adis->data->self_test_mask);
  275. if (ret) {
  276. dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n",
  277. ret);
  278. return ret;
  279. }
  280. msleep(adis->data->startup_delay);
  281. ret = adis_check_status(adis);
  282. if (adis->data->self_test_no_autoclear)
  283. adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 0x00);
  284. return ret;
  285. }
  286. /**
  287. * adis_inital_startup() - Performs device self-test
  288. * @adis: The adis device
  289. *
  290. * Returns 0 if the device is operational, a negative error code otherwise.
  291. *
  292. * This function should be called early on in the device initialization sequence
  293. * to ensure that the device is in a sane and known state and that it is usable.
  294. */
  295. int adis_initial_startup(struct adis *adis)
  296. {
  297. int ret;
  298. ret = adis_self_test(adis);
  299. if (ret) {
  300. dev_err(&adis->spi->dev, "Self-test failed, trying reset.\n");
  301. adis_reset(adis);
  302. msleep(adis->data->startup_delay);
  303. ret = adis_self_test(adis);
  304. if (ret) {
  305. dev_err(&adis->spi->dev, "Second self-test failed, giving up.\n");
  306. return ret;
  307. }
  308. }
  309. return 0;
  310. }
  311. EXPORT_SYMBOL_GPL(adis_initial_startup);
  312. /**
  313. * adis_single_conversion() - Performs a single sample conversion
  314. * @indio_dev: The IIO device
  315. * @chan: The IIO channel
  316. * @error_mask: Mask for the error bit
  317. * @val: Result of the conversion
  318. *
  319. * Returns IIO_VAL_INT on success, a negative error code otherwise.
  320. *
  321. * The function performs a single conversion on a given channel and post
  322. * processes the value accordingly to the channel spec. If a error_mask is given
  323. * the function will check if the mask is set in the returned raw value. If it
  324. * is set the function will perform a self-check. If the device does not report
  325. * a error bit in the channels raw value set error_mask to 0.
  326. */
  327. int adis_single_conversion(struct iio_dev *indio_dev,
  328. const struct iio_chan_spec *chan, unsigned int error_mask, int *val)
  329. {
  330. struct adis *adis = iio_device_get_drvdata(indio_dev);
  331. unsigned int uval;
  332. int ret;
  333. mutex_lock(&indio_dev->mlock);
  334. ret = adis_read_reg(adis, chan->address, &uval,
  335. chan->scan_type.storagebits / 8);
  336. if (ret)
  337. goto err_unlock;
  338. if (uval & error_mask) {
  339. ret = adis_check_status(adis);
  340. if (ret)
  341. goto err_unlock;
  342. }
  343. if (chan->scan_type.sign == 's')
  344. *val = sign_extend32(uval, chan->scan_type.realbits - 1);
  345. else
  346. *val = uval & ((1 << chan->scan_type.realbits) - 1);
  347. ret = IIO_VAL_INT;
  348. err_unlock:
  349. mutex_unlock(&indio_dev->mlock);
  350. return ret;
  351. }
  352. EXPORT_SYMBOL_GPL(adis_single_conversion);
  353. /**
  354. * adis_init() - Initialize adis device structure
  355. * @adis: The adis device
  356. * @indio_dev: The iio device
  357. * @spi: The spi device
  358. * @data: Chip specific data
  359. *
  360. * Returns 0 on success, a negative error code otherwise.
  361. *
  362. * This function must be called, before any other adis helper function may be
  363. * called.
  364. */
  365. int adis_init(struct adis *adis, struct iio_dev *indio_dev,
  366. struct spi_device *spi, const struct adis_data *data)
  367. {
  368. mutex_init(&adis->txrx_lock);
  369. adis->spi = spi;
  370. adis->data = data;
  371. iio_device_set_drvdata(indio_dev, adis);
  372. if (data->has_paging) {
  373. /* Need to set the page before first read/write */
  374. adis->current_page = -1;
  375. } else {
  376. /* Page will always be 0 */
  377. adis->current_page = 0;
  378. }
  379. return adis_enable_irq(adis, false);
  380. }
  381. EXPORT_SYMBOL_GPL(adis_init);
  382. MODULE_LICENSE("GPL");
  383. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  384. MODULE_DESCRIPTION("Common library code for ADIS16XXX devices");