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 2957a1228aSLars-Peter Clausen int adis_write_reg(struct adis *adis, unsigned int reg, 3057a1228aSLars-Peter Clausen unsigned int value, unsigned int size) 31ec04cb04SLars-Peter Clausen { 32484a0bf0SLars-Peter Clausen unsigned int page = reg / ADIS_PAGE_SIZE; 3357a1228aSLars-Peter Clausen int ret, i; 34ec04cb04SLars-Peter Clausen struct spi_message msg; 35ec04cb04SLars-Peter Clausen struct spi_transfer xfers[] = { 36ec04cb04SLars-Peter Clausen { 37ec04cb04SLars-Peter Clausen .tx_buf = adis->tx, 38ec04cb04SLars-Peter Clausen .bits_per_word = 8, 39ec04cb04SLars-Peter Clausen .len = 2, 40ec04cb04SLars-Peter Clausen .cs_change = 1, 41ec04cb04SLars-Peter Clausen .delay_usecs = adis->data->write_delay, 42dca39af8SAlexandru Ardelean .cs_change_delay = adis->data->cs_change_delay, 43dca39af8SAlexandru Ardelean .cs_change_delay_unit = SPI_DELAY_UNIT_USECS, 44ec04cb04SLars-Peter Clausen }, { 45ec04cb04SLars-Peter Clausen .tx_buf = adis->tx + 2, 46ec04cb04SLars-Peter Clausen .bits_per_word = 8, 47ec04cb04SLars-Peter Clausen .len = 2, 4857a1228aSLars-Peter Clausen .cs_change = 1, 4957a1228aSLars-Peter Clausen .delay_usecs = adis->data->write_delay, 50dca39af8SAlexandru Ardelean .cs_change_delay = adis->data->cs_change_delay, 51dca39af8SAlexandru Ardelean .cs_change_delay_unit = SPI_DELAY_UNIT_USECS, 5257a1228aSLars-Peter Clausen }, { 5357a1228aSLars-Peter Clausen .tx_buf = adis->tx + 4, 5457a1228aSLars-Peter Clausen .bits_per_word = 8, 5557a1228aSLars-Peter Clausen .len = 2, 5657a1228aSLars-Peter Clausen .cs_change = 1, 5757a1228aSLars-Peter Clausen .delay_usecs = adis->data->write_delay, 58dca39af8SAlexandru Ardelean .cs_change_delay = adis->data->cs_change_delay, 59dca39af8SAlexandru Ardelean .cs_change_delay_unit = SPI_DELAY_UNIT_USECS, 6057a1228aSLars-Peter Clausen }, { 6157a1228aSLars-Peter Clausen .tx_buf = adis->tx + 6, 6257a1228aSLars-Peter Clausen .bits_per_word = 8, 6357a1228aSLars-Peter Clausen .len = 2, 64ec04cb04SLars-Peter Clausen .delay_usecs = adis->data->write_delay, 65484a0bf0SLars-Peter Clausen }, { 66484a0bf0SLars-Peter Clausen .tx_buf = adis->tx + 8, 67484a0bf0SLars-Peter Clausen .bits_per_word = 8, 68484a0bf0SLars-Peter Clausen .len = 2, 69484a0bf0SLars-Peter Clausen .delay_usecs = adis->data->write_delay, 70ec04cb04SLars-Peter Clausen }, 71ec04cb04SLars-Peter Clausen }; 72ec04cb04SLars-Peter Clausen 73ec04cb04SLars-Peter Clausen mutex_lock(&adis->txrx_lock); 74ec04cb04SLars-Peter Clausen 75ec04cb04SLars-Peter Clausen spi_message_init(&msg); 76484a0bf0SLars-Peter Clausen 77484a0bf0SLars-Peter Clausen if (adis->current_page != page) { 78484a0bf0SLars-Peter Clausen adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID); 79484a0bf0SLars-Peter Clausen adis->tx[1] = page; 80484a0bf0SLars-Peter Clausen spi_message_add_tail(&xfers[0], &msg); 81484a0bf0SLars-Peter Clausen } 82484a0bf0SLars-Peter Clausen 8357a1228aSLars-Peter Clausen switch (size) { 8457a1228aSLars-Peter Clausen case 4: 85484a0bf0SLars-Peter Clausen adis->tx[8] = ADIS_WRITE_REG(reg + 3); 86484a0bf0SLars-Peter Clausen adis->tx[9] = (value >> 24) & 0xff; 87484a0bf0SLars-Peter Clausen adis->tx[6] = ADIS_WRITE_REG(reg + 2); 88484a0bf0SLars-Peter Clausen adis->tx[7] = (value >> 16) & 0xff; 8982d65f9dSGustavo A. R. Silva /* fall through */ 9057a1228aSLars-Peter Clausen case 2: 91484a0bf0SLars-Peter Clausen adis->tx[4] = ADIS_WRITE_REG(reg + 1); 92484a0bf0SLars-Peter Clausen adis->tx[5] = (value >> 8) & 0xff; 9382d65f9dSGustavo A. R. Silva /* fall through */ 9457a1228aSLars-Peter Clausen case 1: 95484a0bf0SLars-Peter Clausen adis->tx[2] = ADIS_WRITE_REG(reg); 96484a0bf0SLars-Peter Clausen adis->tx[3] = value & 0xff; 9757a1228aSLars-Peter Clausen break; 9857a1228aSLars-Peter Clausen default: 9957a1228aSLars-Peter Clausen ret = -EINVAL; 10057a1228aSLars-Peter Clausen goto out_unlock; 10157a1228aSLars-Peter Clausen } 10257a1228aSLars-Peter Clausen 103484a0bf0SLars-Peter Clausen xfers[size].cs_change = 0; 10457a1228aSLars-Peter Clausen 105484a0bf0SLars-Peter Clausen for (i = 1; i <= size; i++) 10657a1228aSLars-Peter Clausen spi_message_add_tail(&xfers[i], &msg); 10757a1228aSLars-Peter Clausen 108ec04cb04SLars-Peter Clausen ret = spi_sync(adis->spi, &msg); 10957a1228aSLars-Peter Clausen if (ret) { 11057a1228aSLars-Peter Clausen dev_err(&adis->spi->dev, "Failed to write register 0x%02X: %d\n", 11157a1228aSLars-Peter Clausen reg, ret); 112484a0bf0SLars-Peter Clausen } else { 113484a0bf0SLars-Peter Clausen adis->current_page = page; 11457a1228aSLars-Peter Clausen } 11557a1228aSLars-Peter Clausen 11657a1228aSLars-Peter Clausen out_unlock: 117ec04cb04SLars-Peter Clausen mutex_unlock(&adis->txrx_lock); 118ec04cb04SLars-Peter Clausen 119ec04cb04SLars-Peter Clausen return ret; 120ec04cb04SLars-Peter Clausen } 12157a1228aSLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_write_reg); 122ec04cb04SLars-Peter Clausen 123ec04cb04SLars-Peter Clausen /** 12457a1228aSLars-Peter Clausen * adis_read_reg() - read 2 bytes from a 16-bit register 125ec04cb04SLars-Peter Clausen * @adis: The adis device 126ec04cb04SLars-Peter Clausen * @reg: The address of the lower of the two registers 127ec04cb04SLars-Peter Clausen * @val: The value read back from the device 128ec04cb04SLars-Peter Clausen */ 12957a1228aSLars-Peter Clausen int adis_read_reg(struct adis *adis, unsigned int reg, 13057a1228aSLars-Peter Clausen unsigned int *val, unsigned int size) 131ec04cb04SLars-Peter Clausen { 132484a0bf0SLars-Peter Clausen unsigned int page = reg / ADIS_PAGE_SIZE; 133ec04cb04SLars-Peter Clausen struct spi_message msg; 134ec04cb04SLars-Peter Clausen int ret; 135ec04cb04SLars-Peter Clausen struct spi_transfer xfers[] = { 136ec04cb04SLars-Peter Clausen { 137ec04cb04SLars-Peter Clausen .tx_buf = adis->tx, 138ec04cb04SLars-Peter Clausen .bits_per_word = 8, 139ec04cb04SLars-Peter Clausen .len = 2, 140ec04cb04SLars-Peter Clausen .cs_change = 1, 141484a0bf0SLars-Peter Clausen .delay_usecs = adis->data->write_delay, 142dca39af8SAlexandru Ardelean .cs_change_delay = adis->data->cs_change_delay, 143dca39af8SAlexandru Ardelean .cs_change_delay_unit = SPI_DELAY_UNIT_USECS, 144ec04cb04SLars-Peter Clausen }, { 14557a1228aSLars-Peter Clausen .tx_buf = adis->tx + 2, 146484a0bf0SLars-Peter Clausen .bits_per_word = 8, 147484a0bf0SLars-Peter Clausen .len = 2, 148484a0bf0SLars-Peter Clausen .cs_change = 1, 149484a0bf0SLars-Peter Clausen .delay_usecs = adis->data->read_delay, 150dca39af8SAlexandru Ardelean .cs_change_delay = adis->data->cs_change_delay, 151dca39af8SAlexandru Ardelean .cs_change_delay_unit = SPI_DELAY_UNIT_USECS, 152484a0bf0SLars-Peter Clausen }, { 153484a0bf0SLars-Peter Clausen .tx_buf = adis->tx + 4, 154ec04cb04SLars-Peter Clausen .rx_buf = adis->rx, 155ec04cb04SLars-Peter Clausen .bits_per_word = 8, 156ec04cb04SLars-Peter Clausen .len = 2, 15757a1228aSLars-Peter Clausen .cs_change = 1, 15857a1228aSLars-Peter Clausen .delay_usecs = adis->data->read_delay, 159dca39af8SAlexandru Ardelean .cs_change_delay = adis->data->cs_change_delay, 160dca39af8SAlexandru Ardelean .cs_change_delay_unit = SPI_DELAY_UNIT_USECS, 16157a1228aSLars-Peter Clausen }, { 16257a1228aSLars-Peter Clausen .rx_buf = adis->rx + 2, 16357a1228aSLars-Peter Clausen .bits_per_word = 8, 16457a1228aSLars-Peter Clausen .len = 2, 165ec04cb04SLars-Peter Clausen .delay_usecs = adis->data->read_delay, 166ec04cb04SLars-Peter Clausen }, 167ec04cb04SLars-Peter Clausen }; 168ec04cb04SLars-Peter Clausen 169ec04cb04SLars-Peter Clausen mutex_lock(&adis->txrx_lock); 170ec04cb04SLars-Peter Clausen spi_message_init(&msg); 17157a1228aSLars-Peter Clausen 172484a0bf0SLars-Peter Clausen if (adis->current_page != page) { 173484a0bf0SLars-Peter Clausen adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID); 174484a0bf0SLars-Peter Clausen adis->tx[1] = page; 175484a0bf0SLars-Peter Clausen spi_message_add_tail(&xfers[0], &msg); 176484a0bf0SLars-Peter Clausen } 177484a0bf0SLars-Peter Clausen 17857a1228aSLars-Peter Clausen switch (size) { 17957a1228aSLars-Peter Clausen case 4: 180484a0bf0SLars-Peter Clausen adis->tx[2] = ADIS_READ_REG(reg + 2); 18157a1228aSLars-Peter Clausen adis->tx[3] = 0; 182ec04cb04SLars-Peter Clausen spi_message_add_tail(&xfers[1], &msg); 18382d65f9dSGustavo A. R. Silva /* fall through */ 184484a0bf0SLars-Peter Clausen case 2: 185484a0bf0SLars-Peter Clausen adis->tx[4] = ADIS_READ_REG(reg); 186484a0bf0SLars-Peter Clausen adis->tx[5] = 0; 18757a1228aSLars-Peter Clausen spi_message_add_tail(&xfers[2], &msg); 188484a0bf0SLars-Peter Clausen spi_message_add_tail(&xfers[3], &msg); 18957a1228aSLars-Peter Clausen break; 19057a1228aSLars-Peter Clausen default: 19157a1228aSLars-Peter Clausen ret = -EINVAL; 19257a1228aSLars-Peter Clausen goto out_unlock; 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); 19957a1228aSLars-Peter Clausen goto out_unlock; 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 21357a1228aSLars-Peter Clausen out_unlock: 214ec04cb04SLars-Peter Clausen mutex_unlock(&adis->txrx_lock); 21557a1228aSLars-Peter Clausen 216ec04cb04SLars-Peter Clausen return ret; 217ec04cb04SLars-Peter Clausen } 21857a1228aSLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_read_reg); 219ec04cb04SLars-Peter Clausen 22078026a6fSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 22178026a6fSLars-Peter Clausen 22278026a6fSLars-Peter Clausen int adis_debugfs_reg_access(struct iio_dev *indio_dev, 22378026a6fSLars-Peter Clausen unsigned int reg, unsigned int writeval, unsigned int *readval) 22478026a6fSLars-Peter Clausen { 22578026a6fSLars-Peter Clausen struct adis *adis = iio_device_get_drvdata(indio_dev); 22678026a6fSLars-Peter Clausen 22778026a6fSLars-Peter Clausen if (readval) { 22878026a6fSLars-Peter Clausen uint16_t val16; 22978026a6fSLars-Peter Clausen int ret; 23078026a6fSLars-Peter Clausen 23178026a6fSLars-Peter Clausen ret = adis_read_reg_16(adis, reg, &val16); 23238262c01SAlexandru Ardelean if (ret == 0) 23378026a6fSLars-Peter Clausen *readval = val16; 23478026a6fSLars-Peter Clausen 23578026a6fSLars-Peter Clausen return ret; 23678026a6fSLars-Peter Clausen } else { 23778026a6fSLars-Peter Clausen return adis_write_reg_16(adis, reg, writeval); 23878026a6fSLars-Peter Clausen } 23978026a6fSLars-Peter Clausen } 24078026a6fSLars-Peter Clausen EXPORT_SYMBOL(adis_debugfs_reg_access); 24178026a6fSLars-Peter Clausen 24278026a6fSLars-Peter Clausen #endif 24378026a6fSLars-Peter Clausen 244ec04cb04SLars-Peter Clausen /** 245ec04cb04SLars-Peter Clausen * adis_enable_irq() - Enable or disable data ready IRQ 246ec04cb04SLars-Peter Clausen * @adis: The adis device 247ec04cb04SLars-Peter Clausen * @enable: Whether to enable the IRQ 248ec04cb04SLars-Peter Clausen * 249ec04cb04SLars-Peter Clausen * Returns 0 on success, negative error code otherwise 250ec04cb04SLars-Peter Clausen */ 251ec04cb04SLars-Peter Clausen int adis_enable_irq(struct adis *adis, bool enable) 252ec04cb04SLars-Peter Clausen { 253ec04cb04SLars-Peter Clausen int ret = 0; 254ec04cb04SLars-Peter Clausen uint16_t msc; 255ec04cb04SLars-Peter Clausen 2562f3abe6cSLars-Peter Clausen if (adis->data->enable_irq) 2572f3abe6cSLars-Peter Clausen return adis->data->enable_irq(adis, enable); 2582f3abe6cSLars-Peter Clausen 259ec04cb04SLars-Peter Clausen ret = adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc); 260ec04cb04SLars-Peter Clausen if (ret) 261ec04cb04SLars-Peter Clausen goto error_ret; 262ec04cb04SLars-Peter Clausen 263ec04cb04SLars-Peter Clausen msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH; 264ec04cb04SLars-Peter Clausen msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2; 265ec04cb04SLars-Peter Clausen if (enable) 266ec04cb04SLars-Peter Clausen msc |= ADIS_MSC_CTRL_DATA_RDY_EN; 267ec04cb04SLars-Peter Clausen else 268ec04cb04SLars-Peter Clausen msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN; 269ec04cb04SLars-Peter Clausen 270ec04cb04SLars-Peter Clausen ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc); 271ec04cb04SLars-Peter Clausen 272ec04cb04SLars-Peter Clausen error_ret: 273ec04cb04SLars-Peter Clausen return ret; 274ec04cb04SLars-Peter Clausen } 275ec04cb04SLars-Peter Clausen EXPORT_SYMBOL(adis_enable_irq); 276ec04cb04SLars-Peter Clausen 277ec04cb04SLars-Peter Clausen /** 278ec04cb04SLars-Peter Clausen * adis_check_status() - Check the device for error conditions 279ec04cb04SLars-Peter Clausen * @adis: The adis device 280ec04cb04SLars-Peter Clausen * 281ec04cb04SLars-Peter Clausen * Returns 0 on success, a negative error code otherwise 282ec04cb04SLars-Peter Clausen */ 283ec04cb04SLars-Peter Clausen int adis_check_status(struct adis *adis) 284ec04cb04SLars-Peter Clausen { 285ec04cb04SLars-Peter Clausen uint16_t status; 286ec04cb04SLars-Peter Clausen int ret; 287ec04cb04SLars-Peter Clausen int i; 288ec04cb04SLars-Peter Clausen 289ec04cb04SLars-Peter Clausen ret = adis_read_reg_16(adis, adis->data->diag_stat_reg, &status); 2906a39ab3bSAlexandru Ardelean if (ret) 291ec04cb04SLars-Peter Clausen return ret; 292ec04cb04SLars-Peter Clausen 293ec04cb04SLars-Peter Clausen status &= adis->data->status_error_mask; 294ec04cb04SLars-Peter Clausen 295ec04cb04SLars-Peter Clausen if (status == 0) 296ec04cb04SLars-Peter Clausen return 0; 297ec04cb04SLars-Peter Clausen 298ec04cb04SLars-Peter Clausen for (i = 0; i < 16; ++i) { 299ec04cb04SLars-Peter Clausen if (status & BIT(i)) { 300ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "%s.\n", 301ec04cb04SLars-Peter Clausen adis->data->status_error_msgs[i]); 302ec04cb04SLars-Peter Clausen } 303ec04cb04SLars-Peter Clausen } 304ec04cb04SLars-Peter Clausen 305ec04cb04SLars-Peter Clausen return -EIO; 306ec04cb04SLars-Peter Clausen } 307ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_check_status); 308ec04cb04SLars-Peter Clausen 309ec04cb04SLars-Peter Clausen /** 310ec04cb04SLars-Peter Clausen * adis_reset() - Reset the device 311ec04cb04SLars-Peter Clausen * @adis: The adis device 312ec04cb04SLars-Peter Clausen * 313ec04cb04SLars-Peter Clausen * Returns 0 on success, a negative error code otherwise 314ec04cb04SLars-Peter Clausen */ 315ec04cb04SLars-Peter Clausen int adis_reset(struct adis *adis) 316ec04cb04SLars-Peter Clausen { 317ec04cb04SLars-Peter Clausen int ret; 318ec04cb04SLars-Peter Clausen 319ec04cb04SLars-Peter Clausen ret = adis_write_reg_8(adis, adis->data->glob_cmd_reg, 320ec04cb04SLars-Peter Clausen ADIS_GLOB_CMD_SW_RESET); 321ec04cb04SLars-Peter Clausen if (ret) 322ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret); 323ec04cb04SLars-Peter Clausen 324ec04cb04SLars-Peter Clausen return ret; 325ec04cb04SLars-Peter Clausen } 326ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_reset); 327ec04cb04SLars-Peter Clausen 328ec04cb04SLars-Peter Clausen static int adis_self_test(struct adis *adis) 329ec04cb04SLars-Peter Clausen { 330ec04cb04SLars-Peter Clausen int ret; 331ec04cb04SLars-Peter Clausen 332ec04cb04SLars-Peter Clausen ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 333ec04cb04SLars-Peter Clausen adis->data->self_test_mask); 334ec04cb04SLars-Peter Clausen if (ret) { 335ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n", 336ec04cb04SLars-Peter Clausen ret); 337ec04cb04SLars-Peter Clausen return ret; 338ec04cb04SLars-Peter Clausen } 339ec04cb04SLars-Peter Clausen 340ec04cb04SLars-Peter Clausen msleep(adis->data->startup_delay); 341ec04cb04SLars-Peter Clausen 342af8a4127SLars-Peter Clausen ret = adis_check_status(adis); 343af8a4127SLars-Peter Clausen 344af8a4127SLars-Peter Clausen if (adis->data->self_test_no_autoclear) 345af8a4127SLars-Peter Clausen adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 0x00); 346af8a4127SLars-Peter Clausen 347af8a4127SLars-Peter Clausen return ret; 348ec04cb04SLars-Peter Clausen } 349ec04cb04SLars-Peter Clausen 350ec04cb04SLars-Peter Clausen /** 351ec04cb04SLars-Peter Clausen * adis_inital_startup() - Performs device self-test 352ec04cb04SLars-Peter Clausen * @adis: The adis device 353ec04cb04SLars-Peter Clausen * 354ec04cb04SLars-Peter Clausen * Returns 0 if the device is operational, a negative error code otherwise. 355ec04cb04SLars-Peter Clausen * 356ec04cb04SLars-Peter Clausen * This function should be called early on in the device initialization sequence 357ec04cb04SLars-Peter Clausen * to ensure that the device is in a sane and known state and that it is usable. 358ec04cb04SLars-Peter Clausen */ 359ec04cb04SLars-Peter Clausen int adis_initial_startup(struct adis *adis) 360ec04cb04SLars-Peter Clausen { 361ec04cb04SLars-Peter Clausen int ret; 362ec04cb04SLars-Peter Clausen 363ec04cb04SLars-Peter Clausen ret = adis_self_test(adis); 364ec04cb04SLars-Peter Clausen if (ret) { 365ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Self-test failed, trying reset.\n"); 366ec04cb04SLars-Peter Clausen adis_reset(adis); 367ec04cb04SLars-Peter Clausen msleep(adis->data->startup_delay); 368ec04cb04SLars-Peter Clausen ret = adis_self_test(adis); 369ec04cb04SLars-Peter Clausen if (ret) { 370ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Second self-test failed, giving up.\n"); 371ec04cb04SLars-Peter Clausen return ret; 372ec04cb04SLars-Peter Clausen } 373ec04cb04SLars-Peter Clausen } 374ec04cb04SLars-Peter Clausen 375ec04cb04SLars-Peter Clausen return 0; 376ec04cb04SLars-Peter Clausen } 377ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_initial_startup); 378ec04cb04SLars-Peter Clausen 379ec04cb04SLars-Peter Clausen /** 380ec04cb04SLars-Peter Clausen * adis_single_conversion() - Performs a single sample conversion 381ec04cb04SLars-Peter Clausen * @indio_dev: The IIO device 382ec04cb04SLars-Peter Clausen * @chan: The IIO channel 383ec04cb04SLars-Peter Clausen * @error_mask: Mask for the error bit 384ec04cb04SLars-Peter Clausen * @val: Result of the conversion 385ec04cb04SLars-Peter Clausen * 386ec04cb04SLars-Peter Clausen * Returns IIO_VAL_INT on success, a negative error code otherwise. 387ec04cb04SLars-Peter Clausen * 388ec04cb04SLars-Peter Clausen * The function performs a single conversion on a given channel and post 389ec04cb04SLars-Peter Clausen * processes the value accordingly to the channel spec. If a error_mask is given 390ec04cb04SLars-Peter Clausen * the function will check if the mask is set in the returned raw value. If it 391ec04cb04SLars-Peter Clausen * is set the function will perform a self-check. If the device does not report 392ec04cb04SLars-Peter Clausen * a error bit in the channels raw value set error_mask to 0. 393ec04cb04SLars-Peter Clausen */ 394ec04cb04SLars-Peter Clausen int adis_single_conversion(struct iio_dev *indio_dev, 395ec04cb04SLars-Peter Clausen const struct iio_chan_spec *chan, unsigned int error_mask, int *val) 396ec04cb04SLars-Peter Clausen { 397ec04cb04SLars-Peter Clausen struct adis *adis = iio_device_get_drvdata(indio_dev); 39857a1228aSLars-Peter Clausen unsigned int uval; 399ec04cb04SLars-Peter Clausen int ret; 400ec04cb04SLars-Peter Clausen 401ec04cb04SLars-Peter Clausen mutex_lock(&indio_dev->mlock); 402ec04cb04SLars-Peter Clausen 40357a1228aSLars-Peter Clausen ret = adis_read_reg(adis, chan->address, &uval, 40457a1228aSLars-Peter Clausen chan->scan_type.storagebits / 8); 405ec04cb04SLars-Peter Clausen if (ret) 406ec04cb04SLars-Peter Clausen goto err_unlock; 407ec04cb04SLars-Peter Clausen 40857a1228aSLars-Peter Clausen if (uval & error_mask) { 409ec04cb04SLars-Peter Clausen ret = adis_check_status(adis); 410ec04cb04SLars-Peter Clausen if (ret) 411ec04cb04SLars-Peter Clausen goto err_unlock; 412ec04cb04SLars-Peter Clausen } 413ec04cb04SLars-Peter Clausen 414ec04cb04SLars-Peter Clausen if (chan->scan_type.sign == 's') 41557a1228aSLars-Peter Clausen *val = sign_extend32(uval, chan->scan_type.realbits - 1); 416ec04cb04SLars-Peter Clausen else 41757a1228aSLars-Peter Clausen *val = uval & ((1 << chan->scan_type.realbits) - 1); 418ec04cb04SLars-Peter Clausen 419ec04cb04SLars-Peter Clausen ret = IIO_VAL_INT; 420ec04cb04SLars-Peter Clausen err_unlock: 421ec04cb04SLars-Peter Clausen mutex_unlock(&indio_dev->mlock); 422ec04cb04SLars-Peter Clausen return ret; 423ec04cb04SLars-Peter Clausen } 424ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_single_conversion); 425ec04cb04SLars-Peter Clausen 426ec04cb04SLars-Peter Clausen /** 427ec04cb04SLars-Peter Clausen * adis_init() - Initialize adis device structure 428ec04cb04SLars-Peter Clausen * @adis: The adis device 429ec04cb04SLars-Peter Clausen * @indio_dev: The iio device 430ec04cb04SLars-Peter Clausen * @spi: The spi device 431ec04cb04SLars-Peter Clausen * @data: Chip specific data 432ec04cb04SLars-Peter Clausen * 433ec04cb04SLars-Peter Clausen * Returns 0 on success, a negative error code otherwise. 434ec04cb04SLars-Peter Clausen * 435ec04cb04SLars-Peter Clausen * This function must be called, before any other adis helper function may be 436ec04cb04SLars-Peter Clausen * called. 437ec04cb04SLars-Peter Clausen */ 438ec04cb04SLars-Peter Clausen int adis_init(struct adis *adis, struct iio_dev *indio_dev, 439ec04cb04SLars-Peter Clausen struct spi_device *spi, const struct adis_data *data) 440ec04cb04SLars-Peter Clausen { 441ec04cb04SLars-Peter Clausen mutex_init(&adis->txrx_lock); 442ec04cb04SLars-Peter Clausen adis->spi = spi; 443ec04cb04SLars-Peter Clausen adis->data = data; 444ec04cb04SLars-Peter Clausen iio_device_set_drvdata(indio_dev, adis); 445ec04cb04SLars-Peter Clausen 446484a0bf0SLars-Peter Clausen if (data->has_paging) { 447484a0bf0SLars-Peter Clausen /* Need to set the page before first read/write */ 448484a0bf0SLars-Peter Clausen adis->current_page = -1; 449484a0bf0SLars-Peter Clausen } else { 450484a0bf0SLars-Peter Clausen /* Page will always be 0 */ 451484a0bf0SLars-Peter Clausen adis->current_page = 0; 452484a0bf0SLars-Peter Clausen } 453484a0bf0SLars-Peter Clausen 454ec04cb04SLars-Peter Clausen return adis_enable_irq(adis, false); 455ec04cb04SLars-Peter Clausen } 456ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_init); 457ec04cb04SLars-Peter Clausen 458ec04cb04SLars-Peter Clausen MODULE_LICENSE("GPL"); 459ec04cb04SLars-Peter Clausen MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 460ec04cb04SLars-Peter Clausen MODULE_DESCRIPTION("Common library code for ADIS16XXX devices"); 461