adis.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. return adis_check_status(adis);
  282. }
  283. /**
  284. * adis_inital_startup() - Performs device self-test
  285. * @adis: The adis device
  286. *
  287. * Returns 0 if the device is operational, a negative error code otherwise.
  288. *
  289. * This function should be called early on in the device initialization sequence
  290. * to ensure that the device is in a sane and known state and that it is usable.
  291. */
  292. int adis_initial_startup(struct adis *adis)
  293. {
  294. int ret;
  295. ret = adis_self_test(adis);
  296. if (ret) {
  297. dev_err(&adis->spi->dev, "Self-test failed, trying reset.\n");
  298. adis_reset(adis);
  299. msleep(adis->data->startup_delay);
  300. ret = adis_self_test(adis);
  301. if (ret) {
  302. dev_err(&adis->spi->dev, "Second self-test failed, giving up.\n");
  303. return ret;
  304. }
  305. }
  306. return 0;
  307. }
  308. EXPORT_SYMBOL_GPL(adis_initial_startup);
  309. /**
  310. * adis_single_conversion() - Performs a single sample conversion
  311. * @indio_dev: The IIO device
  312. * @chan: The IIO channel
  313. * @error_mask: Mask for the error bit
  314. * @val: Result of the conversion
  315. *
  316. * Returns IIO_VAL_INT on success, a negative error code otherwise.
  317. *
  318. * The function performs a single conversion on a given channel and post
  319. * processes the value accordingly to the channel spec. If a error_mask is given
  320. * the function will check if the mask is set in the returned raw value. If it
  321. * is set the function will perform a self-check. If the device does not report
  322. * a error bit in the channels raw value set error_mask to 0.
  323. */
  324. int adis_single_conversion(struct iio_dev *indio_dev,
  325. const struct iio_chan_spec *chan, unsigned int error_mask, int *val)
  326. {
  327. struct adis *adis = iio_device_get_drvdata(indio_dev);
  328. unsigned int uval;
  329. int ret;
  330. mutex_lock(&indio_dev->mlock);
  331. ret = adis_read_reg(adis, chan->address, &uval,
  332. chan->scan_type.storagebits / 8);
  333. if (ret)
  334. goto err_unlock;
  335. if (uval & error_mask) {
  336. ret = adis_check_status(adis);
  337. if (ret)
  338. goto err_unlock;
  339. }
  340. if (chan->scan_type.sign == 's')
  341. *val = sign_extend32(uval, chan->scan_type.realbits - 1);
  342. else
  343. *val = uval & ((1 << chan->scan_type.realbits) - 1);
  344. ret = IIO_VAL_INT;
  345. err_unlock:
  346. mutex_unlock(&indio_dev->mlock);
  347. return ret;
  348. }
  349. EXPORT_SYMBOL_GPL(adis_single_conversion);
  350. /**
  351. * adis_init() - Initialize adis device structure
  352. * @adis: The adis device
  353. * @indio_dev: The iio device
  354. * @spi: The spi device
  355. * @data: Chip specific data
  356. *
  357. * Returns 0 on success, a negative error code otherwise.
  358. *
  359. * This function must be called, before any other adis helper function may be
  360. * called.
  361. */
  362. int adis_init(struct adis *adis, struct iio_dev *indio_dev,
  363. struct spi_device *spi, const struct adis_data *data)
  364. {
  365. mutex_init(&adis->txrx_lock);
  366. adis->spi = spi;
  367. adis->data = data;
  368. iio_device_set_drvdata(indio_dev, adis);
  369. if (data->has_paging) {
  370. /* Need to set the page before first read/write */
  371. adis->current_page = -1;
  372. } else {
  373. /* Page will always be 0 */
  374. adis->current_page = 0;
  375. }
  376. return adis_enable_irq(adis, false);
  377. }
  378. EXPORT_SYMBOL_GPL(adis_init);
  379. MODULE_LICENSE("GPL");
  380. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  381. MODULE_DESCRIPTION("Common library code for ADIS16XXX devices");