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 2532f3abe6cSLars-Peter Clausen if (adis->data->enable_irq) 2542f3abe6cSLars-Peter Clausen return adis->data->enable_irq(adis, enable); 2552f3abe6cSLars-Peter Clausen 256ec04cb04SLars-Peter Clausen ret = adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc); 257ec04cb04SLars-Peter Clausen if (ret) 258ec04cb04SLars-Peter Clausen goto error_ret; 259ec04cb04SLars-Peter Clausen 260ec04cb04SLars-Peter Clausen msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH; 261ec04cb04SLars-Peter Clausen msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2; 262ec04cb04SLars-Peter Clausen if (enable) 263ec04cb04SLars-Peter Clausen msc |= ADIS_MSC_CTRL_DATA_RDY_EN; 264ec04cb04SLars-Peter Clausen else 265ec04cb04SLars-Peter Clausen msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN; 266ec04cb04SLars-Peter Clausen 267ec04cb04SLars-Peter Clausen ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc); 268ec04cb04SLars-Peter Clausen 269ec04cb04SLars-Peter Clausen error_ret: 270ec04cb04SLars-Peter Clausen return ret; 271ec04cb04SLars-Peter Clausen } 272ec04cb04SLars-Peter Clausen EXPORT_SYMBOL(adis_enable_irq); 273ec04cb04SLars-Peter Clausen 274ec04cb04SLars-Peter Clausen /** 275ec04cb04SLars-Peter Clausen * adis_check_status() - Check the device for error conditions 276ec04cb04SLars-Peter Clausen * @adis: The adis device 277ec04cb04SLars-Peter Clausen * 278ec04cb04SLars-Peter Clausen * Returns 0 on success, a negative error code otherwise 279ec04cb04SLars-Peter Clausen */ 280ec04cb04SLars-Peter Clausen int adis_check_status(struct adis *adis) 281ec04cb04SLars-Peter Clausen { 282ec04cb04SLars-Peter Clausen uint16_t status; 283ec04cb04SLars-Peter Clausen int ret; 284ec04cb04SLars-Peter Clausen int i; 285ec04cb04SLars-Peter Clausen 286ec04cb04SLars-Peter Clausen ret = adis_read_reg_16(adis, adis->data->diag_stat_reg, &status); 2876a39ab3bSAlexandru Ardelean if (ret) 288ec04cb04SLars-Peter Clausen return ret; 289ec04cb04SLars-Peter Clausen 290ec04cb04SLars-Peter Clausen status &= adis->data->status_error_mask; 291ec04cb04SLars-Peter Clausen 292ec04cb04SLars-Peter Clausen if (status == 0) 293ec04cb04SLars-Peter Clausen return 0; 294ec04cb04SLars-Peter Clausen 295ec04cb04SLars-Peter Clausen for (i = 0; i < 16; ++i) { 296ec04cb04SLars-Peter Clausen if (status & BIT(i)) { 297ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "%s.\n", 298ec04cb04SLars-Peter Clausen adis->data->status_error_msgs[i]); 299ec04cb04SLars-Peter Clausen } 300ec04cb04SLars-Peter Clausen } 301ec04cb04SLars-Peter Clausen 302ec04cb04SLars-Peter Clausen return -EIO; 303ec04cb04SLars-Peter Clausen } 304ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_check_status); 305ec04cb04SLars-Peter Clausen 306ec04cb04SLars-Peter Clausen /** 307ec04cb04SLars-Peter Clausen * adis_reset() - Reset the device 308ec04cb04SLars-Peter Clausen * @adis: The adis device 309ec04cb04SLars-Peter Clausen * 310ec04cb04SLars-Peter Clausen * Returns 0 on success, a negative error code otherwise 311ec04cb04SLars-Peter Clausen */ 312ec04cb04SLars-Peter Clausen int adis_reset(struct adis *adis) 313ec04cb04SLars-Peter Clausen { 314ec04cb04SLars-Peter Clausen int ret; 315ec04cb04SLars-Peter Clausen 316ec04cb04SLars-Peter Clausen ret = adis_write_reg_8(adis, adis->data->glob_cmd_reg, 317ec04cb04SLars-Peter Clausen ADIS_GLOB_CMD_SW_RESET); 318ec04cb04SLars-Peter Clausen if (ret) 319ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret); 320ec04cb04SLars-Peter Clausen 321ec04cb04SLars-Peter Clausen return ret; 322ec04cb04SLars-Peter Clausen } 323ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_reset); 324ec04cb04SLars-Peter Clausen 325ec04cb04SLars-Peter Clausen static int adis_self_test(struct adis *adis) 326ec04cb04SLars-Peter Clausen { 327ec04cb04SLars-Peter Clausen int ret; 328ec04cb04SLars-Peter Clausen 329ec04cb04SLars-Peter Clausen ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 330ec04cb04SLars-Peter Clausen adis->data->self_test_mask); 331ec04cb04SLars-Peter Clausen if (ret) { 332ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n", 333ec04cb04SLars-Peter Clausen ret); 334ec04cb04SLars-Peter Clausen return ret; 335ec04cb04SLars-Peter Clausen } 336ec04cb04SLars-Peter Clausen 337ec04cb04SLars-Peter Clausen msleep(adis->data->startup_delay); 338ec04cb04SLars-Peter Clausen 339af8a4127SLars-Peter Clausen ret = adis_check_status(adis); 340af8a4127SLars-Peter Clausen 341af8a4127SLars-Peter Clausen if (adis->data->self_test_no_autoclear) 342af8a4127SLars-Peter Clausen adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 0x00); 343af8a4127SLars-Peter Clausen 344af8a4127SLars-Peter Clausen return ret; 345ec04cb04SLars-Peter Clausen } 346ec04cb04SLars-Peter Clausen 347ec04cb04SLars-Peter Clausen /** 348ec04cb04SLars-Peter Clausen * adis_inital_startup() - Performs device self-test 349ec04cb04SLars-Peter Clausen * @adis: The adis device 350ec04cb04SLars-Peter Clausen * 351ec04cb04SLars-Peter Clausen * Returns 0 if the device is operational, a negative error code otherwise. 352ec04cb04SLars-Peter Clausen * 353ec04cb04SLars-Peter Clausen * This function should be called early on in the device initialization sequence 354ec04cb04SLars-Peter Clausen * to ensure that the device is in a sane and known state and that it is usable. 355ec04cb04SLars-Peter Clausen */ 356ec04cb04SLars-Peter Clausen int adis_initial_startup(struct adis *adis) 357ec04cb04SLars-Peter Clausen { 358ec04cb04SLars-Peter Clausen int ret; 359ec04cb04SLars-Peter Clausen 360ec04cb04SLars-Peter Clausen ret = adis_self_test(adis); 361ec04cb04SLars-Peter Clausen if (ret) { 362ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Self-test failed, trying reset.\n"); 363ec04cb04SLars-Peter Clausen adis_reset(adis); 364ec04cb04SLars-Peter Clausen msleep(adis->data->startup_delay); 365ec04cb04SLars-Peter Clausen ret = adis_self_test(adis); 366ec04cb04SLars-Peter Clausen if (ret) { 367ec04cb04SLars-Peter Clausen dev_err(&adis->spi->dev, "Second self-test failed, giving up.\n"); 368ec04cb04SLars-Peter Clausen return ret; 369ec04cb04SLars-Peter Clausen } 370ec04cb04SLars-Peter Clausen } 371ec04cb04SLars-Peter Clausen 372ec04cb04SLars-Peter Clausen return 0; 373ec04cb04SLars-Peter Clausen } 374ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_initial_startup); 375ec04cb04SLars-Peter Clausen 376ec04cb04SLars-Peter Clausen /** 377ec04cb04SLars-Peter Clausen * adis_single_conversion() - Performs a single sample conversion 378ec04cb04SLars-Peter Clausen * @indio_dev: The IIO device 379ec04cb04SLars-Peter Clausen * @chan: The IIO channel 380ec04cb04SLars-Peter Clausen * @error_mask: Mask for the error bit 381ec04cb04SLars-Peter Clausen * @val: Result of the conversion 382ec04cb04SLars-Peter Clausen * 383ec04cb04SLars-Peter Clausen * Returns IIO_VAL_INT on success, a negative error code otherwise. 384ec04cb04SLars-Peter Clausen * 385ec04cb04SLars-Peter Clausen * The function performs a single conversion on a given channel and post 386ec04cb04SLars-Peter Clausen * processes the value accordingly to the channel spec. If a error_mask is given 387ec04cb04SLars-Peter Clausen * the function will check if the mask is set in the returned raw value. If it 388ec04cb04SLars-Peter Clausen * is set the function will perform a self-check. If the device does not report 389ec04cb04SLars-Peter Clausen * a error bit in the channels raw value set error_mask to 0. 390ec04cb04SLars-Peter Clausen */ 391ec04cb04SLars-Peter Clausen int adis_single_conversion(struct iio_dev *indio_dev, 392ec04cb04SLars-Peter Clausen const struct iio_chan_spec *chan, unsigned int error_mask, int *val) 393ec04cb04SLars-Peter Clausen { 394ec04cb04SLars-Peter Clausen struct adis *adis = iio_device_get_drvdata(indio_dev); 39557a1228aSLars-Peter Clausen unsigned int uval; 396ec04cb04SLars-Peter Clausen int ret; 397ec04cb04SLars-Peter Clausen 398ec04cb04SLars-Peter Clausen mutex_lock(&indio_dev->mlock); 399ec04cb04SLars-Peter Clausen 40057a1228aSLars-Peter Clausen ret = adis_read_reg(adis, chan->address, &uval, 40157a1228aSLars-Peter Clausen chan->scan_type.storagebits / 8); 402ec04cb04SLars-Peter Clausen if (ret) 403ec04cb04SLars-Peter Clausen goto err_unlock; 404ec04cb04SLars-Peter Clausen 40557a1228aSLars-Peter Clausen if (uval & error_mask) { 406ec04cb04SLars-Peter Clausen ret = adis_check_status(adis); 407ec04cb04SLars-Peter Clausen if (ret) 408ec04cb04SLars-Peter Clausen goto err_unlock; 409ec04cb04SLars-Peter Clausen } 410ec04cb04SLars-Peter Clausen 411ec04cb04SLars-Peter Clausen if (chan->scan_type.sign == 's') 41257a1228aSLars-Peter Clausen *val = sign_extend32(uval, chan->scan_type.realbits - 1); 413ec04cb04SLars-Peter Clausen else 41457a1228aSLars-Peter Clausen *val = uval & ((1 << chan->scan_type.realbits) - 1); 415ec04cb04SLars-Peter Clausen 416ec04cb04SLars-Peter Clausen ret = IIO_VAL_INT; 417ec04cb04SLars-Peter Clausen err_unlock: 418ec04cb04SLars-Peter Clausen mutex_unlock(&indio_dev->mlock); 419ec04cb04SLars-Peter Clausen return ret; 420ec04cb04SLars-Peter Clausen } 421ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_single_conversion); 422ec04cb04SLars-Peter Clausen 423ec04cb04SLars-Peter Clausen /** 424ec04cb04SLars-Peter Clausen * adis_init() - Initialize adis device structure 425ec04cb04SLars-Peter Clausen * @adis: The adis device 426ec04cb04SLars-Peter Clausen * @indio_dev: The iio device 427ec04cb04SLars-Peter Clausen * @spi: The spi device 428ec04cb04SLars-Peter Clausen * @data: Chip specific data 429ec04cb04SLars-Peter Clausen * 430ec04cb04SLars-Peter Clausen * Returns 0 on success, a negative error code otherwise. 431ec04cb04SLars-Peter Clausen * 432ec04cb04SLars-Peter Clausen * This function must be called, before any other adis helper function may be 433ec04cb04SLars-Peter Clausen * called. 434ec04cb04SLars-Peter Clausen */ 435ec04cb04SLars-Peter Clausen int adis_init(struct adis *adis, struct iio_dev *indio_dev, 436ec04cb04SLars-Peter Clausen struct spi_device *spi, const struct adis_data *data) 437ec04cb04SLars-Peter Clausen { 4386a9afcb1SAlexandru Ardelean mutex_init(&adis->state_lock); 439ec04cb04SLars-Peter Clausen adis->spi = spi; 440ec04cb04SLars-Peter Clausen adis->data = data; 441ec04cb04SLars-Peter Clausen iio_device_set_drvdata(indio_dev, adis); 442ec04cb04SLars-Peter Clausen 443484a0bf0SLars-Peter Clausen if (data->has_paging) { 444484a0bf0SLars-Peter Clausen /* Need to set the page before first read/write */ 445484a0bf0SLars-Peter Clausen adis->current_page = -1; 446484a0bf0SLars-Peter Clausen } else { 447484a0bf0SLars-Peter Clausen /* Page will always be 0 */ 448484a0bf0SLars-Peter Clausen adis->current_page = 0; 449484a0bf0SLars-Peter Clausen } 450484a0bf0SLars-Peter Clausen 451ec04cb04SLars-Peter Clausen return adis_enable_irq(adis, false); 452ec04cb04SLars-Peter Clausen } 453ec04cb04SLars-Peter Clausen EXPORT_SYMBOL_GPL(adis_init); 454ec04cb04SLars-Peter Clausen 455ec04cb04SLars-Peter Clausen MODULE_LICENSE("GPL"); 456ec04cb04SLars-Peter Clausen MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 457ec04cb04SLars-Peter Clausen MODULE_DESCRIPTION("Common library code for ADIS16XXX devices"); 458