iio: chemical: bme680: Fix sensor data read operation

A read operation is happening as follows:

a) Set sensor to forced mode
b) Sensor measures values and update data registers and sleeps again
c) Read data registers

In the current implementation the read operation happens immediately
after the sensor is set to forced mode so the sensor does not have
the time to update properly the registers. This leads to the following
2 problems:

1) The first ever value which is read by the register is always wrong
2) Every read operation, puts the register into forced mode and reads
the data that were calculated in the previous conversion.

This behaviour was tested in 2 ways:

1) The internal meas_status_0 register was read before and after every
read operation in order to verify that the data were ready even before
the register was set to forced mode and also to check that after the
forced mode was set the new data were not yet ready.

2) Physically changing the temperature and measuring the temperature

This commit adds the waiting time in between the set of the forced mode
and the read of the data. The function is taken from the Bosch BME68x
Sensor API [1].

[1]: https://github.com/boschsensortec/BME68x_SensorAPI/blob/v4.4.8/bme68x.c#L490

Fixes: 1b3bd8592780 ("iio: chemical: Add support for Bosch BME680 sensor")
Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com>
Link: https://lore.kernel.org/r/20240606212313.207550-5-vassilisamir@gmail.com
Cc: <Stable@vger.kernel.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by Vasileios Amoiridis and committed by Jonathan Cameron 4241665e fdd478c3

+48
+2
drivers/iio/chemical/bme680.h
··· 54 54 #define BME680_NB_CONV_MASK GENMASK(3, 0) 55 55 56 56 #define BME680_REG_MEAS_STAT_0 0x1D 57 + #define BME680_NEW_DATA_BIT BIT(7) 57 58 #define BME680_GAS_MEAS_BIT BIT(6) 59 + #define BME680_MEAS_BIT BIT(5) 58 60 59 61 /* Calibration Parameters */ 60 62 #define BME680_T2_LSB_REG 0x8A
+46
drivers/iio/chemical/bme680_core.c
··· 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> ··· 533 532 return ilog2(val) + 1; 534 533 } 535 534 535 + /* 536 + * Taken from Bosch BME680 API: 537 + * https://github.com/boschsensortec/BME68x_SensorAPI/blob/v4.4.8/bme68x.c#L490 538 + */ 539 + static int bme680_wait_for_eoc(struct bme680_data *data) 540 + { 541 + struct device *dev = regmap_get_device(data->regmap); 542 + unsigned int check; 543 + int ret; 544 + /* 545 + * (Sum of oversampling ratios * time per oversampling) + 546 + * TPH measurement + gas measurement + wait transition from forced mode 547 + * + heater duration 548 + */ 549 + int wait_eoc_us = ((data->oversampling_temp + data->oversampling_press + 550 + data->oversampling_humid) * 1936) + (477 * 4) + 551 + (477 * 5) + 1000 + (data->heater_dur * 1000); 552 + 553 + usleep_range(wait_eoc_us, wait_eoc_us + 100); 554 + 555 + ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check); 556 + if (ret) { 557 + dev_err(dev, "failed to read measurement status register.\n"); 558 + return ret; 559 + } 560 + if (check & BME680_MEAS_BIT) { 561 + dev_err(dev, "Device measurement cycle incomplete.\n"); 562 + return -EBUSY; 563 + } 564 + if (!(check & BME680_NEW_DATA_BIT)) { 565 + dev_err(dev, "No new data available from the device.\n"); 566 + return -ENODATA; 567 + } 568 + 569 + return 0; 570 + } 571 + 536 572 static int bme680_chip_config(struct bme680_data *data) 537 573 { 538 574 struct device *dev = regmap_get_device(data->regmap); ··· 658 620 /* set forced mode to trigger measurement */ 659 621 ret = bme680_set_mode(data, true); 660 622 if (ret < 0) 623 + return ret; 624 + 625 + ret = bme680_wait_for_eoc(data); 626 + if (ret) 661 627 return ret; 662 628 663 629 ret = regmap_bulk_read(data->regmap, BME680_REG_TEMP_MSB, ··· 778 736 /* set forced mode to trigger measurement */ 779 737 ret = bme680_set_mode(data, true); 780 738 if (ret < 0) 739 + return ret; 740 + 741 + ret = bme680_wait_for_eoc(data); 742 + if (ret) 781 743 return ret; 782 744 783 745 ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check);