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_timeout *timeouts; 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 152 debugfs_create_file_unsafe("serial_number", 0400, 153 indio_dev->debugfs_dentry, adis16136, 154 &adis16136_serial_fops); 155 debugfs_create_file_unsafe("product_id", 0400, 156 indio_dev->debugfs_dentry, 157 adis16136, &adis16136_product_id_fops); 158 debugfs_create_file_unsafe("flash_count", 0400, 159 indio_dev->debugfs_dentry, 160 adis16136, &adis16136_flash_count_fops); 161 162 return 0; 163 } 164 165 #else 166 167 static int adis16136_debugfs_init(struct iio_dev *indio_dev) 168 { 169 return 0; 170 } 171 172 #endif 173 174 static int adis16136_set_freq(struct adis16136 *adis16136, unsigned int freq) 175 { 176 unsigned int t; 177 178 t = 32768 / freq; 179 if (t < 0xf) 180 t = 0xf; 181 else if (t > 0xffff) 182 t = 0xffff; 183 else 184 t--; 185 186 return adis_write_reg_16(&adis16136->adis, ADIS16136_REG_SMPL_PRD, t); 187 } 188 189 static int __adis16136_get_freq(struct adis16136 *adis16136, unsigned int *freq) 190 { 191 uint16_t t; 192 int ret; 193 194 ret = __adis_read_reg_16(&adis16136->adis, ADIS16136_REG_SMPL_PRD, &t); 195 if (ret) 196 return ret; 197 198 *freq = 32768 / (t + 1); 199 200 return 0; 201 } 202 203 static ssize_t adis16136_write_frequency(struct device *dev, 204 struct device_attribute *attr, const char *buf, size_t len) 205 { 206 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 207 struct adis16136 *adis16136 = iio_priv(indio_dev); 208 unsigned int val; 209 int ret; 210 211 ret = kstrtouint(buf, 10, &val); 212 if (ret) 213 return ret; 214 215 if (val == 0) 216 return -EINVAL; 217 218 ret = adis16136_set_freq(adis16136, val); 219 220 return ret ? ret : len; 221 } 222 223 static ssize_t adis16136_read_frequency(struct device *dev, 224 struct device_attribute *attr, char *buf) 225 { 226 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 227 struct adis16136 *adis16136 = iio_priv(indio_dev); 228 struct mutex *slock = &adis16136->adis.state_lock; 229 unsigned int freq; 230 int ret; 231 232 mutex_lock(slock); 233 ret = __adis16136_get_freq(adis16136, &freq); 234 mutex_unlock(slock); 235 if (ret) 236 return ret; 237 238 return sprintf(buf, "%d\n", freq); 239 } 240 241 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, 242 adis16136_read_frequency, 243 adis16136_write_frequency); 244 245 static const unsigned adis16136_3db_divisors[] = { 246 [0] = 2, /* Special case */ 247 [1] = 6, 248 [2] = 12, 249 [3] = 25, 250 [4] = 50, 251 [5] = 100, 252 [6] = 200, 253 [7] = 200, /* Not a valid setting */ 254 }; 255 256 static int adis16136_set_filter(struct iio_dev *indio_dev, int val) 257 { 258 struct adis16136 *adis16136 = iio_priv(indio_dev); 259 struct mutex *slock = &adis16136->adis.state_lock; 260 unsigned int freq; 261 int i, ret; 262 263 mutex_lock(slock); 264 ret = __adis16136_get_freq(adis16136, &freq); 265 if (ret) 266 goto out_unlock; 267 268 for (i = ARRAY_SIZE(adis16136_3db_divisors) - 1; i >= 1; i--) { 269 if (freq / adis16136_3db_divisors[i] >= val) 270 break; 271 } 272 273 ret = __adis_write_reg_16(&adis16136->adis, ADIS16136_REG_AVG_CNT, i); 274 out_unlock: 275 mutex_unlock(slock); 276 277 return ret; 278 } 279 280 static int adis16136_get_filter(struct iio_dev *indio_dev, int *val) 281 { 282 struct adis16136 *adis16136 = iio_priv(indio_dev); 283 struct mutex *slock = &adis16136->adis.state_lock; 284 unsigned int freq; 285 uint16_t val16; 286 int ret; 287 288 mutex_lock(slock); 289 290 ret = __adis_read_reg_16(&adis16136->adis, ADIS16136_REG_AVG_CNT, 291 &val16); 292 if (ret) 293 goto err_unlock; 294 295 ret = __adis16136_get_freq(adis16136, &freq); 296 if (ret) 297 goto err_unlock; 298 299 *val = freq / adis16136_3db_divisors[val16 & 0x07]; 300 301 err_unlock: 302 mutex_unlock(slock); 303 304 return ret ? ret : IIO_VAL_INT; 305 } 306 307 static int adis16136_read_raw(struct iio_dev *indio_dev, 308 const struct iio_chan_spec *chan, int *val, int *val2, long info) 309 { 310 struct adis16136 *adis16136 = iio_priv(indio_dev); 311 uint32_t val32; 312 int ret; 313 314 switch (info) { 315 case IIO_CHAN_INFO_RAW: 316 return adis_single_conversion(indio_dev, chan, 0, val); 317 case IIO_CHAN_INFO_SCALE: 318 switch (chan->type) { 319 case IIO_ANGL_VEL: 320 *val = adis16136->chip_info->precision; 321 *val2 = (adis16136->chip_info->fullscale << 16); 322 return IIO_VAL_FRACTIONAL; 323 case IIO_TEMP: 324 *val = 10; 325 *val2 = 697000; /* 0.010697 degree Celsius */ 326 return IIO_VAL_INT_PLUS_MICRO; 327 default: 328 return -EINVAL; 329 } 330 case IIO_CHAN_INFO_CALIBBIAS: 331 ret = adis_read_reg_32(&adis16136->adis, 332 ADIS16136_REG_GYRO_OFF2, &val32); 333 if (ret) 334 return ret; 335 336 *val = sign_extend32(val32, 31); 337 338 return IIO_VAL_INT; 339 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 340 return adis16136_get_filter(indio_dev, val); 341 default: 342 return -EINVAL; 343 } 344 } 345 346 static int adis16136_write_raw(struct iio_dev *indio_dev, 347 const struct iio_chan_spec *chan, int val, int val2, long info) 348 { 349 struct adis16136 *adis16136 = iio_priv(indio_dev); 350 351 switch (info) { 352 case IIO_CHAN_INFO_CALIBBIAS: 353 return adis_write_reg_32(&adis16136->adis, 354 ADIS16136_REG_GYRO_OFF2, val); 355 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 356 return adis16136_set_filter(indio_dev, val); 357 default: 358 break; 359 } 360 361 return -EINVAL; 362 } 363 364 enum { 365 ADIS16136_SCAN_GYRO, 366 ADIS16136_SCAN_TEMP, 367 }; 368 369 static const struct iio_chan_spec adis16136_channels[] = { 370 { 371 .type = IIO_ANGL_VEL, 372 .modified = 1, 373 .channel2 = IIO_MOD_X, 374 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 375 BIT(IIO_CHAN_INFO_CALIBBIAS) | 376 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 377 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 378 379 .address = ADIS16136_REG_GYRO_OUT2, 380 .scan_index = ADIS16136_SCAN_GYRO, 381 .scan_type = { 382 .sign = 's', 383 .realbits = 32, 384 .storagebits = 32, 385 .endianness = IIO_BE, 386 }, 387 }, { 388 .type = IIO_TEMP, 389 .indexed = 1, 390 .channel = 0, 391 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 392 BIT(IIO_CHAN_INFO_SCALE), 393 .address = ADIS16136_REG_TEMP_OUT, 394 .scan_index = ADIS16136_SCAN_TEMP, 395 .scan_type = { 396 .sign = 's', 397 .realbits = 16, 398 .storagebits = 16, 399 .endianness = IIO_BE, 400 }, 401 }, 402 IIO_CHAN_SOFT_TIMESTAMP(2), 403 }; 404 405 static struct attribute *adis16136_attributes[] = { 406 &iio_dev_attr_sampling_frequency.dev_attr.attr, 407 NULL 408 }; 409 410 static const struct attribute_group adis16136_attribute_group = { 411 .attrs = adis16136_attributes, 412 }; 413 414 static const struct iio_info adis16136_info = { 415 .attrs = &adis16136_attribute_group, 416 .read_raw = &adis16136_read_raw, 417 .write_raw = &adis16136_write_raw, 418 .update_scan_mode = adis_update_scan_mode, 419 .debugfs_reg_access = adis_debugfs_reg_access, 420 }; 421 422 static int adis16136_stop_device(struct iio_dev *indio_dev) 423 { 424 struct adis16136 *adis16136 = iio_priv(indio_dev); 425 int ret; 426 427 ret = adis_write_reg_16(&adis16136->adis, ADIS16136_REG_SLP_CTRL, 0xff); 428 if (ret) 429 dev_err(&indio_dev->dev, 430 "Could not power down device: %d\n", ret); 431 432 return ret; 433 } 434 435 static int adis16136_initial_setup(struct iio_dev *indio_dev) 436 { 437 struct adis16136 *adis16136 = iio_priv(indio_dev); 438 unsigned int device_id; 439 uint16_t prod_id; 440 int ret; 441 442 ret = adis_initial_startup(&adis16136->adis); 443 if (ret) 444 return ret; 445 446 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_PROD_ID, 447 &prod_id); 448 if (ret) 449 return ret; 450 451 ret = sscanf(indio_dev->name, "adis%u\n", &device_id); 452 if (ret != 1) 453 return -EINVAL; 454 455 if (prod_id != device_id) 456 dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.", 457 device_id, prod_id); 458 459 return 0; 460 } 461 462 static const char * const adis16136_status_error_msgs[] = { 463 [ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL] = "Flash update failed", 464 [ADIS16136_DIAG_STAT_SPI_FAIL] = "SPI failure", 465 [ADIS16136_DIAG_STAT_SELF_TEST_FAIL] = "Self test error", 466 [ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL] = "Flash checksum error", 467 }; 468 469 static const struct adis_data adis16136_data = { 470 .diag_stat_reg = ADIS16136_REG_DIAG_STAT, 471 .glob_cmd_reg = ADIS16136_REG_GLOB_CMD, 472 .msc_ctrl_reg = ADIS16136_REG_MSC_CTRL, 473 474 .self_test_mask = ADIS16136_MSC_CTRL_SELF_TEST, 475 476 .read_delay = 10, 477 .write_delay = 10, 478 479 .status_error_msgs = adis16136_status_error_msgs, 480 .status_error_mask = BIT(ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL) | 481 BIT(ADIS16136_DIAG_STAT_SPI_FAIL) | 482 BIT(ADIS16136_DIAG_STAT_SELF_TEST_FAIL) | 483 BIT(ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL), 484 }; 485 486 enum adis16136_id { 487 ID_ADIS16133, 488 ID_ADIS16135, 489 ID_ADIS16136, 490 ID_ADIS16137, 491 }; 492 493 static const struct adis_timeout adis16133_timeouts = { 494 .reset_ms = 75, 495 .sw_reset_ms = 75, 496 .self_test_ms = 50, 497 }; 498 499 static const struct adis_timeout adis16136_timeouts = { 500 .reset_ms = 128, 501 .sw_reset_ms = 75, 502 .self_test_ms = 245, 503 }; 504 505 static const struct adis16136_chip_info adis16136_chip_info[] = { 506 [ID_ADIS16133] = { 507 .precision = IIO_DEGREE_TO_RAD(1200), 508 .fullscale = 24000, 509 .timeouts = &adis16133_timeouts, 510 }, 511 [ID_ADIS16135] = { 512 .precision = IIO_DEGREE_TO_RAD(300), 513 .fullscale = 24000, 514 .timeouts = &adis16133_timeouts, 515 }, 516 [ID_ADIS16136] = { 517 .precision = IIO_DEGREE_TO_RAD(450), 518 .fullscale = 24623, 519 .timeouts = &adis16136_timeouts, 520 }, 521 [ID_ADIS16137] = { 522 .precision = IIO_DEGREE_TO_RAD(1000), 523 .fullscale = 24609, 524 .timeouts = &adis16136_timeouts, 525 }, 526 }; 527 528 static struct adis_data *adis16136_adis_data_alloc(struct adis16136 *st, 529 struct device *dev) 530 { 531 struct adis_data *data; 532 533 data = devm_kmalloc(dev, sizeof(struct adis_data), GFP_KERNEL); 534 if (!data) 535 return ERR_PTR(-ENOMEM); 536 537 memcpy(data, &adis16136_data, sizeof(*data)); 538 539 data->timeouts = st->chip_info->timeouts; 540 541 return data; 542 } 543 544 static int adis16136_probe(struct spi_device *spi) 545 { 546 const struct spi_device_id *id = spi_get_device_id(spi); 547 struct adis16136 *adis16136; 548 struct iio_dev *indio_dev; 549 const struct adis_data *adis16136_data; 550 int ret; 551 552 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adis16136)); 553 if (indio_dev == NULL) 554 return -ENOMEM; 555 556 spi_set_drvdata(spi, indio_dev); 557 558 adis16136 = iio_priv(indio_dev); 559 560 adis16136->chip_info = &adis16136_chip_info[id->driver_data]; 561 indio_dev->dev.parent = &spi->dev; 562 indio_dev->name = spi_get_device_id(spi)->name; 563 indio_dev->channels = adis16136_channels; 564 indio_dev->num_channels = ARRAY_SIZE(adis16136_channels); 565 indio_dev->info = &adis16136_info; 566 indio_dev->modes = INDIO_DIRECT_MODE; 567 568 adis16136_data = adis16136_adis_data_alloc(adis16136, &spi->dev); 569 if (IS_ERR(adis16136_data)) 570 return PTR_ERR(adis16136_data); 571 572 ret = adis_init(&adis16136->adis, indio_dev, spi, adis16136_data); 573 if (ret) 574 return ret; 575 576 ret = adis_setup_buffer_and_trigger(&adis16136->adis, indio_dev, NULL); 577 if (ret) 578 return ret; 579 580 ret = adis16136_initial_setup(indio_dev); 581 if (ret) 582 goto error_cleanup_buffer; 583 584 ret = iio_device_register(indio_dev); 585 if (ret) 586 goto error_stop_device; 587 588 adis16136_debugfs_init(indio_dev); 589 590 return 0; 591 592 error_stop_device: 593 adis16136_stop_device(indio_dev); 594 error_cleanup_buffer: 595 adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev); 596 return ret; 597 } 598 599 static int adis16136_remove(struct spi_device *spi) 600 { 601 struct iio_dev *indio_dev = spi_get_drvdata(spi); 602 struct adis16136 *adis16136 = iio_priv(indio_dev); 603 604 iio_device_unregister(indio_dev); 605 adis16136_stop_device(indio_dev); 606 607 adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev); 608 609 return 0; 610 } 611 612 static const struct spi_device_id adis16136_ids[] = { 613 { "adis16133", ID_ADIS16133 }, 614 { "adis16135", ID_ADIS16135 }, 615 { "adis16136", ID_ADIS16136 }, 616 { "adis16137", ID_ADIS16137 }, 617 { } 618 }; 619 MODULE_DEVICE_TABLE(spi, adis16136_ids); 620 621 static struct spi_driver adis16136_driver = { 622 .driver = { 623 .name = "adis16136", 624 }, 625 .id_table = adis16136_ids, 626 .probe = adis16136_probe, 627 .remove = adis16136_remove, 628 }; 629 module_spi_driver(adis16136_driver); 630 631 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 632 MODULE_DESCRIPTION("Analog Devices ADIS16133/ADIS16135/ADIS16136 gyroscope driver"); 633 MODULE_LICENSE("GPL v2"); 634