|
10 | 10 | */
|
11 | 11 | #include <linux/acpi.h>
|
12 | 12 | #include <linux/bitfield.h>
|
| 13 | +#include <linux/delay.h> |
13 | 14 | #include <linux/device.h>
|
14 | 15 | #include <linux/module.h>
|
15 | 16 | #include <linux/log2.h>
|
@@ -532,6 +533,43 @@ static u8 bme680_oversampling_to_reg(u8 val)
|
532 | 533 | return ilog2(val) + 1;
|
533 | 534 | }
|
534 | 535 |
|
| 536 | +/* |
| 537 | + * Taken from Bosch BME680 API: |
| 538 | + * https://github.com/boschsensortec/BME68x_SensorAPI/blob/v4.4.8/bme68x.c#L490 |
| 539 | + */ |
| 540 | +static int bme680_wait_for_eoc(struct bme680_data *data) |
| 541 | +{ |
| 542 | + struct device *dev = regmap_get_device(data->regmap); |
| 543 | + unsigned int check; |
| 544 | + int ret; |
| 545 | + /* |
| 546 | + * (Sum of oversampling ratios * time per oversampling) + |
| 547 | + * TPH measurement + gas measurement + wait transition from forced mode |
| 548 | + * + heater duration |
| 549 | + */ |
| 550 | + int wait_eoc_us = ((data->oversampling_temp + data->oversampling_press + |
| 551 | + data->oversampling_humid) * 1936) + (477 * 4) + |
| 552 | + (477 * 5) + 1000 + (data->heater_dur * 1000); |
| 553 | + |
| 554 | + usleep_range(wait_eoc_us, wait_eoc_us + 100); |
| 555 | + |
| 556 | + ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check); |
| 557 | + if (ret) { |
| 558 | + dev_err(dev, "failed to read measurement status register.\n"); |
| 559 | + return ret; |
| 560 | + } |
| 561 | + if (check & BME680_MEAS_BIT) { |
| 562 | + dev_err(dev, "Device measurement cycle incomplete.\n"); |
| 563 | + return -EBUSY; |
| 564 | + } |
| 565 | + if (!(check & BME680_NEW_DATA_BIT)) { |
| 566 | + dev_err(dev, "No new data available from the device.\n"); |
| 567 | + return -ENODATA; |
| 568 | + } |
| 569 | + |
| 570 | + return 0; |
| 571 | +} |
| 572 | + |
535 | 573 | static int bme680_chip_config(struct bme680_data *data)
|
536 | 574 | {
|
537 | 575 | struct device *dev = regmap_get_device(data->regmap);
|
@@ -622,6 +660,10 @@ static int bme680_read_temp(struct bme680_data *data, int *val)
|
622 | 660 | if (ret < 0)
|
623 | 661 | return ret;
|
624 | 662 |
|
| 663 | + ret = bme680_wait_for_eoc(data); |
| 664 | + if (ret) |
| 665 | + return ret; |
| 666 | + |
625 | 667 | ret = regmap_bulk_read(data->regmap, BME680_REG_TEMP_MSB,
|
626 | 668 | &tmp, 3);
|
627 | 669 | if (ret < 0) {
|
@@ -738,6 +780,10 @@ static int bme680_read_gas(struct bme680_data *data,
|
738 | 780 | if (ret < 0)
|
739 | 781 | return ret;
|
740 | 782 |
|
| 783 | + ret = bme680_wait_for_eoc(data); |
| 784 | + if (ret) |
| 785 | + return ret; |
| 786 | + |
741 | 787 | ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check);
|
742 | 788 | if (check & BME680_GAS_MEAS_BIT) {
|
743 | 789 | dev_err(dev, "gas measurement incomplete\n");
|
|
0 commit comments