| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Common library for ADIS16XXX devices |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright 2012 Analog Devices Inc. |
|---|
| 5 | 6 | * Author: Lars-Peter Clausen <lars@metafoo.de> |
|---|
| 6 | | - * |
|---|
| 7 | | - * Licensed under the GPL-2 or later. |
|---|
| 8 | 7 | */ |
|---|
| 9 | 8 | |
|---|
| 10 | 9 | #include <linux/delay.h> |
|---|
| 10 | +#include <linux/gpio/consumer.h> |
|---|
| 11 | 11 | #include <linux/mutex.h> |
|---|
| 12 | 12 | #include <linux/device.h> |
|---|
| 13 | 13 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 27 | 27 | #define ADIS_MSC_CTRL_DATA_RDY_DIO2 BIT(0) |
|---|
| 28 | 28 | #define ADIS_GLOB_CMD_SW_RESET BIT(7) |
|---|
| 29 | 29 | |
|---|
| 30 | | -int adis_write_reg(struct adis *adis, unsigned int reg, |
|---|
| 31 | | - unsigned int value, unsigned int size) |
|---|
| 30 | +/** |
|---|
| 31 | + * __adis_write_reg() - write N bytes to register (unlocked version) |
|---|
| 32 | + * @adis: The adis device |
|---|
| 33 | + * @reg: The address of the lower of the two registers |
|---|
| 34 | + * @value: The value to write to device (up to 4 bytes) |
|---|
| 35 | + * @size: The size of the @value (in bytes) |
|---|
| 36 | + */ |
|---|
| 37 | +int __adis_write_reg(struct adis *adis, unsigned int reg, unsigned int value, |
|---|
| 38 | + unsigned int size) |
|---|
| 32 | 39 | { |
|---|
| 33 | 40 | unsigned int page = reg / ADIS_PAGE_SIZE; |
|---|
| 34 | 41 | int ret, i; |
|---|
| .. | .. |
|---|
| 39 | 46 | .bits_per_word = 8, |
|---|
| 40 | 47 | .len = 2, |
|---|
| 41 | 48 | .cs_change = 1, |
|---|
| 42 | | - .delay_usecs = adis->data->write_delay, |
|---|
| 49 | + .delay.value = adis->data->write_delay, |
|---|
| 50 | + .delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 51 | + .cs_change_delay.value = adis->data->cs_change_delay, |
|---|
| 52 | + .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 43 | 53 | }, { |
|---|
| 44 | 54 | .tx_buf = adis->tx + 2, |
|---|
| 45 | 55 | .bits_per_word = 8, |
|---|
| 46 | 56 | .len = 2, |
|---|
| 47 | 57 | .cs_change = 1, |
|---|
| 48 | | - .delay_usecs = adis->data->write_delay, |
|---|
| 58 | + .delay.value = adis->data->write_delay, |
|---|
| 59 | + .delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 60 | + .cs_change_delay.value = adis->data->cs_change_delay, |
|---|
| 61 | + .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 49 | 62 | }, { |
|---|
| 50 | 63 | .tx_buf = adis->tx + 4, |
|---|
| 51 | 64 | .bits_per_word = 8, |
|---|
| 52 | 65 | .len = 2, |
|---|
| 53 | 66 | .cs_change = 1, |
|---|
| 54 | | - .delay_usecs = adis->data->write_delay, |
|---|
| 67 | + .delay.value = adis->data->write_delay, |
|---|
| 68 | + .delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 69 | + .cs_change_delay.value = adis->data->cs_change_delay, |
|---|
| 70 | + .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 55 | 71 | }, { |
|---|
| 56 | 72 | .tx_buf = adis->tx + 6, |
|---|
| 57 | 73 | .bits_per_word = 8, |
|---|
| 58 | 74 | .len = 2, |
|---|
| 59 | | - .delay_usecs = adis->data->write_delay, |
|---|
| 75 | + .delay.value = adis->data->write_delay, |
|---|
| 76 | + .delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 60 | 77 | }, { |
|---|
| 61 | 78 | .tx_buf = adis->tx + 8, |
|---|
| 62 | 79 | .bits_per_word = 8, |
|---|
| 63 | 80 | .len = 2, |
|---|
| 64 | | - .delay_usecs = adis->data->write_delay, |
|---|
| 81 | + .delay.value = adis->data->write_delay, |
|---|
| 82 | + .delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 65 | 83 | }, |
|---|
| 66 | 84 | }; |
|---|
| 67 | | - |
|---|
| 68 | | - mutex_lock(&adis->txrx_lock); |
|---|
| 69 | 85 | |
|---|
| 70 | 86 | spi_message_init(&msg); |
|---|
| 71 | 87 | |
|---|
| .. | .. |
|---|
| 81 | 97 | adis->tx[9] = (value >> 24) & 0xff; |
|---|
| 82 | 98 | adis->tx[6] = ADIS_WRITE_REG(reg + 2); |
|---|
| 83 | 99 | adis->tx[7] = (value >> 16) & 0xff; |
|---|
| 84 | | - /* fall through */ |
|---|
| 100 | + fallthrough; |
|---|
| 85 | 101 | case 2: |
|---|
| 86 | 102 | adis->tx[4] = ADIS_WRITE_REG(reg + 1); |
|---|
| 87 | 103 | adis->tx[5] = (value >> 8) & 0xff; |
|---|
| 88 | | - /* fall through */ |
|---|
| 104 | + fallthrough; |
|---|
| 89 | 105 | case 1: |
|---|
| 90 | 106 | adis->tx[2] = ADIS_WRITE_REG(reg); |
|---|
| 91 | 107 | adis->tx[3] = value & 0xff; |
|---|
| 92 | 108 | break; |
|---|
| 93 | 109 | default: |
|---|
| 94 | | - ret = -EINVAL; |
|---|
| 95 | | - goto out_unlock; |
|---|
| 110 | + return -EINVAL; |
|---|
| 96 | 111 | } |
|---|
| 97 | 112 | |
|---|
| 98 | 113 | xfers[size].cs_change = 0; |
|---|
| .. | .. |
|---|
| 103 | 118 | ret = spi_sync(adis->spi, &msg); |
|---|
| 104 | 119 | if (ret) { |
|---|
| 105 | 120 | dev_err(&adis->spi->dev, "Failed to write register 0x%02X: %d\n", |
|---|
| 106 | | - reg, ret); |
|---|
| 121 | + reg, ret); |
|---|
| 107 | 122 | } else { |
|---|
| 108 | 123 | adis->current_page = page; |
|---|
| 109 | 124 | } |
|---|
| 110 | 125 | |
|---|
| 111 | | -out_unlock: |
|---|
| 112 | | - mutex_unlock(&adis->txrx_lock); |
|---|
| 113 | | - |
|---|
| 114 | 126 | return ret; |
|---|
| 115 | 127 | } |
|---|
| 116 | | -EXPORT_SYMBOL_GPL(adis_write_reg); |
|---|
| 128 | +EXPORT_SYMBOL_NS_GPL(__adis_write_reg, IIO_ADISLIB); |
|---|
| 117 | 129 | |
|---|
| 118 | 130 | /** |
|---|
| 119 | | - * adis_read_reg() - read 2 bytes from a 16-bit register |
|---|
| 131 | + * __adis_read_reg() - read N bytes from register (unlocked version) |
|---|
| 120 | 132 | * @adis: The adis device |
|---|
| 121 | 133 | * @reg: The address of the lower of the two registers |
|---|
| 122 | 134 | * @val: The value read back from the device |
|---|
| 135 | + * @size: The size of the @val buffer |
|---|
| 123 | 136 | */ |
|---|
| 124 | | -int adis_read_reg(struct adis *adis, unsigned int reg, |
|---|
| 125 | | - unsigned int *val, unsigned int size) |
|---|
| 137 | +int __adis_read_reg(struct adis *adis, unsigned int reg, unsigned int *val, |
|---|
| 138 | + unsigned int size) |
|---|
| 126 | 139 | { |
|---|
| 127 | 140 | unsigned int page = reg / ADIS_PAGE_SIZE; |
|---|
| 128 | 141 | struct spi_message msg; |
|---|
| .. | .. |
|---|
| 133 | 146 | .bits_per_word = 8, |
|---|
| 134 | 147 | .len = 2, |
|---|
| 135 | 148 | .cs_change = 1, |
|---|
| 136 | | - .delay_usecs = adis->data->write_delay, |
|---|
| 149 | + .delay.value = adis->data->write_delay, |
|---|
| 150 | + .delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 151 | + .cs_change_delay.value = adis->data->cs_change_delay, |
|---|
| 152 | + .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 137 | 153 | }, { |
|---|
| 138 | 154 | .tx_buf = adis->tx + 2, |
|---|
| 139 | 155 | .bits_per_word = 8, |
|---|
| 140 | 156 | .len = 2, |
|---|
| 141 | 157 | .cs_change = 1, |
|---|
| 142 | | - .delay_usecs = adis->data->read_delay, |
|---|
| 158 | + .delay.value = adis->data->read_delay, |
|---|
| 159 | + .delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 160 | + .cs_change_delay.value = adis->data->cs_change_delay, |
|---|
| 161 | + .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 143 | 162 | }, { |
|---|
| 144 | 163 | .tx_buf = adis->tx + 4, |
|---|
| 145 | 164 | .rx_buf = adis->rx, |
|---|
| 146 | 165 | .bits_per_word = 8, |
|---|
| 147 | 166 | .len = 2, |
|---|
| 148 | 167 | .cs_change = 1, |
|---|
| 149 | | - .delay_usecs = adis->data->read_delay, |
|---|
| 168 | + .delay.value = adis->data->read_delay, |
|---|
| 169 | + .delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 170 | + .cs_change_delay.value = adis->data->cs_change_delay, |
|---|
| 171 | + .cs_change_delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 150 | 172 | }, { |
|---|
| 151 | 173 | .rx_buf = adis->rx + 2, |
|---|
| 152 | 174 | .bits_per_word = 8, |
|---|
| 153 | 175 | .len = 2, |
|---|
| 154 | | - .delay_usecs = adis->data->read_delay, |
|---|
| 176 | + .delay.value = adis->data->read_delay, |
|---|
| 177 | + .delay.unit = SPI_DELAY_UNIT_USECS, |
|---|
| 155 | 178 | }, |
|---|
| 156 | 179 | }; |
|---|
| 157 | 180 | |
|---|
| 158 | | - mutex_lock(&adis->txrx_lock); |
|---|
| 159 | 181 | spi_message_init(&msg); |
|---|
| 160 | 182 | |
|---|
| 161 | 183 | if (adis->current_page != page) { |
|---|
| .. | .. |
|---|
| 169 | 191 | adis->tx[2] = ADIS_READ_REG(reg + 2); |
|---|
| 170 | 192 | adis->tx[3] = 0; |
|---|
| 171 | 193 | spi_message_add_tail(&xfers[1], &msg); |
|---|
| 172 | | - /* fall through */ |
|---|
| 194 | + fallthrough; |
|---|
| 173 | 195 | case 2: |
|---|
| 174 | 196 | adis->tx[4] = ADIS_READ_REG(reg); |
|---|
| 175 | 197 | adis->tx[5] = 0; |
|---|
| .. | .. |
|---|
| 177 | 199 | spi_message_add_tail(&xfers[3], &msg); |
|---|
| 178 | 200 | break; |
|---|
| 179 | 201 | default: |
|---|
| 180 | | - ret = -EINVAL; |
|---|
| 181 | | - goto out_unlock; |
|---|
| 202 | + return -EINVAL; |
|---|
| 182 | 203 | } |
|---|
| 183 | 204 | |
|---|
| 184 | 205 | ret = spi_sync(adis->spi, &msg); |
|---|
| 185 | 206 | if (ret) { |
|---|
| 186 | 207 | dev_err(&adis->spi->dev, "Failed to read register 0x%02X: %d\n", |
|---|
| 187 | | - reg, ret); |
|---|
| 188 | | - goto out_unlock; |
|---|
| 189 | | - } else { |
|---|
| 190 | | - adis->current_page = page; |
|---|
| 208 | + reg, ret); |
|---|
| 209 | + return ret; |
|---|
| 191 | 210 | } |
|---|
| 211 | + |
|---|
| 212 | + adis->current_page = page; |
|---|
| 192 | 213 | |
|---|
| 193 | 214 | switch (size) { |
|---|
| 194 | 215 | case 4: |
|---|
| .. | .. |
|---|
| 199 | 220 | break; |
|---|
| 200 | 221 | } |
|---|
| 201 | 222 | |
|---|
| 202 | | -out_unlock: |
|---|
| 203 | | - mutex_unlock(&adis->txrx_lock); |
|---|
| 204 | | - |
|---|
| 205 | 223 | return ret; |
|---|
| 206 | 224 | } |
|---|
| 207 | | -EXPORT_SYMBOL_GPL(adis_read_reg); |
|---|
| 225 | +EXPORT_SYMBOL_NS_GPL(__adis_read_reg, IIO_ADISLIB); |
|---|
| 226 | +/** |
|---|
| 227 | + * __adis_update_bits_base() - ADIS Update bits function - Unlocked version |
|---|
| 228 | + * @adis: The adis device |
|---|
| 229 | + * @reg: The address of the lower of the two registers |
|---|
| 230 | + * @mask: Bitmask to change |
|---|
| 231 | + * @val: Value to be written |
|---|
| 232 | + * @size: Size of the register to update |
|---|
| 233 | + * |
|---|
| 234 | + * Updates the desired bits of @reg in accordance with @mask and @val. |
|---|
| 235 | + */ |
|---|
| 236 | +int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask, |
|---|
| 237 | + const u32 val, u8 size) |
|---|
| 238 | +{ |
|---|
| 239 | + int ret; |
|---|
| 240 | + u32 __val; |
|---|
| 241 | + |
|---|
| 242 | + ret = __adis_read_reg(adis, reg, &__val, size); |
|---|
| 243 | + if (ret) |
|---|
| 244 | + return ret; |
|---|
| 245 | + |
|---|
| 246 | + __val = (__val & ~mask) | (val & mask); |
|---|
| 247 | + |
|---|
| 248 | + return __adis_write_reg(adis, reg, __val, size); |
|---|
| 249 | +} |
|---|
| 250 | +EXPORT_SYMBOL_NS_GPL(__adis_update_bits_base, IIO_ADISLIB); |
|---|
| 208 | 251 | |
|---|
| 209 | 252 | #ifdef CONFIG_DEBUG_FS |
|---|
| 210 | 253 | |
|---|
| 211 | | -int adis_debugfs_reg_access(struct iio_dev *indio_dev, |
|---|
| 212 | | - unsigned int reg, unsigned int writeval, unsigned int *readval) |
|---|
| 254 | +int adis_debugfs_reg_access(struct iio_dev *indio_dev, unsigned int reg, |
|---|
| 255 | + unsigned int writeval, unsigned int *readval) |
|---|
| 213 | 256 | { |
|---|
| 214 | 257 | struct adis *adis = iio_device_get_drvdata(indio_dev); |
|---|
| 215 | 258 | |
|---|
| 216 | 259 | if (readval) { |
|---|
| 217 | | - uint16_t val16; |
|---|
| 260 | + u16 val16; |
|---|
| 218 | 261 | int ret; |
|---|
| 219 | 262 | |
|---|
| 220 | 263 | ret = adis_read_reg_16(adis, reg, &val16); |
|---|
| 221 | | - *readval = val16; |
|---|
| 264 | + if (ret == 0) |
|---|
| 265 | + *readval = val16; |
|---|
| 222 | 266 | |
|---|
| 223 | 267 | return ret; |
|---|
| 224 | | - } else { |
|---|
| 225 | | - return adis_write_reg_16(adis, reg, writeval); |
|---|
| 226 | 268 | } |
|---|
| 269 | + |
|---|
| 270 | + return adis_write_reg_16(adis, reg, writeval); |
|---|
| 227 | 271 | } |
|---|
| 228 | | -EXPORT_SYMBOL(adis_debugfs_reg_access); |
|---|
| 272 | +EXPORT_SYMBOL_NS(adis_debugfs_reg_access, IIO_ADISLIB); |
|---|
| 229 | 273 | |
|---|
| 230 | 274 | #endif |
|---|
| 231 | 275 | |
|---|
| 232 | 276 | /** |
|---|
| 233 | | - * adis_enable_irq() - Enable or disable data ready IRQ |
|---|
| 277 | + * __adis_enable_irq() - Enable or disable data ready IRQ (unlocked) |
|---|
| 234 | 278 | * @adis: The adis device |
|---|
| 235 | 279 | * @enable: Whether to enable the IRQ |
|---|
| 236 | 280 | * |
|---|
| 237 | 281 | * Returns 0 on success, negative error code otherwise |
|---|
| 238 | 282 | */ |
|---|
| 239 | | -int adis_enable_irq(struct adis *adis, bool enable) |
|---|
| 283 | +int __adis_enable_irq(struct adis *adis, bool enable) |
|---|
| 240 | 284 | { |
|---|
| 241 | | - int ret = 0; |
|---|
| 242 | | - uint16_t msc; |
|---|
| 285 | + int ret; |
|---|
| 286 | + u16 msc; |
|---|
| 243 | 287 | |
|---|
| 244 | 288 | if (adis->data->enable_irq) |
|---|
| 245 | 289 | return adis->data->enable_irq(adis, enable); |
|---|
| 246 | 290 | |
|---|
| 247 | | - ret = adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc); |
|---|
| 291 | + if (adis->data->unmasked_drdy) { |
|---|
| 292 | + if (enable) |
|---|
| 293 | + enable_irq(adis->spi->irq); |
|---|
| 294 | + else |
|---|
| 295 | + disable_irq(adis->spi->irq); |
|---|
| 296 | + |
|---|
| 297 | + return 0; |
|---|
| 298 | + } |
|---|
| 299 | + |
|---|
| 300 | + ret = __adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc); |
|---|
| 248 | 301 | if (ret) |
|---|
| 249 | | - goto error_ret; |
|---|
| 302 | + return ret; |
|---|
| 250 | 303 | |
|---|
| 251 | 304 | msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH; |
|---|
| 252 | 305 | msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2; |
|---|
| .. | .. |
|---|
| 255 | 308 | else |
|---|
| 256 | 309 | msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN; |
|---|
| 257 | 310 | |
|---|
| 258 | | - ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc); |
|---|
| 259 | | - |
|---|
| 260 | | -error_ret: |
|---|
| 261 | | - return ret; |
|---|
| 311 | + return __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc); |
|---|
| 262 | 312 | } |
|---|
| 263 | | -EXPORT_SYMBOL(adis_enable_irq); |
|---|
| 313 | +EXPORT_SYMBOL_NS(__adis_enable_irq, IIO_ADISLIB); |
|---|
| 264 | 314 | |
|---|
| 265 | 315 | /** |
|---|
| 266 | | - * adis_check_status() - Check the device for error conditions |
|---|
| 316 | + * __adis_check_status() - Check the device for error conditions (unlocked) |
|---|
| 267 | 317 | * @adis: The adis device |
|---|
| 268 | 318 | * |
|---|
| 269 | 319 | * Returns 0 on success, a negative error code otherwise |
|---|
| 270 | 320 | */ |
|---|
| 271 | | -int adis_check_status(struct adis *adis) |
|---|
| 321 | +int __adis_check_status(struct adis *adis) |
|---|
| 272 | 322 | { |
|---|
| 273 | | - uint16_t status; |
|---|
| 323 | + u16 status; |
|---|
| 274 | 324 | int ret; |
|---|
| 275 | 325 | int i; |
|---|
| 276 | 326 | |
|---|
| 277 | | - ret = adis_read_reg_16(adis, adis->data->diag_stat_reg, &status); |
|---|
| 278 | | - if (ret < 0) |
|---|
| 327 | + ret = __adis_read_reg_16(adis, adis->data->diag_stat_reg, &status); |
|---|
| 328 | + if (ret) |
|---|
| 279 | 329 | return ret; |
|---|
| 280 | 330 | |
|---|
| 281 | 331 | status &= adis->data->status_error_mask; |
|---|
| .. | .. |
|---|
| 292 | 342 | |
|---|
| 293 | 343 | return -EIO; |
|---|
| 294 | 344 | } |
|---|
| 295 | | -EXPORT_SYMBOL_GPL(adis_check_status); |
|---|
| 345 | +EXPORT_SYMBOL_NS_GPL(__adis_check_status, IIO_ADISLIB); |
|---|
| 296 | 346 | |
|---|
| 297 | 347 | /** |
|---|
| 298 | | - * adis_reset() - Reset the device |
|---|
| 348 | + * __adis_reset() - Reset the device (unlocked version) |
|---|
| 299 | 349 | * @adis: The adis device |
|---|
| 300 | 350 | * |
|---|
| 301 | 351 | * Returns 0 on success, a negative error code otherwise |
|---|
| 302 | 352 | */ |
|---|
| 303 | | -int adis_reset(struct adis *adis) |
|---|
| 353 | +int __adis_reset(struct adis *adis) |
|---|
| 304 | 354 | { |
|---|
| 305 | 355 | int ret; |
|---|
| 356 | + const struct adis_timeout *timeouts = adis->data->timeouts; |
|---|
| 306 | 357 | |
|---|
| 307 | | - ret = adis_write_reg_8(adis, adis->data->glob_cmd_reg, |
|---|
| 308 | | - ADIS_GLOB_CMD_SW_RESET); |
|---|
| 309 | | - if (ret) |
|---|
| 358 | + ret = __adis_write_reg_8(adis, adis->data->glob_cmd_reg, |
|---|
| 359 | + ADIS_GLOB_CMD_SW_RESET); |
|---|
| 360 | + if (ret) { |
|---|
| 310 | 361 | dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret); |
|---|
| 362 | + return ret; |
|---|
| 363 | + } |
|---|
| 311 | 364 | |
|---|
| 312 | | - return ret; |
|---|
| 365 | + msleep(timeouts->sw_reset_ms); |
|---|
| 366 | + |
|---|
| 367 | + return 0; |
|---|
| 313 | 368 | } |
|---|
| 314 | | -EXPORT_SYMBOL_GPL(adis_reset); |
|---|
| 369 | +EXPORT_SYMBOL_NS_GPL(__adis_reset, IIO_ADIS_LIB); |
|---|
| 315 | 370 | |
|---|
| 316 | 371 | static int adis_self_test(struct adis *adis) |
|---|
| 317 | 372 | { |
|---|
| 318 | 373 | int ret; |
|---|
| 374 | + const struct adis_timeout *timeouts = adis->data->timeouts; |
|---|
| 319 | 375 | |
|---|
| 320 | | - ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg, |
|---|
| 321 | | - adis->data->self_test_mask); |
|---|
| 376 | + ret = __adis_write_reg_16(adis, adis->data->self_test_reg, |
|---|
| 377 | + adis->data->self_test_mask); |
|---|
| 322 | 378 | if (ret) { |
|---|
| 323 | 379 | dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n", |
|---|
| 324 | 380 | ret); |
|---|
| 325 | 381 | return ret; |
|---|
| 326 | 382 | } |
|---|
| 327 | 383 | |
|---|
| 328 | | - msleep(adis->data->startup_delay); |
|---|
| 384 | + msleep(timeouts->self_test_ms); |
|---|
| 329 | 385 | |
|---|
| 330 | | - ret = adis_check_status(adis); |
|---|
| 386 | + ret = __adis_check_status(adis); |
|---|
| 331 | 387 | |
|---|
| 332 | 388 | if (adis->data->self_test_no_autoclear) |
|---|
| 333 | | - adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 0x00); |
|---|
| 389 | + __adis_write_reg_16(adis, adis->data->self_test_reg, 0x00); |
|---|
| 334 | 390 | |
|---|
| 335 | 391 | return ret; |
|---|
| 336 | 392 | } |
|---|
| 337 | 393 | |
|---|
| 338 | 394 | /** |
|---|
| 339 | | - * adis_inital_startup() - Performs device self-test |
|---|
| 395 | + * __adis_initial_startup() - Device initial setup |
|---|
| 340 | 396 | * @adis: The adis device |
|---|
| 397 | + * |
|---|
| 398 | + * The function performs a HW reset via a reset pin that should be specified |
|---|
| 399 | + * via GPIOLIB. If no pin is configured a SW reset will be performed. |
|---|
| 400 | + * The RST pin for the ADIS devices should be configured as ACTIVE_LOW. |
|---|
| 401 | + * |
|---|
| 402 | + * After the self-test operation is performed, the function will also check |
|---|
| 403 | + * that the product ID is as expected. This assumes that drivers providing |
|---|
| 404 | + * 'prod_id_reg' will also provide the 'prod_id'. |
|---|
| 341 | 405 | * |
|---|
| 342 | 406 | * Returns 0 if the device is operational, a negative error code otherwise. |
|---|
| 343 | 407 | * |
|---|
| 344 | 408 | * This function should be called early on in the device initialization sequence |
|---|
| 345 | 409 | * to ensure that the device is in a sane and known state and that it is usable. |
|---|
| 346 | 410 | */ |
|---|
| 347 | | -int adis_initial_startup(struct adis *adis) |
|---|
| 411 | +int __adis_initial_startup(struct adis *adis) |
|---|
| 348 | 412 | { |
|---|
| 413 | + const struct adis_timeout *timeouts = adis->data->timeouts; |
|---|
| 414 | + struct gpio_desc *gpio; |
|---|
| 415 | + u16 prod_id; |
|---|
| 349 | 416 | int ret; |
|---|
| 350 | 417 | |
|---|
| 351 | | - ret = adis_self_test(adis); |
|---|
| 352 | | - if (ret) { |
|---|
| 353 | | - dev_err(&adis->spi->dev, "Self-test failed, trying reset.\n"); |
|---|
| 354 | | - adis_reset(adis); |
|---|
| 355 | | - msleep(adis->data->startup_delay); |
|---|
| 356 | | - ret = adis_self_test(adis); |
|---|
| 357 | | - if (ret) { |
|---|
| 358 | | - dev_err(&adis->spi->dev, "Second self-test failed, giving up.\n"); |
|---|
| 418 | + /* check if the device has rst pin low */ |
|---|
| 419 | + gpio = devm_gpiod_get_optional(&adis->spi->dev, "reset", GPIOD_OUT_HIGH); |
|---|
| 420 | + if (IS_ERR(gpio)) |
|---|
| 421 | + return PTR_ERR(gpio); |
|---|
| 422 | + |
|---|
| 423 | + if (gpio) { |
|---|
| 424 | + usleep_range(10, 12); |
|---|
| 425 | + /* bring device out of reset */ |
|---|
| 426 | + gpiod_set_value_cansleep(gpio, 0); |
|---|
| 427 | + msleep(timeouts->reset_ms); |
|---|
| 428 | + } else { |
|---|
| 429 | + ret = __adis_reset(adis); |
|---|
| 430 | + if (ret) |
|---|
| 359 | 431 | return ret; |
|---|
| 360 | | - } |
|---|
| 361 | 432 | } |
|---|
| 433 | + |
|---|
| 434 | + ret = adis_self_test(adis); |
|---|
| 435 | + if (ret) |
|---|
| 436 | + return ret; |
|---|
| 437 | + |
|---|
| 438 | + /* |
|---|
| 439 | + * don't bother calling this if we can't unmask the IRQ as in this case |
|---|
| 440 | + * the IRQ is most likely not yet requested and we will request it |
|---|
| 441 | + * with 'IRQF_NO_AUTOEN' anyways. |
|---|
| 442 | + */ |
|---|
| 443 | + if (!adis->data->unmasked_drdy) |
|---|
| 444 | + __adis_enable_irq(adis, false); |
|---|
| 445 | + |
|---|
| 446 | + if (!adis->data->prod_id_reg) |
|---|
| 447 | + return 0; |
|---|
| 448 | + |
|---|
| 449 | + ret = adis_read_reg_16(adis, adis->data->prod_id_reg, &prod_id); |
|---|
| 450 | + if (ret) |
|---|
| 451 | + return ret; |
|---|
| 452 | + |
|---|
| 453 | + if (prod_id != adis->data->prod_id) |
|---|
| 454 | + dev_warn(&adis->spi->dev, |
|---|
| 455 | + "Device ID(%u) and product ID(%u) do not match.\n", |
|---|
| 456 | + adis->data->prod_id, prod_id); |
|---|
| 362 | 457 | |
|---|
| 363 | 458 | return 0; |
|---|
| 364 | 459 | } |
|---|
| 365 | | -EXPORT_SYMBOL_GPL(adis_initial_startup); |
|---|
| 460 | +EXPORT_SYMBOL_NS_GPL(__adis_initial_startup, IIO_ADISLIB); |
|---|
| 366 | 461 | |
|---|
| 367 | 462 | /** |
|---|
| 368 | 463 | * adis_single_conversion() - Performs a single sample conversion |
|---|
| .. | .. |
|---|
| 380 | 475 | * a error bit in the channels raw value set error_mask to 0. |
|---|
| 381 | 476 | */ |
|---|
| 382 | 477 | int adis_single_conversion(struct iio_dev *indio_dev, |
|---|
| 383 | | - const struct iio_chan_spec *chan, unsigned int error_mask, int *val) |
|---|
| 478 | + const struct iio_chan_spec *chan, |
|---|
| 479 | + unsigned int error_mask, int *val) |
|---|
| 384 | 480 | { |
|---|
| 385 | 481 | struct adis *adis = iio_device_get_drvdata(indio_dev); |
|---|
| 386 | 482 | unsigned int uval; |
|---|
| 387 | 483 | int ret; |
|---|
| 388 | 484 | |
|---|
| 389 | | - mutex_lock(&indio_dev->mlock); |
|---|
| 485 | + mutex_lock(&adis->state_lock); |
|---|
| 390 | 486 | |
|---|
| 391 | | - ret = adis_read_reg(adis, chan->address, &uval, |
|---|
| 392 | | - chan->scan_type.storagebits / 8); |
|---|
| 487 | + ret = __adis_read_reg(adis, chan->address, &uval, |
|---|
| 488 | + chan->scan_type.storagebits / 8); |
|---|
| 393 | 489 | if (ret) |
|---|
| 394 | 490 | goto err_unlock; |
|---|
| 395 | 491 | |
|---|
| 396 | 492 | if (uval & error_mask) { |
|---|
| 397 | | - ret = adis_check_status(adis); |
|---|
| 493 | + ret = __adis_check_status(adis); |
|---|
| 398 | 494 | if (ret) |
|---|
| 399 | 495 | goto err_unlock; |
|---|
| 400 | 496 | } |
|---|
| .. | .. |
|---|
| 406 | 502 | |
|---|
| 407 | 503 | ret = IIO_VAL_INT; |
|---|
| 408 | 504 | err_unlock: |
|---|
| 409 | | - mutex_unlock(&indio_dev->mlock); |
|---|
| 505 | + mutex_unlock(&adis->state_lock); |
|---|
| 410 | 506 | return ret; |
|---|
| 411 | 507 | } |
|---|
| 412 | | -EXPORT_SYMBOL_GPL(adis_single_conversion); |
|---|
| 508 | +EXPORT_SYMBOL_NS_GPL(adis_single_conversion, IIO_ADISLIB); |
|---|
| 413 | 509 | |
|---|
| 414 | 510 | /** |
|---|
| 415 | 511 | * adis_init() - Initialize adis device structure |
|---|
| .. | .. |
|---|
| 424 | 520 | * called. |
|---|
| 425 | 521 | */ |
|---|
| 426 | 522 | int adis_init(struct adis *adis, struct iio_dev *indio_dev, |
|---|
| 427 | | - struct spi_device *spi, const struct adis_data *data) |
|---|
| 523 | + struct spi_device *spi, const struct adis_data *data) |
|---|
| 428 | 524 | { |
|---|
| 429 | | - mutex_init(&adis->txrx_lock); |
|---|
| 525 | + if (!data || !data->timeouts) { |
|---|
| 526 | + dev_err(&spi->dev, "No config data or timeouts not defined!\n"); |
|---|
| 527 | + return -EINVAL; |
|---|
| 528 | + } |
|---|
| 529 | + |
|---|
| 530 | + mutex_init(&adis->state_lock); |
|---|
| 430 | 531 | adis->spi = spi; |
|---|
| 431 | 532 | adis->data = data; |
|---|
| 432 | 533 | iio_device_set_drvdata(indio_dev, adis); |
|---|
| .. | .. |
|---|
| 439 | 540 | adis->current_page = 0; |
|---|
| 440 | 541 | } |
|---|
| 441 | 542 | |
|---|
| 442 | | - return adis_enable_irq(adis, false); |
|---|
| 543 | + return 0; |
|---|
| 443 | 544 | } |
|---|
| 444 | | -EXPORT_SYMBOL_GPL(adis_init); |
|---|
| 545 | +EXPORT_SYMBOL_NS_GPL(adis_init, IIO_ADISLIB); |
|---|
| 445 | 546 | |
|---|
| 446 | 547 | MODULE_LICENSE("GPL"); |
|---|
| 447 | 548 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
|---|