180503b23SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2ec04cb04SLars-Peter Clausen /* 3ec04cb04SLars-Peter Clausen * Common library for ADIS16XXX devices 4ec04cb04SLars-Peter Clausen * 5ec04cb04SLars-Peter Clausen * Copyright 2012 Analog Devices Inc. 6ec04cb04SLars-Peter Clausen * Author: Lars-Peter Clausen <lars@metafoo.de> 7ec04cb04SLars-Peter Clausen */ 8ec04cb04SLars-Peter Clausen 9ec04cb04SLars-Peter Clausen #include <linux/delay.h> 10ec04cb04SLars-Peter Clausen #include <linux/mutex.h> 11ec04cb04SLars-Peter Clausen #include <linux/device.h> 12ec04cb04SLars-Peter Clausen #include <linux/kernel.h> 13ec04cb04SLars-Peter Clausen #include <linux/spi/spi.h> 14ec04cb04SLars-Peter Clausen #include <linux/slab.h> 15ec04cb04SLars-Peter Clausen #include <linux/sysfs.h> 16ec04cb04SLars-Peter Clausen #include <linux/module.h> 17ec04cb04SLars-Peter Clausen #include <asm/unaligned.h> 18ec04cb04SLars-Peter Clausen 19ec04cb04SLars-Peter Clausen #include <linux/iio/iio.h> 20ec04cb04SLars-Peter Clausen #include <linux/iio/sysfs.h> 21ec04cb04SLars-Peter Clausen #include <linux/iio/buffer.h> 22ec04cb04SLars-Peter Clausen #include <linux/iio/imu/adis.h> 23ec04cb04SLars-Peter Clausen 24ec04cb04SLars-Peter Clausen #define ADIS_MSC_CTRL_DATA_RDY_EN BIT(2) 25ec04cb04SLars-Peter Clausen #define ADIS_MSC_CTRL_DATA_RDY_POL_HIGH BIT(1) 26ec04cb04SLars-Peter Clausen #define ADIS_MSC_CTRL_DATA_RDY_DIO2 BIT(0) 27ec04cb04SLars-Peter Clausen #define ADIS_GLOB_CMD_SW_RESET BIT(7) 28ec04cb04SLars-Peter Clausen 29770d4656SAlexandru Ardelean /** 30770d4656SAlexandru Ardelean * __adis_write_reg() - write N bytes to register (unlocked version) 31770d4656SAlexandru Ardelean * @adis: The adis device 32770d4656SAlexandru Ardelean * @reg: The address of the lower of the two registers 33770d4656SAlexandru Ardelean * @value: The value to write to device (up to 4 bytes) 34770d4656SAlexandru Ardelean * @size: The size of the @value (in bytes) 35770d4656SAlexandru Ardelean */ 36770d4656SAlexandru Ardelean int __adis_write_reg(struct adis *adis, unsigned int reg, 3757a1228aSLars-Peter Clausen unsigned int value, unsigned int size) 38ec04cb04SLars-Peter Clausen { 39484a0bf0SLars-Peter Clausen unsigned int page = reg / ADIS_PAGE_SIZE; 4057a1228aSLars-Peter Clausen int ret, i; 41ec04cb04SLars-Peter Clausen struct spi_message msg; 42ec04cb04SLars-Peter Clausen struct spi_transfer xfers[] = { 43ec04cb04SLars-Peter Clausen { 44ec04cb04SLars-Peter Clausen .tx_buf = adis->tx, 45ec04cb04SLars-Peter Clausen .bits_per_word = 8, 46ec04cb04SLars-Peter Clausen .len = 2, 47ec04cb04SLars-Peter Clausen .cs_change = 1, 48ec04cb04SLars-Peter Clausen .delay_usecs = adis->data->write_delay, 49329f0dacSAlexandru Ardelean .cs_change_delay.value = adis->data->cs_change_delay, 50329f0dacSAlexandru Ardelean .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, 51ec04cb04SLars-Peter Clausen }, { 52ec04cb04SLars-Peter Clausen .tx_buf = adis->tx + 2, 53ec04cb04SLars-Peter Clausen .bits_per_word = 8, 54ec04cb04SLars-Peter Clausen .len = 2, 5557a1228aSLars-Peter Clausen .cs_change = 1, 5657a1228aSLars-Peter Clausen .delay_usecs = adis->data->write_delay, 57329f0dacSAlexandru Ardelean .cs_change_delay.value = adis->data->cs_change_delay, 58329f0dacSAlexandru Ardelean .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, 5957a1228aSLars-Peter Clausen }, { 6057a1228aSLars-Peter Clausen .tx_buf = adis->tx + 4, 6157a1228aSLars-Peter Clausen .bits_per_word = 8, 6257a1228aSLars-Peter Clausen .len = 2, 6357a1228aSLars-Peter Clausen .cs_change = 1, 6457a1228aSLars-Peter Clausen .delay_usecs = adis->data->write_delay, 65329f0dacSAlexandru Ardelean .cs_change_delay.value = adis->data->cs_change_delay, 66329f0dacSAlexandru Ardelean .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, 6757a1228aSLars-Peter Clausen }, { 6857a1228aSLars-Peter Clausen .tx_buf = adis->tx + 6, 6957a1228aSLars-Peter Clausen .bits_per_word = 8, 7057a1228aSLars-Peter Clausen .len = 2, 71ec04cb04SLars-Peter Clausen .delay_usecs = adis->data->write_delay, 72484a0bf0SLars-Peter Clausen }, { 73484a0bf0SLars-Peter Clausen .tx_buf = adis->tx + 8, 74484a0bf0SLars-Peter Clausen .bits_per_word = 8, 75484a0bf0SLars-Peter Clausen .len = 2, 76484a0bf0SLars-Peter Clausen .delay_usecs = adis->data->write_delay, 77ec04cb04SLars-Peter Clausen }, 78ec04cb04SLars-Peter Clausen }; 79ec04cb04SLars-Peter Clausen 80ec04cb04SLars-Peter Clausen spi_message_init(&msg); 81484a0bf0SLars-Peter Clausen 82484a0bf0SLars-Peter Clausen if (adis->current_page != page) { 83484a0bf0SLars-Peter Clausen adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID); 84484a0bf0SLars-Peter Clausen adis->tx[1] = page; 85484a0bf0SLars-Peter Clausen spi_message_add_tail(&xfers[0], &msg); 86484a0bf0SLars-Peter Clausen } 87484a0bf0SLars-Peter Clausen 8857a1228aSLars-Peter Clausen switch (size) { 8957a1228aSLars-Peter Clausen case 4: 90484a0bf0SLars-Peter Clausen adis->tx[8] = ADIS_WRITE_REG(reg + 3); 91484a0bf0SLars-Peter Clausen adis->tx[9] = (value >> 24) & 0xff; 92484a0bf0SLars-Peter Clausen adis->tx[6] = ADIS_WRITE_REG(reg + 2); 93484a0bf0SLars-Peter Clausen adis->tx[7] = (value >> 16) & 0xff; 9482d65f9dSGustavo A. R. Silva /* fall through */ 9557a1228aSLars-Peter Clausen case 2: 96484a0bf0SLars-Peter Clausen adis->tx[4] = ADIS_WRITE_REG(reg + 1); 97484a0bf0SLars-Peter Clausen adis->tx[5] = (value >> 8) & 0xff; 9882d65f9dSGustavo A. R. Silva /* fall through */ 9957a1228aSLars-Peter Clausen case 1: 100484a0bf0SLars-Peter Clausen adis->tx[2] = ADIS_WRITE_REG(reg); 101484a0bf0SLars-Peter Clausen adis->tx[3] = value & 0xff; 10257a1228aSLars-Peter Clausen break; 10357a1228aSLars-Peter Clausen default: 104770d4656SAlexandru Ardelean return -EINVAL; 10557a1228aSLars-Peter Clausen } 10657a1228aSLars-Peter Clausen 107484a0bf0SLars-Peter Clausen xfers[size].cs_change = 0; 10857a1228aSLars-Peter Clausen 109484a0bf0SLars-Peter Clausen for (i = 1; i <= size; i++) 11057a1228aSLars-Peter Clausen spi_message_add_tail(&xfers[i], &msg); 11157a1228aSLars-Peter Clausen 112ec04cb04SLars-Peter Clausen ret = spi_sync(adis->spi, &msg); 11357a1228aSLars-Peter Clausen if (ret) { 11457a1228aSLars-Peter Clausen dev_err(&adis->spi->dev, "Failed to write register 0x%02X: %d\n", 11557a1228aSLars-Peter Clausen reg, ret); 116484a0bf0SLars-Peter Clausen } else { 117484a0bf0SLars-Peter Clausen adis->current_page = page; 11857a1228aSLars-Peter Clausen } 11957a1228aSLars-Peter Clausen 120ec04cb04SLars-Peter Clausen return ret; 121ec04cb04SLars-Peter Clausen } 122770d4656SAlexandru Ardelean EXPORT_SYMBOL_GPL(__adis_write_reg); 123ec04cb04SLars-Peter Clausen 124ec04cb04SLars-Peter Clausen /** 125770d4656SAlexandru Ardelean * __adis_read_reg() - read N bytes from register (unlocked version) 126ec04cb04SLars-Peter Clausen * @adis: The adis device 127ec04cb04SLars-Peter Clausen * @reg: The address of the lower of the two registers 128ec04cb04SLars-Peter Clausen * @val: The value read back from the device 129770d4656SAlexandru Ardelean * @size: The size of the @val buffer 130ec04cb04SLars-Peter Clausen */ 131770d4656SAlexandru Ardelean int __adis_read_reg(struct adis *adis, unsigned int reg, 13257a1228aSLars-Peter Clausen unsigned int *val, unsigned int size) 133ec04cb04SLars-Peter Clausen { 134484a0bf0SLars-Peter Clausen unsigned int page = reg / ADIS_PAGE_SIZE; 135ec04cb04SLars-Peter Clausen struct spi_message msg; 136ec04cb04SLars-Peter Clausen int ret; 137ec04cb04SLars-Peter Clausen struct spi_transfer xfers[] = { 138ec04cb04SLars-Peter Clausen { 139ec04cb04SLars-Peter Clausen .tx_buf = adis->tx, 140ec04cb04SLars-Peter Clausen .bits_per_word = 8, 141ec04cb04SLars-Peter Clausen .len = 2, 142ec04cb04SLars-Peter Clausen .cs_change = 1, 143484a0bf0SLars-Peter Clausen .delay_usecs = adis->data->write_delay, 144329f0dacSAlexandru Ardelean .cs_change_delay.value = adis->data->cs_change_delay, 145329f0dacSAlexandru Ardelean .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, 146ec04cb04SLars-Peter Clausen }, { 14757a1228aSLars-Peter Clausen .tx_buf = adis->tx + 2, 148484a0bf0SLars-Peter Clausen .bits_per_word = 8, 149484a0bf0SLars-Peter Clausen .len = 2, 150484a0bf0SLars-Peter Clausen .cs_change = 1, 151484a0bf0SLars-Peter Clausen .delay_usecs = adis->data->read_delay, 152329f0dacSAlexandru Ardelean .cs_change_delay.value = adis->data->cs_change_delay, 153329f0dacSAlexandru Ardelean .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, 154484a0bf0SLars-Peter Clausen }, { 155484a0bf0SLars-Peter Clausen .tx_buf = adis->tx + 4, 156ec04cb04SLars-Peter Clausen .rx_buf = adis->rx, 157ec04cb04SLars-Peter Clausen .bits_per_word = 8, 158ec04cb04SLars-Peter Clausen .len = 2, 15957a1228aSLars-Peter Clausen .cs_change = 1, 16057a1228aSLars-Peter Clausen .delay_usecs = adis->data->read_delay, 161329f0dacSAlexandru Ardelean .cs_change_delay.value = adis->data->cs_change_delay, 162329f0dacSAlexandru Ardelean .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, 16357a1228aSLars-Peter Clausen }, { 16457a1228aSLars-Peter Clausen .rx_buf = adis->rx + 2, 16557a1228aSLars-Peter Clausen .bits_per_word = 8, 16657a1228aSLars-Peter Clausen .len = 2, 167ec04cb04SLars-Peter Clausen .delay_usecs = adis->data->read_delay, 168ec04cb04SLars-Peter Clausen }, 169ec04cb04SLars-Peter Clausen }; 170ec04cb04SLars-Peter Clausen 171ec04cb04SLars-Peter Clausen spi_message_init(&msg); 17257a1228aSLars-Peter Clausen 173484a0bf0SLars-Peter Clausen if (adis->current_page != page) { 174484a0bf0SLars-Peter Clausen adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID); 175484a0bf0SLars-Peter Clausen adis->tx[1] = page; 176484a0bf0SLars-Peter Clausen spi_message_add_tail(&xfers[0], &msg); 177484a0bf0SLars-Peter Clausen } 178484a0bf0SLars-Peter Clausen 17957a1228aSLars-Peter Clausen switch (size) { 18057a1228aSLars-Peter Clausen case 4: 181484a0bf0SLars-Peter Clausen adis->tx[2] = ADIS_READ_REG(reg + 2); 18257a1228aSLars-Peter Clausen adis->tx[3] = 0; 183ec04cb04SLars-Peter Clausen spi_message_add_tail(&xfers[1], &msg); 18482d65f9dSGustavo A. R. Silva /* fall through */ 185484a0bf0SLars-Peter Clausen case 2: 186484a0bf0SLars-Peter Clausen adis->tx[4] = ADIS_READ_REG(reg); 187484a0bf0SLars-Peter Clausen adis->tx[5] = 0; 18857a1228aSLars-Peter Clausen spi_message_add_tail(&xfers[2], &msg); 189484a0bf0SLars-Peter Clausen spi_message_add_tail(&xfers[3], &msg); 19057a1228aSLars-Peter Clausen break; 19157a1228aSLars-Peter Clausen default: 192770d4656SAlexandru Ardelean return -EINVAL; 19357a1228aSLars-Peter Clausen } 19457a1228aSLars-Peter Clausen 195ec04cb04SLars-Peter Clausen ret = spi_sync(adis->spi, &msg); 196ec04cb04SLars-Peter Clausen if (ret) { 19757a1228aSLars-Peter Clausen dev_err(&adis->spi->dev, "Failed to read register 0x%02X: %d\n", 198ec04cb04SLars-Peter Clausen reg, ret); 199770d4656SAlexandru Ardelean return ret; 200484a0bf0SLars-Peter Clausen } else { 201484a0bf0SLars-Peter Clausen adis->current_page = page; 202ec04cb04SLars-Peter Clausen } 203ec04cb04SLars-Peter Clausen 20457a1228aSLars-Peter Clausen switch (size) { 20557a1228aSLars-Peter Clausen case 4: 20657a1228aSLars-Peter Clausen *val = get_unaligned_be32(adis->rx); 20757a1228aSLars-Peter Clausen break; 20857a1228aSLars-Peter Clausen case 2: 20957a1228aSLars-Peter Clausen *val = get_unaligned_be16(adis->rx + 2); 21057a1228aSLars-Peter Clausen break; 21157a1228aSLars-Peter Clausen } 21257a1228aSLars-Peter Clausen 213ec04cb04SLars-Peter Clausen return ret; 214ec04cb04SLars-Peter Clausen } 215770d4656SAlexandru Ardelean EXPORT_SYMBOL_GPL(__adis_read_reg); 216ec04cb04SLars-Peter Clausen 21778026a6fSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 21878026a6fSLars-Peter Clausen 21978026a6fSLars-Peter Clausen int adis_debugfs_reg_access(struct iio_dev *indio_dev, 22078026a6fSLars-Peter Clausen unsigned int reg, unsigned int writeval, unsigned int *readval) 22178026a6fSLars-Peter Clausen { 22278026a6fSLars-Peter Clausen struct adis *adis = iio_device_get_drvdata(indio_dev); 22378026a6fSLars-Peter Clausen 22478026a6fSLars-Peter Clausen if (readval) { 22578026a6fSLars-Peter Clausen uint16_t val16; 22678026a6fSLars-Peter Clausen int ret; 22778026a6fSLars-Peter Clausen 22878026a6fSLars-Peter Clausen ret = adis_read_reg_16(adis, reg, &val16); 22938262c01SAlexandru Ardelean if (ret == 0) 23078026a6fSLars-Peter Clausen *readval = val16; 23178026a6fSLars-Peter Clausen 23278026a6fSLars-Peter Clausen return ret; 23378026a6fSLars-Peter Clausen } else { 23478026a6fSLars-Peter Clausen return adis_write_reg_16(adis, reg, writeval); 23578026a6fSLars-Peter Clausen } 23678026a6fSLars-Peter Clausen } 23778026a6fSLars-Peter Clausen EXPORT_SYMBOL(adis_debugfs_reg_access); 23878026a6fSLars-Peter Clausen 23978026a6fSLars-Peter Clausen #endif 24078026a6fSLars-Peter Clausen 241ec04cb04SLars-Peter Clausen /** 242ec04cb04SLars-Peter Clausen * adis_enable_irq() - Enable or disable data ready IRQ 243ec04cb04SLars-Peter Clausen * @adis: The adis device 244ec04cb04SLars-Peter Clausen * @enable: Whether to enable the IRQ 245ec04cb04SLars-Peter Clausen * 246ec04cb04SLars-Peter Clausen * Returns 0 on success, negative error code otherwise 247ec04cb04SLars-Peter Clausen */ 248ec04cb04SLars-Peter Clausen int adis_enable_irq(struct adis *adis, bool enable) 249ec04cb04SLars-Peter Clausen { 250ec04cb04SLars-Peter Clausen int ret = 0; 251ec04cb04SLars-Peter Clausen uint16_t msc; 252ec04cb04SLars-Peter Clausen 253100bfa38SAlexandru Ardelean mutex_lock(&adis->state_lock); 2542f3abe6cSLars-Peter Clausen 255100bfa38SAlexandru Ardelean if (adis->data->enable_irq) { 256100bfa38SAlexandru Ardelean ret = adis->data->enable_irq(adis, enable); 257100bfa38SAlexandru Ardelean goto out_unlock; 258100bfa38SAlexandru Ardelean } 259100bfa38SAlexandru Ardelean 260100bfa38SAlexandru Ardelean ret = __adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc); 261ec04cb04SLars-Peter Clausen if (ret) 262100bfa38SAlexandru Ardelean goto out_unlock; 263ec04cb04SLars-Peter Clausen 264ec04cb04SLars-Peter Clausen msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH; 265ec04cb04SLars-Peter Clausen msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2; 266ec04cb04SLars-Peter Clausen if (enable) 267ec04cb04SLars-Peter Clausen msc |= ADIS_MSC_CTRL_DATA_RDY_EN; 268ec04cb04SLars-Peter Clausen else 269ec04cb04SLars-Peter Clausen msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN; 270ec04cb04SLars-Peter Clausen 271100bfa38SAlexandru Ardelean ret = __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc); 272ec04cb04SLars-Peter Clausen 273100bfa38SAlexandru Ardelean out_unlock: 274100bfa38SAlexandru Ardelean mutex_unlock(&adis->state_lock); 275ec04cb04SLars-Peter Clausen return ret; 276ec04cb04SLars-Peter Clausen } 277ec04cb04SLars-Peter Clausen EXPORT_SYMBOL(adis_enable_irq); 278ec04cb04SLars-Peter Clausen 279ec04cb04SLars-Peter Clausen /** 2806a4d6a7dSAlexandru Ardelean * __adis_check_status() - Check the device for error conditions (unlocked) 281ec04cb04SLars-Peter Clausen * @adis: The adis device 282ec04cb04SLars-Peter Clausen * 283ec04cb04SLars-Peter Clausen * Returns 0 on success, a negative error code otherwise 284ec04cb04SLars-Peter Clausen */ 2856a4d6a7dSAlexandru Ardelean int __adis_check_status(struct adis *adis) 286ec04cb04SLars-Peter Clausen { 287ec04cb04SLars-Peter Clausen uint16_t status; 288ec04cb04SLars-Peter Clausen int ret; 289ec04cb04SLars-Peter Clausen int i; 290ec04cb04SLars-Peter Clausen 2916a4d6a7dSAlexandru Ardelean ret = __adis_read_reg_16(adis, adis->data->diag_stat_reg, &status); 2926a39ab3bSAlexandru Ardelean if (ret) 293ec04cb04SLars-Peter Clausen return ret; 294ec04cb04SLars-Peter Clausen 295ec04cb04SLars-Peter Clausen status &= adis->data->status_error_mask; 296ec04cb04SLars-Peter Clausen 297ec04cb04SLars-Peter Clausen if (status == 0) 298ec04cb04SLars-Peter Clausen return 0; 299ec04cb04SLars-Peter Clausen 300ec04cb04SLars-Peter Clausen for (i = 0; i < 16; ++i) { 301ec04cb04SLars-Peter Clausen if (status & BIT(i)) { 302ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "%s.\n", 303ec04cb04SLars-Peter Clausen adis->data->status_error_msgs[i]); 304ec04cb04SLars-Peter Clausen } 305ec04cb04SLars-Peter Clausen } 306ec04cb04SLars-Peter Clausen 307ec04cb04SLars-Peter Clausen return -EIO; 308ec04cb04SLars-Peter Clausen } 3096a4d6a7dSAlexandru Ardelean EXPORT_SYMBOL_GPL(__adis_check_status); 310ec04cb04SLars-Peter Clausen 311ec04cb04SLars-Peter Clausen /** 312762ab093SAlexandru Ardelean * __adis_reset() - Reset the device (unlocked version) 313ec04cb04SLars-Peter Clausen * @adis: The adis device 314ec04cb04SLars-Peter Clausen * 315ec04cb04SLars-Peter Clausen * Returns 0 on success, a negative error code otherwise 316ec04cb04SLars-Peter Clausen */ 317762ab093SAlexandru Ardelean int __adis_reset(struct adis *adis) 318ec04cb04SLars-Peter Clausen { 319ec04cb04SLars-Peter Clausen int ret; 320380b107bSNuno Sá const struct adis_timeout *timeouts = adis->data->timeouts; 321ec04cb04SLars-Peter Clausen 322762ab093SAlexandru Ardelean ret = __adis_write_reg_8(adis, adis->data->glob_cmd_reg, 323ec04cb04SLars-Peter Clausen ADIS_GLOB_CMD_SW_RESET); 324380b107bSNuno Sá if (ret) { 325ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret); 326ec04cb04SLars-Peter Clausen return ret; 327ec04cb04SLars-Peter Clausen } 328380b107bSNuno Sá 329380b107bSNuno Sá msleep(timeouts->sw_reset_ms); 330380b107bSNuno Sá 331380b107bSNuno Sá return 0; 332380b107bSNuno Sá } 333762ab093SAlexandru Ardelean EXPORT_SYMBOL_GPL(__adis_reset); 334ec04cb04SLars-Peter Clausen 335ec04cb04SLars-Peter Clausen static int adis_self_test(struct adis *adis) 336ec04cb04SLars-Peter Clausen { 337ec04cb04SLars-Peter Clausen int ret; 338380b107bSNuno Sá const struct adis_timeout *timeouts = adis->data->timeouts; 339ec04cb04SLars-Peter Clausen 340cb5a07f1SAlexandru Ardelean ret = __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 341ec04cb04SLars-Peter Clausen adis->data->self_test_mask); 342ec04cb04SLars-Peter Clausen if (ret) { 343ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n", 344ec04cb04SLars-Peter Clausen ret); 345ec04cb04SLars-Peter Clausen return ret; 346ec04cb04SLars-Peter Clausen } 347ec04cb04SLars-Peter Clausen 348380b107bSNuno Sá msleep(timeouts->self_test_ms); 349ec04cb04SLars-Peter Clausen 350cb5a07f1SAlexandru Ardelean ret = __adis_check_status(adis); 351af8a4127SLars-Peter Clausen 352af8a4127SLars-Peter Clausen if (adis->data->self_test_no_autoclear) 353cb5a07f1SAlexandru Ardelean __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 0x00); 354af8a4127SLars-Peter Clausen 355af8a4127SLars-Peter Clausen return ret; 356ec04cb04SLars-Peter Clausen } 357ec04cb04SLars-Peter Clausen 358ec04cb04SLars-Peter Clausen /** 359ec04cb04SLars-Peter Clausen * adis_inital_startup() - Performs device self-test 360ec04cb04SLars-Peter Clausen * @adis: The adis device 361ec04cb04SLars-Peter Clausen * 362ec04cb04SLars-Peter Clausen * Returns 0 if the device is operational, a negative error code otherwise. 363ec04cb04SLars-Peter Clausen * 364ec04cb04SLars-Peter Clausen * This function should be called early on in the device initialization sequence 365ec04cb04SLars-Peter Clausen * to ensure that the device is in a sane and known state and that it is usable. 366ec04cb04SLars-Peter Clausen */ 367ec04cb04SLars-Peter Clausen int adis_initial_startup(struct adis *adis) 368ec04cb04SLars-Peter Clausen { 369ec04cb04SLars-Peter Clausen int ret; 370ec04cb04SLars-Peter Clausen 371cb5a07f1SAlexandru Ardelean mutex_lock(&adis->state_lock); 372cb5a07f1SAlexandru Ardelean 373ec04cb04SLars-Peter Clausen ret = adis_self_test(adis); 374ec04cb04SLars-Peter Clausen if (ret) { 375ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Self-test failed, trying reset.\n"); 376cb5a07f1SAlexandru Ardelean __adis_reset(adis); 377ec04cb04SLars-Peter Clausen ret = adis_self_test(adis); 378ec04cb04SLars-Peter Clausen if (ret) { 379ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Second self-test failed, giving up.\n"); 380cb5a07f1SAlexandru Ardelean goto out_unlock; 381ec04cb04SLars-Peter Clausen } 382ec04cb04SLars-Peter Clausen } 383ec04cb04SLars-Peter Clausen 384cb5a07f1SAlexandru Ardelean out_unlock: 385cb5a07f1SAlexandru Ardelean mutex_unlock(&adis->state_lock); 386cb5a07f1SAlexandru Ardelean return ret; 387ec04cb04SLars-Peter Clausen } 388ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_initial_startup); 389ec04cb04SLars-Peter Clausen 390ec04cb04SLars-Peter Clausen /** 391ec04cb04SLars-Peter Clausen * adis_single_conversion() - Performs a single sample conversion 392ec04cb04SLars-Peter Clausen * @indio_dev: The IIO device 393ec04cb04SLars-Peter Clausen * @chan: The IIO channel 394ec04cb04SLars-Peter Clausen * @error_mask: Mask for the error bit 395ec04cb04SLars-Peter Clausen * @val: Result of the conversion 396ec04cb04SLars-Peter Clausen * 397ec04cb04SLars-Peter Clausen * Returns IIO_VAL_INT on success, a negative error code otherwise. 398ec04cb04SLars-Peter Clausen * 399ec04cb04SLars-Peter Clausen * The function performs a single conversion on a given channel and post 400ec04cb04SLars-Peter Clausen * processes the value accordingly to the channel spec. If a error_mask is given 401ec04cb04SLars-Peter Clausen * the function will check if the mask is set in the returned raw value. If it 402ec04cb04SLars-Peter Clausen * is set the function will perform a self-check. If the device does not report 403ec04cb04SLars-Peter Clausen * a error bit in the channels raw value set error_mask to 0. 404ec04cb04SLars-Peter Clausen */ 405ec04cb04SLars-Peter Clausen int adis_single_conversion(struct iio_dev *indio_dev, 406ec04cb04SLars-Peter Clausen const struct iio_chan_spec *chan, unsigned int error_mask, int *val) 407ec04cb04SLars-Peter Clausen { 408ec04cb04SLars-Peter Clausen struct adis *adis = iio_device_get_drvdata(indio_dev); 40957a1228aSLars-Peter Clausen unsigned int uval; 410ec04cb04SLars-Peter Clausen int ret; 411ec04cb04SLars-Peter Clausen 412c5485a5dSAlexandru Ardelean mutex_lock(&adis->state_lock); 413ec04cb04SLars-Peter Clausen 414c5485a5dSAlexandru Ardelean ret = __adis_read_reg(adis, chan->address, &uval, 41557a1228aSLars-Peter Clausen chan->scan_type.storagebits / 8); 416ec04cb04SLars-Peter Clausen if (ret) 417ec04cb04SLars-Peter Clausen goto err_unlock; 418ec04cb04SLars-Peter Clausen 41957a1228aSLars-Peter Clausen if (uval & error_mask) { 420c5485a5dSAlexandru Ardelean ret = __adis_check_status(adis); 421ec04cb04SLars-Peter Clausen if (ret) 422ec04cb04SLars-Peter Clausen goto err_unlock; 423ec04cb04SLars-Peter Clausen } 424ec04cb04SLars-Peter Clausen 425ec04cb04SLars-Peter Clausen if (chan->scan_type.sign == 's') 42657a1228aSLars-Peter Clausen *val = sign_extend32(uval, chan->scan_type.realbits - 1); 427ec04cb04SLars-Peter Clausen else 42857a1228aSLars-Peter Clausen *val = uval & ((1 << chan->scan_type.realbits) - 1); 429ec04cb04SLars-Peter Clausen 430ec04cb04SLars-Peter Clausen ret = IIO_VAL_INT; 431ec04cb04SLars-Peter Clausen err_unlock: 432c5485a5dSAlexandru Ardelean mutex_unlock(&adis->state_lock); 433ec04cb04SLars-Peter Clausen return ret; 434ec04cb04SLars-Peter Clausen } 435ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_single_conversion); 436ec04cb04SLars-Peter Clausen 437ec04cb04SLars-Peter Clausen /** 438ec04cb04SLars-Peter Clausen * adis_init() - Initialize adis device structure 439ec04cb04SLars-Peter Clausen * @adis: The adis device 440ec04cb04SLars-Peter Clausen * @indio_dev: The iio device 441ec04cb04SLars-Peter Clausen * @spi: The spi device 442ec04cb04SLars-Peter Clausen * @data: Chip specific data 443ec04cb04SLars-Peter Clausen * 444ec04cb04SLars-Peter Clausen * Returns 0 on success, a negative error code otherwise. 445ec04cb04SLars-Peter Clausen * 446ec04cb04SLars-Peter Clausen * This function must be called, before any other adis helper function may be 447ec04cb04SLars-Peter Clausen * called. 448ec04cb04SLars-Peter Clausen */ 449ec04cb04SLars-Peter Clausen int adis_init(struct adis *adis, struct iio_dev *indio_dev, 450ec04cb04SLars-Peter Clausen struct spi_device *spi, const struct adis_data *data) 451ec04cb04SLars-Peter Clausen { 452380b107bSNuno Sá if (!data || !data->timeouts) { 453380b107bSNuno Sá dev_err(&spi->dev, "No config data or timeouts not defined!\n"); 454380b107bSNuno Sá return -EINVAL; 455380b107bSNuno Sá } 456380b107bSNuno Sá 4576a9afcb1SAlexandru Ardelean mutex_init(&adis->state_lock); 458ec04cb04SLars-Peter Clausen adis->spi = spi; 459ec04cb04SLars-Peter Clausen adis->data = data; 460ec04cb04SLars-Peter Clausen iio_device_set_drvdata(indio_dev, adis); 461ec04cb04SLars-Peter Clausen 462484a0bf0SLars-Peter Clausen if (data->has_paging) { 463484a0bf0SLars-Peter Clausen /* Need to set the page before first read/write */ 464484a0bf0SLars-Peter Clausen adis->current_page = -1; 465484a0bf0SLars-Peter Clausen } else { 466484a0bf0SLars-Peter Clausen /* Page will always be 0 */ 467484a0bf0SLars-Peter Clausen adis->current_page = 0; 468484a0bf0SLars-Peter Clausen } 469484a0bf0SLars-Peter Clausen 470ec04cb04SLars-Peter Clausen return adis_enable_irq(adis, false); 471ec04cb04SLars-Peter Clausen } 472ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_init); 473ec04cb04SLars-Peter Clausen 474ec04cb04SLars-Peter Clausen MODULE_LICENSE("GPL"); 475ec04cb04SLars-Peter Clausen MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 476ec04cb04SLars-Peter Clausen MODULE_DESCRIPTION("Common library code for ADIS16XXX devices"); 477