adis.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Common library for ADIS16XXX devices
  4. *
  5. * Copyright 2012 Analog Devices Inc.
  6. * Author: Lars-Peter Clausen <[email protected]>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/gpio/consumer.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/module.h>
  15. #include <asm/unaligned.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/imu/adis.h>
  18. #define ADIS_MSC_CTRL_DATA_RDY_EN BIT(2)
  19. #define ADIS_MSC_CTRL_DATA_RDY_POL_HIGH BIT(1)
  20. #define ADIS_MSC_CTRL_DATA_RDY_DIO2 BIT(0)
  21. #define ADIS_GLOB_CMD_SW_RESET BIT(7)
  22. /**
  23. * __adis_write_reg() - write N bytes to register (unlocked version)
  24. * @adis: The adis device
  25. * @reg: The address of the lower of the two registers
  26. * @value: The value to write to device (up to 4 bytes)
  27. * @size: The size of the @value (in bytes)
  28. */
  29. int __adis_write_reg(struct adis *adis, unsigned int reg, unsigned int value,
  30. unsigned int size)
  31. {
  32. unsigned int page = reg / ADIS_PAGE_SIZE;
  33. int ret, i;
  34. struct spi_message msg;
  35. struct spi_transfer xfers[] = {
  36. {
  37. .tx_buf = adis->tx,
  38. .bits_per_word = 8,
  39. .len = 2,
  40. .cs_change = 1,
  41. .delay.value = adis->data->write_delay,
  42. .delay.unit = SPI_DELAY_UNIT_USECS,
  43. .cs_change_delay.value = adis->data->cs_change_delay,
  44. .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
  45. }, {
  46. .tx_buf = adis->tx + 2,
  47. .bits_per_word = 8,
  48. .len = 2,
  49. .cs_change = 1,
  50. .delay.value = adis->data->write_delay,
  51. .delay.unit = SPI_DELAY_UNIT_USECS,
  52. .cs_change_delay.value = adis->data->cs_change_delay,
  53. .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
  54. }, {
  55. .tx_buf = adis->tx + 4,
  56. .bits_per_word = 8,
  57. .len = 2,
  58. .cs_change = 1,
  59. .delay.value = adis->data->write_delay,
  60. .delay.unit = SPI_DELAY_UNIT_USECS,
  61. .cs_change_delay.value = adis->data->cs_change_delay,
  62. .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
  63. }, {
  64. .tx_buf = adis->tx + 6,
  65. .bits_per_word = 8,
  66. .len = 2,
  67. .delay.value = adis->data->write_delay,
  68. .delay.unit = SPI_DELAY_UNIT_USECS,
  69. }, {
  70. .tx_buf = adis->tx + 8,
  71. .bits_per_word = 8,
  72. .len = 2,
  73. .delay.value = adis->data->write_delay,
  74. .delay.unit = SPI_DELAY_UNIT_USECS,
  75. },
  76. };
  77. spi_message_init(&msg);
  78. if (adis->current_page != page) {
  79. adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
  80. adis->tx[1] = page;
  81. spi_message_add_tail(&xfers[0], &msg);
  82. }
  83. switch (size) {
  84. case 4:
  85. adis->tx[8] = ADIS_WRITE_REG(reg + 3);
  86. adis->tx[9] = (value >> 24) & 0xff;
  87. adis->tx[6] = ADIS_WRITE_REG(reg + 2);
  88. adis->tx[7] = (value >> 16) & 0xff;
  89. fallthrough;
  90. case 2:
  91. adis->tx[4] = ADIS_WRITE_REG(reg + 1);
  92. adis->tx[5] = (value >> 8) & 0xff;
  93. fallthrough;
  94. case 1:
  95. adis->tx[2] = ADIS_WRITE_REG(reg);
  96. adis->tx[3] = value & 0xff;
  97. break;
  98. default:
  99. return -EINVAL;
  100. }
  101. xfers[size].cs_change = 0;
  102. for (i = 1; i <= size; i++)
  103. spi_message_add_tail(&xfers[i], &msg);
  104. ret = spi_sync(adis->spi, &msg);
  105. if (ret) {
  106. dev_err(&adis->spi->dev, "Failed to write register 0x%02X: %d\n",
  107. reg, ret);
  108. } else {
  109. adis->current_page = page;
  110. }
  111. return ret;
  112. }
  113. EXPORT_SYMBOL_NS_GPL(__adis_write_reg, IIO_ADISLIB);
  114. /**
  115. * __adis_read_reg() - read N bytes from register (unlocked version)
  116. * @adis: The adis device
  117. * @reg: The address of the lower of the two registers
  118. * @val: The value read back from the device
  119. * @size: The size of the @val buffer
  120. */
  121. int __adis_read_reg(struct adis *adis, unsigned int reg, unsigned int *val,
  122. unsigned int size)
  123. {
  124. unsigned int page = reg / ADIS_PAGE_SIZE;
  125. struct spi_message msg;
  126. int ret;
  127. struct spi_transfer xfers[] = {
  128. {
  129. .tx_buf = adis->tx,
  130. .bits_per_word = 8,
  131. .len = 2,
  132. .cs_change = 1,
  133. .delay.value = adis->data->write_delay,
  134. .delay.unit = SPI_DELAY_UNIT_USECS,
  135. .cs_change_delay.value = adis->data->cs_change_delay,
  136. .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
  137. }, {
  138. .tx_buf = adis->tx + 2,
  139. .bits_per_word = 8,
  140. .len = 2,
  141. .cs_change = 1,
  142. .delay.value = adis->data->read_delay,
  143. .delay.unit = SPI_DELAY_UNIT_USECS,
  144. .cs_change_delay.value = adis->data->cs_change_delay,
  145. .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
  146. }, {
  147. .tx_buf = adis->tx + 4,
  148. .rx_buf = adis->rx,
  149. .bits_per_word = 8,
  150. .len = 2,
  151. .cs_change = 1,
  152. .delay.value = adis->data->read_delay,
  153. .delay.unit = SPI_DELAY_UNIT_USECS,
  154. .cs_change_delay.value = adis->data->cs_change_delay,
  155. .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
  156. }, {
  157. .rx_buf = adis->rx + 2,
  158. .bits_per_word = 8,
  159. .len = 2,
  160. .delay.value = adis->data->read_delay,
  161. .delay.unit = SPI_DELAY_UNIT_USECS,
  162. },
  163. };
  164. spi_message_init(&msg);
  165. if (adis->current_page != page) {
  166. adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
  167. adis->tx[1] = page;
  168. spi_message_add_tail(&xfers[0], &msg);
  169. }
  170. switch (size) {
  171. case 4:
  172. adis->tx[2] = ADIS_READ_REG(reg + 2);
  173. adis->tx[3] = 0;
  174. spi_message_add_tail(&xfers[1], &msg);
  175. fallthrough;
  176. case 2:
  177. adis->tx[4] = ADIS_READ_REG(reg);
  178. adis->tx[5] = 0;
  179. spi_message_add_tail(&xfers[2], &msg);
  180. spi_message_add_tail(&xfers[3], &msg);
  181. break;
  182. default:
  183. return -EINVAL;
  184. }
  185. ret = spi_sync(adis->spi, &msg);
  186. if (ret) {
  187. dev_err(&adis->spi->dev, "Failed to read register 0x%02X: %d\n",
  188. reg, ret);
  189. return ret;
  190. }
  191. adis->current_page = page;
  192. switch (size) {
  193. case 4:
  194. *val = get_unaligned_be32(adis->rx);
  195. break;
  196. case 2:
  197. *val = get_unaligned_be16(adis->rx + 2);
  198. break;
  199. }
  200. return ret;
  201. }
  202. EXPORT_SYMBOL_NS_GPL(__adis_read_reg, IIO_ADISLIB);
  203. /**
  204. * __adis_update_bits_base() - ADIS Update bits function - Unlocked version
  205. * @adis: The adis device
  206. * @reg: The address of the lower of the two registers
  207. * @mask: Bitmask to change
  208. * @val: Value to be written
  209. * @size: Size of the register to update
  210. *
  211. * Updates the desired bits of @reg in accordance with @mask and @val.
  212. */
  213. int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask,
  214. const u32 val, u8 size)
  215. {
  216. int ret;
  217. u32 __val;
  218. ret = __adis_read_reg(adis, reg, &__val, size);
  219. if (ret)
  220. return ret;
  221. __val = (__val & ~mask) | (val & mask);
  222. return __adis_write_reg(adis, reg, __val, size);
  223. }
  224. EXPORT_SYMBOL_NS_GPL(__adis_update_bits_base, IIO_ADISLIB);
  225. #ifdef CONFIG_DEBUG_FS
  226. int adis_debugfs_reg_access(struct iio_dev *indio_dev, unsigned int reg,
  227. unsigned int writeval, unsigned int *readval)
  228. {
  229. struct adis *adis = iio_device_get_drvdata(indio_dev);
  230. if (readval) {
  231. u16 val16;
  232. int ret;
  233. ret = adis_read_reg_16(adis, reg, &val16);
  234. if (ret == 0)
  235. *readval = val16;
  236. return ret;
  237. }
  238. return adis_write_reg_16(adis, reg, writeval);
  239. }
  240. EXPORT_SYMBOL_NS(adis_debugfs_reg_access, IIO_ADISLIB);
  241. #endif
  242. /**
  243. * __adis_enable_irq() - Enable or disable data ready IRQ (unlocked)
  244. * @adis: The adis device
  245. * @enable: Whether to enable the IRQ
  246. *
  247. * Returns 0 on success, negative error code otherwise
  248. */
  249. int __adis_enable_irq(struct adis *adis, bool enable)
  250. {
  251. int ret;
  252. u16 msc;
  253. if (adis->data->enable_irq)
  254. return adis->data->enable_irq(adis, enable);
  255. if (adis->data->unmasked_drdy) {
  256. if (enable)
  257. enable_irq(adis->spi->irq);
  258. else
  259. disable_irq(adis->spi->irq);
  260. return 0;
  261. }
  262. ret = __adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc);
  263. if (ret)
  264. return ret;
  265. msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH;
  266. msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2;
  267. if (enable)
  268. msc |= ADIS_MSC_CTRL_DATA_RDY_EN;
  269. else
  270. msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN;
  271. return __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc);
  272. }
  273. EXPORT_SYMBOL_NS(__adis_enable_irq, IIO_ADISLIB);
  274. /**
  275. * __adis_check_status() - Check the device for error conditions (unlocked)
  276. * @adis: The adis device
  277. *
  278. * Returns 0 on success, a negative error code otherwise
  279. */
  280. int __adis_check_status(struct adis *adis)
  281. {
  282. u16 status;
  283. int ret;
  284. int i;
  285. ret = __adis_read_reg_16(adis, adis->data->diag_stat_reg, &status);
  286. if (ret)
  287. return ret;
  288. status &= adis->data->status_error_mask;
  289. if (status == 0)
  290. return 0;
  291. for (i = 0; i < 16; ++i) {
  292. if (status & BIT(i)) {
  293. dev_err(&adis->spi->dev, "%s.\n",
  294. adis->data->status_error_msgs[i]);
  295. }
  296. }
  297. return -EIO;
  298. }
  299. EXPORT_SYMBOL_NS_GPL(__adis_check_status, IIO_ADISLIB);
  300. /**
  301. * __adis_reset() - Reset the device (unlocked version)
  302. * @adis: The adis device
  303. *
  304. * Returns 0 on success, a negative error code otherwise
  305. */
  306. int __adis_reset(struct adis *adis)
  307. {
  308. int ret;
  309. const struct adis_timeout *timeouts = adis->data->timeouts;
  310. ret = __adis_write_reg_8(adis, adis->data->glob_cmd_reg,
  311. ADIS_GLOB_CMD_SW_RESET);
  312. if (ret) {
  313. dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret);
  314. return ret;
  315. }
  316. msleep(timeouts->sw_reset_ms);
  317. return 0;
  318. }
  319. EXPORT_SYMBOL_NS_GPL(__adis_reset, IIO_ADIS_LIB);
  320. static int adis_self_test(struct adis *adis)
  321. {
  322. int ret;
  323. const struct adis_timeout *timeouts = adis->data->timeouts;
  324. ret = __adis_write_reg_16(adis, adis->data->self_test_reg,
  325. adis->data->self_test_mask);
  326. if (ret) {
  327. dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n",
  328. ret);
  329. return ret;
  330. }
  331. msleep(timeouts->self_test_ms);
  332. ret = __adis_check_status(adis);
  333. if (adis->data->self_test_no_autoclear)
  334. __adis_write_reg_16(adis, adis->data->self_test_reg, 0x00);
  335. return ret;
  336. }
  337. /**
  338. * __adis_initial_startup() - Device initial setup
  339. * @adis: The adis device
  340. *
  341. * The function performs a HW reset via a reset pin that should be specified
  342. * via GPIOLIB. If no pin is configured a SW reset will be performed.
  343. * The RST pin for the ADIS devices should be configured as ACTIVE_LOW.
  344. *
  345. * After the self-test operation is performed, the function will also check
  346. * that the product ID is as expected. This assumes that drivers providing
  347. * 'prod_id_reg' will also provide the 'prod_id'.
  348. *
  349. * Returns 0 if the device is operational, a negative error code otherwise.
  350. *
  351. * This function should be called early on in the device initialization sequence
  352. * to ensure that the device is in a sane and known state and that it is usable.
  353. */
  354. int __adis_initial_startup(struct adis *adis)
  355. {
  356. const struct adis_timeout *timeouts = adis->data->timeouts;
  357. struct gpio_desc *gpio;
  358. u16 prod_id;
  359. int ret;
  360. /* check if the device has rst pin low */
  361. gpio = devm_gpiod_get_optional(&adis->spi->dev, "reset", GPIOD_OUT_HIGH);
  362. if (IS_ERR(gpio))
  363. return PTR_ERR(gpio);
  364. if (gpio) {
  365. usleep_range(10, 12);
  366. /* bring device out of reset */
  367. gpiod_set_value_cansleep(gpio, 0);
  368. msleep(timeouts->reset_ms);
  369. } else {
  370. ret = __adis_reset(adis);
  371. if (ret)
  372. return ret;
  373. }
  374. ret = adis_self_test(adis);
  375. if (ret)
  376. return ret;
  377. /*
  378. * don't bother calling this if we can't unmask the IRQ as in this case
  379. * the IRQ is most likely not yet requested and we will request it
  380. * with 'IRQF_NO_AUTOEN' anyways.
  381. */
  382. if (!adis->data->unmasked_drdy)
  383. __adis_enable_irq(adis, false);
  384. if (!adis->data->prod_id_reg)
  385. return 0;
  386. ret = adis_read_reg_16(adis, adis->data->prod_id_reg, &prod_id);
  387. if (ret)
  388. return ret;
  389. if (prod_id != adis->data->prod_id)
  390. dev_warn(&adis->spi->dev,
  391. "Device ID(%u) and product ID(%u) do not match.\n",
  392. adis->data->prod_id, prod_id);
  393. return 0;
  394. }
  395. EXPORT_SYMBOL_NS_GPL(__adis_initial_startup, IIO_ADISLIB);
  396. /**
  397. * adis_single_conversion() - Performs a single sample conversion
  398. * @indio_dev: The IIO device
  399. * @chan: The IIO channel
  400. * @error_mask: Mask for the error bit
  401. * @val: Result of the conversion
  402. *
  403. * Returns IIO_VAL_INT on success, a negative error code otherwise.
  404. *
  405. * The function performs a single conversion on a given channel and post
  406. * processes the value accordingly to the channel spec. If a error_mask is given
  407. * the function will check if the mask is set in the returned raw value. If it
  408. * is set the function will perform a self-check. If the device does not report
  409. * a error bit in the channels raw value set error_mask to 0.
  410. */
  411. int adis_single_conversion(struct iio_dev *indio_dev,
  412. const struct iio_chan_spec *chan,
  413. unsigned int error_mask, int *val)
  414. {
  415. struct adis *adis = iio_device_get_drvdata(indio_dev);
  416. unsigned int uval;
  417. int ret;
  418. mutex_lock(&adis->state_lock);
  419. ret = __adis_read_reg(adis, chan->address, &uval,
  420. chan->scan_type.storagebits / 8);
  421. if (ret)
  422. goto err_unlock;
  423. if (uval & error_mask) {
  424. ret = __adis_check_status(adis);
  425. if (ret)
  426. goto err_unlock;
  427. }
  428. if (chan->scan_type.sign == 's')
  429. *val = sign_extend32(uval, chan->scan_type.realbits - 1);
  430. else
  431. *val = uval & ((1 << chan->scan_type.realbits) - 1);
  432. ret = IIO_VAL_INT;
  433. err_unlock:
  434. mutex_unlock(&adis->state_lock);
  435. return ret;
  436. }
  437. EXPORT_SYMBOL_NS_GPL(adis_single_conversion, IIO_ADISLIB);
  438. /**
  439. * adis_init() - Initialize adis device structure
  440. * @adis: The adis device
  441. * @indio_dev: The iio device
  442. * @spi: The spi device
  443. * @data: Chip specific data
  444. *
  445. * Returns 0 on success, a negative error code otherwise.
  446. *
  447. * This function must be called, before any other adis helper function may be
  448. * called.
  449. */
  450. int adis_init(struct adis *adis, struct iio_dev *indio_dev,
  451. struct spi_device *spi, const struct adis_data *data)
  452. {
  453. if (!data || !data->timeouts) {
  454. dev_err(&spi->dev, "No config data or timeouts not defined!\n");
  455. return -EINVAL;
  456. }
  457. mutex_init(&adis->state_lock);
  458. adis->spi = spi;
  459. adis->data = data;
  460. iio_device_set_drvdata(indio_dev, adis);
  461. if (data->has_paging) {
  462. /* Need to set the page before first read/write */
  463. adis->current_page = -1;
  464. } else {
  465. /* Page will always be 0 */
  466. adis->current_page = 0;
  467. }
  468. return 0;
  469. }
  470. EXPORT_SYMBOL_NS_GPL(adis_init, IIO_ADISLIB);
  471. MODULE_LICENSE("GPL");
  472. MODULE_AUTHOR("Lars-Peter Clausen <[email protected]>");
  473. MODULE_DESCRIPTION("Common library code for ADIS16XXX devices");