adis.c 13 KB

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