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 st->adis.spi->chip_select, 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 ret, sps; 501 502 switch (info) { 503 case IIO_CHAN_INFO_CALIBBIAS: 504 ret = adis_write_reg_16(&st->adis, 505 adis16400_addresses[chan->scan_index], val); 506 return ret; 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_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 adis_dev_unlock(&st->adis); 518 return sps; 519 } 520 521 ret = __adis16400_set_filter(indio_dev, sps, 522 val * 1000 + val2 / 1000); 523 adis_dev_unlock(&st->adis); 524 return ret; 525 case IIO_CHAN_INFO_SAMP_FREQ: 526 sps = val * 1000 + val2 / 1000; 527 528 if (sps <= 0) 529 return -EINVAL; 530 531 adis_dev_lock(&st->adis); 532 ret = st->variant->set_freq(st, sps); 533 adis_dev_unlock(&st->adis); 534 return ret; 535 default: 536 return -EINVAL; 537 } 538 } 539 540 static int adis16400_read_raw(struct iio_dev *indio_dev, 541 struct iio_chan_spec const *chan, int *val, int *val2, long info) 542 { 543 struct adis16400_state *st = iio_priv(indio_dev); 544 int16_t val16; 545 int ret; 546 547 switch (info) { 548 case IIO_CHAN_INFO_RAW: 549 return adis_single_conversion(indio_dev, chan, 0, val); 550 case IIO_CHAN_INFO_SCALE: 551 switch (chan->type) { 552 case IIO_ANGL_VEL: 553 *val = 0; 554 *val2 = st->variant->gyro_scale_micro; 555 return IIO_VAL_INT_PLUS_MICRO; 556 case IIO_VOLTAGE: 557 *val = 0; 558 if (chan->channel == 0) { 559 *val = 2; 560 *val2 = 418000; /* 2.418 mV */ 561 } else { 562 *val = 0; 563 *val2 = 805800; /* 805.8 uV */ 564 } 565 return IIO_VAL_INT_PLUS_MICRO; 566 case IIO_ACCEL: 567 *val = 0; 568 *val2 = st->variant->accel_scale_micro; 569 return IIO_VAL_INT_PLUS_MICRO; 570 case IIO_MAGN: 571 *val = 0; 572 *val2 = 500; /* 0.5 mgauss */ 573 return IIO_VAL_INT_PLUS_MICRO; 574 case IIO_TEMP: 575 *val = st->variant->temp_scale_nano / 1000000; 576 *val2 = (st->variant->temp_scale_nano % 1000000); 577 return IIO_VAL_INT_PLUS_MICRO; 578 case IIO_PRESSURE: 579 /* 20 uBar = 0.002kPascal */ 580 *val = 0; 581 *val2 = 2000; 582 return IIO_VAL_INT_PLUS_MICRO; 583 default: 584 return -EINVAL; 585 } 586 case IIO_CHAN_INFO_CALIBBIAS: 587 ret = adis_read_reg_16(&st->adis, 588 adis16400_addresses[chan->scan_index], &val16); 589 if (ret) 590 return ret; 591 val16 = sign_extend32(val16, 11); 592 *val = val16; 593 return IIO_VAL_INT; 594 case IIO_CHAN_INFO_OFFSET: 595 /* currently only temperature */ 596 *val = st->variant->temp_offset; 597 return IIO_VAL_INT; 598 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 599 adis_dev_lock(&st->adis); 600 /* Need both the number of taps and the sampling frequency */ 601 ret = __adis_read_reg_16(&st->adis, 602 ADIS16400_SENS_AVG, 603 &val16); 604 if (ret) { 605 adis_dev_unlock(&st->adis); 606 return ret; 607 } 608 ret = st->variant->get_freq(st); 609 adis_dev_unlock(&st->adis); 610 if (ret) 611 return ret; 612 ret /= adis16400_3db_divisors[val16 & 0x07]; 613 *val = ret / 1000; 614 *val2 = (ret % 1000) * 1000; 615 return IIO_VAL_INT_PLUS_MICRO; 616 case IIO_CHAN_INFO_SAMP_FREQ: 617 adis_dev_lock(&st->adis); 618 ret = st->variant->get_freq(st); 619 adis_dev_unlock(&st->adis); 620 if (ret) 621 return ret; 622 *val = ret / 1000; 623 *val2 = (ret % 1000) * 1000; 624 return IIO_VAL_INT_PLUS_MICRO; 625 default: 626 return -EINVAL; 627 } 628 } 629 630 #if IS_ENABLED(CONFIG_IIO_BUFFER) 631 static irqreturn_t adis16400_trigger_handler(int irq, void *p) 632 { 633 struct iio_poll_func *pf = p; 634 struct iio_dev *indio_dev = pf->indio_dev; 635 struct adis16400_state *st = iio_priv(indio_dev); 636 struct adis *adis = &st->adis; 637 void *buffer; 638 int ret; 639 640 ret = spi_sync(adis->spi, &adis->msg); 641 if (ret) 642 dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret); 643 644 if (st->variant->flags & ADIS16400_BURST_DIAG_STAT) 645 buffer = adis->buffer + sizeof(u16); 646 else 647 buffer = adis->buffer; 648 649 iio_push_to_buffers_with_timestamp(indio_dev, buffer, 650 pf->timestamp); 651 652 iio_trigger_notify_done(indio_dev->trig); 653 654 return IRQ_HANDLED; 655 } 656 #else 657 #define adis16400_trigger_handler NULL 658 #endif /* IS_ENABLED(CONFIG_IIO_BUFFER) */ 659 660 #define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si, chn) { \ 661 .type = IIO_VOLTAGE, \ 662 .indexed = 1, \ 663 .channel = chn, \ 664 .extend_name = name, \ 665 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 666 BIT(IIO_CHAN_INFO_SCALE), \ 667 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 668 .address = (addr), \ 669 .scan_index = (si), \ 670 .scan_type = { \ 671 .sign = 'u', \ 672 .realbits = (bits), \ 673 .storagebits = 16, \ 674 .shift = 0, \ 675 .endianness = IIO_BE, \ 676 }, \ 677 } 678 679 #define ADIS16400_SUPPLY_CHAN(addr, bits) \ 680 ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY, 0) 681 682 #define ADIS16400_AUX_ADC_CHAN(addr, bits) \ 683 ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC, 1) 684 685 #define ADIS16400_GYRO_CHAN(mod, addr, bits) { \ 686 .type = IIO_ANGL_VEL, \ 687 .modified = 1, \ 688 .channel2 = IIO_MOD_ ## mod, \ 689 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 690 BIT(IIO_CHAN_INFO_CALIBBIAS), \ 691 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 692 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 693 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 694 .address = addr, \ 695 .scan_index = ADIS16400_SCAN_GYRO_ ## mod, \ 696 .scan_type = { \ 697 .sign = 's', \ 698 .realbits = (bits), \ 699 .storagebits = 16, \ 700 .shift = 0, \ 701 .endianness = IIO_BE, \ 702 }, \ 703 } 704 705 #define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \ 706 .type = IIO_ACCEL, \ 707 .modified = 1, \ 708 .channel2 = IIO_MOD_ ## mod, \ 709 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 710 BIT(IIO_CHAN_INFO_CALIBBIAS), \ 711 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 712 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 713 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 714 .address = (addr), \ 715 .scan_index = ADIS16400_SCAN_ACC_ ## mod, \ 716 .scan_type = { \ 717 .sign = 's', \ 718 .realbits = (bits), \ 719 .storagebits = 16, \ 720 .shift = 0, \ 721 .endianness = IIO_BE, \ 722 }, \ 723 } 724 725 #define ADIS16400_MAGN_CHAN(mod, addr, bits) { \ 726 .type = IIO_MAGN, \ 727 .modified = 1, \ 728 .channel2 = IIO_MOD_ ## mod, \ 729 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 730 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 731 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 732 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 733 .address = (addr), \ 734 .scan_index = ADIS16400_SCAN_MAGN_ ## mod, \ 735 .scan_type = { \ 736 .sign = 's', \ 737 .realbits = (bits), \ 738 .storagebits = 16, \ 739 .shift = 0, \ 740 .endianness = IIO_BE, \ 741 }, \ 742 } 743 744 #define ADIS16400_MOD_TEMP_NAME_X "x" 745 #define ADIS16400_MOD_TEMP_NAME_Y "y" 746 #define ADIS16400_MOD_TEMP_NAME_Z "z" 747 748 #define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \ 749 .type = IIO_TEMP, \ 750 .indexed = 1, \ 751 .channel = 0, \ 752 .extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \ 753 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 754 BIT(IIO_CHAN_INFO_OFFSET) | \ 755 BIT(IIO_CHAN_INFO_SCALE), \ 756 .info_mask_shared_by_type = \ 757 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 758 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 759 .address = (addr), \ 760 .scan_index = ADIS16350_SCAN_TEMP_ ## mod, \ 761 .scan_type = { \ 762 .sign = 's', \ 763 .realbits = (bits), \ 764 .storagebits = 16, \ 765 .shift = 0, \ 766 .endianness = IIO_BE, \ 767 }, \ 768 } 769 770 #define ADIS16400_TEMP_CHAN(addr, bits) { \ 771 .type = IIO_TEMP, \ 772 .indexed = 1, \ 773 .channel = 0, \ 774 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 775 BIT(IIO_CHAN_INFO_OFFSET) | \ 776 BIT(IIO_CHAN_INFO_SCALE), \ 777 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 778 .address = (addr), \ 779 .scan_index = ADIS16350_SCAN_TEMP_X, \ 780 .scan_type = { \ 781 .sign = 's', \ 782 .realbits = (bits), \ 783 .storagebits = 16, \ 784 .shift = 0, \ 785 .endianness = IIO_BE, \ 786 }, \ 787 } 788 789 #define ADIS16400_INCLI_CHAN(mod, addr, bits) { \ 790 .type = IIO_INCLI, \ 791 .modified = 1, \ 792 .channel2 = IIO_MOD_ ## mod, \ 793 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 794 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 795 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 796 .address = (addr), \ 797 .scan_index = ADIS16300_SCAN_INCLI_ ## mod, \ 798 .scan_type = { \ 799 .sign = 's', \ 800 .realbits = (bits), \ 801 .storagebits = 16, \ 802 .shift = 0, \ 803 .endianness = IIO_BE, \ 804 }, \ 805 } 806 807 static const struct iio_chan_spec adis16400_channels[] = { 808 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 14), 809 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14), 810 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14), 811 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14), 812 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14), 813 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14), 814 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14), 815 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14), 816 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14), 817 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14), 818 ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT, 12), 819 ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC, 12), 820 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 821 }; 822 823 static const struct iio_chan_spec adis16445_channels[] = { 824 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16), 825 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16), 826 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16), 827 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16), 828 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16), 829 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16), 830 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12), 831 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 832 }; 833 834 static const struct iio_chan_spec adis16448_channels[] = { 835 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16), 836 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16), 837 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16), 838 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16), 839 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16), 840 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16), 841 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 16), 842 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 16), 843 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 16), 844 { 845 .type = IIO_PRESSURE, 846 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 847 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 848 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 849 .address = ADIS16448_BARO_OUT, 850 .scan_index = ADIS16400_SCAN_BARO, 851 .scan_type = { 852 .sign = 's', 853 .realbits = 16, 854 .storagebits = 16, 855 .endianness = IIO_BE, 856 }, 857 }, 858 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12), 859 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 860 }; 861 862 static const struct iio_chan_spec adis16350_channels[] = { 863 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12), 864 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14), 865 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14), 866 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14), 867 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14), 868 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14), 869 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14), 870 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14), 871 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14), 872 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14), 873 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12), 874 ADIS16400_MOD_TEMP_CHAN(X, ADIS16350_XTEMP_OUT, 12), 875 ADIS16400_MOD_TEMP_CHAN(Y, ADIS16350_YTEMP_OUT, 12), 876 ADIS16400_MOD_TEMP_CHAN(Z, ADIS16350_ZTEMP_OUT, 12), 877 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 878 }; 879 880 static const struct iio_chan_spec adis16300_channels[] = { 881 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12), 882 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14), 883 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14), 884 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14), 885 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14), 886 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12), 887 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12), 888 ADIS16400_INCLI_CHAN(X, ADIS16300_PITCH_OUT, 13), 889 ADIS16400_INCLI_CHAN(Y, ADIS16300_ROLL_OUT, 13), 890 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 891 }; 892 893 static const struct iio_chan_spec adis16334_channels[] = { 894 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14), 895 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14), 896 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14), 897 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14), 898 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14), 899 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14), 900 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12), 901 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 902 }; 903 904 static const char * const adis16400_status_error_msgs[] = { 905 [ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure", 906 [ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure", 907 [ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure", 908 [ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure", 909 [ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure", 910 [ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure", 911 [ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active", 912 [ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active", 913 [ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error", 914 [ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error", 915 [ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange", 916 [ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure", 917 [ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed", 918 [ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V", 919 [ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V", 920 }; 921 922 #define ADIS16400_DATA(_timeouts, _burst_len) \ 923 { \ 924 .msc_ctrl_reg = ADIS16400_MSC_CTRL, \ 925 .glob_cmd_reg = ADIS16400_GLOB_CMD, \ 926 .diag_stat_reg = ADIS16400_DIAG_STAT, \ 927 .read_delay = 50, \ 928 .write_delay = 50, \ 929 .self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST, \ 930 .self_test_reg = ADIS16400_MSC_CTRL, \ 931 .status_error_msgs = adis16400_status_error_msgs, \ 932 .status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) | \ 933 BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) | \ 934 BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) | \ 935 BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) | \ 936 BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) | \ 937 BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) | \ 938 BIT(ADIS16400_DIAG_STAT_ALARM2) | \ 939 BIT(ADIS16400_DIAG_STAT_ALARM1) | \ 940 BIT(ADIS16400_DIAG_STAT_FLASH_CHK) | \ 941 BIT(ADIS16400_DIAG_STAT_SELF_TEST) | \ 942 BIT(ADIS16400_DIAG_STAT_OVERFLOW) | \ 943 BIT(ADIS16400_DIAG_STAT_SPI_FAIL) | \ 944 BIT(ADIS16400_DIAG_STAT_FLASH_UPT) | \ 945 BIT(ADIS16400_DIAG_STAT_POWER_HIGH) | \ 946 BIT(ADIS16400_DIAG_STAT_POWER_LOW), \ 947 .timeouts = (_timeouts), \ 948 .burst_reg_cmd = ADIS16400_GLOB_CMD, \ 949 .burst_len = (_burst_len), \ 950 .burst_max_speed_hz = ADIS16400_SPI_BURST \ 951 } 952 953 static const struct adis_timeout adis16300_timeouts = { 954 .reset_ms = ADIS16400_STARTUP_DELAY, 955 .sw_reset_ms = ADIS16400_STARTUP_DELAY, 956 .self_test_ms = ADIS16400_STARTUP_DELAY, 957 }; 958 959 static const struct adis_timeout adis16334_timeouts = { 960 .reset_ms = 60, 961 .sw_reset_ms = 60, 962 .self_test_ms = 14, 963 }; 964 965 static const struct adis_timeout adis16362_timeouts = { 966 .reset_ms = 130, 967 .sw_reset_ms = 130, 968 .self_test_ms = 12, 969 }; 970 971 static const struct adis_timeout adis16400_timeouts = { 972 .reset_ms = 170, 973 .sw_reset_ms = 170, 974 .self_test_ms = 12, 975 }; 976 977 static const struct adis_timeout adis16445_timeouts = { 978 .reset_ms = 55, 979 .sw_reset_ms = 55, 980 .self_test_ms = 16, 981 }; 982 983 static const struct adis_timeout adis16448_timeouts = { 984 .reset_ms = 90, 985 .sw_reset_ms = 90, 986 .self_test_ms = 45, 987 }; 988 989 static struct adis16400_chip_info adis16400_chips[] = { 990 [ADIS16300] = { 991 .channels = adis16300_channels, 992 .num_channels = ARRAY_SIZE(adis16300_channels), 993 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE | 994 ADIS16400_HAS_SERIAL_NUMBER, 995 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */ 996 .accel_scale_micro = 5884, 997 .temp_scale_nano = 140000000, /* 0.14 C */ 998 .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */ 999 .set_freq = adis16400_set_freq, 1000 .get_freq = adis16400_get_freq, 1001 .adis_data = ADIS16400_DATA(&adis16300_timeouts, 18), 1002 }, 1003 [ADIS16334] = { 1004 .channels = adis16334_channels, 1005 .num_channels = ARRAY_SIZE(adis16334_channels), 1006 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST | 1007 ADIS16400_HAS_SERIAL_NUMBER, 1008 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */ 1009 .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */ 1010 .temp_scale_nano = 67850000, /* 0.06785 C */ 1011 .temp_offset = 25000000 / 67850, /* 25 C = 0x00 */ 1012 .set_freq = adis16334_set_freq, 1013 .get_freq = adis16334_get_freq, 1014 .adis_data = ADIS16400_DATA(&adis16334_timeouts, 0), 1015 }, 1016 [ADIS16350] = { 1017 .channels = adis16350_channels, 1018 .num_channels = ARRAY_SIZE(adis16350_channels), 1019 .gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */ 1020 .accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */ 1021 .temp_scale_nano = 145300000, /* 0.1453 C */ 1022 .temp_offset = 25000000 / 145300, /* 25 C = 0x00 */ 1023 .flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE, 1024 .set_freq = adis16400_set_freq, 1025 .get_freq = adis16400_get_freq, 1026 .adis_data = ADIS16400_DATA(&adis16300_timeouts, 0), 1027 }, 1028 [ADIS16360] = { 1029 .channels = adis16350_channels, 1030 .num_channels = ARRAY_SIZE(adis16350_channels), 1031 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE | 1032 ADIS16400_HAS_SERIAL_NUMBER, 1033 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */ 1034 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */ 1035 .temp_scale_nano = 136000000, /* 0.136 C */ 1036 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */ 1037 .set_freq = adis16400_set_freq, 1038 .get_freq = adis16400_get_freq, 1039 .adis_data = ADIS16400_DATA(&adis16300_timeouts, 28), 1040 }, 1041 [ADIS16362] = { 1042 .channels = adis16350_channels, 1043 .num_channels = ARRAY_SIZE(adis16350_channels), 1044 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE | 1045 ADIS16400_HAS_SERIAL_NUMBER, 1046 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */ 1047 .accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */ 1048 .temp_scale_nano = 136000000, /* 0.136 C */ 1049 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */ 1050 .set_freq = adis16400_set_freq, 1051 .get_freq = adis16400_get_freq, 1052 .adis_data = ADIS16400_DATA(&adis16362_timeouts, 28), 1053 }, 1054 [ADIS16364] = { 1055 .channels = adis16350_channels, 1056 .num_channels = ARRAY_SIZE(adis16350_channels), 1057 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE | 1058 ADIS16400_HAS_SERIAL_NUMBER, 1059 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */ 1060 .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */ 1061 .temp_scale_nano = 136000000, /* 0.136 C */ 1062 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */ 1063 .set_freq = adis16400_set_freq, 1064 .get_freq = adis16400_get_freq, 1065 .adis_data = ADIS16400_DATA(&adis16362_timeouts, 28), 1066 }, 1067 [ADIS16367] = { 1068 .channels = adis16350_channels, 1069 .num_channels = ARRAY_SIZE(adis16350_channels), 1070 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE | 1071 ADIS16400_HAS_SERIAL_NUMBER, 1072 .gyro_scale_micro = IIO_DEGREE_TO_RAD(2000), /* 0.2 deg/s */ 1073 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */ 1074 .temp_scale_nano = 136000000, /* 0.136 C */ 1075 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */ 1076 .set_freq = adis16400_set_freq, 1077 .get_freq = adis16400_get_freq, 1078 .adis_data = ADIS16400_DATA(&adis16300_timeouts, 28), 1079 }, 1080 [ADIS16400] = { 1081 .channels = adis16400_channels, 1082 .num_channels = ARRAY_SIZE(adis16400_channels), 1083 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE, 1084 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */ 1085 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */ 1086 .temp_scale_nano = 140000000, /* 0.14 C */ 1087 .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */ 1088 .set_freq = adis16400_set_freq, 1089 .get_freq = adis16400_get_freq, 1090 .adis_data = ADIS16400_DATA(&adis16400_timeouts, 24), 1091 }, 1092 [ADIS16445] = { 1093 .channels = adis16445_channels, 1094 .num_channels = ARRAY_SIZE(adis16445_channels), 1095 .flags = ADIS16400_HAS_PROD_ID | 1096 ADIS16400_HAS_SERIAL_NUMBER | 1097 ADIS16400_BURST_DIAG_STAT, 1098 .gyro_scale_micro = IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */ 1099 .accel_scale_micro = IIO_G_TO_M_S_2(250), /* 1/4000 g */ 1100 .temp_scale_nano = 73860000, /* 0.07386 C */ 1101 .temp_offset = 31000000 / 73860, /* 31 C = 0x00 */ 1102 .set_freq = adis16334_set_freq, 1103 .get_freq = adis16334_get_freq, 1104 .adis_data = ADIS16400_DATA(&adis16445_timeouts, 16), 1105 }, 1106 [ADIS16448] = { 1107 .channels = adis16448_channels, 1108 .num_channels = ARRAY_SIZE(adis16448_channels), 1109 .flags = ADIS16400_HAS_PROD_ID | 1110 ADIS16400_HAS_SERIAL_NUMBER | 1111 ADIS16400_BURST_DIAG_STAT, 1112 .gyro_scale_micro = IIO_DEGREE_TO_RAD(40000), /* 0.04 deg/s */ 1113 .accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */ 1114 .temp_scale_nano = 73860000, /* 0.07386 C */ 1115 .temp_offset = 31000000 / 73860, /* 31 C = 0x00 */ 1116 .set_freq = adis16334_set_freq, 1117 .get_freq = adis16334_get_freq, 1118 .adis_data = ADIS16400_DATA(&adis16448_timeouts, 24), 1119 } 1120 }; 1121 1122 static const struct iio_info adis16400_info = { 1123 .read_raw = &adis16400_read_raw, 1124 .write_raw = &adis16400_write_raw, 1125 .update_scan_mode = adis_update_scan_mode, 1126 .debugfs_reg_access = adis_debugfs_reg_access, 1127 }; 1128 1129 static void adis16400_setup_chan_mask(struct adis16400_state *st) 1130 { 1131 const struct adis16400_chip_info *chip_info = st->variant; 1132 unsigned int i; 1133 1134 for (i = 0; i < chip_info->num_channels; i++) { 1135 const struct iio_chan_spec *ch = &chip_info->channels[i]; 1136 1137 if (ch->scan_index >= 0 && 1138 ch->scan_index != ADIS16400_SCAN_TIMESTAMP) 1139 st->avail_scan_mask[0] |= BIT(ch->scan_index); 1140 } 1141 } 1142 1143 static void adis16400_stop(void *data) 1144 { 1145 adis16400_stop_device(data); 1146 } 1147 1148 static int adis16400_probe(struct spi_device *spi) 1149 { 1150 struct adis16400_state *st; 1151 struct iio_dev *indio_dev; 1152 int ret; 1153 const struct adis_data *adis16400_data; 1154 1155 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 1156 if (indio_dev == NULL) 1157 return -ENOMEM; 1158 1159 st = iio_priv(indio_dev); 1160 1161 /* setup the industrialio driver allocated elements */ 1162 st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data]; 1163 indio_dev->name = spi_get_device_id(spi)->name; 1164 indio_dev->channels = st->variant->channels; 1165 indio_dev->num_channels = st->variant->num_channels; 1166 indio_dev->info = &adis16400_info; 1167 indio_dev->modes = INDIO_DIRECT_MODE; 1168 1169 if (!(st->variant->flags & ADIS16400_NO_BURST)) { 1170 adis16400_setup_chan_mask(st); 1171 indio_dev->available_scan_masks = st->avail_scan_mask; 1172 } 1173 1174 adis16400_data = &st->variant->adis_data; 1175 1176 ret = adis_init(&st->adis, indio_dev, spi, adis16400_data); 1177 if (ret) 1178 return ret; 1179 1180 ret = devm_adis_setup_buffer_and_trigger(&st->adis, indio_dev, adis16400_trigger_handler); 1181 if (ret) 1182 return ret; 1183 1184 /* Get the device into a sane initial state */ 1185 ret = adis16400_initial_setup(indio_dev); 1186 if (ret) 1187 return ret; 1188 1189 ret = devm_add_action_or_reset(&spi->dev, adis16400_stop, indio_dev); 1190 if (ret) 1191 return ret; 1192 1193 ret = devm_iio_device_register(&spi->dev, indio_dev); 1194 if (ret) 1195 return ret; 1196 1197 adis16400_debugfs_init(indio_dev); 1198 return 0; 1199 } 1200 1201 static const struct spi_device_id adis16400_id[] = { 1202 {"adis16300", ADIS16300}, 1203 {"adis16305", ADIS16300}, 1204 {"adis16334", ADIS16334}, 1205 {"adis16350", ADIS16350}, 1206 {"adis16354", ADIS16350}, 1207 {"adis16355", ADIS16350}, 1208 {"adis16360", ADIS16360}, 1209 {"adis16362", ADIS16362}, 1210 {"adis16364", ADIS16364}, 1211 {"adis16365", ADIS16360}, 1212 {"adis16367", ADIS16367}, 1213 {"adis16400", ADIS16400}, 1214 {"adis16405", ADIS16400}, 1215 {"adis16445", ADIS16445}, 1216 {"adis16448", ADIS16448}, 1217 {} 1218 }; 1219 MODULE_DEVICE_TABLE(spi, adis16400_id); 1220 1221 static struct spi_driver adis16400_driver = { 1222 .driver = { 1223 .name = "adis16400", 1224 }, 1225 .id_table = adis16400_id, 1226 .probe = adis16400_probe, 1227 }; 1228 module_spi_driver(adis16400_driver); 1229 1230 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>"); 1231 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver"); 1232 MODULE_LICENSE("GPL v2"); 1233