1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Common library for ADIS16XXX devices 4 * 5 * Copyright 2012 Analog Devices Inc. 6 * Author: Lars-Peter Clausen <lars@metafoo.de> 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> 22 #include <linux/iio/imu/adis.h> 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 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, 37 unsigned int value, unsigned int size) 38 { 39 unsigned int page = reg / ADIS_PAGE_SIZE; 40 int ret, i; 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, 49 .cs_change_delay = adis->data->cs_change_delay, 50 .cs_change_delay_unit = SPI_DELAY_UNIT_USECS, 51 }, { 52 .tx_buf = adis->tx + 2, 53 .bits_per_word = 8, 54 .len = 2, 55 .cs_change = 1, 56 .delay_usecs = adis->data->write_delay, 57 .cs_change_delay = adis->data->cs_change_delay, 58 .cs_change_delay_unit = SPI_DELAY_UNIT_USECS, 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, 65 .cs_change_delay = adis->data->cs_change_delay, 66 .cs_change_delay_unit = SPI_DELAY_UNIT_USECS, 67 }, { 68 .tx_buf = adis->tx + 6, 69 .bits_per_word = 8, 70 .len = 2, 71 .delay_usecs = adis->data->write_delay, 72 }, { 73 .tx_buf = adis->tx + 8, 74 .bits_per_word = 8, 75 .len = 2, 76 .delay_usecs = adis->data->write_delay, 77 }, 78 }; 79 80 spi_message_init(&msg); 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 88 switch (size) { 89 case 4: 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; 94 /* fall through */ 95 case 2: 96 adis->tx[4] = ADIS_WRITE_REG(reg + 1); 97 adis->tx[5] = (value >> 8) & 0xff; 98 /* fall through */ 99 case 1: 100 adis->tx[2] = ADIS_WRITE_REG(reg); 101 adis->tx[3] = value & 0xff; 102 break; 103 default: 104 return -EINVAL; 105 } 106 107 xfers[size].cs_change = 0; 108 109 for (i = 1; i <= size; i++) 110 spi_message_add_tail(&xfers[i], &msg); 111 112 ret = spi_sync(adis->spi, &msg); 113 if (ret) { 114 dev_err(&adis->spi->dev, "Failed to write register 0x%02X: %d\n", 115 reg, ret); 116 } else { 117 adis->current_page = page; 118 } 119 120 return ret; 121 } 122 EXPORT_SYMBOL_GPL(__adis_write_reg); 123 124 /** 125 * __adis_read_reg() - read N bytes from register (unlocked version) 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 129 * @size: The size of the @val buffer 130 */ 131 int __adis_read_reg(struct adis *adis, unsigned int reg, 132 unsigned int *val, unsigned int size) 133 { 134 unsigned int page = reg / ADIS_PAGE_SIZE; 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, 143 .delay_usecs = adis->data->write_delay, 144 .cs_change_delay = adis->data->cs_change_delay, 145 .cs_change_delay_unit = SPI_DELAY_UNIT_USECS, 146 }, { 147 .tx_buf = adis->tx + 2, 148 .bits_per_word = 8, 149 .len = 2, 150 .cs_change = 1, 151 .delay_usecs = adis->data->read_delay, 152 .cs_change_delay = adis->data->cs_change_delay, 153 .cs_change_delay_unit = SPI_DELAY_UNIT_USECS, 154 }, { 155 .tx_buf = adis->tx + 4, 156 .rx_buf = adis->rx, 157 .bits_per_word = 8, 158 .len = 2, 159 .cs_change = 1, 160 .delay_usecs = adis->data->read_delay, 161 .cs_change_delay = adis->data->cs_change_delay, 162 .cs_change_delay_unit = SPI_DELAY_UNIT_USECS, 163 }, { 164 .rx_buf = adis->rx + 2, 165 .bits_per_word = 8, 166 .len = 2, 167 .delay_usecs = adis->data->read_delay, 168 }, 169 }; 170 171 spi_message_init(&msg); 172 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 179 switch (size) { 180 case 4: 181 adis->tx[2] = ADIS_READ_REG(reg + 2); 182 adis->tx[3] = 0; 183 spi_message_add_tail(&xfers[1], &msg); 184 /* fall through */ 185 case 2: 186 adis->tx[4] = ADIS_READ_REG(reg); 187 adis->tx[5] = 0; 188 spi_message_add_tail(&xfers[2], &msg); 189 spi_message_add_tail(&xfers[3], &msg); 190 break; 191 default: 192 return -EINVAL; 193 } 194 195 ret = spi_sync(adis->spi, &msg); 196 if (ret) { 197 dev_err(&adis->spi->dev, "Failed to read register 0x%02X: %d\n", 198 reg, ret); 199 return ret; 200 } else { 201 adis->current_page = page; 202 } 203 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 213 return ret; 214 } 215 EXPORT_SYMBOL_GPL(__adis_read_reg); 216 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); 229 if (ret == 0) 230 *readval = val16; 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 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 253 mutex_lock(&adis->state_lock); 254 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); 261 if (ret) 262 goto out_unlock; 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 271 ret = __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc); 272 273 out_unlock: 274 mutex_unlock(&adis->state_lock); 275 return ret; 276 } 277 EXPORT_SYMBOL(adis_enable_irq); 278 279 /** 280 * __adis_check_status() - Check the device for error conditions (unlocked) 281 * @adis: The adis device 282 * 283 * Returns 0 on success, a negative error code otherwise 284 */ 285 int __adis_check_status(struct adis *adis) 286 { 287 uint16_t status; 288 int ret; 289 int i; 290 291 ret = __adis_read_reg_16(adis, adis->data->diag_stat_reg, &status); 292 if (ret) 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 } 309 EXPORT_SYMBOL_GPL(__adis_check_status); 310 311 /** 312 * adis_reset() - Reset the device 313 * @adis: The adis device 314 * 315 * Returns 0 on success, a negative error code otherwise 316 */ 317 int adis_reset(struct adis *adis) 318 { 319 int ret; 320 321 ret = adis_write_reg_8(adis, adis->data->glob_cmd_reg, 322 ADIS_GLOB_CMD_SW_RESET); 323 if (ret) 324 dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret); 325 326 return ret; 327 } 328 EXPORT_SYMBOL_GPL(adis_reset); 329 330 static int adis_self_test(struct adis *adis) 331 { 332 int ret; 333 334 ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 335 adis->data->self_test_mask); 336 if (ret) { 337 dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n", 338 ret); 339 return ret; 340 } 341 342 msleep(adis->data->startup_delay); 343 344 ret = adis_check_status(adis); 345 346 if (adis->data->self_test_no_autoclear) 347 adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 0x00); 348 349 return ret; 350 } 351 352 /** 353 * adis_inital_startup() - Performs device self-test 354 * @adis: The adis device 355 * 356 * Returns 0 if the device is operational, a negative error code otherwise. 357 * 358 * This function should be called early on in the device initialization sequence 359 * to ensure that the device is in a sane and known state and that it is usable. 360 */ 361 int adis_initial_startup(struct adis *adis) 362 { 363 int ret; 364 365 ret = adis_self_test(adis); 366 if (ret) { 367 dev_err(&adis->spi->dev, "Self-test failed, trying reset.\n"); 368 adis_reset(adis); 369 msleep(adis->data->startup_delay); 370 ret = adis_self_test(adis); 371 if (ret) { 372 dev_err(&adis->spi->dev, "Second self-test failed, giving up.\n"); 373 return ret; 374 } 375 } 376 377 return 0; 378 } 379 EXPORT_SYMBOL_GPL(adis_initial_startup); 380 381 /** 382 * adis_single_conversion() - Performs a single sample conversion 383 * @indio_dev: The IIO device 384 * @chan: The IIO channel 385 * @error_mask: Mask for the error bit 386 * @val: Result of the conversion 387 * 388 * Returns IIO_VAL_INT on success, a negative error code otherwise. 389 * 390 * The function performs a single conversion on a given channel and post 391 * processes the value accordingly to the channel spec. If a error_mask is given 392 * the function will check if the mask is set in the returned raw value. If it 393 * is set the function will perform a self-check. If the device does not report 394 * a error bit in the channels raw value set error_mask to 0. 395 */ 396 int adis_single_conversion(struct iio_dev *indio_dev, 397 const struct iio_chan_spec *chan, unsigned int error_mask, int *val) 398 { 399 struct adis *adis = iio_device_get_drvdata(indio_dev); 400 unsigned int uval; 401 int ret; 402 403 mutex_lock(&indio_dev->mlock); 404 405 ret = adis_read_reg(adis, chan->address, &uval, 406 chan->scan_type.storagebits / 8); 407 if (ret) 408 goto err_unlock; 409 410 if (uval & error_mask) { 411 ret = adis_check_status(adis); 412 if (ret) 413 goto err_unlock; 414 } 415 416 if (chan->scan_type.sign == 's') 417 *val = sign_extend32(uval, chan->scan_type.realbits - 1); 418 else 419 *val = uval & ((1 << chan->scan_type.realbits) - 1); 420 421 ret = IIO_VAL_INT; 422 err_unlock: 423 mutex_unlock(&indio_dev->mlock); 424 return ret; 425 } 426 EXPORT_SYMBOL_GPL(adis_single_conversion); 427 428 /** 429 * adis_init() - Initialize adis device structure 430 * @adis: The adis device 431 * @indio_dev: The iio device 432 * @spi: The spi device 433 * @data: Chip specific data 434 * 435 * Returns 0 on success, a negative error code otherwise. 436 * 437 * This function must be called, before any other adis helper function may be 438 * called. 439 */ 440 int adis_init(struct adis *adis, struct iio_dev *indio_dev, 441 struct spi_device *spi, const struct adis_data *data) 442 { 443 mutex_init(&adis->state_lock); 444 adis->spi = spi; 445 adis->data = data; 446 iio_device_set_drvdata(indio_dev, adis); 447 448 if (data->has_paging) { 449 /* Need to set the page before first read/write */ 450 adis->current_page = -1; 451 } else { 452 /* Page will always be 0 */ 453 adis->current_page = 0; 454 } 455 456 return adis_enable_irq(adis, false); 457 } 458 EXPORT_SYMBOL_GPL(adis_init); 459 460 MODULE_LICENSE("GPL"); 461 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 462 MODULE_DESCRIPTION("Common library code for ADIS16XXX devices"); 463