1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com> 4 * Copyright (c) 2012 Bosch Sensortec GmbH 5 * Copyright (c) 2012 Unixphere AB 6 * Copyright (c) 2014 Intel Corporation 7 * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org> 8 * 9 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor. 10 * 11 * Datasheet: 12 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-121.pdf 13 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP280-DS001-12.pdf 14 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280_DS001-11.pdf 15 */ 16 17 #define pr_fmt(fmt) "bmp280: " fmt 18 19 #include <linux/device.h> 20 #include <linux/module.h> 21 #include <linux/regmap.h> 22 #include <linux/delay.h> 23 #include <linux/iio/iio.h> 24 #include <linux/iio/sysfs.h> 25 #include <linux/gpio/consumer.h> 26 #include <linux/regulator/consumer.h> 27 #include <linux/interrupt.h> 28 #include <linux/irq.h> /* For irq_get_irq_data() */ 29 #include <linux/completion.h> 30 #include <linux/pm_runtime.h> 31 #include <linux/random.h> 32 33 #include "bmp280.h" 34 35 /* 36 * These enums are used for indexing into the array of calibration 37 * coefficients for BMP180. 38 */ 39 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD }; 40 41 struct bmp180_calib { 42 s16 AC1; 43 s16 AC2; 44 s16 AC3; 45 u16 AC4; 46 u16 AC5; 47 u16 AC6; 48 s16 B1; 49 s16 B2; 50 s16 MB; 51 s16 MC; 52 s16 MD; 53 }; 54 55 /* See datasheet Section 4.2.2. */ 56 struct bmp280_calib { 57 u16 T1; 58 s16 T2; 59 s16 T3; 60 u16 P1; 61 s16 P2; 62 s16 P3; 63 s16 P4; 64 s16 P5; 65 s16 P6; 66 s16 P7; 67 s16 P8; 68 s16 P9; 69 u8 H1; 70 s16 H2; 71 u8 H3; 72 s16 H4; 73 s16 H5; 74 s8 H6; 75 }; 76 77 static const char *const bmp280_supply_names[] = { 78 "vddd", "vdda" 79 }; 80 81 #define BMP280_NUM_SUPPLIES ARRAY_SIZE(bmp280_supply_names) 82 83 struct bmp280_data { 84 struct device *dev; 85 struct mutex lock; 86 struct regmap *regmap; 87 struct completion done; 88 bool use_eoc; 89 const struct bmp280_chip_info *chip_info; 90 union { 91 struct bmp180_calib bmp180; 92 struct bmp280_calib bmp280; 93 } calib; 94 struct regulator_bulk_data supplies[BMP280_NUM_SUPPLIES]; 95 unsigned int start_up_time; /* in microseconds */ 96 97 /* log of base 2 of oversampling rate */ 98 u8 oversampling_press; 99 u8 oversampling_temp; 100 u8 oversampling_humid; 101 102 /* 103 * Carryover value from temperature conversion, used in pressure 104 * calculation. 105 */ 106 s32 t_fine; 107 }; 108 109 struct bmp280_chip_info { 110 const int *oversampling_temp_avail; 111 int num_oversampling_temp_avail; 112 113 const int *oversampling_press_avail; 114 int num_oversampling_press_avail; 115 116 const int *oversampling_humid_avail; 117 int num_oversampling_humid_avail; 118 119 int (*chip_config)(struct bmp280_data *); 120 int (*read_temp)(struct bmp280_data *, int *); 121 int (*read_press)(struct bmp280_data *, int *, int *); 122 int (*read_humid)(struct bmp280_data *, int *, int *); 123 }; 124 125 /* 126 * These enums are used for indexing into the array of compensation 127 * parameters for BMP280. 128 */ 129 enum { T1, T2, T3 }; 130 enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 }; 131 132 static const struct iio_chan_spec bmp280_channels[] = { 133 { 134 .type = IIO_PRESSURE, 135 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 136 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 137 }, 138 { 139 .type = IIO_TEMP, 140 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 141 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 142 }, 143 { 144 .type = IIO_HUMIDITYRELATIVE, 145 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 146 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 147 }, 148 }; 149 150 static int bmp280_read_calib(struct bmp280_data *data, 151 struct bmp280_calib *calib, 152 unsigned int chip) 153 { 154 int ret; 155 unsigned int tmp; 156 __le16 l16; 157 __be16 b16; 158 struct device *dev = data->dev; 159 __le16 t_buf[BMP280_COMP_TEMP_REG_COUNT / 2]; 160 __le16 p_buf[BMP280_COMP_PRESS_REG_COUNT / 2]; 161 162 /* Read temperature calibration values. */ 163 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START, 164 t_buf, BMP280_COMP_TEMP_REG_COUNT); 165 if (ret < 0) { 166 dev_err(data->dev, 167 "failed to read temperature calibration parameters\n"); 168 return ret; 169 } 170 171 /* Toss the temperature calibration data into the entropy pool */ 172 add_device_randomness(t_buf, sizeof(t_buf)); 173 174 calib->T1 = le16_to_cpu(t_buf[T1]); 175 calib->T2 = le16_to_cpu(t_buf[T2]); 176 calib->T3 = le16_to_cpu(t_buf[T3]); 177 178 /* Read pressure calibration values. */ 179 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START, 180 p_buf, BMP280_COMP_PRESS_REG_COUNT); 181 if (ret < 0) { 182 dev_err(data->dev, 183 "failed to read pressure calibration parameters\n"); 184 return ret; 185 } 186 187 /* Toss the pressure calibration data into the entropy pool */ 188 add_device_randomness(p_buf, sizeof(p_buf)); 189 190 calib->P1 = le16_to_cpu(p_buf[P1]); 191 calib->P2 = le16_to_cpu(p_buf[P2]); 192 calib->P3 = le16_to_cpu(p_buf[P3]); 193 calib->P4 = le16_to_cpu(p_buf[P4]); 194 calib->P5 = le16_to_cpu(p_buf[P5]); 195 calib->P6 = le16_to_cpu(p_buf[P6]); 196 calib->P7 = le16_to_cpu(p_buf[P7]); 197 calib->P8 = le16_to_cpu(p_buf[P8]); 198 calib->P9 = le16_to_cpu(p_buf[P9]); 199 200 /* 201 * Read humidity calibration values. 202 * Due to some odd register addressing we cannot just 203 * do a big bulk read. Instead, we have to read each Hx 204 * value separately and sometimes do some bit shifting... 205 * Humidity data is only available on BME280. 206 */ 207 if (chip != BME280_CHIP_ID) 208 return 0; 209 210 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &tmp); 211 if (ret < 0) { 212 dev_err(dev, "failed to read H1 comp value\n"); 213 return ret; 214 } 215 calib->H1 = tmp; 216 217 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &l16, 2); 218 if (ret < 0) { 219 dev_err(dev, "failed to read H2 comp value\n"); 220 return ret; 221 } 222 calib->H2 = sign_extend32(le16_to_cpu(l16), 15); 223 224 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp); 225 if (ret < 0) { 226 dev_err(dev, "failed to read H3 comp value\n"); 227 return ret; 228 } 229 calib->H3 = tmp; 230 231 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &b16, 2); 232 if (ret < 0) { 233 dev_err(dev, "failed to read H4 comp value\n"); 234 return ret; 235 } 236 calib->H4 = sign_extend32(((be16_to_cpu(b16) >> 4) & 0xff0) | 237 (be16_to_cpu(b16) & 0xf), 11); 238 239 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &l16, 2); 240 if (ret < 0) { 241 dev_err(dev, "failed to read H5 comp value\n"); 242 return ret; 243 } 244 calib->H5 = sign_extend32(((le16_to_cpu(l16) >> 4) & 0xfff), 11); 245 246 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp); 247 if (ret < 0) { 248 dev_err(dev, "failed to read H6 comp value\n"); 249 return ret; 250 } 251 calib->H6 = sign_extend32(tmp, 7); 252 253 return 0; 254 } 255 /* 256 * Returns humidity in percent, resolution is 0.01 percent. Output value of 257 * "47445" represents 47445/1024 = 46.333 %RH. 258 * 259 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula". 260 */ 261 static u32 bmp280_compensate_humidity(struct bmp280_data *data, 262 s32 adc_humidity) 263 { 264 s32 var; 265 struct bmp280_calib *calib = &data->calib.bmp280; 266 267 var = ((s32)data->t_fine) - (s32)76800; 268 var = ((((adc_humidity << 14) - (calib->H4 << 20) - (calib->H5 * var)) 269 + (s32)16384) >> 15) * (((((((var * calib->H6) >> 10) 270 * (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10) 271 + (s32)2097152) * calib->H2 + 8192) >> 14); 272 var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4; 273 274 var = clamp_val(var, 0, 419430400); 275 276 return var >> 12; 277 }; 278 279 /* 280 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of 281 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global 282 * value. 283 * 284 * Taken from datasheet, Section 3.11.3, "Compensation formula". 285 */ 286 static s32 bmp280_compensate_temp(struct bmp280_data *data, 287 s32 adc_temp) 288 { 289 s32 var1, var2; 290 struct bmp280_calib *calib = &data->calib.bmp280; 291 292 var1 = (((adc_temp >> 3) - ((s32)calib->T1 << 1)) * 293 ((s32)calib->T2)) >> 11; 294 var2 = (((((adc_temp >> 4) - ((s32)calib->T1)) * 295 ((adc_temp >> 4) - ((s32)calib->T1))) >> 12) * 296 ((s32)calib->T3)) >> 14; 297 data->t_fine = var1 + var2; 298 299 return (data->t_fine * 5 + 128) >> 8; 300 } 301 302 /* 303 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 304 * integer bits and 8 fractional bits). Output value of "24674867" 305 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa 306 * 307 * Taken from datasheet, Section 3.11.3, "Compensation formula". 308 */ 309 static u32 bmp280_compensate_press(struct bmp280_data *data, 310 s32 adc_press) 311 { 312 s64 var1, var2, p; 313 struct bmp280_calib *calib = &data->calib.bmp280; 314 315 var1 = ((s64)data->t_fine) - 128000; 316 var2 = var1 * var1 * (s64)calib->P6; 317 var2 += (var1 * (s64)calib->P5) << 17; 318 var2 += ((s64)calib->P4) << 35; 319 var1 = ((var1 * var1 * (s64)calib->P3) >> 8) + 320 ((var1 * (s64)calib->P2) << 12); 321 var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33; 322 323 if (var1 == 0) 324 return 0; 325 326 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125; 327 p = div64_s64(p, var1); 328 var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25; 329 var2 = ((s64)(calib->P8) * p) >> 19; 330 p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4); 331 332 return (u32)p; 333 } 334 335 static int bmp280_read_temp(struct bmp280_data *data, 336 int *val) 337 { 338 int ret; 339 __be32 tmp = 0; 340 s32 adc_temp, comp_temp; 341 342 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, &tmp, 3); 343 if (ret < 0) { 344 dev_err(data->dev, "failed to read temperature\n"); 345 return ret; 346 } 347 348 adc_temp = be32_to_cpu(tmp) >> 12; 349 if (adc_temp == BMP280_TEMP_SKIPPED) { 350 /* reading was skipped */ 351 dev_err(data->dev, "reading temperature skipped\n"); 352 return -EIO; 353 } 354 comp_temp = bmp280_compensate_temp(data, adc_temp); 355 356 /* 357 * val might be NULL if we're called by the read_press routine, 358 * who only cares about the carry over t_fine value. 359 */ 360 if (val) { 361 *val = comp_temp * 10; 362 return IIO_VAL_INT; 363 } 364 365 return 0; 366 } 367 368 static int bmp280_read_press(struct bmp280_data *data, 369 int *val, int *val2) 370 { 371 int ret; 372 __be32 tmp = 0; 373 s32 adc_press; 374 u32 comp_press; 375 376 /* Read and compensate temperature so we get a reading of t_fine. */ 377 ret = bmp280_read_temp(data, NULL); 378 if (ret < 0) 379 return ret; 380 381 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, &tmp, 3); 382 if (ret < 0) { 383 dev_err(data->dev, "failed to read pressure\n"); 384 return ret; 385 } 386 387 adc_press = be32_to_cpu(tmp) >> 12; 388 if (adc_press == BMP280_PRESS_SKIPPED) { 389 /* reading was skipped */ 390 dev_err(data->dev, "reading pressure skipped\n"); 391 return -EIO; 392 } 393 comp_press = bmp280_compensate_press(data, adc_press); 394 395 *val = comp_press; 396 *val2 = 256000; 397 398 return IIO_VAL_FRACTIONAL; 399 } 400 401 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2) 402 { 403 __be16 tmp; 404 int ret; 405 s32 adc_humidity; 406 u32 comp_humidity; 407 408 /* Read and compensate temperature so we get a reading of t_fine. */ 409 ret = bmp280_read_temp(data, NULL); 410 if (ret < 0) 411 return ret; 412 413 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB, &tmp, 2); 414 if (ret < 0) { 415 dev_err(data->dev, "failed to read humidity\n"); 416 return ret; 417 } 418 419 adc_humidity = be16_to_cpu(tmp); 420 if (adc_humidity == BMP280_HUMIDITY_SKIPPED) { 421 /* reading was skipped */ 422 dev_err(data->dev, "reading humidity skipped\n"); 423 return -EIO; 424 } 425 comp_humidity = bmp280_compensate_humidity(data, adc_humidity); 426 427 *val = comp_humidity * 1000 / 1024; 428 429 return IIO_VAL_INT; 430 } 431 432 static int bmp280_read_raw(struct iio_dev *indio_dev, 433 struct iio_chan_spec const *chan, 434 int *val, int *val2, long mask) 435 { 436 int ret; 437 struct bmp280_data *data = iio_priv(indio_dev); 438 439 pm_runtime_get_sync(data->dev); 440 mutex_lock(&data->lock); 441 442 switch (mask) { 443 case IIO_CHAN_INFO_PROCESSED: 444 switch (chan->type) { 445 case IIO_HUMIDITYRELATIVE: 446 ret = data->chip_info->read_humid(data, val, val2); 447 break; 448 case IIO_PRESSURE: 449 ret = data->chip_info->read_press(data, val, val2); 450 break; 451 case IIO_TEMP: 452 ret = data->chip_info->read_temp(data, val); 453 break; 454 default: 455 ret = -EINVAL; 456 break; 457 } 458 break; 459 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 460 switch (chan->type) { 461 case IIO_HUMIDITYRELATIVE: 462 *val = 1 << data->oversampling_humid; 463 ret = IIO_VAL_INT; 464 break; 465 case IIO_PRESSURE: 466 *val = 1 << data->oversampling_press; 467 ret = IIO_VAL_INT; 468 break; 469 case IIO_TEMP: 470 *val = 1 << data->oversampling_temp; 471 ret = IIO_VAL_INT; 472 break; 473 default: 474 ret = -EINVAL; 475 break; 476 } 477 break; 478 default: 479 ret = -EINVAL; 480 break; 481 } 482 483 mutex_unlock(&data->lock); 484 pm_runtime_mark_last_busy(data->dev); 485 pm_runtime_put_autosuspend(data->dev); 486 487 return ret; 488 } 489 490 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data, 491 int val) 492 { 493 int i; 494 const int *avail = data->chip_info->oversampling_humid_avail; 495 const int n = data->chip_info->num_oversampling_humid_avail; 496 497 for (i = 0; i < n; i++) { 498 if (avail[i] == val) { 499 data->oversampling_humid = ilog2(val); 500 501 return data->chip_info->chip_config(data); 502 } 503 } 504 return -EINVAL; 505 } 506 507 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data, 508 int val) 509 { 510 int i; 511 const int *avail = data->chip_info->oversampling_temp_avail; 512 const int n = data->chip_info->num_oversampling_temp_avail; 513 514 for (i = 0; i < n; i++) { 515 if (avail[i] == val) { 516 data->oversampling_temp = ilog2(val); 517 518 return data->chip_info->chip_config(data); 519 } 520 } 521 return -EINVAL; 522 } 523 524 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data, 525 int val) 526 { 527 int i; 528 const int *avail = data->chip_info->oversampling_press_avail; 529 const int n = data->chip_info->num_oversampling_press_avail; 530 531 for (i = 0; i < n; i++) { 532 if (avail[i] == val) { 533 data->oversampling_press = ilog2(val); 534 535 return data->chip_info->chip_config(data); 536 } 537 } 538 return -EINVAL; 539 } 540 541 static int bmp280_write_raw(struct iio_dev *indio_dev, 542 struct iio_chan_spec const *chan, 543 int val, int val2, long mask) 544 { 545 int ret = 0; 546 struct bmp280_data *data = iio_priv(indio_dev); 547 548 switch (mask) { 549 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 550 pm_runtime_get_sync(data->dev); 551 mutex_lock(&data->lock); 552 switch (chan->type) { 553 case IIO_HUMIDITYRELATIVE: 554 ret = bmp280_write_oversampling_ratio_humid(data, val); 555 break; 556 case IIO_PRESSURE: 557 ret = bmp280_write_oversampling_ratio_press(data, val); 558 break; 559 case IIO_TEMP: 560 ret = bmp280_write_oversampling_ratio_temp(data, val); 561 break; 562 default: 563 ret = -EINVAL; 564 break; 565 } 566 mutex_unlock(&data->lock); 567 pm_runtime_mark_last_busy(data->dev); 568 pm_runtime_put_autosuspend(data->dev); 569 break; 570 default: 571 return -EINVAL; 572 } 573 574 return ret; 575 } 576 577 static int bmp280_read_avail(struct iio_dev *indio_dev, 578 struct iio_chan_spec const *chan, 579 const int **vals, int *type, int *length, 580 long mask) 581 { 582 struct bmp280_data *data = iio_priv(indio_dev); 583 584 switch (mask) { 585 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 586 switch (chan->type) { 587 case IIO_PRESSURE: 588 *vals = data->chip_info->oversampling_press_avail; 589 *length = data->chip_info->num_oversampling_press_avail; 590 break; 591 case IIO_TEMP: 592 *vals = data->chip_info->oversampling_temp_avail; 593 *length = data->chip_info->num_oversampling_temp_avail; 594 break; 595 default: 596 return -EINVAL; 597 } 598 *type = IIO_VAL_INT; 599 return IIO_AVAIL_LIST; 600 default: 601 return -EINVAL; 602 } 603 } 604 605 static const struct iio_info bmp280_info = { 606 .read_raw = &bmp280_read_raw, 607 .read_avail = &bmp280_read_avail, 608 .write_raw = &bmp280_write_raw, 609 }; 610 611 static int bmp280_chip_config(struct bmp280_data *data) 612 { 613 int ret; 614 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) | 615 BMP280_OSRS_PRESS_X(data->oversampling_press + 1); 616 617 ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS, 618 BMP280_OSRS_TEMP_MASK | 619 BMP280_OSRS_PRESS_MASK | 620 BMP280_MODE_MASK, 621 osrs | BMP280_MODE_NORMAL); 622 if (ret < 0) { 623 dev_err(data->dev, 624 "failed to write ctrl_meas register\n"); 625 return ret; 626 } 627 628 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG, 629 BMP280_FILTER_MASK, 630 BMP280_FILTER_4X); 631 if (ret < 0) { 632 dev_err(data->dev, 633 "failed to write config register\n"); 634 return ret; 635 } 636 637 return ret; 638 } 639 640 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 }; 641 642 static const struct bmp280_chip_info bmp280_chip_info = { 643 .oversampling_temp_avail = bmp280_oversampling_avail, 644 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), 645 646 .oversampling_press_avail = bmp280_oversampling_avail, 647 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), 648 649 .chip_config = bmp280_chip_config, 650 .read_temp = bmp280_read_temp, 651 .read_press = bmp280_read_press, 652 }; 653 654 static int bme280_chip_config(struct bmp280_data *data) 655 { 656 int ret; 657 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1); 658 659 /* 660 * Oversampling of humidity must be set before oversampling of 661 * temperature/pressure is set to become effective. 662 */ 663 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY, 664 BMP280_OSRS_HUMIDITY_MASK, osrs); 665 666 if (ret < 0) 667 return ret; 668 669 return bmp280_chip_config(data); 670 } 671 672 static const struct bmp280_chip_info bme280_chip_info = { 673 .oversampling_temp_avail = bmp280_oversampling_avail, 674 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), 675 676 .oversampling_press_avail = bmp280_oversampling_avail, 677 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), 678 679 .oversampling_humid_avail = bmp280_oversampling_avail, 680 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail), 681 682 .chip_config = bme280_chip_config, 683 .read_temp = bmp280_read_temp, 684 .read_press = bmp280_read_press, 685 .read_humid = bmp280_read_humid, 686 }; 687 688 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas) 689 { 690 int ret; 691 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 }; 692 unsigned int delay_us; 693 unsigned int ctrl; 694 695 if (data->use_eoc) 696 reinit_completion(&data->done); 697 698 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas); 699 if (ret) 700 return ret; 701 702 if (data->use_eoc) { 703 /* 704 * If we have a completion interrupt, use it, wait up to 705 * 100ms. The longest conversion time listed is 76.5 ms for 706 * advanced resolution mode. 707 */ 708 ret = wait_for_completion_timeout(&data->done, 709 1 + msecs_to_jiffies(100)); 710 if (!ret) 711 dev_err(data->dev, "timeout waiting for completion\n"); 712 } else { 713 if (ctrl_meas == BMP180_MEAS_TEMP) 714 delay_us = 4500; 715 else 716 delay_us = 717 conversion_time_max[data->oversampling_press]; 718 719 usleep_range(delay_us, delay_us + 1000); 720 } 721 722 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl); 723 if (ret) 724 return ret; 725 726 /* The value of this bit reset to "0" after conversion is complete */ 727 if (ctrl & BMP180_MEAS_SCO) 728 return -EIO; 729 730 return 0; 731 } 732 733 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val) 734 { 735 __be16 tmp; 736 int ret; 737 738 ret = bmp180_measure(data, BMP180_MEAS_TEMP); 739 if (ret) 740 return ret; 741 742 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, &tmp, 2); 743 if (ret) 744 return ret; 745 746 *val = be16_to_cpu(tmp); 747 748 return 0; 749 } 750 751 static int bmp180_read_calib(struct bmp280_data *data, 752 struct bmp180_calib *calib) 753 { 754 int ret; 755 int i; 756 __be16 buf[BMP180_REG_CALIB_COUNT / 2]; 757 758 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf, 759 sizeof(buf)); 760 761 if (ret < 0) 762 return ret; 763 764 /* None of the words has the value 0 or 0xFFFF */ 765 for (i = 0; i < ARRAY_SIZE(buf); i++) { 766 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff)) 767 return -EIO; 768 } 769 770 /* Toss the calibration data into the entropy pool */ 771 add_device_randomness(buf, sizeof(buf)); 772 773 calib->AC1 = be16_to_cpu(buf[AC1]); 774 calib->AC2 = be16_to_cpu(buf[AC2]); 775 calib->AC3 = be16_to_cpu(buf[AC3]); 776 calib->AC4 = be16_to_cpu(buf[AC4]); 777 calib->AC5 = be16_to_cpu(buf[AC5]); 778 calib->AC6 = be16_to_cpu(buf[AC6]); 779 calib->B1 = be16_to_cpu(buf[B1]); 780 calib->B2 = be16_to_cpu(buf[B2]); 781 calib->MB = be16_to_cpu(buf[MB]); 782 calib->MC = be16_to_cpu(buf[MC]); 783 calib->MD = be16_to_cpu(buf[MD]); 784 785 return 0; 786 } 787 788 /* 789 * Returns temperature in DegC, resolution is 0.1 DegC. 790 * t_fine carries fine temperature as global value. 791 * 792 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature". 793 */ 794 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp) 795 { 796 s32 x1, x2; 797 struct bmp180_calib *calib = &data->calib.bmp180; 798 799 x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15; 800 x2 = (calib->MC << 11) / (x1 + calib->MD); 801 data->t_fine = x1 + x2; 802 803 return (data->t_fine + 8) >> 4; 804 } 805 806 static int bmp180_read_temp(struct bmp280_data *data, int *val) 807 { 808 int ret; 809 s32 adc_temp, comp_temp; 810 811 ret = bmp180_read_adc_temp(data, &adc_temp); 812 if (ret) 813 return ret; 814 815 comp_temp = bmp180_compensate_temp(data, adc_temp); 816 817 /* 818 * val might be NULL if we're called by the read_press routine, 819 * who only cares about the carry over t_fine value. 820 */ 821 if (val) { 822 *val = comp_temp * 100; 823 return IIO_VAL_INT; 824 } 825 826 return 0; 827 } 828 829 static int bmp180_read_adc_press(struct bmp280_data *data, int *val) 830 { 831 int ret; 832 __be32 tmp = 0; 833 u8 oss = data->oversampling_press; 834 835 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss)); 836 if (ret) 837 return ret; 838 839 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, &tmp, 3); 840 if (ret) 841 return ret; 842 843 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss); 844 845 return 0; 846 } 847 848 /* 849 * Returns pressure in Pa, resolution is 1 Pa. 850 * 851 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature". 852 */ 853 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press) 854 { 855 s32 x1, x2, x3, p; 856 s32 b3, b6; 857 u32 b4, b7; 858 s32 oss = data->oversampling_press; 859 struct bmp180_calib *calib = &data->calib.bmp180; 860 861 b6 = data->t_fine - 4000; 862 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11; 863 x2 = calib->AC2 * b6 >> 11; 864 x3 = x1 + x2; 865 b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4; 866 x1 = calib->AC3 * b6 >> 13; 867 x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16; 868 x3 = (x1 + x2 + 2) >> 2; 869 b4 = calib->AC4 * (u32)(x3 + 32768) >> 15; 870 b7 = ((u32)adc_press - b3) * (50000 >> oss); 871 if (b7 < 0x80000000) 872 p = (b7 * 2) / b4; 873 else 874 p = (b7 / b4) * 2; 875 876 x1 = (p >> 8) * (p >> 8); 877 x1 = (x1 * 3038) >> 16; 878 x2 = (-7357 * p) >> 16; 879 880 return p + ((x1 + x2 + 3791) >> 4); 881 } 882 883 static int bmp180_read_press(struct bmp280_data *data, 884 int *val, int *val2) 885 { 886 int ret; 887 s32 adc_press; 888 u32 comp_press; 889 890 /* Read and compensate temperature so we get a reading of t_fine. */ 891 ret = bmp180_read_temp(data, NULL); 892 if (ret) 893 return ret; 894 895 ret = bmp180_read_adc_press(data, &adc_press); 896 if (ret) 897 return ret; 898 899 comp_press = bmp180_compensate_press(data, adc_press); 900 901 *val = comp_press; 902 *val2 = 1000; 903 904 return IIO_VAL_FRACTIONAL; 905 } 906 907 static int bmp180_chip_config(struct bmp280_data *data) 908 { 909 return 0; 910 } 911 912 static const int bmp180_oversampling_temp_avail[] = { 1 }; 913 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 }; 914 915 static const struct bmp280_chip_info bmp180_chip_info = { 916 .oversampling_temp_avail = bmp180_oversampling_temp_avail, 917 .num_oversampling_temp_avail = 918 ARRAY_SIZE(bmp180_oversampling_temp_avail), 919 920 .oversampling_press_avail = bmp180_oversampling_press_avail, 921 .num_oversampling_press_avail = 922 ARRAY_SIZE(bmp180_oversampling_press_avail), 923 924 .chip_config = bmp180_chip_config, 925 .read_temp = bmp180_read_temp, 926 .read_press = bmp180_read_press, 927 }; 928 929 static irqreturn_t bmp085_eoc_irq(int irq, void *d) 930 { 931 struct bmp280_data *data = d; 932 933 complete(&data->done); 934 935 return IRQ_HANDLED; 936 } 937 938 static int bmp085_fetch_eoc_irq(struct device *dev, 939 const char *name, 940 int irq, 941 struct bmp280_data *data) 942 { 943 unsigned long irq_trig; 944 int ret; 945 946 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq)); 947 if (irq_trig != IRQF_TRIGGER_RISING) { 948 dev_err(dev, "non-rising trigger given for EOC interrupt, trying to enforce it\n"); 949 irq_trig = IRQF_TRIGGER_RISING; 950 } 951 952 init_completion(&data->done); 953 954 ret = devm_request_threaded_irq(dev, 955 irq, 956 bmp085_eoc_irq, 957 NULL, 958 irq_trig, 959 name, 960 data); 961 if (ret) { 962 /* Bail out without IRQ but keep the driver in place */ 963 dev_err(dev, "unable to request DRDY IRQ\n"); 964 return 0; 965 } 966 967 data->use_eoc = true; 968 return 0; 969 } 970 971 static void bmp280_pm_disable(void *data) 972 { 973 struct device *dev = data; 974 975 pm_runtime_get_sync(dev); 976 pm_runtime_put_noidle(dev); 977 pm_runtime_disable(dev); 978 } 979 980 static void bmp280_regulators_disable(void *data) 981 { 982 struct regulator_bulk_data *supplies = data; 983 984 regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies); 985 } 986 987 int bmp280_common_probe(struct device *dev, 988 struct regmap *regmap, 989 unsigned int chip, 990 const char *name, 991 int irq) 992 { 993 int ret; 994 struct iio_dev *indio_dev; 995 struct bmp280_data *data; 996 unsigned int chip_id; 997 struct gpio_desc *gpiod; 998 999 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 1000 if (!indio_dev) 1001 return -ENOMEM; 1002 1003 data = iio_priv(indio_dev); 1004 mutex_init(&data->lock); 1005 data->dev = dev; 1006 1007 indio_dev->name = name; 1008 indio_dev->channels = bmp280_channels; 1009 indio_dev->info = &bmp280_info; 1010 indio_dev->modes = INDIO_DIRECT_MODE; 1011 1012 switch (chip) { 1013 case BMP180_CHIP_ID: 1014 indio_dev->num_channels = 2; 1015 data->chip_info = &bmp180_chip_info; 1016 data->oversampling_press = ilog2(8); 1017 data->oversampling_temp = ilog2(1); 1018 data->start_up_time = 10000; 1019 break; 1020 case BMP280_CHIP_ID: 1021 indio_dev->num_channels = 2; 1022 data->chip_info = &bmp280_chip_info; 1023 data->oversampling_press = ilog2(16); 1024 data->oversampling_temp = ilog2(2); 1025 data->start_up_time = 2000; 1026 break; 1027 case BME280_CHIP_ID: 1028 indio_dev->num_channels = 3; 1029 data->chip_info = &bme280_chip_info; 1030 data->oversampling_press = ilog2(16); 1031 data->oversampling_humid = ilog2(16); 1032 data->oversampling_temp = ilog2(2); 1033 data->start_up_time = 2000; 1034 break; 1035 default: 1036 return -EINVAL; 1037 } 1038 1039 /* Bring up regulators */ 1040 regulator_bulk_set_supply_names(data->supplies, 1041 bmp280_supply_names, 1042 BMP280_NUM_SUPPLIES); 1043 1044 ret = devm_regulator_bulk_get(dev, 1045 BMP280_NUM_SUPPLIES, data->supplies); 1046 if (ret) { 1047 dev_err(dev, "failed to get regulators\n"); 1048 return ret; 1049 } 1050 1051 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies); 1052 if (ret) { 1053 dev_err(dev, "failed to enable regulators\n"); 1054 return ret; 1055 } 1056 1057 ret = devm_add_action_or_reset(dev, bmp280_regulators_disable, 1058 data->supplies); 1059 if (ret) 1060 return ret; 1061 1062 /* Wait to make sure we started up properly */ 1063 usleep_range(data->start_up_time, data->start_up_time + 100); 1064 1065 /* Bring chip out of reset if there is an assigned GPIO line */ 1066 gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 1067 /* Deassert the signal */ 1068 if (gpiod) { 1069 dev_info(dev, "release reset\n"); 1070 gpiod_set_value(gpiod, 0); 1071 } 1072 1073 data->regmap = regmap; 1074 ret = regmap_read(regmap, BMP280_REG_ID, &chip_id); 1075 if (ret < 0) 1076 return ret; 1077 if (chip_id != chip) { 1078 dev_err(dev, "bad chip id: expected %x got %x\n", 1079 chip, chip_id); 1080 return -EINVAL; 1081 } 1082 1083 ret = data->chip_info->chip_config(data); 1084 if (ret < 0) 1085 return ret; 1086 1087 dev_set_drvdata(dev, indio_dev); 1088 1089 /* 1090 * Some chips have calibration parameters "programmed into the devices' 1091 * non-volatile memory during production". Let's read them out at probe 1092 * time once. They will not change. 1093 */ 1094 if (chip_id == BMP180_CHIP_ID) { 1095 ret = bmp180_read_calib(data, &data->calib.bmp180); 1096 if (ret < 0) { 1097 dev_err(data->dev, 1098 "failed to read calibration coefficients\n"); 1099 return ret; 1100 } 1101 } else if (chip_id == BMP280_CHIP_ID || chip_id == BME280_CHIP_ID) { 1102 ret = bmp280_read_calib(data, &data->calib.bmp280, chip_id); 1103 if (ret < 0) { 1104 dev_err(data->dev, 1105 "failed to read calibration coefficients\n"); 1106 return ret; 1107 } 1108 } 1109 1110 /* 1111 * Attempt to grab an optional EOC IRQ - only the BMP085 has this 1112 * however as it happens, the BMP085 shares the chip ID of BMP180 1113 * so we look for an IRQ if we have that. 1114 */ 1115 if (irq > 0 || (chip_id == BMP180_CHIP_ID)) { 1116 ret = bmp085_fetch_eoc_irq(dev, name, irq, data); 1117 if (ret) 1118 return ret; 1119 } 1120 1121 /* Enable runtime PM */ 1122 pm_runtime_get_noresume(dev); 1123 pm_runtime_set_active(dev); 1124 pm_runtime_enable(dev); 1125 /* 1126 * Set autosuspend to two orders of magnitude larger than the 1127 * start-up time. 1128 */ 1129 pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10); 1130 pm_runtime_use_autosuspend(dev); 1131 pm_runtime_put(dev); 1132 1133 ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev); 1134 if (ret) 1135 return ret; 1136 1137 return devm_iio_device_register(dev, indio_dev); 1138 } 1139 EXPORT_SYMBOL(bmp280_common_probe); 1140 1141 static int bmp280_runtime_suspend(struct device *dev) 1142 { 1143 struct iio_dev *indio_dev = dev_get_drvdata(dev); 1144 struct bmp280_data *data = iio_priv(indio_dev); 1145 1146 return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies); 1147 } 1148 1149 static int bmp280_runtime_resume(struct device *dev) 1150 { 1151 struct iio_dev *indio_dev = dev_get_drvdata(dev); 1152 struct bmp280_data *data = iio_priv(indio_dev); 1153 int ret; 1154 1155 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies); 1156 if (ret) 1157 return ret; 1158 usleep_range(data->start_up_time, data->start_up_time + 100); 1159 return data->chip_info->chip_config(data); 1160 } 1161 1162 EXPORT_RUNTIME_DEV_PM_OPS(bmp280_dev_pm_ops, bmp280_runtime_suspend, 1163 bmp280_runtime_resume, NULL); 1164 1165 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); 1166 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor"); 1167 MODULE_LICENSE("GPL v2"); 1168