1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * adis16400.c support Analog Devices ADIS16400/5 4 * 3d 2g Linear Accelerometers, 5 * 3d Gyroscopes, 6 * 3d Magnetometers via SPI 7 * 8 * Copyright (c) 2009 Manuel Stahl <manuel.stahl@iis.fraunhofer.de> 9 * Copyright (c) 2007 Jonathan Cameron <jic23@kernel.org> 10 * Copyright (c) 2011 Analog Devices Inc. 11 */ 12 13 #include <linux/irq.h> 14 #include <linux/device.h> 15 #include <linux/kernel.h> 16 #include <linux/spi/spi.h> 17 #include <linux/module.h> 18 #include <linux/debugfs.h> 19 #include <linux/bitops.h> 20 21 #include <linux/iio/iio.h> 22 #include <linux/iio/buffer.h> 23 #include <linux/iio/trigger_consumer.h> 24 #include <linux/iio/imu/adis.h> 25 26 #define ADIS16400_STARTUP_DELAY 290 /* ms */ 27 #define ADIS16400_MTEST_DELAY 90 /* ms */ 28 29 #define ADIS16400_FLASH_CNT 0x00 /* Flash memory write count */ 30 #define ADIS16400_SUPPLY_OUT 0x02 /* Power supply measurement */ 31 #define ADIS16400_XGYRO_OUT 0x04 /* X-axis gyroscope output */ 32 #define ADIS16400_YGYRO_OUT 0x06 /* Y-axis gyroscope output */ 33 #define ADIS16400_ZGYRO_OUT 0x08 /* Z-axis gyroscope output */ 34 #define ADIS16400_XACCL_OUT 0x0A /* X-axis accelerometer output */ 35 #define ADIS16400_YACCL_OUT 0x0C /* Y-axis accelerometer output */ 36 #define ADIS16400_ZACCL_OUT 0x0E /* Z-axis accelerometer output */ 37 #define ADIS16400_XMAGN_OUT 0x10 /* X-axis magnetometer measurement */ 38 #define ADIS16400_YMAGN_OUT 0x12 /* Y-axis magnetometer measurement */ 39 #define ADIS16400_ZMAGN_OUT 0x14 /* Z-axis magnetometer measurement */ 40 #define ADIS16400_TEMP_OUT 0x16 /* Temperature output */ 41 #define ADIS16400_AUX_ADC 0x18 /* Auxiliary ADC measurement */ 42 43 #define ADIS16350_XTEMP_OUT 0x10 /* X-axis gyroscope temperature measurement */ 44 #define ADIS16350_YTEMP_OUT 0x12 /* Y-axis gyroscope temperature measurement */ 45 #define ADIS16350_ZTEMP_OUT 0x14 /* Z-axis gyroscope temperature measurement */ 46 47 #define ADIS16300_PITCH_OUT 0x12 /* X axis inclinometer output measurement */ 48 #define ADIS16300_ROLL_OUT 0x14 /* Y axis inclinometer output measurement */ 49 #define ADIS16300_AUX_ADC 0x16 /* Auxiliary ADC measurement */ 50 51 #define ADIS16448_BARO_OUT 0x16 /* Barometric pressure output */ 52 #define ADIS16448_TEMP_OUT 0x18 /* Temperature output */ 53 54 /* Calibration parameters */ 55 #define ADIS16400_XGYRO_OFF 0x1A /* X-axis gyroscope bias offset factor */ 56 #define ADIS16400_YGYRO_OFF 0x1C /* Y-axis gyroscope bias offset factor */ 57 #define ADIS16400_ZGYRO_OFF 0x1E /* Z-axis gyroscope bias offset factor */ 58 #define ADIS16400_XACCL_OFF 0x20 /* X-axis acceleration bias offset factor */ 59 #define ADIS16400_YACCL_OFF 0x22 /* Y-axis acceleration bias offset factor */ 60 #define ADIS16400_ZACCL_OFF 0x24 /* Z-axis acceleration bias offset factor */ 61 #define ADIS16400_XMAGN_HIF 0x26 /* X-axis magnetometer, hard-iron factor */ 62 #define ADIS16400_YMAGN_HIF 0x28 /* Y-axis magnetometer, hard-iron factor */ 63 #define ADIS16400_ZMAGN_HIF 0x2A /* Z-axis magnetometer, hard-iron factor */ 64 #define ADIS16400_XMAGN_SIF 0x2C /* X-axis magnetometer, soft-iron factor */ 65 #define ADIS16400_YMAGN_SIF 0x2E /* Y-axis magnetometer, soft-iron factor */ 66 #define ADIS16400_ZMAGN_SIF 0x30 /* Z-axis magnetometer, soft-iron factor */ 67 68 #define ADIS16400_GPIO_CTRL 0x32 /* Auxiliary digital input/output control */ 69 #define ADIS16400_MSC_CTRL 0x34 /* Miscellaneous control */ 70 #define ADIS16400_SMPL_PRD 0x36 /* Internal sample period (rate) control */ 71 #define ADIS16400_SENS_AVG 0x38 /* Dynamic range and digital filter control */ 72 #define ADIS16400_SLP_CNT 0x3A /* Sleep mode control */ 73 #define ADIS16400_DIAG_STAT 0x3C /* System status */ 74 75 /* Alarm functions */ 76 #define ADIS16400_GLOB_CMD 0x3E /* System command */ 77 #define ADIS16400_ALM_MAG1 0x40 /* Alarm 1 amplitude threshold */ 78 #define ADIS16400_ALM_MAG2 0x42 /* Alarm 2 amplitude threshold */ 79 #define ADIS16400_ALM_SMPL1 0x44 /* Alarm 1 sample size */ 80 #define ADIS16400_ALM_SMPL2 0x46 /* Alarm 2 sample size */ 81 #define ADIS16400_ALM_CTRL 0x48 /* Alarm control */ 82 #define ADIS16400_AUX_DAC 0x4A /* Auxiliary DAC data */ 83 84 #define ADIS16334_LOT_ID1 0x52 /* Lot identification code 1 */ 85 #define ADIS16334_LOT_ID2 0x54 /* Lot identification code 2 */ 86 #define ADIS16400_PRODUCT_ID 0x56 /* Product identifier */ 87 #define ADIS16334_SERIAL_NUMBER 0x58 /* Serial number, lot specific */ 88 89 #define ADIS16400_ERROR_ACTIVE (1<<14) 90 #define ADIS16400_NEW_DATA (1<<14) 91 92 /* MSC_CTRL */ 93 #define ADIS16400_MSC_CTRL_MEM_TEST (1<<11) 94 #define ADIS16400_MSC_CTRL_INT_SELF_TEST (1<<10) 95 #define ADIS16400_MSC_CTRL_NEG_SELF_TEST (1<<9) 96 #define ADIS16400_MSC_CTRL_POS_SELF_TEST (1<<8) 97 #define ADIS16400_MSC_CTRL_GYRO_BIAS (1<<7) 98 #define ADIS16400_MSC_CTRL_ACCL_ALIGN (1<<6) 99 #define ADIS16400_MSC_CTRL_DATA_RDY_EN (1<<2) 100 #define ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH (1<<1) 101 #define ADIS16400_MSC_CTRL_DATA_RDY_DIO2 (1<<0) 102 103 /* SMPL_PRD */ 104 #define ADIS16400_SMPL_PRD_TIME_BASE (1<<7) 105 #define ADIS16400_SMPL_PRD_DIV_MASK 0x7F 106 107 /* DIAG_STAT */ 108 #define ADIS16400_DIAG_STAT_ZACCL_FAIL 15 109 #define ADIS16400_DIAG_STAT_YACCL_FAIL 14 110 #define ADIS16400_DIAG_STAT_XACCL_FAIL 13 111 #define ADIS16400_DIAG_STAT_XGYRO_FAIL 12 112 #define ADIS16400_DIAG_STAT_YGYRO_FAIL 11 113 #define ADIS16400_DIAG_STAT_ZGYRO_FAIL 10 114 #define ADIS16400_DIAG_STAT_ALARM2 9 115 #define ADIS16400_DIAG_STAT_ALARM1 8 116 #define ADIS16400_DIAG_STAT_FLASH_CHK 6 117 #define ADIS16400_DIAG_STAT_SELF_TEST 5 118 #define ADIS16400_DIAG_STAT_OVERFLOW 4 119 #define ADIS16400_DIAG_STAT_SPI_FAIL 3 120 #define ADIS16400_DIAG_STAT_FLASH_UPT 2 121 #define ADIS16400_DIAG_STAT_POWER_HIGH 1 122 #define ADIS16400_DIAG_STAT_POWER_LOW 0 123 124 /* GLOB_CMD */ 125 #define ADIS16400_GLOB_CMD_SW_RESET (1<<7) 126 #define ADIS16400_GLOB_CMD_P_AUTO_NULL (1<<4) 127 #define ADIS16400_GLOB_CMD_FLASH_UPD (1<<3) 128 #define ADIS16400_GLOB_CMD_DAC_LATCH (1<<2) 129 #define ADIS16400_GLOB_CMD_FAC_CALIB (1<<1) 130 #define ADIS16400_GLOB_CMD_AUTO_NULL (1<<0) 131 132 /* SLP_CNT */ 133 #define ADIS16400_SLP_CNT_POWER_OFF (1<<8) 134 135 #define ADIS16334_RATE_DIV_SHIFT 8 136 #define ADIS16334_RATE_INT_CLK BIT(0) 137 138 #define ADIS16400_SPI_SLOW (u32)(300 * 1000) 139 #define ADIS16400_SPI_BURST (u32)(1000 * 1000) 140 #define ADIS16400_SPI_FAST (u32)(2000 * 1000) 141 142 #define ADIS16400_HAS_PROD_ID BIT(0) 143 #define ADIS16400_NO_BURST BIT(1) 144 #define ADIS16400_HAS_SLOW_MODE BIT(2) 145 #define ADIS16400_HAS_SERIAL_NUMBER BIT(3) 146 #define ADIS16400_BURST_DIAG_STAT BIT(4) 147 148 struct adis16400_state; 149 150 struct adis16400_chip_info { 151 const struct iio_chan_spec *channels; 152 const struct adis_data adis_data; 153 const int num_channels; 154 const long flags; 155 unsigned int gyro_scale_micro; 156 unsigned int accel_scale_micro; 157 int temp_scale_nano; 158 int temp_offset; 159 /* set_freq() & get_freq() need to avoid using ADIS lib's state lock */ 160 int (*set_freq)(struct adis16400_state *st, unsigned int freq); 161 int (*get_freq)(struct adis16400_state *st); 162 }; 163 164 /** 165 * struct adis16400_state - device instance specific data 166 * @variant: chip variant info 167 * @filt_int: integer part of requested filter frequency 168 * @adis: adis device 169 * @avail_scan_mask: NULL terminated array of bitmaps of channels 170 * that must be enabled together 171 **/ 172 struct adis16400_state { 173 struct adis16400_chip_info *variant; 174 int filt_int; 175 176 struct adis adis; 177 unsigned long avail_scan_mask[2]; 178 }; 179 180 /* At the moment triggers are only used for ring buffer 181 * filling. This may change! 182 */ 183 184 enum { 185 ADIS16400_SCAN_SUPPLY, 186 ADIS16400_SCAN_GYRO_X, 187 ADIS16400_SCAN_GYRO_Y, 188 ADIS16400_SCAN_GYRO_Z, 189 ADIS16400_SCAN_ACC_X, 190 ADIS16400_SCAN_ACC_Y, 191 ADIS16400_SCAN_ACC_Z, 192 ADIS16400_SCAN_MAGN_X, 193 ADIS16400_SCAN_MAGN_Y, 194 ADIS16400_SCAN_MAGN_Z, 195 ADIS16400_SCAN_BARO, 196 ADIS16350_SCAN_TEMP_X, 197 ADIS16350_SCAN_TEMP_Y, 198 ADIS16350_SCAN_TEMP_Z, 199 ADIS16300_SCAN_INCLI_X, 200 ADIS16300_SCAN_INCLI_Y, 201 ADIS16400_SCAN_ADC, 202 ADIS16400_SCAN_TIMESTAMP, 203 }; 204 205 #ifdef CONFIG_DEBUG_FS 206 207 static ssize_t adis16400_show_serial_number(struct file *file, 208 char __user *userbuf, size_t count, loff_t *ppos) 209 { 210 struct adis16400_state *st = file->private_data; 211 u16 lot1, lot2, serial_number; 212 char buf[16]; 213 size_t len; 214 int ret; 215 216 ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID1, &lot1); 217 if (ret) 218 return ret; 219 220 ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID2, &lot2); 221 if (ret) 222 return ret; 223 224 ret = adis_read_reg_16(&st->adis, ADIS16334_SERIAL_NUMBER, 225 &serial_number); 226 if (ret) 227 return ret; 228 229 len = snprintf(buf, sizeof(buf), "%.4x-%.4x-%.4x\n", lot1, lot2, 230 serial_number); 231 232 return simple_read_from_buffer(userbuf, count, ppos, buf, len); 233 } 234 235 static const struct file_operations adis16400_serial_number_fops = { 236 .open = simple_open, 237 .read = adis16400_show_serial_number, 238 .llseek = default_llseek, 239 .owner = THIS_MODULE, 240 }; 241 242 static int adis16400_show_product_id(void *arg, u64 *val) 243 { 244 struct adis16400_state *st = arg; 245 uint16_t prod_id; 246 int ret; 247 248 ret = adis_read_reg_16(&st->adis, ADIS16400_PRODUCT_ID, &prod_id); 249 if (ret) 250 return ret; 251 252 *val = prod_id; 253 254 return 0; 255 } 256 DEFINE_DEBUGFS_ATTRIBUTE(adis16400_product_id_fops, 257 adis16400_show_product_id, NULL, "%lld\n"); 258 259 static int adis16400_show_flash_count(void *arg, u64 *val) 260 { 261 struct adis16400_state *st = arg; 262 uint16_t flash_count; 263 int ret; 264 265 ret = adis_read_reg_16(&st->adis, ADIS16400_FLASH_CNT, &flash_count); 266 if (ret) 267 return ret; 268 269 *val = flash_count; 270 271 return 0; 272 } 273 DEFINE_DEBUGFS_ATTRIBUTE(adis16400_flash_count_fops, 274 adis16400_show_flash_count, NULL, "%lld\n"); 275 276 static int adis16400_debugfs_init(struct iio_dev *indio_dev) 277 { 278 struct adis16400_state *st = iio_priv(indio_dev); 279 struct dentry *d = iio_get_debugfs_dentry(indio_dev); 280 281 if (st->variant->flags & ADIS16400_HAS_SERIAL_NUMBER) 282 debugfs_create_file_unsafe("serial_number", 0400, 283 d, st, &adis16400_serial_number_fops); 284 if (st->variant->flags & ADIS16400_HAS_PROD_ID) 285 debugfs_create_file_unsafe("product_id", 0400, 286 d, st, &adis16400_product_id_fops); 287 debugfs_create_file_unsafe("flash_count", 0400, 288 d, st, &adis16400_flash_count_fops); 289 290 return 0; 291 } 292 293 #else 294 295 static int adis16400_debugfs_init(struct iio_dev *indio_dev) 296 { 297 return 0; 298 } 299 300 #endif 301 302 enum adis16400_chip_variant { 303 ADIS16300, 304 ADIS16334, 305 ADIS16350, 306 ADIS16360, 307 ADIS16362, 308 ADIS16364, 309 ADIS16367, 310 ADIS16400, 311 ADIS16445, 312 ADIS16448, 313 }; 314 315 static int adis16334_get_freq(struct adis16400_state *st) 316 { 317 int ret; 318 uint16_t t; 319 320 ret = __adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t); 321 if (ret) 322 return ret; 323 324 t >>= ADIS16334_RATE_DIV_SHIFT; 325 326 return 819200 >> t; 327 } 328 329 static int adis16334_set_freq(struct adis16400_state *st, unsigned int freq) 330 { 331 unsigned int t; 332 333 if (freq < 819200) 334 t = ilog2(819200 / freq); 335 else 336 t = 0; 337 338 if (t > 0x31) 339 t = 0x31; 340 341 t <<= ADIS16334_RATE_DIV_SHIFT; 342 t |= ADIS16334_RATE_INT_CLK; 343 344 return __adis_write_reg_16(&st->adis, ADIS16400_SMPL_PRD, t); 345 } 346 347 static int adis16400_get_freq(struct adis16400_state *st) 348 { 349 int sps, ret; 350 uint16_t t; 351 352 ret = __adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t); 353 if (ret) 354 return ret; 355 356 sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 52851 : 1638404; 357 sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1; 358 359 return sps; 360 } 361 362 static int adis16400_set_freq(struct adis16400_state *st, unsigned int freq) 363 { 364 unsigned int t; 365 uint8_t val = 0; 366 367 t = 1638404 / freq; 368 if (t >= 128) { 369 val |= ADIS16400_SMPL_PRD_TIME_BASE; 370 t = 52851 / freq; 371 if (t >= 128) 372 t = 127; 373 } else if (t != 0) { 374 t--; 375 } 376 377 val |= t; 378 379 if (t >= 0x0A || (val & ADIS16400_SMPL_PRD_TIME_BASE)) 380 st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW; 381 else 382 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST; 383 384 return __adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val); 385 } 386 387 static const unsigned int adis16400_3db_divisors[] = { 388 [0] = 2, /* Special case */ 389 [1] = 6, 390 [2] = 12, 391 [3] = 25, 392 [4] = 50, 393 [5] = 100, 394 [6] = 200, 395 [7] = 200, /* Not a valid setting */ 396 }; 397 398 static int __adis16400_set_filter(struct iio_dev *indio_dev, int sps, int val) 399 { 400 struct adis16400_state *st = iio_priv(indio_dev); 401 uint16_t val16; 402 int i, ret; 403 404 for (i = ARRAY_SIZE(adis16400_3db_divisors) - 1; i >= 1; i--) { 405 if (sps / adis16400_3db_divisors[i] >= val) 406 break; 407 } 408 409 ret = __adis_read_reg_16(&st->adis, ADIS16400_SENS_AVG, &val16); 410 if (ret) 411 return ret; 412 413 ret = __adis_write_reg_16(&st->adis, ADIS16400_SENS_AVG, 414 (val16 & ~0x07) | i); 415 return ret; 416 } 417 418 /* Power down the device */ 419 static int adis16400_stop_device(struct iio_dev *indio_dev) 420 { 421 struct adis16400_state *st = iio_priv(indio_dev); 422 int ret; 423 424 ret = adis_write_reg_16(&st->adis, ADIS16400_SLP_CNT, 425 ADIS16400_SLP_CNT_POWER_OFF); 426 if (ret) 427 dev_err(&indio_dev->dev, 428 "problem with turning device off: SLP_CNT"); 429 430 return ret; 431 } 432 433 static int adis16400_initial_setup(struct iio_dev *indio_dev) 434 { 435 struct adis16400_state *st = iio_priv(indio_dev); 436 uint16_t prod_id, smp_prd; 437 unsigned int device_id; 438 int ret; 439 440 /* use low spi speed for init if the device has a slow mode */ 441 if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) 442 st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW; 443 else 444 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST; 445 st->adis.spi->mode = SPI_MODE_3; 446 spi_setup(st->adis.spi); 447 448 ret = __adis_initial_startup(&st->adis); 449 if (ret) 450 return ret; 451 452 if (st->variant->flags & ADIS16400_HAS_PROD_ID) { 453 ret = adis_read_reg_16(&st->adis, 454 ADIS16400_PRODUCT_ID, &prod_id); 455 if (ret) 456 goto err_ret; 457 458 if (sscanf(indio_dev->name, "adis%u\n", &device_id) != 1) { 459 ret = -EINVAL; 460 goto err_ret; 461 } 462 463 if (prod_id != device_id) 464 dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.", 465 device_id, prod_id); 466 467 dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n", 468 indio_dev->name, prod_id, 469 spi_get_chipselect(st->adis.spi, 0), st->adis.spi->irq); 470 } 471 /* use high spi speed if possible */ 472 if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) { 473 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &smp_prd); 474 if (ret) 475 goto err_ret; 476 477 if ((smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) { 478 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST; 479 spi_setup(st->adis.spi); 480 } 481 } 482 483 err_ret: 484 return ret; 485 } 486 487 static const uint8_t adis16400_addresses[] = { 488 [ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF, 489 [ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF, 490 [ADIS16400_SCAN_GYRO_Z] = ADIS16400_ZGYRO_OFF, 491 [ADIS16400_SCAN_ACC_X] = ADIS16400_XACCL_OFF, 492 [ADIS16400_SCAN_ACC_Y] = ADIS16400_YACCL_OFF, 493 [ADIS16400_SCAN_ACC_Z] = ADIS16400_ZACCL_OFF, 494 }; 495 496 static int adis16400_write_raw(struct iio_dev *indio_dev, 497 struct iio_chan_spec const *chan, int val, int val2, long info) 498 { 499 struct adis16400_state *st = iio_priv(indio_dev); 500 int sps; 501 502 switch (info) { 503 case IIO_CHAN_INFO_CALIBBIAS: 504 return adis_write_reg_16(&st->adis, 505 adis16400_addresses[chan->scan_index], 506 val); 507 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 508 /* 509 * Need to cache values so we can update if the frequency 510 * changes. 511 */ 512 adis_dev_auto_scoped_lock(&st->adis) { 513 st->filt_int = val; 514 /* Work out update to current value */ 515 sps = st->variant->get_freq(st); 516 if (sps < 0) 517 return sps; 518 519 return __adis16400_set_filter(indio_dev, sps, 520 val * 1000 + val2 / 1000); 521 } 522 unreachable(); 523 case IIO_CHAN_INFO_SAMP_FREQ: 524 sps = val * 1000 + val2 / 1000; 525 526 if (sps <= 0) 527 return -EINVAL; 528 529 adis_dev_auto_scoped_lock(&st->adis) 530 return st->variant->set_freq(st, sps); 531 unreachable(); 532 default: 533 return -EINVAL; 534 } 535 } 536 537 static int adis16400_read_raw(struct iio_dev *indio_dev, 538 struct iio_chan_spec const *chan, int *val, int *val2, long info) 539 { 540 struct adis16400_state *st = iio_priv(indio_dev); 541 int16_t val16; 542 int ret; 543 544 switch (info) { 545 case IIO_CHAN_INFO_RAW: 546 return adis_single_conversion(indio_dev, chan, 0, val); 547 case IIO_CHAN_INFO_SCALE: 548 switch (chan->type) { 549 case IIO_ANGL_VEL: 550 *val = 0; 551 *val2 = st->variant->gyro_scale_micro; 552 return IIO_VAL_INT_PLUS_MICRO; 553 case IIO_VOLTAGE: 554 *val = 0; 555 if (chan->channel == 0) { 556 *val = 2; 557 *val2 = 418000; /* 2.418 mV */ 558 } else { 559 *val = 0; 560 *val2 = 805800; /* 805.8 uV */ 561 } 562 return IIO_VAL_INT_PLUS_MICRO; 563 case IIO_ACCEL: 564 *val = 0; 565 *val2 = st->variant->accel_scale_micro; 566 return IIO_VAL_INT_PLUS_MICRO; 567 case IIO_MAGN: 568 *val = 0; 569 *val2 = 500; /* 0.5 mgauss */ 570 return IIO_VAL_INT_PLUS_MICRO; 571 case IIO_TEMP: 572 *val = st->variant->temp_scale_nano / 1000000; 573 *val2 = (st->variant->temp_scale_nano % 1000000); 574 return IIO_VAL_INT_PLUS_MICRO; 575 case IIO_PRESSURE: 576 /* 20 uBar = 0.002kPascal */ 577 *val = 0; 578 *val2 = 2000; 579 return IIO_VAL_INT_PLUS_MICRO; 580 default: 581 return -EINVAL; 582 } 583 case IIO_CHAN_INFO_CALIBBIAS: 584 ret = adis_read_reg_16(&st->adis, 585 adis16400_addresses[chan->scan_index], &val16); 586 if (ret) 587 return ret; 588 val16 = sign_extend32(val16, 11); 589 *val = val16; 590 return IIO_VAL_INT; 591 case IIO_CHAN_INFO_OFFSET: 592 /* currently only temperature */ 593 *val = st->variant->temp_offset; 594 return IIO_VAL_INT; 595 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 596 adis_dev_auto_scoped_lock(&st->adis) { 597 /* 598 * Need both the number of taps and the sampling 599 * frequency 600 */ 601 ret = __adis_read_reg_16(&st->adis, ADIS16400_SENS_AVG, 602 &val16); 603 if (ret) 604 return ret; 605 606 ret = st->variant->get_freq(st); 607 if (ret) 608 return ret; 609 } 610 ret /= adis16400_3db_divisors[val16 & 0x07]; 611 *val = ret / 1000; 612 *val2 = (ret % 1000) * 1000; 613 return IIO_VAL_INT_PLUS_MICRO; 614 case IIO_CHAN_INFO_SAMP_FREQ: 615 adis_dev_auto_scoped_lock(&st->adis) { 616 ret = st->variant->get_freq(st); 617 if (ret) 618 return ret; 619 } 620 *val = ret / 1000; 621 *val2 = (ret % 1000) * 1000; 622 return IIO_VAL_INT_PLUS_MICRO; 623 default: 624 return -EINVAL; 625 } 626 } 627 628 #if IS_ENABLED(CONFIG_IIO_BUFFER) 629 static irqreturn_t adis16400_trigger_handler(int irq, void *p) 630 { 631 struct iio_poll_func *pf = p; 632 struct iio_dev *indio_dev = pf->indio_dev; 633 struct adis16400_state *st = iio_priv(indio_dev); 634 struct adis *adis = &st->adis; 635 void *buffer; 636 int ret; 637 638 ret = spi_sync(adis->spi, &adis->msg); 639 if (ret) 640 dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret); 641 642 if (st->variant->flags & ADIS16400_BURST_DIAG_STAT) { 643 buffer = adis->buffer + sizeof(u16); 644 /* 645 * The size here is always larger than, or equal to the true 646 * size of the channel data. This may result in a larger copy 647 * than necessary, but as the target buffer will be 648 * buffer->scan_bytes this will be safe. 649 */ 650 iio_push_to_buffers_with_ts_unaligned(indio_dev, buffer, 651 indio_dev->scan_bytes - sizeof(pf->timestamp), 652 pf->timestamp); 653 } else { 654 iio_push_to_buffers_with_timestamp(indio_dev, 655 adis->buffer, 656 pf->timestamp); 657 } 658 659 660 iio_trigger_notify_done(indio_dev->trig); 661 662 return IRQ_HANDLED; 663 } 664 #else 665 #define adis16400_trigger_handler NULL 666 #endif /* IS_ENABLED(CONFIG_IIO_BUFFER) */ 667 668 #define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si, chn) { \ 669 .type = IIO_VOLTAGE, \ 670 .indexed = 1, \ 671 .channel = chn, \ 672 .extend_name = name, \ 673 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 674 BIT(IIO_CHAN_INFO_SCALE), \ 675 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 676 .address = (addr), \ 677 .scan_index = (si), \ 678 .scan_type = { \ 679 .sign = 'u', \ 680 .realbits = (bits), \ 681 .storagebits = 16, \ 682 .shift = 0, \ 683 .endianness = IIO_BE, \ 684 }, \ 685 } 686 687 #define ADIS16400_SUPPLY_CHAN(addr, bits) \ 688 ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY, 0) 689 690 #define ADIS16400_AUX_ADC_CHAN(addr, bits) \ 691 ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC, 1) 692 693 #define ADIS16400_GYRO_CHAN(mod, addr, bits) { \ 694 .type = IIO_ANGL_VEL, \ 695 .modified = 1, \ 696 .channel2 = IIO_MOD_ ## mod, \ 697 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 698 BIT(IIO_CHAN_INFO_CALIBBIAS), \ 699 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 700 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 701 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 702 .address = addr, \ 703 .scan_index = ADIS16400_SCAN_GYRO_ ## mod, \ 704 .scan_type = { \ 705 .sign = 's', \ 706 .realbits = (bits), \ 707 .storagebits = 16, \ 708 .shift = 0, \ 709 .endianness = IIO_BE, \ 710 }, \ 711 } 712 713 #define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \ 714 .type = IIO_ACCEL, \ 715 .modified = 1, \ 716 .channel2 = IIO_MOD_ ## mod, \ 717 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 718 BIT(IIO_CHAN_INFO_CALIBBIAS), \ 719 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 720 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 721 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 722 .address = (addr), \ 723 .scan_index = ADIS16400_SCAN_ACC_ ## mod, \ 724 .scan_type = { \ 725 .sign = 's', \ 726 .realbits = (bits), \ 727 .storagebits = 16, \ 728 .shift = 0, \ 729 .endianness = IIO_BE, \ 730 }, \ 731 } 732 733 #define ADIS16400_MAGN_CHAN(mod, addr, bits) { \ 734 .type = IIO_MAGN, \ 735 .modified = 1, \ 736 .channel2 = IIO_MOD_ ## mod, \ 737 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 738 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 739 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 740 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 741 .address = (addr), \ 742 .scan_index = ADIS16400_SCAN_MAGN_ ## mod, \ 743 .scan_type = { \ 744 .sign = 's', \ 745 .realbits = (bits), \ 746 .storagebits = 16, \ 747 .shift = 0, \ 748 .endianness = IIO_BE, \ 749 }, \ 750 } 751 752 #define ADIS16400_MOD_TEMP_NAME_X "x" 753 #define ADIS16400_MOD_TEMP_NAME_Y "y" 754 #define ADIS16400_MOD_TEMP_NAME_Z "z" 755 756 #define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \ 757 .type = IIO_TEMP, \ 758 .indexed = 1, \ 759 .channel = 0, \ 760 .extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \ 761 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 762 BIT(IIO_CHAN_INFO_OFFSET) | \ 763 BIT(IIO_CHAN_INFO_SCALE), \ 764 .info_mask_shared_by_type = \ 765 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 766 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 767 .address = (addr), \ 768 .scan_index = ADIS16350_SCAN_TEMP_ ## mod, \ 769 .scan_type = { \ 770 .sign = 's', \ 771 .realbits = (bits), \ 772 .storagebits = 16, \ 773 .shift = 0, \ 774 .endianness = IIO_BE, \ 775 }, \ 776 } 777 778 #define ADIS16400_TEMP_CHAN(addr, bits) { \ 779 .type = IIO_TEMP, \ 780 .indexed = 1, \ 781 .channel = 0, \ 782 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 783 BIT(IIO_CHAN_INFO_OFFSET) | \ 784 BIT(IIO_CHAN_INFO_SCALE), \ 785 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 786 .address = (addr), \ 787 .scan_index = ADIS16350_SCAN_TEMP_X, \ 788 .scan_type = { \ 789 .sign = 's', \ 790 .realbits = (bits), \ 791 .storagebits = 16, \ 792 .shift = 0, \ 793 .endianness = IIO_BE, \ 794 }, \ 795 } 796 797 #define ADIS16400_INCLI_CHAN(mod, addr, bits) { \ 798 .type = IIO_INCLI, \ 799 .modified = 1, \ 800 .channel2 = IIO_MOD_ ## mod, \ 801 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 802 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 803 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 804 .address = (addr), \ 805 .scan_index = ADIS16300_SCAN_INCLI_ ## mod, \ 806 .scan_type = { \ 807 .sign = 's', \ 808 .realbits = (bits), \ 809 .storagebits = 16, \ 810 .shift = 0, \ 811 .endianness = IIO_BE, \ 812 }, \ 813 } 814 815 static const struct iio_chan_spec adis16400_channels[] = { 816 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 14), 817 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14), 818 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14), 819 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14), 820 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14), 821 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14), 822 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14), 823 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14), 824 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14), 825 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14), 826 ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT, 12), 827 ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC, 12), 828 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 829 }; 830 831 static const struct iio_chan_spec adis16445_channels[] = { 832 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16), 833 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16), 834 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16), 835 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16), 836 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16), 837 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16), 838 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12), 839 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 840 }; 841 842 static const struct iio_chan_spec adis16448_channels[] = { 843 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16), 844 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16), 845 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16), 846 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16), 847 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16), 848 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16), 849 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 16), 850 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 16), 851 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 16), 852 { 853 .type = IIO_PRESSURE, 854 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 855 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 856 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 857 .address = ADIS16448_BARO_OUT, 858 .scan_index = ADIS16400_SCAN_BARO, 859 .scan_type = { 860 .sign = 's', 861 .realbits = 16, 862 .storagebits = 16, 863 .endianness = IIO_BE, 864 }, 865 }, 866 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12), 867 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 868 }; 869 870 static const struct iio_chan_spec adis16350_channels[] = { 871 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12), 872 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14), 873 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14), 874 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14), 875 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14), 876 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14), 877 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14), 878 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14), 879 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14), 880 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14), 881 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12), 882 ADIS16400_MOD_TEMP_CHAN(X, ADIS16350_XTEMP_OUT, 12), 883 ADIS16400_MOD_TEMP_CHAN(Y, ADIS16350_YTEMP_OUT, 12), 884 ADIS16400_MOD_TEMP_CHAN(Z, ADIS16350_ZTEMP_OUT, 12), 885 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 886 }; 887 888 static const struct iio_chan_spec adis16300_channels[] = { 889 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12), 890 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14), 891 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14), 892 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14), 893 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14), 894 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12), 895 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12), 896 ADIS16400_INCLI_CHAN(X, ADIS16300_PITCH_OUT, 13), 897 ADIS16400_INCLI_CHAN(Y, ADIS16300_ROLL_OUT, 13), 898 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 899 }; 900 901 static const struct iio_chan_spec adis16334_channels[] = { 902 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14), 903 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14), 904 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14), 905 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14), 906 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14), 907 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14), 908 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12), 909 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 910 }; 911 912 static const char * const adis16400_status_error_msgs[] = { 913 [ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure", 914 [ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure", 915 [ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure", 916 [ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure", 917 [ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure", 918 [ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure", 919 [ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active", 920 [ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active", 921 [ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error", 922 [ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error", 923 [ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange", 924 [ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure", 925 [ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed", 926 [ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V", 927 [ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V", 928 }; 929 930 #define ADIS16400_DATA(_timeouts, _burst_len) \ 931 { \ 932 .msc_ctrl_reg = ADIS16400_MSC_CTRL, \ 933 .glob_cmd_reg = ADIS16400_GLOB_CMD, \ 934 .diag_stat_reg = ADIS16400_DIAG_STAT, \ 935 .read_delay = 50, \ 936 .write_delay = 50, \ 937 .self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST, \ 938 .self_test_reg = ADIS16400_MSC_CTRL, \ 939 .status_error_msgs = adis16400_status_error_msgs, \ 940 .status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) | \ 941 BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) | \ 942 BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) | \ 943 BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) | \ 944 BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) | \ 945 BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) | \ 946 BIT(ADIS16400_DIAG_STAT_ALARM2) | \ 947 BIT(ADIS16400_DIAG_STAT_ALARM1) | \ 948 BIT(ADIS16400_DIAG_STAT_FLASH_CHK) | \ 949 BIT(ADIS16400_DIAG_STAT_SELF_TEST) | \ 950 BIT(ADIS16400_DIAG_STAT_OVERFLOW) | \ 951 BIT(ADIS16400_DIAG_STAT_SPI_FAIL) | \ 952 BIT(ADIS16400_DIAG_STAT_FLASH_UPT) | \ 953 BIT(ADIS16400_DIAG_STAT_POWER_HIGH) | \ 954 BIT(ADIS16400_DIAG_STAT_POWER_LOW), \ 955 .timeouts = (_timeouts), \ 956 .burst_reg_cmd = ADIS16400_GLOB_CMD, \ 957 .burst_len = (_burst_len), \ 958 .burst_max_speed_hz = ADIS16400_SPI_BURST \ 959 } 960 961 static const struct adis_timeout adis16300_timeouts = { 962 .reset_ms = ADIS16400_STARTUP_DELAY, 963 .sw_reset_ms = ADIS16400_STARTUP_DELAY, 964 .self_test_ms = ADIS16400_STARTUP_DELAY, 965 }; 966 967 static const struct adis_timeout adis16334_timeouts = { 968 .reset_ms = 60, 969 .sw_reset_ms = 60, 970 .self_test_ms = 14, 971 }; 972 973 static const struct adis_timeout adis16362_timeouts = { 974 .reset_ms = 130, 975 .sw_reset_ms = 130, 976 .self_test_ms = 12, 977 }; 978 979 static const struct adis_timeout adis16400_timeouts = { 980 .reset_ms = 170, 981 .sw_reset_ms = 170, 982 .self_test_ms = 12, 983 }; 984 985 static const struct adis_timeout adis16445_timeouts = { 986 .reset_ms = 55, 987 .sw_reset_ms = 55, 988 .self_test_ms = 16, 989 }; 990 991 static const struct adis_timeout adis16448_timeouts = { 992 .reset_ms = 90, 993 .sw_reset_ms = 90, 994 .self_test_ms = 45, 995 }; 996 997 static struct adis16400_chip_info adis16400_chips[] = { 998 [ADIS16300] = { 999 .channels = adis16300_channels, 1000 .num_channels = ARRAY_SIZE(adis16300_channels), 1001 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE | 1002 ADIS16400_HAS_SERIAL_NUMBER, 1003 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */ 1004 .accel_scale_micro = 5884, 1005 .temp_scale_nano = 140000000, /* 0.14 C */ 1006 .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */ 1007 .set_freq = adis16400_set_freq, 1008 .get_freq = adis16400_get_freq, 1009 .adis_data = ADIS16400_DATA(&adis16300_timeouts, 18), 1010 }, 1011 [ADIS16334] = { 1012 .channels = adis16334_channels, 1013 .num_channels = ARRAY_SIZE(adis16334_channels), 1014 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST | 1015 ADIS16400_HAS_SERIAL_NUMBER, 1016 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */ 1017 .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */ 1018 .temp_scale_nano = 67850000, /* 0.06785 C */ 1019 .temp_offset = 25000000 / 67850, /* 25 C = 0x00 */ 1020 .set_freq = adis16334_set_freq, 1021 .get_freq = adis16334_get_freq, 1022 .adis_data = ADIS16400_DATA(&adis16334_timeouts, 0), 1023 }, 1024 [ADIS16350] = { 1025 .channels = adis16350_channels, 1026 .num_channels = ARRAY_SIZE(adis16350_channels), 1027 .gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */ 1028 .accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */ 1029 .temp_scale_nano = 145300000, /* 0.1453 C */ 1030 .temp_offset = 25000000 / 145300, /* 25 C = 0x00 */ 1031 .flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE, 1032 .set_freq = adis16400_set_freq, 1033 .get_freq = adis16400_get_freq, 1034 .adis_data = ADIS16400_DATA(&adis16300_timeouts, 0), 1035 }, 1036 [ADIS16360] = { 1037 .channels = adis16350_channels, 1038 .num_channels = ARRAY_SIZE(adis16350_channels), 1039 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE | 1040 ADIS16400_HAS_SERIAL_NUMBER, 1041 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */ 1042 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */ 1043 .temp_scale_nano = 136000000, /* 0.136 C */ 1044 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */ 1045 .set_freq = adis16400_set_freq, 1046 .get_freq = adis16400_get_freq, 1047 .adis_data = ADIS16400_DATA(&adis16300_timeouts, 28), 1048 }, 1049 [ADIS16362] = { 1050 .channels = adis16350_channels, 1051 .num_channels = ARRAY_SIZE(adis16350_channels), 1052 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE | 1053 ADIS16400_HAS_SERIAL_NUMBER, 1054 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */ 1055 .accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */ 1056 .temp_scale_nano = 136000000, /* 0.136 C */ 1057 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */ 1058 .set_freq = adis16400_set_freq, 1059 .get_freq = adis16400_get_freq, 1060 .adis_data = ADIS16400_DATA(&adis16362_timeouts, 28), 1061 }, 1062 [ADIS16364] = { 1063 .channels = adis16350_channels, 1064 .num_channels = ARRAY_SIZE(adis16350_channels), 1065 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE | 1066 ADIS16400_HAS_SERIAL_NUMBER, 1067 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */ 1068 .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */ 1069 .temp_scale_nano = 136000000, /* 0.136 C */ 1070 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */ 1071 .set_freq = adis16400_set_freq, 1072 .get_freq = adis16400_get_freq, 1073 .adis_data = ADIS16400_DATA(&adis16362_timeouts, 28), 1074 }, 1075 [ADIS16367] = { 1076 .channels = adis16350_channels, 1077 .num_channels = ARRAY_SIZE(adis16350_channels), 1078 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE | 1079 ADIS16400_HAS_SERIAL_NUMBER, 1080 .gyro_scale_micro = IIO_DEGREE_TO_RAD(2000), /* 0.2 deg/s */ 1081 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */ 1082 .temp_scale_nano = 136000000, /* 0.136 C */ 1083 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */ 1084 .set_freq = adis16400_set_freq, 1085 .get_freq = adis16400_get_freq, 1086 .adis_data = ADIS16400_DATA(&adis16300_timeouts, 28), 1087 }, 1088 [ADIS16400] = { 1089 .channels = adis16400_channels, 1090 .num_channels = ARRAY_SIZE(adis16400_channels), 1091 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE, 1092 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */ 1093 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */ 1094 .temp_scale_nano = 140000000, /* 0.14 C */ 1095 .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */ 1096 .set_freq = adis16400_set_freq, 1097 .get_freq = adis16400_get_freq, 1098 .adis_data = ADIS16400_DATA(&adis16400_timeouts, 24), 1099 }, 1100 [ADIS16445] = { 1101 .channels = adis16445_channels, 1102 .num_channels = ARRAY_SIZE(adis16445_channels), 1103 .flags = ADIS16400_HAS_PROD_ID | 1104 ADIS16400_HAS_SERIAL_NUMBER | 1105 ADIS16400_BURST_DIAG_STAT, 1106 .gyro_scale_micro = IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */ 1107 .accel_scale_micro = IIO_G_TO_M_S_2(250), /* 1/4000 g */ 1108 .temp_scale_nano = 73860000, /* 0.07386 C */ 1109 .temp_offset = 31000000 / 73860, /* 31 C = 0x00 */ 1110 .set_freq = adis16334_set_freq, 1111 .get_freq = adis16334_get_freq, 1112 .adis_data = ADIS16400_DATA(&adis16445_timeouts, 16), 1113 }, 1114 [ADIS16448] = { 1115 .channels = adis16448_channels, 1116 .num_channels = ARRAY_SIZE(adis16448_channels), 1117 .flags = ADIS16400_HAS_PROD_ID | 1118 ADIS16400_HAS_SERIAL_NUMBER | 1119 ADIS16400_BURST_DIAG_STAT, 1120 .gyro_scale_micro = IIO_DEGREE_TO_RAD(40000), /* 0.04 deg/s */ 1121 .accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */ 1122 .temp_scale_nano = 73860000, /* 0.07386 C */ 1123 .temp_offset = 31000000 / 73860, /* 31 C = 0x00 */ 1124 .set_freq = adis16334_set_freq, 1125 .get_freq = adis16334_get_freq, 1126 .adis_data = ADIS16400_DATA(&adis16448_timeouts, 24), 1127 } 1128 }; 1129 1130 static const struct iio_info adis16400_info = { 1131 .read_raw = &adis16400_read_raw, 1132 .write_raw = &adis16400_write_raw, 1133 .update_scan_mode = adis_update_scan_mode, 1134 .debugfs_reg_access = adis_debugfs_reg_access, 1135 }; 1136 1137 static void adis16400_setup_chan_mask(struct adis16400_state *st) 1138 { 1139 const struct adis16400_chip_info *chip_info = st->variant; 1140 unsigned int i; 1141 1142 for (i = 0; i < chip_info->num_channels; i++) { 1143 const struct iio_chan_spec *ch = &chip_info->channels[i]; 1144 1145 if (ch->scan_index >= 0 && 1146 ch->scan_index != ADIS16400_SCAN_TIMESTAMP) 1147 st->avail_scan_mask[0] |= BIT(ch->scan_index); 1148 } 1149 } 1150 1151 static void adis16400_stop(void *data) 1152 { 1153 adis16400_stop_device(data); 1154 } 1155 1156 static int adis16400_probe(struct spi_device *spi) 1157 { 1158 struct adis16400_state *st; 1159 struct iio_dev *indio_dev; 1160 int ret; 1161 const struct adis_data *adis16400_data; 1162 1163 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 1164 if (indio_dev == NULL) 1165 return -ENOMEM; 1166 1167 st = iio_priv(indio_dev); 1168 1169 /* setup the industrialio driver allocated elements */ 1170 st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data]; 1171 indio_dev->name = spi_get_device_id(spi)->name; 1172 indio_dev->channels = st->variant->channels; 1173 indio_dev->num_channels = st->variant->num_channels; 1174 indio_dev->info = &adis16400_info; 1175 indio_dev->modes = INDIO_DIRECT_MODE; 1176 1177 if (!(st->variant->flags & ADIS16400_NO_BURST)) { 1178 adis16400_setup_chan_mask(st); 1179 indio_dev->available_scan_masks = st->avail_scan_mask; 1180 } 1181 1182 adis16400_data = &st->variant->adis_data; 1183 1184 ret = adis_init(&st->adis, indio_dev, spi, adis16400_data); 1185 if (ret) 1186 return ret; 1187 1188 ret = devm_adis_setup_buffer_and_trigger(&st->adis, indio_dev, adis16400_trigger_handler); 1189 if (ret) 1190 return ret; 1191 1192 /* Get the device into a sane initial state */ 1193 ret = adis16400_initial_setup(indio_dev); 1194 if (ret) 1195 return ret; 1196 1197 ret = devm_add_action_or_reset(&spi->dev, adis16400_stop, indio_dev); 1198 if (ret) 1199 return ret; 1200 1201 ret = devm_iio_device_register(&spi->dev, indio_dev); 1202 if (ret) 1203 return ret; 1204 1205 adis16400_debugfs_init(indio_dev); 1206 return 0; 1207 } 1208 1209 static const struct spi_device_id adis16400_id[] = { 1210 {"adis16300", ADIS16300}, 1211 {"adis16305", ADIS16300}, 1212 {"adis16334", ADIS16334}, 1213 {"adis16350", ADIS16350}, 1214 {"adis16354", ADIS16350}, 1215 {"adis16355", ADIS16350}, 1216 {"adis16360", ADIS16360}, 1217 {"adis16362", ADIS16362}, 1218 {"adis16364", ADIS16364}, 1219 {"adis16365", ADIS16360}, 1220 {"adis16367", ADIS16367}, 1221 {"adis16400", ADIS16400}, 1222 {"adis16405", ADIS16400}, 1223 {"adis16445", ADIS16445}, 1224 {"adis16448", ADIS16448}, 1225 {} 1226 }; 1227 MODULE_DEVICE_TABLE(spi, adis16400_id); 1228 1229 static struct spi_driver adis16400_driver = { 1230 .driver = { 1231 .name = "adis16400", 1232 }, 1233 .id_table = adis16400_id, 1234 .probe = adis16400_probe, 1235 }; 1236 module_spi_driver(adis16400_driver); 1237 1238 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>"); 1239 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver"); 1240 MODULE_LICENSE("GPL v2"); 1241 MODULE_IMPORT_NS(IIO_ADISLIB); 1242