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, 49dca39af8SAlexandru Ardelean .cs_change_delay = adis->data->cs_change_delay, 50dca39af8SAlexandru 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, 57dca39af8SAlexandru Ardelean .cs_change_delay = adis->data->cs_change_delay, 58dca39af8SAlexandru 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, 65dca39af8SAlexandru Ardelean .cs_change_delay = adis->data->cs_change_delay, 66dca39af8SAlexandru 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, 144dca39af8SAlexandru Ardelean .cs_change_delay = adis->data->cs_change_delay, 145dca39af8SAlexandru 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, 152dca39af8SAlexandru Ardelean .cs_change_delay = adis->data->cs_change_delay, 153dca39af8SAlexandru 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, 161dca39af8SAlexandru Ardelean .cs_change_delay = adis->data->cs_change_delay, 162dca39af8SAlexandru 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 /** 280ec04cb04SLars-Peter Clausen * adis_check_status() - Check the device for error conditions 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 */ 285ec04cb04SLars-Peter Clausen 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 291ec04cb04SLars-Peter Clausen 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 } 309ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_check_status); 310ec04cb04SLars-Peter Clausen 311ec04cb04SLars-Peter Clausen /** 312ec04cb04SLars-Peter Clausen * adis_reset() - Reset the device 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 */ 317ec04cb04SLars-Peter Clausen int adis_reset(struct adis *adis) 318ec04cb04SLars-Peter Clausen { 319ec04cb04SLars-Peter Clausen int ret; 320ec04cb04SLars-Peter Clausen 321ec04cb04SLars-Peter Clausen ret = adis_write_reg_8(adis, adis->data->glob_cmd_reg, 322ec04cb04SLars-Peter Clausen ADIS_GLOB_CMD_SW_RESET); 323ec04cb04SLars-Peter Clausen if (ret) 324ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret); 325ec04cb04SLars-Peter Clausen 326ec04cb04SLars-Peter Clausen return ret; 327ec04cb04SLars-Peter Clausen } 328ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_reset); 329ec04cb04SLars-Peter Clausen 330ec04cb04SLars-Peter Clausen static int adis_self_test(struct adis *adis) 331ec04cb04SLars-Peter Clausen { 332ec04cb04SLars-Peter Clausen int ret; 333ec04cb04SLars-Peter Clausen 334ec04cb04SLars-Peter Clausen ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 335ec04cb04SLars-Peter Clausen adis->data->self_test_mask); 336ec04cb04SLars-Peter Clausen if (ret) { 337ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n", 338ec04cb04SLars-Peter Clausen ret); 339ec04cb04SLars-Peter Clausen return ret; 340ec04cb04SLars-Peter Clausen } 341ec04cb04SLars-Peter Clausen 342ec04cb04SLars-Peter Clausen msleep(adis->data->startup_delay); 343ec04cb04SLars-Peter Clausen 344af8a4127SLars-Peter Clausen ret = adis_check_status(adis); 345af8a4127SLars-Peter Clausen 346af8a4127SLars-Peter Clausen if (adis->data->self_test_no_autoclear) 347af8a4127SLars-Peter Clausen adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 0x00); 348af8a4127SLars-Peter Clausen 349af8a4127SLars-Peter Clausen return ret; 350ec04cb04SLars-Peter Clausen } 351ec04cb04SLars-Peter Clausen 352ec04cb04SLars-Peter Clausen /** 353ec04cb04SLars-Peter Clausen * adis_inital_startup() - Performs device self-test 354ec04cb04SLars-Peter Clausen * @adis: The adis device 355ec04cb04SLars-Peter Clausen * 356ec04cb04SLars-Peter Clausen * Returns 0 if the device is operational, a negative error code otherwise. 357ec04cb04SLars-Peter Clausen * 358ec04cb04SLars-Peter Clausen * This function should be called early on in the device initialization sequence 359ec04cb04SLars-Peter Clausen * to ensure that the device is in a sane and known state and that it is usable. 360ec04cb04SLars-Peter Clausen */ 361ec04cb04SLars-Peter Clausen int adis_initial_startup(struct adis *adis) 362ec04cb04SLars-Peter Clausen { 363ec04cb04SLars-Peter Clausen int ret; 364ec04cb04SLars-Peter Clausen 365ec04cb04SLars-Peter Clausen ret = adis_self_test(adis); 366ec04cb04SLars-Peter Clausen if (ret) { 367ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Self-test failed, trying reset.\n"); 368ec04cb04SLars-Peter Clausen adis_reset(adis); 369ec04cb04SLars-Peter Clausen msleep(adis->data->startup_delay); 370ec04cb04SLars-Peter Clausen ret = adis_self_test(adis); 371ec04cb04SLars-Peter Clausen if (ret) { 372ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Second self-test failed, giving up.\n"); 373ec04cb04SLars-Peter Clausen return ret; 374ec04cb04SLars-Peter Clausen } 375ec04cb04SLars-Peter Clausen } 376ec04cb04SLars-Peter Clausen 377ec04cb04SLars-Peter Clausen return 0; 378ec04cb04SLars-Peter Clausen } 379ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_initial_startup); 380ec04cb04SLars-Peter Clausen 381ec04cb04SLars-Peter Clausen /** 382ec04cb04SLars-Peter Clausen * adis_single_conversion() - Performs a single sample conversion 383ec04cb04SLars-Peter Clausen * @indio_dev: The IIO device 384ec04cb04SLars-Peter Clausen * @chan: The IIO channel 385ec04cb04SLars-Peter Clausen * @error_mask: Mask for the error bit 386ec04cb04SLars-Peter Clausen * @val: Result of the conversion 387ec04cb04SLars-Peter Clausen * 388ec04cb04SLars-Peter Clausen * Returns IIO_VAL_INT on success, a negative error code otherwise. 389ec04cb04SLars-Peter Clausen * 390ec04cb04SLars-Peter Clausen * The function performs a single conversion on a given channel and post 391ec04cb04SLars-Peter Clausen * processes the value accordingly to the channel spec. If a error_mask is given 392ec04cb04SLars-Peter Clausen * the function will check if the mask is set in the returned raw value. If it 393ec04cb04SLars-Peter Clausen * is set the function will perform a self-check. If the device does not report 394ec04cb04SLars-Peter Clausen * a error bit in the channels raw value set error_mask to 0. 395ec04cb04SLars-Peter Clausen */ 396ec04cb04SLars-Peter Clausen int adis_single_conversion(struct iio_dev *indio_dev, 397ec04cb04SLars-Peter Clausen const struct iio_chan_spec *chan, unsigned int error_mask, int *val) 398ec04cb04SLars-Peter Clausen { 399ec04cb04SLars-Peter Clausen struct adis *adis = iio_device_get_drvdata(indio_dev); 40057a1228aSLars-Peter Clausen unsigned int uval; 401ec04cb04SLars-Peter Clausen int ret; 402ec04cb04SLars-Peter Clausen 403ec04cb04SLars-Peter Clausen mutex_lock(&indio_dev->mlock); 404ec04cb04SLars-Peter Clausen 40557a1228aSLars-Peter Clausen ret = adis_read_reg(adis, chan->address, &uval, 40657a1228aSLars-Peter Clausen chan->scan_type.storagebits / 8); 407ec04cb04SLars-Peter Clausen if (ret) 408ec04cb04SLars-Peter Clausen goto err_unlock; 409ec04cb04SLars-Peter Clausen 41057a1228aSLars-Peter Clausen if (uval & error_mask) { 411ec04cb04SLars-Peter Clausen ret = adis_check_status(adis); 412ec04cb04SLars-Peter Clausen if (ret) 413ec04cb04SLars-Peter Clausen goto err_unlock; 414ec04cb04SLars-Peter Clausen } 415ec04cb04SLars-Peter Clausen 416ec04cb04SLars-Peter Clausen if (chan->scan_type.sign == 's') 41757a1228aSLars-Peter Clausen *val = sign_extend32(uval, chan->scan_type.realbits - 1); 418ec04cb04SLars-Peter Clausen else 41957a1228aSLars-Peter Clausen *val = uval & ((1 << chan->scan_type.realbits) - 1); 420ec04cb04SLars-Peter Clausen 421ec04cb04SLars-Peter Clausen ret = IIO_VAL_INT; 422ec04cb04SLars-Peter Clausen err_unlock: 423ec04cb04SLars-Peter Clausen mutex_unlock(&indio_dev->mlock); 424ec04cb04SLars-Peter Clausen return ret; 425ec04cb04SLars-Peter Clausen } 426ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_single_conversion); 427ec04cb04SLars-Peter Clausen 428ec04cb04SLars-Peter Clausen /** 429ec04cb04SLars-Peter Clausen * adis_init() - Initialize adis device structure 430ec04cb04SLars-Peter Clausen * @adis: The adis device 431ec04cb04SLars-Peter Clausen * @indio_dev: The iio device 432ec04cb04SLars-Peter Clausen * @spi: The spi device 433ec04cb04SLars-Peter Clausen * @data: Chip specific data 434ec04cb04SLars-Peter Clausen * 435ec04cb04SLars-Peter Clausen * Returns 0 on success, a negative error code otherwise. 436ec04cb04SLars-Peter Clausen * 437ec04cb04SLars-Peter Clausen * This function must be called, before any other adis helper function may be 438ec04cb04SLars-Peter Clausen * called. 439ec04cb04SLars-Peter Clausen */ 440ec04cb04SLars-Peter Clausen int adis_init(struct adis *adis, struct iio_dev *indio_dev, 441ec04cb04SLars-Peter Clausen struct spi_device *spi, const struct adis_data *data) 442ec04cb04SLars-Peter Clausen { 4436a9afcb1SAlexandru Ardelean mutex_init(&adis->state_lock); 444ec04cb04SLars-Peter Clausen adis->spi = spi; 445ec04cb04SLars-Peter Clausen adis->data = data; 446ec04cb04SLars-Peter Clausen iio_device_set_drvdata(indio_dev, adis); 447ec04cb04SLars-Peter Clausen 448484a0bf0SLars-Peter Clausen if (data->has_paging) { 449484a0bf0SLars-Peter Clausen /* Need to set the page before first read/write */ 450484a0bf0SLars-Peter Clausen adis->current_page = -1; 451484a0bf0SLars-Peter Clausen } else { 452484a0bf0SLars-Peter Clausen /* Page will always be 0 */ 453484a0bf0SLars-Peter Clausen adis->current_page = 0; 454484a0bf0SLars-Peter Clausen } 455484a0bf0SLars-Peter Clausen 456ec04cb04SLars-Peter Clausen return adis_enable_irq(adis, false); 457ec04cb04SLars-Peter Clausen } 458ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_init); 459ec04cb04SLars-Peter Clausen 460ec04cb04SLars-Peter Clausen MODULE_LICENSE("GPL"); 461ec04cb04SLars-Peter Clausen MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 462ec04cb04SLars-Peter Clausen MODULE_DESCRIPTION("Common library code for ADIS16XXX devices"); 463