Thomas Gleixner | 80503b2 | 2019-05-24 12:04:09 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Common library for ADIS16XXX devices |
| 4 | * |
| 5 | * Copyright 2012 Analog Devices Inc. |
| 6 | * Author: Lars-Peter Clausen <lars@metafoo.de> |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/delay.h> |
| 10 | #include <linux/mutex.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/spi/spi.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/sysfs.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <asm/unaligned.h> |
| 18 | |
| 19 | #include <linux/iio/iio.h> |
| 20 | #include <linux/iio/sysfs.h> |
| 21 | #include <linux/iio/buffer.h> |
Lars-Peter Clausen | ec04cb0 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 22 | #include <linux/iio/imu/adis.h> |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 23 | |
| 24 | #define ADIS_MSC_CTRL_DATA_RDY_EN BIT(2) |
| 25 | #define ADIS_MSC_CTRL_DATA_RDY_POL_HIGH BIT(1) |
| 26 | #define ADIS_MSC_CTRL_DATA_RDY_DIO2 BIT(0) |
| 27 | #define ADIS_GLOB_CMD_SW_RESET BIT(7) |
| 28 | |
Alexandru Ardelean | 770d465 | 2019-11-22 15:24:12 +0200 | [diff] [blame] | 29 | /** |
| 30 | * __adis_write_reg() - write N bytes to register (unlocked version) |
| 31 | * @adis: The adis device |
| 32 | * @reg: The address of the lower of the two registers |
| 33 | * @value: The value to write to device (up to 4 bytes) |
| 34 | * @size: The size of the @value (in bytes) |
| 35 | */ |
| 36 | int __adis_write_reg(struct adis *adis, unsigned int reg, |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 37 | unsigned int value, unsigned int size) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 38 | { |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 39 | unsigned int page = reg / ADIS_PAGE_SIZE; |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 40 | int ret, i; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 41 | struct spi_message msg; |
| 42 | struct spi_transfer xfers[] = { |
| 43 | { |
| 44 | .tx_buf = adis->tx, |
| 45 | .bits_per_word = 8, |
| 46 | .len = 2, |
| 47 | .cs_change = 1, |
| 48 | .delay_usecs = adis->data->write_delay, |
Alexandru Ardelean | 329f0da | 2019-09-26 13:51:31 +0300 | [diff] [blame] | 49 | .cs_change_delay.value = adis->data->cs_change_delay, |
| 50 | .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 51 | }, { |
| 52 | .tx_buf = adis->tx + 2, |
| 53 | .bits_per_word = 8, |
| 54 | .len = 2, |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 55 | .cs_change = 1, |
| 56 | .delay_usecs = adis->data->write_delay, |
Alexandru Ardelean | 329f0da | 2019-09-26 13:51:31 +0300 | [diff] [blame] | 57 | .cs_change_delay.value = adis->data->cs_change_delay, |
| 58 | .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 59 | }, { |
| 60 | .tx_buf = adis->tx + 4, |
| 61 | .bits_per_word = 8, |
| 62 | .len = 2, |
| 63 | .cs_change = 1, |
| 64 | .delay_usecs = adis->data->write_delay, |
Alexandru Ardelean | 329f0da | 2019-09-26 13:51:31 +0300 | [diff] [blame] | 65 | .cs_change_delay.value = adis->data->cs_change_delay, |
| 66 | .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 67 | }, { |
| 68 | .tx_buf = adis->tx + 6, |
| 69 | .bits_per_word = 8, |
| 70 | .len = 2, |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 71 | .delay_usecs = adis->data->write_delay, |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 72 | }, { |
| 73 | .tx_buf = adis->tx + 8, |
| 74 | .bits_per_word = 8, |
| 75 | .len = 2, |
| 76 | .delay_usecs = adis->data->write_delay, |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 77 | }, |
| 78 | }; |
| 79 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 80 | spi_message_init(&msg); |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 81 | |
| 82 | if (adis->current_page != page) { |
| 83 | adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID); |
| 84 | adis->tx[1] = page; |
| 85 | spi_message_add_tail(&xfers[0], &msg); |
| 86 | } |
| 87 | |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 88 | switch (size) { |
| 89 | case 4: |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 90 | adis->tx[8] = ADIS_WRITE_REG(reg + 3); |
| 91 | adis->tx[9] = (value >> 24) & 0xff; |
| 92 | adis->tx[6] = ADIS_WRITE_REG(reg + 2); |
| 93 | adis->tx[7] = (value >> 16) & 0xff; |
Gustavo A. R. Silva | 82d65f9 | 2018-07-03 14:35:50 -0500 | [diff] [blame] | 94 | /* fall through */ |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 95 | case 2: |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 96 | adis->tx[4] = ADIS_WRITE_REG(reg + 1); |
| 97 | adis->tx[5] = (value >> 8) & 0xff; |
Gustavo A. R. Silva | 82d65f9 | 2018-07-03 14:35:50 -0500 | [diff] [blame] | 98 | /* fall through */ |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 99 | case 1: |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 100 | adis->tx[2] = ADIS_WRITE_REG(reg); |
| 101 | adis->tx[3] = value & 0xff; |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 102 | break; |
| 103 | default: |
Alexandru Ardelean | 770d465 | 2019-11-22 15:24:12 +0200 | [diff] [blame] | 104 | return -EINVAL; |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 107 | xfers[size].cs_change = 0; |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 108 | |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 109 | for (i = 1; i <= size; i++) |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 110 | spi_message_add_tail(&xfers[i], &msg); |
| 111 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 112 | ret = spi_sync(adis->spi, &msg); |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 113 | if (ret) { |
| 114 | dev_err(&adis->spi->dev, "Failed to write register 0x%02X: %d\n", |
| 115 | reg, ret); |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 116 | } else { |
| 117 | adis->current_page = page; |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 120 | return ret; |
| 121 | } |
Alexandru Ardelean | 770d465 | 2019-11-22 15:24:12 +0200 | [diff] [blame] | 122 | EXPORT_SYMBOL_GPL(__adis_write_reg); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 123 | |
| 124 | /** |
Alexandru Ardelean | 770d465 | 2019-11-22 15:24:12 +0200 | [diff] [blame] | 125 | * __adis_read_reg() - read N bytes from register (unlocked version) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 126 | * @adis: The adis device |
| 127 | * @reg: The address of the lower of the two registers |
| 128 | * @val: The value read back from the device |
Alexandru Ardelean | 770d465 | 2019-11-22 15:24:12 +0200 | [diff] [blame] | 129 | * @size: The size of the @val buffer |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 130 | */ |
Alexandru Ardelean | 770d465 | 2019-11-22 15:24:12 +0200 | [diff] [blame] | 131 | int __adis_read_reg(struct adis *adis, unsigned int reg, |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 132 | unsigned int *val, unsigned int size) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 133 | { |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 134 | unsigned int page = reg / ADIS_PAGE_SIZE; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 135 | struct spi_message msg; |
| 136 | int ret; |
| 137 | struct spi_transfer xfers[] = { |
| 138 | { |
| 139 | .tx_buf = adis->tx, |
| 140 | .bits_per_word = 8, |
| 141 | .len = 2, |
| 142 | .cs_change = 1, |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 143 | .delay_usecs = adis->data->write_delay, |
Alexandru Ardelean | 329f0da | 2019-09-26 13:51:31 +0300 | [diff] [blame] | 144 | .cs_change_delay.value = adis->data->cs_change_delay, |
| 145 | .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 146 | }, { |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 147 | .tx_buf = adis->tx + 2, |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 148 | .bits_per_word = 8, |
| 149 | .len = 2, |
| 150 | .cs_change = 1, |
| 151 | .delay_usecs = adis->data->read_delay, |
Alexandru Ardelean | 329f0da | 2019-09-26 13:51:31 +0300 | [diff] [blame] | 152 | .cs_change_delay.value = adis->data->cs_change_delay, |
| 153 | .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 154 | }, { |
| 155 | .tx_buf = adis->tx + 4, |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 156 | .rx_buf = adis->rx, |
| 157 | .bits_per_word = 8, |
| 158 | .len = 2, |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 159 | .cs_change = 1, |
| 160 | .delay_usecs = adis->data->read_delay, |
Alexandru Ardelean | 329f0da | 2019-09-26 13:51:31 +0300 | [diff] [blame] | 161 | .cs_change_delay.value = adis->data->cs_change_delay, |
| 162 | .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 163 | }, { |
| 164 | .rx_buf = adis->rx + 2, |
| 165 | .bits_per_word = 8, |
| 166 | .len = 2, |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 167 | .delay_usecs = adis->data->read_delay, |
| 168 | }, |
| 169 | }; |
| 170 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 171 | spi_message_init(&msg); |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 172 | |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 173 | if (adis->current_page != page) { |
| 174 | adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID); |
| 175 | adis->tx[1] = page; |
| 176 | spi_message_add_tail(&xfers[0], &msg); |
| 177 | } |
| 178 | |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 179 | switch (size) { |
| 180 | case 4: |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 181 | adis->tx[2] = ADIS_READ_REG(reg + 2); |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 182 | adis->tx[3] = 0; |
| 183 | spi_message_add_tail(&xfers[1], &msg); |
Gustavo A. R. Silva | 82d65f9 | 2018-07-03 14:35:50 -0500 | [diff] [blame] | 184 | /* fall through */ |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 185 | case 2: |
| 186 | adis->tx[4] = ADIS_READ_REG(reg); |
| 187 | adis->tx[5] = 0; |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 188 | spi_message_add_tail(&xfers[2], &msg); |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 189 | spi_message_add_tail(&xfers[3], &msg); |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 190 | break; |
| 191 | default: |
Alexandru Ardelean | 770d465 | 2019-11-22 15:24:12 +0200 | [diff] [blame] | 192 | return -EINVAL; |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 195 | ret = spi_sync(adis->spi, &msg); |
| 196 | if (ret) { |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 197 | dev_err(&adis->spi->dev, "Failed to read register 0x%02X: %d\n", |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 198 | reg, ret); |
Alexandru Ardelean | 770d465 | 2019-11-22 15:24:12 +0200 | [diff] [blame] | 199 | return ret; |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 200 | } else { |
| 201 | adis->current_page = page; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 202 | } |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 203 | |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 204 | switch (size) { |
| 205 | case 4: |
| 206 | *val = get_unaligned_be32(adis->rx); |
| 207 | break; |
| 208 | case 2: |
| 209 | *val = get_unaligned_be16(adis->rx + 2); |
| 210 | break; |
| 211 | } |
| 212 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 213 | return ret; |
| 214 | } |
Alexandru Ardelean | 770d465 | 2019-11-22 15:24:12 +0200 | [diff] [blame] | 215 | EXPORT_SYMBOL_GPL(__adis_read_reg); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 216 | |
Lars-Peter Clausen | 78026a6 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 217 | #ifdef CONFIG_DEBUG_FS |
| 218 | |
| 219 | int adis_debugfs_reg_access(struct iio_dev *indio_dev, |
| 220 | unsigned int reg, unsigned int writeval, unsigned int *readval) |
| 221 | { |
| 222 | struct adis *adis = iio_device_get_drvdata(indio_dev); |
| 223 | |
| 224 | if (readval) { |
| 225 | uint16_t val16; |
| 226 | int ret; |
| 227 | |
| 228 | ret = adis_read_reg_16(adis, reg, &val16); |
Alexandru Ardelean | 38262c0 | 2019-11-01 11:35:04 +0200 | [diff] [blame] | 229 | if (ret == 0) |
| 230 | *readval = val16; |
Lars-Peter Clausen | 78026a6 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 231 | |
| 232 | return ret; |
| 233 | } else { |
| 234 | return adis_write_reg_16(adis, reg, writeval); |
| 235 | } |
| 236 | } |
| 237 | EXPORT_SYMBOL(adis_debugfs_reg_access); |
| 238 | |
| 239 | #endif |
| 240 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 241 | /** |
| 242 | * adis_enable_irq() - Enable or disable data ready IRQ |
| 243 | * @adis: The adis device |
| 244 | * @enable: Whether to enable the IRQ |
| 245 | * |
| 246 | * Returns 0 on success, negative error code otherwise |
| 247 | */ |
| 248 | int adis_enable_irq(struct adis *adis, bool enable) |
| 249 | { |
| 250 | int ret = 0; |
| 251 | uint16_t msc; |
| 252 | |
Alexandru Ardelean | 100bfa3 | 2019-11-22 15:24:13 +0200 | [diff] [blame] | 253 | mutex_lock(&adis->state_lock); |
Lars-Peter Clausen | 2f3abe6 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 254 | |
Alexandru Ardelean | 100bfa3 | 2019-11-22 15:24:13 +0200 | [diff] [blame] | 255 | if (adis->data->enable_irq) { |
| 256 | ret = adis->data->enable_irq(adis, enable); |
| 257 | goto out_unlock; |
| 258 | } |
| 259 | |
| 260 | ret = __adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 261 | if (ret) |
Alexandru Ardelean | 100bfa3 | 2019-11-22 15:24:13 +0200 | [diff] [blame] | 262 | goto out_unlock; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 263 | |
| 264 | msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH; |
| 265 | msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2; |
| 266 | if (enable) |
| 267 | msc |= ADIS_MSC_CTRL_DATA_RDY_EN; |
| 268 | else |
| 269 | msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN; |
| 270 | |
Alexandru Ardelean | 100bfa3 | 2019-11-22 15:24:13 +0200 | [diff] [blame] | 271 | ret = __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 272 | |
Alexandru Ardelean | 100bfa3 | 2019-11-22 15:24:13 +0200 | [diff] [blame] | 273 | out_unlock: |
| 274 | mutex_unlock(&adis->state_lock); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 275 | return ret; |
| 276 | } |
| 277 | EXPORT_SYMBOL(adis_enable_irq); |
| 278 | |
| 279 | /** |
Alexandru Ardelean | 6a4d6a7 | 2019-11-22 15:24:14 +0200 | [diff] [blame] | 280 | * __adis_check_status() - Check the device for error conditions (unlocked) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 281 | * @adis: The adis device |
| 282 | * |
| 283 | * Returns 0 on success, a negative error code otherwise |
| 284 | */ |
Alexandru Ardelean | 6a4d6a7 | 2019-11-22 15:24:14 +0200 | [diff] [blame] | 285 | int __adis_check_status(struct adis *adis) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 286 | { |
| 287 | uint16_t status; |
| 288 | int ret; |
| 289 | int i; |
| 290 | |
Alexandru Ardelean | 6a4d6a7 | 2019-11-22 15:24:14 +0200 | [diff] [blame] | 291 | ret = __adis_read_reg_16(adis, adis->data->diag_stat_reg, &status); |
Alexandru Ardelean | 6a39ab3 | 2019-11-01 11:35:00 +0200 | [diff] [blame] | 292 | if (ret) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 293 | return ret; |
| 294 | |
| 295 | status &= adis->data->status_error_mask; |
| 296 | |
| 297 | if (status == 0) |
| 298 | return 0; |
| 299 | |
| 300 | for (i = 0; i < 16; ++i) { |
| 301 | if (status & BIT(i)) { |
| 302 | dev_err(&adis->spi->dev, "%s.\n", |
| 303 | adis->data->status_error_msgs[i]); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return -EIO; |
| 308 | } |
Alexandru Ardelean | 6a4d6a7 | 2019-11-22 15:24:14 +0200 | [diff] [blame] | 309 | EXPORT_SYMBOL_GPL(__adis_check_status); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 310 | |
| 311 | /** |
Alexandru Ardelean | 762ab09 | 2019-11-22 15:24:15 +0200 | [diff] [blame] | 312 | * __adis_reset() - Reset the device (unlocked version) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 313 | * @adis: The adis device |
| 314 | * |
| 315 | * Returns 0 on success, a negative error code otherwise |
| 316 | */ |
Alexandru Ardelean | 762ab09 | 2019-11-22 15:24:15 +0200 | [diff] [blame] | 317 | int __adis_reset(struct adis *adis) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 318 | { |
| 319 | int ret; |
Nuno Sá | 380b107 | 2020-01-07 13:17:04 +0200 | [diff] [blame^] | 320 | const struct adis_timeout *timeouts = adis->data->timeouts; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 321 | |
Alexandru Ardelean | 762ab09 | 2019-11-22 15:24:15 +0200 | [diff] [blame] | 322 | ret = __adis_write_reg_8(adis, adis->data->glob_cmd_reg, |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 323 | ADIS_GLOB_CMD_SW_RESET); |
Nuno Sá | 380b107 | 2020-01-07 13:17:04 +0200 | [diff] [blame^] | 324 | if (ret) { |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 325 | dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret); |
Nuno Sá | 380b107 | 2020-01-07 13:17:04 +0200 | [diff] [blame^] | 326 | return ret; |
| 327 | } |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 328 | |
Nuno Sá | 380b107 | 2020-01-07 13:17:04 +0200 | [diff] [blame^] | 329 | msleep(timeouts->sw_reset_ms); |
| 330 | |
| 331 | return 0; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 332 | } |
Alexandru Ardelean | 762ab09 | 2019-11-22 15:24:15 +0200 | [diff] [blame] | 333 | EXPORT_SYMBOL_GPL(__adis_reset); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 334 | |
| 335 | static int adis_self_test(struct adis *adis) |
| 336 | { |
| 337 | int ret; |
Nuno Sá | 380b107 | 2020-01-07 13:17:04 +0200 | [diff] [blame^] | 338 | const struct adis_timeout *timeouts = adis->data->timeouts; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 339 | |
Alexandru Ardelean | cb5a07f | 2019-11-22 15:24:16 +0200 | [diff] [blame] | 340 | ret = __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 341 | adis->data->self_test_mask); |
| 342 | if (ret) { |
| 343 | dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n", |
| 344 | ret); |
| 345 | return ret; |
| 346 | } |
| 347 | |
Nuno Sá | 380b107 | 2020-01-07 13:17:04 +0200 | [diff] [blame^] | 348 | msleep(timeouts->self_test_ms); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 349 | |
Alexandru Ardelean | cb5a07f | 2019-11-22 15:24:16 +0200 | [diff] [blame] | 350 | ret = __adis_check_status(adis); |
Lars-Peter Clausen | af8a412 | 2016-04-15 16:59:38 +0200 | [diff] [blame] | 351 | |
| 352 | if (adis->data->self_test_no_autoclear) |
Alexandru Ardelean | cb5a07f | 2019-11-22 15:24:16 +0200 | [diff] [blame] | 353 | __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 0x00); |
Lars-Peter Clausen | af8a412 | 2016-04-15 16:59:38 +0200 | [diff] [blame] | 354 | |
| 355 | return ret; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
| 359 | * adis_inital_startup() - Performs device self-test |
| 360 | * @adis: The adis device |
| 361 | * |
| 362 | * Returns 0 if the device is operational, a negative error code otherwise. |
| 363 | * |
| 364 | * This function should be called early on in the device initialization sequence |
| 365 | * to ensure that the device is in a sane and known state and that it is usable. |
| 366 | */ |
| 367 | int adis_initial_startup(struct adis *adis) |
| 368 | { |
| 369 | int ret; |
| 370 | |
Alexandru Ardelean | cb5a07f | 2019-11-22 15:24:16 +0200 | [diff] [blame] | 371 | mutex_lock(&adis->state_lock); |
| 372 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 373 | ret = adis_self_test(adis); |
| 374 | if (ret) { |
| 375 | dev_err(&adis->spi->dev, "Self-test failed, trying reset.\n"); |
Alexandru Ardelean | cb5a07f | 2019-11-22 15:24:16 +0200 | [diff] [blame] | 376 | __adis_reset(adis); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 377 | ret = adis_self_test(adis); |
| 378 | if (ret) { |
| 379 | dev_err(&adis->spi->dev, "Second self-test failed, giving up.\n"); |
Alexandru Ardelean | cb5a07f | 2019-11-22 15:24:16 +0200 | [diff] [blame] | 380 | goto out_unlock; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Alexandru Ardelean | cb5a07f | 2019-11-22 15:24:16 +0200 | [diff] [blame] | 384 | out_unlock: |
| 385 | mutex_unlock(&adis->state_lock); |
| 386 | return ret; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 387 | } |
| 388 | EXPORT_SYMBOL_GPL(adis_initial_startup); |
| 389 | |
| 390 | /** |
| 391 | * adis_single_conversion() - Performs a single sample conversion |
| 392 | * @indio_dev: The IIO device |
| 393 | * @chan: The IIO channel |
| 394 | * @error_mask: Mask for the error bit |
| 395 | * @val: Result of the conversion |
| 396 | * |
| 397 | * Returns IIO_VAL_INT on success, a negative error code otherwise. |
| 398 | * |
| 399 | * The function performs a single conversion on a given channel and post |
| 400 | * processes the value accordingly to the channel spec. If a error_mask is given |
| 401 | * the function will check if the mask is set in the returned raw value. If it |
| 402 | * is set the function will perform a self-check. If the device does not report |
| 403 | * a error bit in the channels raw value set error_mask to 0. |
| 404 | */ |
| 405 | int adis_single_conversion(struct iio_dev *indio_dev, |
| 406 | const struct iio_chan_spec *chan, unsigned int error_mask, int *val) |
| 407 | { |
| 408 | struct adis *adis = iio_device_get_drvdata(indio_dev); |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 409 | unsigned int uval; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 410 | int ret; |
| 411 | |
Alexandru Ardelean | c5485a5 | 2019-11-22 15:24:17 +0200 | [diff] [blame] | 412 | mutex_lock(&adis->state_lock); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 413 | |
Alexandru Ardelean | c5485a5 | 2019-11-22 15:24:17 +0200 | [diff] [blame] | 414 | ret = __adis_read_reg(adis, chan->address, &uval, |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 415 | chan->scan_type.storagebits / 8); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 416 | if (ret) |
| 417 | goto err_unlock; |
| 418 | |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 419 | if (uval & error_mask) { |
Alexandru Ardelean | c5485a5 | 2019-11-22 15:24:17 +0200 | [diff] [blame] | 420 | ret = __adis_check_status(adis); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 421 | if (ret) |
| 422 | goto err_unlock; |
| 423 | } |
| 424 | |
| 425 | if (chan->scan_type.sign == 's') |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 426 | *val = sign_extend32(uval, chan->scan_type.realbits - 1); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 427 | else |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 428 | *val = uval & ((1 << chan->scan_type.realbits) - 1); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 429 | |
| 430 | ret = IIO_VAL_INT; |
| 431 | err_unlock: |
Alexandru Ardelean | c5485a5 | 2019-11-22 15:24:17 +0200 | [diff] [blame] | 432 | mutex_unlock(&adis->state_lock); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 433 | return ret; |
| 434 | } |
| 435 | EXPORT_SYMBOL_GPL(adis_single_conversion); |
| 436 | |
| 437 | /** |
| 438 | * adis_init() - Initialize adis device structure |
| 439 | * @adis: The adis device |
| 440 | * @indio_dev: The iio device |
| 441 | * @spi: The spi device |
| 442 | * @data: Chip specific data |
| 443 | * |
| 444 | * Returns 0 on success, a negative error code otherwise. |
| 445 | * |
| 446 | * This function must be called, before any other adis helper function may be |
| 447 | * called. |
| 448 | */ |
| 449 | int adis_init(struct adis *adis, struct iio_dev *indio_dev, |
| 450 | struct spi_device *spi, const struct adis_data *data) |
| 451 | { |
Nuno Sá | 380b107 | 2020-01-07 13:17:04 +0200 | [diff] [blame^] | 452 | if (!data || !data->timeouts) { |
| 453 | dev_err(&spi->dev, "No config data or timeouts not defined!\n"); |
| 454 | return -EINVAL; |
| 455 | } |
| 456 | |
Alexandru Ardelean | 6a9afcb | 2019-11-22 15:24:11 +0200 | [diff] [blame] | 457 | mutex_init(&adis->state_lock); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 458 | adis->spi = spi; |
| 459 | adis->data = data; |
| 460 | iio_device_set_drvdata(indio_dev, adis); |
| 461 | |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 462 | if (data->has_paging) { |
| 463 | /* Need to set the page before first read/write */ |
| 464 | adis->current_page = -1; |
| 465 | } else { |
| 466 | /* Page will always be 0 */ |
| 467 | adis->current_page = 0; |
| 468 | } |
| 469 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 470 | return adis_enable_irq(adis, false); |
| 471 | } |
| 472 | EXPORT_SYMBOL_GPL(adis_init); |
| 473 | |
| 474 | MODULE_LICENSE("GPL"); |
| 475 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
| 476 | MODULE_DESCRIPTION("Common library code for ADIS16XXX devices"); |