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