1 /* 2 * BMI160 - Bosch IMU (accel, gyro plus external magnetometer) 3 * 4 * Copyright (c) 2016, Intel Corporation. 5 * 6 * This file is subject to the terms and conditions of version 2 of 7 * the GNU General Public License. See the file COPYING in the main 8 * directory of this archive for more details. 9 * 10 * IIO core driver for BMI160, with support for I2C/SPI busses 11 * 12 * TODO: magnetometer, interrupts, hardware FIFO 13 */ 14 #include <linux/module.h> 15 #include <linux/regmap.h> 16 #include <linux/acpi.h> 17 #include <linux/delay.h> 18 19 #include <linux/iio/iio.h> 20 #include <linux/iio/triggered_buffer.h> 21 #include <linux/iio/trigger_consumer.h> 22 #include <linux/iio/buffer.h> 23 #include <linux/iio/sysfs.h> 24 25 #include "bmi160.h" 26 27 #define BMI160_REG_CHIP_ID 0x00 28 #define BMI160_CHIP_ID_VAL 0xD1 29 30 #define BMI160_REG_PMU_STATUS 0x03 31 32 /* X axis data low byte address, the rest can be obtained using axis offset */ 33 #define BMI160_REG_DATA_MAGN_XOUT_L 0x04 34 #define BMI160_REG_DATA_GYRO_XOUT_L 0x0C 35 #define BMI160_REG_DATA_ACCEL_XOUT_L 0x12 36 37 #define BMI160_REG_ACCEL_CONFIG 0x40 38 #define BMI160_ACCEL_CONFIG_ODR_MASK GENMASK(3, 0) 39 #define BMI160_ACCEL_CONFIG_BWP_MASK GENMASK(6, 4) 40 41 #define BMI160_REG_ACCEL_RANGE 0x41 42 #define BMI160_ACCEL_RANGE_2G 0x03 43 #define BMI160_ACCEL_RANGE_4G 0x05 44 #define BMI160_ACCEL_RANGE_8G 0x08 45 #define BMI160_ACCEL_RANGE_16G 0x0C 46 47 #define BMI160_REG_GYRO_CONFIG 0x42 48 #define BMI160_GYRO_CONFIG_ODR_MASK GENMASK(3, 0) 49 #define BMI160_GYRO_CONFIG_BWP_MASK GENMASK(5, 4) 50 51 #define BMI160_REG_GYRO_RANGE 0x43 52 #define BMI160_GYRO_RANGE_2000DPS 0x00 53 #define BMI160_GYRO_RANGE_1000DPS 0x01 54 #define BMI160_GYRO_RANGE_500DPS 0x02 55 #define BMI160_GYRO_RANGE_250DPS 0x03 56 #define BMI160_GYRO_RANGE_125DPS 0x04 57 58 #define BMI160_REG_CMD 0x7E 59 #define BMI160_CMD_ACCEL_PM_SUSPEND 0x10 60 #define BMI160_CMD_ACCEL_PM_NORMAL 0x11 61 #define BMI160_CMD_ACCEL_PM_LOW_POWER 0x12 62 #define BMI160_CMD_GYRO_PM_SUSPEND 0x14 63 #define BMI160_CMD_GYRO_PM_NORMAL 0x15 64 #define BMI160_CMD_GYRO_PM_FAST_STARTUP 0x17 65 #define BMI160_CMD_SOFTRESET 0xB6 66 67 #define BMI160_REG_DUMMY 0x7F 68 69 #define BMI160_ACCEL_PMU_MIN_USLEEP 3200 70 #define BMI160_ACCEL_PMU_MAX_USLEEP 3800 71 #define BMI160_GYRO_PMU_MIN_USLEEP 55000 72 #define BMI160_GYRO_PMU_MAX_USLEEP 80000 73 #define BMI160_SOFTRESET_USLEEP 1000 74 75 #define BMI160_CHANNEL(_type, _axis, _index) { \ 76 .type = _type, \ 77 .modified = 1, \ 78 .channel2 = IIO_MOD_##_axis, \ 79 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 80 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 81 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 82 .scan_index = _index, \ 83 .scan_type = { \ 84 .sign = 's', \ 85 .realbits = 16, \ 86 .storagebits = 16, \ 87 .endianness = IIO_LE, \ 88 }, \ 89 } 90 91 /* scan indexes follow DATA register order */ 92 enum bmi160_scan_axis { 93 BMI160_SCAN_EXT_MAGN_X = 0, 94 BMI160_SCAN_EXT_MAGN_Y, 95 BMI160_SCAN_EXT_MAGN_Z, 96 BMI160_SCAN_RHALL, 97 BMI160_SCAN_GYRO_X, 98 BMI160_SCAN_GYRO_Y, 99 BMI160_SCAN_GYRO_Z, 100 BMI160_SCAN_ACCEL_X, 101 BMI160_SCAN_ACCEL_Y, 102 BMI160_SCAN_ACCEL_Z, 103 BMI160_SCAN_TIMESTAMP, 104 }; 105 106 enum bmi160_sensor_type { 107 BMI160_ACCEL = 0, 108 BMI160_GYRO, 109 BMI160_EXT_MAGN, 110 BMI160_NUM_SENSORS /* must be last */ 111 }; 112 113 struct bmi160_data { 114 struct regmap *regmap; 115 }; 116 117 const struct regmap_config bmi160_regmap_config = { 118 .reg_bits = 8, 119 .val_bits = 8, 120 }; 121 EXPORT_SYMBOL(bmi160_regmap_config); 122 123 struct bmi160_regs { 124 u8 data; /* LSB byte register for X-axis */ 125 u8 config; 126 u8 config_odr_mask; 127 u8 config_bwp_mask; 128 u8 range; 129 u8 pmu_cmd_normal; 130 u8 pmu_cmd_suspend; 131 }; 132 133 static struct bmi160_regs bmi160_regs[] = { 134 [BMI160_ACCEL] = { 135 .data = BMI160_REG_DATA_ACCEL_XOUT_L, 136 .config = BMI160_REG_ACCEL_CONFIG, 137 .config_odr_mask = BMI160_ACCEL_CONFIG_ODR_MASK, 138 .config_bwp_mask = BMI160_ACCEL_CONFIG_BWP_MASK, 139 .range = BMI160_REG_ACCEL_RANGE, 140 .pmu_cmd_normal = BMI160_CMD_ACCEL_PM_NORMAL, 141 .pmu_cmd_suspend = BMI160_CMD_ACCEL_PM_SUSPEND, 142 }, 143 [BMI160_GYRO] = { 144 .data = BMI160_REG_DATA_GYRO_XOUT_L, 145 .config = BMI160_REG_GYRO_CONFIG, 146 .config_odr_mask = BMI160_GYRO_CONFIG_ODR_MASK, 147 .config_bwp_mask = BMI160_GYRO_CONFIG_BWP_MASK, 148 .range = BMI160_REG_GYRO_RANGE, 149 .pmu_cmd_normal = BMI160_CMD_GYRO_PM_NORMAL, 150 .pmu_cmd_suspend = BMI160_CMD_GYRO_PM_SUSPEND, 151 }, 152 }; 153 154 struct bmi160_pmu_time { 155 unsigned long min; 156 unsigned long max; 157 }; 158 159 static struct bmi160_pmu_time bmi160_pmu_time[] = { 160 [BMI160_ACCEL] = { 161 .min = BMI160_ACCEL_PMU_MIN_USLEEP, 162 .max = BMI160_ACCEL_PMU_MAX_USLEEP 163 }, 164 [BMI160_GYRO] = { 165 .min = BMI160_GYRO_PMU_MIN_USLEEP, 166 .max = BMI160_GYRO_PMU_MIN_USLEEP, 167 }, 168 }; 169 170 struct bmi160_scale { 171 u8 bits; 172 int uscale; 173 }; 174 175 struct bmi160_odr { 176 u8 bits; 177 int odr; 178 int uodr; 179 }; 180 181 static const struct bmi160_scale bmi160_accel_scale[] = { 182 { BMI160_ACCEL_RANGE_2G, 598}, 183 { BMI160_ACCEL_RANGE_4G, 1197}, 184 { BMI160_ACCEL_RANGE_8G, 2394}, 185 { BMI160_ACCEL_RANGE_16G, 4788}, 186 }; 187 188 static const struct bmi160_scale bmi160_gyro_scale[] = { 189 { BMI160_GYRO_RANGE_2000DPS, 1065}, 190 { BMI160_GYRO_RANGE_1000DPS, 532}, 191 { BMI160_GYRO_RANGE_500DPS, 266}, 192 { BMI160_GYRO_RANGE_250DPS, 133}, 193 { BMI160_GYRO_RANGE_125DPS, 66}, 194 }; 195 196 struct bmi160_scale_item { 197 const struct bmi160_scale *tbl; 198 int num; 199 }; 200 201 static const struct bmi160_scale_item bmi160_scale_table[] = { 202 [BMI160_ACCEL] = { 203 .tbl = bmi160_accel_scale, 204 .num = ARRAY_SIZE(bmi160_accel_scale), 205 }, 206 [BMI160_GYRO] = { 207 .tbl = bmi160_gyro_scale, 208 .num = ARRAY_SIZE(bmi160_gyro_scale), 209 }, 210 }; 211 212 static const struct bmi160_odr bmi160_accel_odr[] = { 213 {0x01, 0, 781250}, 214 {0x02, 1, 562500}, 215 {0x03, 3, 125000}, 216 {0x04, 6, 250000}, 217 {0x05, 12, 500000}, 218 {0x06, 25, 0}, 219 {0x07, 50, 0}, 220 {0x08, 100, 0}, 221 {0x09, 200, 0}, 222 {0x0A, 400, 0}, 223 {0x0B, 800, 0}, 224 {0x0C, 1600, 0}, 225 }; 226 227 static const struct bmi160_odr bmi160_gyro_odr[] = { 228 {0x06, 25, 0}, 229 {0x07, 50, 0}, 230 {0x08, 100, 0}, 231 {0x09, 200, 0}, 232 {0x0A, 400, 0}, 233 {0x0B, 800, 0}, 234 {0x0C, 1600, 0}, 235 {0x0D, 3200, 0}, 236 }; 237 238 struct bmi160_odr_item { 239 const struct bmi160_odr *tbl; 240 int num; 241 }; 242 243 static const struct bmi160_odr_item bmi160_odr_table[] = { 244 [BMI160_ACCEL] = { 245 .tbl = bmi160_accel_odr, 246 .num = ARRAY_SIZE(bmi160_accel_odr), 247 }, 248 [BMI160_GYRO] = { 249 .tbl = bmi160_gyro_odr, 250 .num = ARRAY_SIZE(bmi160_gyro_odr), 251 }, 252 }; 253 254 static const struct iio_chan_spec bmi160_channels[] = { 255 BMI160_CHANNEL(IIO_ACCEL, X, BMI160_SCAN_ACCEL_X), 256 BMI160_CHANNEL(IIO_ACCEL, Y, BMI160_SCAN_ACCEL_Y), 257 BMI160_CHANNEL(IIO_ACCEL, Z, BMI160_SCAN_ACCEL_Z), 258 BMI160_CHANNEL(IIO_ANGL_VEL, X, BMI160_SCAN_GYRO_X), 259 BMI160_CHANNEL(IIO_ANGL_VEL, Y, BMI160_SCAN_GYRO_Y), 260 BMI160_CHANNEL(IIO_ANGL_VEL, Z, BMI160_SCAN_GYRO_Z), 261 IIO_CHAN_SOFT_TIMESTAMP(BMI160_SCAN_TIMESTAMP), 262 }; 263 264 static enum bmi160_sensor_type bmi160_to_sensor(enum iio_chan_type iio_type) 265 { 266 switch (iio_type) { 267 case IIO_ACCEL: 268 return BMI160_ACCEL; 269 case IIO_ANGL_VEL: 270 return BMI160_GYRO; 271 default: 272 return -EINVAL; 273 } 274 } 275 276 static 277 int bmi160_set_mode(struct bmi160_data *data, enum bmi160_sensor_type t, 278 bool mode) 279 { 280 int ret; 281 u8 cmd; 282 283 if (mode) 284 cmd = bmi160_regs[t].pmu_cmd_normal; 285 else 286 cmd = bmi160_regs[t].pmu_cmd_suspend; 287 288 ret = regmap_write(data->regmap, BMI160_REG_CMD, cmd); 289 if (ret < 0) 290 return ret; 291 292 usleep_range(bmi160_pmu_time[t].min, bmi160_pmu_time[t].max); 293 294 return 0; 295 } 296 297 static 298 int bmi160_set_scale(struct bmi160_data *data, enum bmi160_sensor_type t, 299 int uscale) 300 { 301 int i; 302 303 for (i = 0; i < bmi160_scale_table[t].num; i++) 304 if (bmi160_scale_table[t].tbl[i].uscale == uscale) 305 break; 306 307 if (i == bmi160_scale_table[t].num) 308 return -EINVAL; 309 310 return regmap_write(data->regmap, bmi160_regs[t].range, 311 bmi160_scale_table[t].tbl[i].bits); 312 } 313 314 static 315 int bmi160_get_scale(struct bmi160_data *data, enum bmi160_sensor_type t, 316 int *uscale) 317 { 318 int i, ret, val; 319 320 ret = regmap_read(data->regmap, bmi160_regs[t].range, &val); 321 if (ret < 0) 322 return ret; 323 324 for (i = 0; i < bmi160_scale_table[t].num; i++) 325 if (bmi160_scale_table[t].tbl[i].bits == val) { 326 *uscale = bmi160_scale_table[t].tbl[i].uscale; 327 return 0; 328 } 329 330 return -EINVAL; 331 } 332 333 static int bmi160_get_data(struct bmi160_data *data, int chan_type, 334 int axis, int *val) 335 { 336 u8 reg; 337 int ret; 338 __le16 sample; 339 enum bmi160_sensor_type t = bmi160_to_sensor(chan_type); 340 341 reg = bmi160_regs[t].data + (axis - IIO_MOD_X) * sizeof(__le16); 342 343 ret = regmap_bulk_read(data->regmap, reg, &sample, sizeof(__le16)); 344 if (ret < 0) 345 return ret; 346 347 *val = sign_extend32(le16_to_cpu(sample), 15); 348 349 return 0; 350 } 351 352 static 353 int bmi160_set_odr(struct bmi160_data *data, enum bmi160_sensor_type t, 354 int odr, int uodr) 355 { 356 int i; 357 358 for (i = 0; i < bmi160_odr_table[t].num; i++) 359 if (bmi160_odr_table[t].tbl[i].odr == odr && 360 bmi160_odr_table[t].tbl[i].uodr == uodr) 361 break; 362 363 if (i >= bmi160_odr_table[t].num) 364 return -EINVAL; 365 366 return regmap_update_bits(data->regmap, 367 bmi160_regs[t].config, 368 bmi160_regs[t].config_odr_mask, 369 bmi160_odr_table[t].tbl[i].bits); 370 } 371 372 static int bmi160_get_odr(struct bmi160_data *data, enum bmi160_sensor_type t, 373 int *odr, int *uodr) 374 { 375 int i, val, ret; 376 377 ret = regmap_read(data->regmap, bmi160_regs[t].config, &val); 378 if (ret < 0) 379 return ret; 380 381 val &= bmi160_regs[t].config_odr_mask; 382 383 for (i = 0; i < bmi160_odr_table[t].num; i++) 384 if (val == bmi160_odr_table[t].tbl[i].bits) 385 break; 386 387 if (i >= bmi160_odr_table[t].num) 388 return -EINVAL; 389 390 *odr = bmi160_odr_table[t].tbl[i].odr; 391 *uodr = bmi160_odr_table[t].tbl[i].uodr; 392 393 return 0; 394 } 395 396 static irqreturn_t bmi160_trigger_handler(int irq, void *p) 397 { 398 struct iio_poll_func *pf = p; 399 struct iio_dev *indio_dev = pf->indio_dev; 400 struct bmi160_data *data = iio_priv(indio_dev); 401 s16 buf[16]; /* 3 sens x 3 axis x s16 + 3 x s16 pad + 4 x s16 tstamp */ 402 int i, ret, j = 0, base = BMI160_REG_DATA_MAGN_XOUT_L; 403 __le16 sample; 404 405 for_each_set_bit(i, indio_dev->active_scan_mask, 406 indio_dev->masklength) { 407 ret = regmap_bulk_read(data->regmap, base + i * sizeof(__le16), 408 &sample, sizeof(__le16)); 409 if (ret < 0) 410 goto done; 411 buf[j++] = sample; 412 } 413 414 iio_push_to_buffers_with_timestamp(indio_dev, buf, 415 iio_get_time_ns(indio_dev)); 416 done: 417 iio_trigger_notify_done(indio_dev->trig); 418 return IRQ_HANDLED; 419 } 420 421 static int bmi160_read_raw(struct iio_dev *indio_dev, 422 struct iio_chan_spec const *chan, 423 int *val, int *val2, long mask) 424 { 425 int ret; 426 struct bmi160_data *data = iio_priv(indio_dev); 427 428 switch (mask) { 429 case IIO_CHAN_INFO_RAW: 430 ret = bmi160_get_data(data, chan->type, chan->channel2, val); 431 if (ret < 0) 432 return ret; 433 return IIO_VAL_INT; 434 case IIO_CHAN_INFO_SCALE: 435 *val = 0; 436 ret = bmi160_get_scale(data, 437 bmi160_to_sensor(chan->type), val2); 438 return ret < 0 ? ret : IIO_VAL_INT_PLUS_MICRO; 439 case IIO_CHAN_INFO_SAMP_FREQ: 440 ret = bmi160_get_odr(data, bmi160_to_sensor(chan->type), 441 val, val2); 442 return ret < 0 ? ret : IIO_VAL_INT_PLUS_MICRO; 443 default: 444 return -EINVAL; 445 } 446 447 return 0; 448 } 449 450 static int bmi160_write_raw(struct iio_dev *indio_dev, 451 struct iio_chan_spec const *chan, 452 int val, int val2, long mask) 453 { 454 struct bmi160_data *data = iio_priv(indio_dev); 455 456 switch (mask) { 457 case IIO_CHAN_INFO_SCALE: 458 return bmi160_set_scale(data, 459 bmi160_to_sensor(chan->type), val2); 460 break; 461 case IIO_CHAN_INFO_SAMP_FREQ: 462 return bmi160_set_odr(data, bmi160_to_sensor(chan->type), 463 val, val2); 464 default: 465 return -EINVAL; 466 } 467 468 return 0; 469 } 470 471 static 472 IIO_CONST_ATTR(in_accel_sampling_frequency_available, 473 "0.78125 1.5625 3.125 6.25 12.5 25 50 100 200 400 800 1600"); 474 static 475 IIO_CONST_ATTR(in_anglvel_sampling_frequency_available, 476 "25 50 100 200 400 800 1600 3200"); 477 static 478 IIO_CONST_ATTR(in_accel_scale_available, 479 "0.000598 0.001197 0.002394 0.004788"); 480 static 481 IIO_CONST_ATTR(in_anglvel_scale_available, 482 "0.001065 0.000532 0.000266 0.000133 0.000066"); 483 484 static struct attribute *bmi160_attrs[] = { 485 &iio_const_attr_in_accel_sampling_frequency_available.dev_attr.attr, 486 &iio_const_attr_in_anglvel_sampling_frequency_available.dev_attr.attr, 487 &iio_const_attr_in_accel_scale_available.dev_attr.attr, 488 &iio_const_attr_in_anglvel_scale_available.dev_attr.attr, 489 NULL, 490 }; 491 492 static const struct attribute_group bmi160_attrs_group = { 493 .attrs = bmi160_attrs, 494 }; 495 496 static const struct iio_info bmi160_info = { 497 .driver_module = THIS_MODULE, 498 .read_raw = bmi160_read_raw, 499 .write_raw = bmi160_write_raw, 500 .attrs = &bmi160_attrs_group, 501 }; 502 503 static const char *bmi160_match_acpi_device(struct device *dev) 504 { 505 const struct acpi_device_id *id; 506 507 id = acpi_match_device(dev->driver->acpi_match_table, dev); 508 if (!id) 509 return NULL; 510 511 return dev_name(dev); 512 } 513 514 static int bmi160_chip_init(struct bmi160_data *data, bool use_spi) 515 { 516 int ret; 517 unsigned int val; 518 struct device *dev = regmap_get_device(data->regmap); 519 520 ret = regmap_write(data->regmap, BMI160_REG_CMD, BMI160_CMD_SOFTRESET); 521 if (ret < 0) 522 return ret; 523 524 usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1); 525 526 /* 527 * CS rising edge is needed before starting SPI, so do a dummy read 528 * See Section 3.2.1, page 86 of the datasheet 529 */ 530 if (use_spi) { 531 ret = regmap_read(data->regmap, BMI160_REG_DUMMY, &val); 532 if (ret < 0) 533 return ret; 534 } 535 536 ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val); 537 if (ret < 0) { 538 dev_err(dev, "Error reading chip id\n"); 539 return ret; 540 } 541 if (val != BMI160_CHIP_ID_VAL) { 542 dev_err(dev, "Wrong chip id, got %x expected %x\n", 543 val, BMI160_CHIP_ID_VAL); 544 return -ENODEV; 545 } 546 547 ret = bmi160_set_mode(data, BMI160_ACCEL, true); 548 if (ret < 0) 549 return ret; 550 551 ret = bmi160_set_mode(data, BMI160_GYRO, true); 552 if (ret < 0) 553 return ret; 554 555 return 0; 556 } 557 558 static void bmi160_chip_uninit(struct bmi160_data *data) 559 { 560 bmi160_set_mode(data, BMI160_GYRO, false); 561 bmi160_set_mode(data, BMI160_ACCEL, false); 562 } 563 564 int bmi160_core_probe(struct device *dev, struct regmap *regmap, 565 const char *name, bool use_spi) 566 { 567 struct iio_dev *indio_dev; 568 struct bmi160_data *data; 569 int ret; 570 571 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 572 if (!indio_dev) 573 return -ENOMEM; 574 575 data = iio_priv(indio_dev); 576 dev_set_drvdata(dev, indio_dev); 577 data->regmap = regmap; 578 579 ret = bmi160_chip_init(data, use_spi); 580 if (ret < 0) 581 return ret; 582 583 if (!name && ACPI_HANDLE(dev)) 584 name = bmi160_match_acpi_device(dev); 585 586 indio_dev->dev.parent = dev; 587 indio_dev->channels = bmi160_channels; 588 indio_dev->num_channels = ARRAY_SIZE(bmi160_channels); 589 indio_dev->name = name; 590 indio_dev->modes = INDIO_DIRECT_MODE; 591 indio_dev->info = &bmi160_info; 592 593 ret = iio_triggered_buffer_setup(indio_dev, NULL, 594 bmi160_trigger_handler, NULL); 595 if (ret < 0) 596 goto uninit; 597 598 ret = iio_device_register(indio_dev); 599 if (ret < 0) 600 goto buffer_cleanup; 601 602 return 0; 603 buffer_cleanup: 604 iio_triggered_buffer_cleanup(indio_dev); 605 uninit: 606 bmi160_chip_uninit(data); 607 return ret; 608 } 609 EXPORT_SYMBOL_GPL(bmi160_core_probe); 610 611 void bmi160_core_remove(struct device *dev) 612 { 613 struct iio_dev *indio_dev = dev_get_drvdata(dev); 614 struct bmi160_data *data = iio_priv(indio_dev); 615 616 iio_device_unregister(indio_dev); 617 iio_triggered_buffer_cleanup(indio_dev); 618 bmi160_chip_uninit(data); 619 } 620 EXPORT_SYMBOL_GPL(bmi160_core_remove); 621 622 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com"); 623 MODULE_DESCRIPTION("Bosch BMI160 driver"); 624 MODULE_LICENSE("GPL v2"); 625