adis.c 13 KB

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