adis.c 9.9 KB

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