1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ADIS16133/ADIS16135/ADIS16136 gyroscope driver 4 * 5 * Copyright 2012 Analog Devices Inc. 6 * Author: Lars-Peter Clausen <lars@metafoo.de> 7 */ 8 9 #include <linux/interrupt.h> 10 #include <linux/delay.h> 11 #include <linux/mutex.h> 12 #include <linux/device.h> 13 #include <linux/kernel.h> 14 #include <linux/spi/spi.h> 15 #include <linux/slab.h> 16 #include <linux/sysfs.h> 17 #include <linux/module.h> 18 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 #include <linux/debugfs.h> 25 26 #define ADIS16136_REG_FLASH_CNT 0x00 27 #define ADIS16136_REG_TEMP_OUT 0x02 28 #define ADIS16136_REG_GYRO_OUT2 0x04 29 #define ADIS16136_REG_GYRO_OUT 0x06 30 #define ADIS16136_REG_GYRO_OFF2 0x08 31 #define ADIS16136_REG_GYRO_OFF 0x0A 32 #define ADIS16136_REG_ALM_MAG1 0x10 33 #define ADIS16136_REG_ALM_MAG2 0x12 34 #define ADIS16136_REG_ALM_SAMPL1 0x14 35 #define ADIS16136_REG_ALM_SAMPL2 0x16 36 #define ADIS16136_REG_ALM_CTRL 0x18 37 #define ADIS16136_REG_GPIO_CTRL 0x1A 38 #define ADIS16136_REG_MSC_CTRL 0x1C 39 #define ADIS16136_REG_SMPL_PRD 0x1E 40 #define ADIS16136_REG_AVG_CNT 0x20 41 #define ADIS16136_REG_DEC_RATE 0x22 42 #define ADIS16136_REG_SLP_CTRL 0x24 43 #define ADIS16136_REG_DIAG_STAT 0x26 44 #define ADIS16136_REG_GLOB_CMD 0x28 45 #define ADIS16136_REG_LOT1 0x32 46 #define ADIS16136_REG_LOT2 0x34 47 #define ADIS16136_REG_LOT3 0x36 48 #define ADIS16136_REG_PROD_ID 0x38 49 #define ADIS16136_REG_SERIAL_NUM 0x3A 50 51 #define ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL 2 52 #define ADIS16136_DIAG_STAT_SPI_FAIL 3 53 #define ADIS16136_DIAG_STAT_SELF_TEST_FAIL 5 54 #define ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL 6 55 56 #define ADIS16136_MSC_CTRL_MEMORY_TEST BIT(11) 57 #define ADIS16136_MSC_CTRL_SELF_TEST BIT(10) 58 59 struct adis16136_chip_info { 60 unsigned int precision; 61 unsigned int fullscale; 62 const struct adis_data adis_data; 63 }; 64 65 struct adis16136 { 66 const struct adis16136_chip_info *chip_info; 67 68 struct adis adis; 69 }; 70 71 #ifdef CONFIG_DEBUG_FS 72 73 static ssize_t adis16136_show_serial(struct file *file, 74 char __user *userbuf, size_t count, loff_t *ppos) 75 { 76 struct adis16136 *adis16136 = file->private_data; 77 uint16_t lot1, lot2, lot3, serial; 78 char buf[20]; 79 size_t len; 80 int ret; 81 82 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_SERIAL_NUM, 83 &serial); 84 if (ret) 85 return ret; 86 87 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT1, &lot1); 88 if (ret) 89 return ret; 90 91 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT2, &lot2); 92 if (ret) 93 return ret; 94 95 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT3, &lot3); 96 if (ret) 97 return ret; 98 99 len = snprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2, 100 lot3, serial); 101 102 return simple_read_from_buffer(userbuf, count, ppos, buf, len); 103 } 104 105 static const struct file_operations adis16136_serial_fops = { 106 .open = simple_open, 107 .read = adis16136_show_serial, 108 .llseek = default_llseek, 109 .owner = THIS_MODULE, 110 }; 111 112 static int adis16136_show_product_id(void *arg, u64 *val) 113 { 114 struct adis16136 *adis16136 = arg; 115 u16 prod_id; 116 int ret; 117 118 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_PROD_ID, 119 &prod_id); 120 if (ret) 121 return ret; 122 123 *val = prod_id; 124 125 return 0; 126 } 127 DEFINE_DEBUGFS_ATTRIBUTE(adis16136_product_id_fops, 128 adis16136_show_product_id, NULL, "%llu\n"); 129 130 static int adis16136_show_flash_count(void *arg, u64 *val) 131 { 132 struct adis16136 *adis16136 = arg; 133 uint16_t flash_count; 134 int ret; 135 136 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_FLASH_CNT, 137 &flash_count); 138 if (ret) 139 return ret; 140 141 *val = flash_count; 142 143 return 0; 144 } 145 DEFINE_DEBUGFS_ATTRIBUTE(adis16136_flash_count_fops, 146 adis16136_show_flash_count, NULL, "%lld\n"); 147 148 static int adis16136_debugfs_init(struct iio_dev *indio_dev) 149 { 150 struct adis16136 *adis16136 = iio_priv(indio_dev); 151 struct dentry *d = iio_get_debugfs_dentry(indio_dev); 152 153 debugfs_create_file_unsafe("serial_number", 0400, 154 d, adis16136, &adis16136_serial_fops); 155 debugfs_create_file_unsafe("product_id", 0400, 156 d, adis16136, &adis16136_product_id_fops); 157 debugfs_create_file_unsafe("flash_count", 0400, 158 d, adis16136, &adis16136_flash_count_fops); 159 160 return 0; 161 } 162 163 #else 164 165 static int adis16136_debugfs_init(struct iio_dev *indio_dev) 166 { 167 return 0; 168 } 169 170 #endif 171 172 static int adis16136_set_freq(struct adis16136 *adis16136, unsigned int freq) 173 { 174 unsigned int t; 175 176 t = 32768 / freq; 177 if (t < 0xf) 178 t = 0xf; 179 else if (t > 0xffff) 180 t = 0xffff; 181 else 182 t--; 183 184 return adis_write_reg_16(&adis16136->adis, ADIS16136_REG_SMPL_PRD, t); 185 } 186 187 static int __adis16136_get_freq(struct adis16136 *adis16136, unsigned int *freq) 188 { 189 uint16_t t; 190 int ret; 191 192 ret = __adis_read_reg_16(&adis16136->adis, ADIS16136_REG_SMPL_PRD, &t); 193 if (ret) 194 return ret; 195 196 *freq = 32768 / (t + 1); 197 198 return 0; 199 } 200 201 static ssize_t adis16136_write_frequency(struct device *dev, 202 struct device_attribute *attr, const char *buf, size_t len) 203 { 204 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 205 struct adis16136 *adis16136 = iio_priv(indio_dev); 206 unsigned int val; 207 int ret; 208 209 ret = kstrtouint(buf, 10, &val); 210 if (ret) 211 return ret; 212 213 if (val == 0) 214 return -EINVAL; 215 216 ret = adis16136_set_freq(adis16136, val); 217 218 return ret ? ret : len; 219 } 220 221 static ssize_t adis16136_read_frequency(struct device *dev, 222 struct device_attribute *attr, char *buf) 223 { 224 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 225 struct adis16136 *adis16136 = iio_priv(indio_dev); 226 struct mutex *slock = &adis16136->adis.state_lock; 227 unsigned int freq; 228 int ret; 229 230 mutex_lock(slock); 231 ret = __adis16136_get_freq(adis16136, &freq); 232 mutex_unlock(slock); 233 if (ret) 234 return ret; 235 236 return sprintf(buf, "%d\n", freq); 237 } 238 239 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, 240 adis16136_read_frequency, 241 adis16136_write_frequency); 242 243 static const unsigned adis16136_3db_divisors[] = { 244 [0] = 2, /* Special case */ 245 [1] = 6, 246 [2] = 12, 247 [3] = 25, 248 [4] = 50, 249 [5] = 100, 250 [6] = 200, 251 [7] = 200, /* Not a valid setting */ 252 }; 253 254 static int adis16136_set_filter(struct iio_dev *indio_dev, int val) 255 { 256 struct adis16136 *adis16136 = iio_priv(indio_dev); 257 struct mutex *slock = &adis16136->adis.state_lock; 258 unsigned int freq; 259 int i, ret; 260 261 mutex_lock(slock); 262 ret = __adis16136_get_freq(adis16136, &freq); 263 if (ret) 264 goto out_unlock; 265 266 for (i = ARRAY_SIZE(adis16136_3db_divisors) - 1; i >= 1; i--) { 267 if (freq / adis16136_3db_divisors[i] >= val) 268 break; 269 } 270 271 ret = __adis_write_reg_16(&adis16136->adis, ADIS16136_REG_AVG_CNT, i); 272 out_unlock: 273 mutex_unlock(slock); 274 275 return ret; 276 } 277 278 static int adis16136_get_filter(struct iio_dev *indio_dev, int *val) 279 { 280 struct adis16136 *adis16136 = iio_priv(indio_dev); 281 struct mutex *slock = &adis16136->adis.state_lock; 282 unsigned int freq; 283 uint16_t val16; 284 int ret; 285 286 mutex_lock(slock); 287 288 ret = __adis_read_reg_16(&adis16136->adis, ADIS16136_REG_AVG_CNT, 289 &val16); 290 if (ret) 291 goto err_unlock; 292 293 ret = __adis16136_get_freq(adis16136, &freq); 294 if (ret) 295 goto err_unlock; 296 297 *val = freq / adis16136_3db_divisors[val16 & 0x07]; 298 299 err_unlock: 300 mutex_unlock(slock); 301 302 return ret ? ret : IIO_VAL_INT; 303 } 304 305 static int adis16136_read_raw(struct iio_dev *indio_dev, 306 const struct iio_chan_spec *chan, int *val, int *val2, long info) 307 { 308 struct adis16136 *adis16136 = iio_priv(indio_dev); 309 uint32_t val32; 310 int ret; 311 312 switch (info) { 313 case IIO_CHAN_INFO_RAW: 314 return adis_single_conversion(indio_dev, chan, 0, val); 315 case IIO_CHAN_INFO_SCALE: 316 switch (chan->type) { 317 case IIO_ANGL_VEL: 318 *val = adis16136->chip_info->precision; 319 *val2 = (adis16136->chip_info->fullscale << 16); 320 return IIO_VAL_FRACTIONAL; 321 case IIO_TEMP: 322 *val = 10; 323 *val2 = 697000; /* 0.010697 degree Celsius */ 324 return IIO_VAL_INT_PLUS_MICRO; 325 default: 326 return -EINVAL; 327 } 328 case IIO_CHAN_INFO_CALIBBIAS: 329 ret = adis_read_reg_32(&adis16136->adis, 330 ADIS16136_REG_GYRO_OFF2, &val32); 331 if (ret) 332 return ret; 333 334 *val = sign_extend32(val32, 31); 335 336 return IIO_VAL_INT; 337 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 338 return adis16136_get_filter(indio_dev, val); 339 default: 340 return -EINVAL; 341 } 342 } 343 344 static int adis16136_write_raw(struct iio_dev *indio_dev, 345 const struct iio_chan_spec *chan, int val, int val2, long info) 346 { 347 struct adis16136 *adis16136 = iio_priv(indio_dev); 348 349 switch (info) { 350 case IIO_CHAN_INFO_CALIBBIAS: 351 return adis_write_reg_32(&adis16136->adis, 352 ADIS16136_REG_GYRO_OFF2, val); 353 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 354 return adis16136_set_filter(indio_dev, val); 355 default: 356 break; 357 } 358 359 return -EINVAL; 360 } 361 362 enum { 363 ADIS16136_SCAN_GYRO, 364 ADIS16136_SCAN_TEMP, 365 }; 366 367 static const struct iio_chan_spec adis16136_channels[] = { 368 { 369 .type = IIO_ANGL_VEL, 370 .modified = 1, 371 .channel2 = IIO_MOD_X, 372 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 373 BIT(IIO_CHAN_INFO_CALIBBIAS) | 374 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 375 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 376 377 .address = ADIS16136_REG_GYRO_OUT2, 378 .scan_index = ADIS16136_SCAN_GYRO, 379 .scan_type = { 380 .sign = 's', 381 .realbits = 32, 382 .storagebits = 32, 383 .endianness = IIO_BE, 384 }, 385 }, { 386 .type = IIO_TEMP, 387 .indexed = 1, 388 .channel = 0, 389 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 390 BIT(IIO_CHAN_INFO_SCALE), 391 .address = ADIS16136_REG_TEMP_OUT, 392 .scan_index = ADIS16136_SCAN_TEMP, 393 .scan_type = { 394 .sign = 's', 395 .realbits = 16, 396 .storagebits = 16, 397 .endianness = IIO_BE, 398 }, 399 }, 400 IIO_CHAN_SOFT_TIMESTAMP(2), 401 }; 402 403 static struct attribute *adis16136_attributes[] = { 404 &iio_dev_attr_sampling_frequency.dev_attr.attr, 405 NULL 406 }; 407 408 static const struct attribute_group adis16136_attribute_group = { 409 .attrs = adis16136_attributes, 410 }; 411 412 static const struct iio_info adis16136_info = { 413 .attrs = &adis16136_attribute_group, 414 .read_raw = &adis16136_read_raw, 415 .write_raw = &adis16136_write_raw, 416 .update_scan_mode = adis_update_scan_mode, 417 .debugfs_reg_access = adis_debugfs_reg_access, 418 }; 419 420 static int adis16136_stop_device(struct iio_dev *indio_dev) 421 { 422 struct adis16136 *adis16136 = iio_priv(indio_dev); 423 int ret; 424 425 ret = adis_write_reg_16(&adis16136->adis, ADIS16136_REG_SLP_CTRL, 0xff); 426 if (ret) 427 dev_err(&indio_dev->dev, 428 "Could not power down device: %d\n", ret); 429 430 return ret; 431 } 432 433 static int adis16136_initial_setup(struct iio_dev *indio_dev) 434 { 435 struct adis16136 *adis16136 = iio_priv(indio_dev); 436 unsigned int device_id; 437 uint16_t prod_id; 438 int ret; 439 440 ret = adis_initial_startup(&adis16136->adis); 441 if (ret) 442 return ret; 443 444 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_PROD_ID, 445 &prod_id); 446 if (ret) 447 return ret; 448 449 ret = sscanf(indio_dev->name, "adis%u\n", &device_id); 450 if (ret != 1) 451 return -EINVAL; 452 453 if (prod_id != device_id) 454 dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.", 455 device_id, prod_id); 456 457 return 0; 458 } 459 460 static const char * const adis16136_status_error_msgs[] = { 461 [ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL] = "Flash update failed", 462 [ADIS16136_DIAG_STAT_SPI_FAIL] = "SPI failure", 463 [ADIS16136_DIAG_STAT_SELF_TEST_FAIL] = "Self test error", 464 [ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL] = "Flash checksum error", 465 }; 466 467 #define ADIS16136_DATA(_timeouts) \ 468 { \ 469 .diag_stat_reg = ADIS16136_REG_DIAG_STAT, \ 470 .glob_cmd_reg = ADIS16136_REG_GLOB_CMD, \ 471 .msc_ctrl_reg = ADIS16136_REG_MSC_CTRL, \ 472 .self_test_reg = ADIS16136_REG_MSC_CTRL, \ 473 .self_test_mask = ADIS16136_MSC_CTRL_SELF_TEST, \ 474 .read_delay = 10, \ 475 .write_delay = 10, \ 476 .status_error_msgs = adis16136_status_error_msgs, \ 477 .status_error_mask = BIT(ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL) | \ 478 BIT(ADIS16136_DIAG_STAT_SPI_FAIL) | \ 479 BIT(ADIS16136_DIAG_STAT_SELF_TEST_FAIL) | \ 480 BIT(ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL), \ 481 .timeouts = (_timeouts), \ 482 } 483 484 enum adis16136_id { 485 ID_ADIS16133, 486 ID_ADIS16135, 487 ID_ADIS16136, 488 ID_ADIS16137, 489 }; 490 491 static const struct adis_timeout adis16133_timeouts = { 492 .reset_ms = 75, 493 .sw_reset_ms = 75, 494 .self_test_ms = 50, 495 }; 496 497 static const struct adis_timeout adis16136_timeouts = { 498 .reset_ms = 128, 499 .sw_reset_ms = 75, 500 .self_test_ms = 245, 501 }; 502 503 static const struct adis16136_chip_info adis16136_chip_info[] = { 504 [ID_ADIS16133] = { 505 .precision = IIO_DEGREE_TO_RAD(1200), 506 .fullscale = 24000, 507 .adis_data = ADIS16136_DATA(&adis16133_timeouts), 508 }, 509 [ID_ADIS16135] = { 510 .precision = IIO_DEGREE_TO_RAD(300), 511 .fullscale = 24000, 512 .adis_data = ADIS16136_DATA(&adis16133_timeouts), 513 }, 514 [ID_ADIS16136] = { 515 .precision = IIO_DEGREE_TO_RAD(450), 516 .fullscale = 24623, 517 .adis_data = ADIS16136_DATA(&adis16136_timeouts), 518 }, 519 [ID_ADIS16137] = { 520 .precision = IIO_DEGREE_TO_RAD(1000), 521 .fullscale = 24609, 522 .adis_data = ADIS16136_DATA(&adis16136_timeouts), 523 }, 524 }; 525 526 static int adis16136_probe(struct spi_device *spi) 527 { 528 const struct spi_device_id *id = spi_get_device_id(spi); 529 struct adis16136 *adis16136; 530 struct iio_dev *indio_dev; 531 const struct adis_data *adis16136_data; 532 int ret; 533 534 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adis16136)); 535 if (indio_dev == NULL) 536 return -ENOMEM; 537 538 spi_set_drvdata(spi, indio_dev); 539 540 adis16136 = iio_priv(indio_dev); 541 542 adis16136->chip_info = &adis16136_chip_info[id->driver_data]; 543 indio_dev->dev.parent = &spi->dev; 544 indio_dev->name = spi_get_device_id(spi)->name; 545 indio_dev->channels = adis16136_channels; 546 indio_dev->num_channels = ARRAY_SIZE(adis16136_channels); 547 indio_dev->info = &adis16136_info; 548 indio_dev->modes = INDIO_DIRECT_MODE; 549 550 adis16136_data = &adis16136->chip_info->adis_data; 551 552 ret = adis_init(&adis16136->adis, indio_dev, spi, adis16136_data); 553 if (ret) 554 return ret; 555 556 ret = adis_setup_buffer_and_trigger(&adis16136->adis, indio_dev, NULL); 557 if (ret) 558 return ret; 559 560 ret = adis16136_initial_setup(indio_dev); 561 if (ret) 562 goto error_cleanup_buffer; 563 564 ret = iio_device_register(indio_dev); 565 if (ret) 566 goto error_stop_device; 567 568 adis16136_debugfs_init(indio_dev); 569 570 return 0; 571 572 error_stop_device: 573 adis16136_stop_device(indio_dev); 574 error_cleanup_buffer: 575 adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev); 576 return ret; 577 } 578 579 static int adis16136_remove(struct spi_device *spi) 580 { 581 struct iio_dev *indio_dev = spi_get_drvdata(spi); 582 struct adis16136 *adis16136 = iio_priv(indio_dev); 583 584 iio_device_unregister(indio_dev); 585 adis16136_stop_device(indio_dev); 586 587 adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev); 588 589 return 0; 590 } 591 592 static const struct spi_device_id adis16136_ids[] = { 593 { "adis16133", ID_ADIS16133 }, 594 { "adis16135", ID_ADIS16135 }, 595 { "adis16136", ID_ADIS16136 }, 596 { "adis16137", ID_ADIS16137 }, 597 { } 598 }; 599 MODULE_DEVICE_TABLE(spi, adis16136_ids); 600 601 static struct spi_driver adis16136_driver = { 602 .driver = { 603 .name = "adis16136", 604 }, 605 .id_table = adis16136_ids, 606 .probe = adis16136_probe, 607 .remove = adis16136_remove, 608 }; 609 module_spi_driver(adis16136_driver); 610 611 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 612 MODULE_DESCRIPTION("Analog Devices ADIS16133/ADIS16135/ADIS16136 gyroscope driver"); 613 MODULE_LICENSE("GPL v2"); 614