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