From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751615AbdGYJLo (ORCPT ); Tue, 25 Jul 2017 05:11:44 -0400 Received: from mout.kundenserver.de ([212.227.126.130]:56660 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751454AbdGYJLm (ORCPT ); Tue, 25 Jul 2017 05:11:42 -0400 Date: Tue, 25 Jul 2017 11:11:14 +0200 From: Andreas Klinger To: jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, linus.walleij@linaro.org, yongjun_wei@trendmicro.com.cn, a.mathur@samsung.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] iio: bmp280: properly initialize device for humidity reading Message-ID: <20170725091114.GA25223@arbeit> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Provags-ID: V03:K0:4uF4D7+Lmdh2m6ULY9renFyYo0n3AFhyQT4kI6nvKy+QXP5iA8Y cLpFBo2QJ/tDVS7lOq/7zG0v36Zlse+fBUL5w0/P4oCqu2EAI4EdwUWJTA1cXcxoO+5xWGG +ONebzDTxPLsGsK17vpzi7R2s2GyZSzmaCsOdClMRnqVEOIaTMdzk70MoZzzTMCDn71MvMR X6ZyZdS3ToOR+pIGG681w== X-UI-Out-Filterresults: notjunk:1;V01:K0:ivqY9JX41KI=:qncnEJRMGVWf7nd7EsG6Nn w3M81H35JShxeMgXhp0Apwp8jmTLTai3/dsjm3xlxVFCmZEaM5DxznRF1T7Lw6LknIBUnrRlH 4ZsAyIs+qOYDkwrs9sB3KX6TWIXhrAVg5dcgKPlCL4LOTKGadp5xQLnVoujuDiLup7akY+PPn 7aUY79eQSS3zCrPBdvIkNQkxWeiKGebt0X8f+xbmBuPLtZO3nDrcIpo0n2BrsFw50lTBciblf IZTVrBYfps60Qnp6Duw42XcFkeicPNTgHi4CU0Oma7bqgMYU4FhaLSP2umbU++RuQFb6m3gD1 mQyFu4pyAQlzw3HNnWFZ2YqhRf4vw+bMUceomX/ZjgGbANqmvTnYAVt08oD9ODHkRTCGAknhU Bgm8FbbWwcohAAFlOOS0aRZ6cq1CemGq+Pa/5nzqbGOhQ0C3tk/Qzfsa9Fws1/h3VPJk9upSs RnawVQRZdtDj6rhgu0LdsU1n4C29xcKlBNOL7xnJnq0y0vBc2QfLDmbUS+C2qlUkqOqcIjqRc Cbx60qNefYx1qy0zphwD2uRTojvKkp2Rx+H0i6faJd2OswVAXfGew0OZd5ZFegR+xmQXVoRpV CWNjbwgmScvSePt8Fy/JasLM60L31imEOmav9P9C5giPIuxvVxAHcCdQxHyfRHhhDWhPjGkPK 159dvCNftEE1P/bx4S5MjAkxQSST61Fu1sVeLnC1ugXkJ1PXnXGvUv9DYykKqExEWiEd7Rz2H LZJY0tF48Om3SQXMeEIuSwZKK4FwjuKXWlAFFQ== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If the device is not initialized at least once it happens that the humidity reading is skipped, which means the special value 0x8000 is delivered. For omitting this case the oversampling of the humidity must be set before the oversampling of the temperature und pressure is set as written in the datasheet of the BME280. Furthermore proper error detection is added in case a skipped value is read from the device. This is done also for pressure and temperature reading. Especially it don't make sense to compensate this value and treat it as regular value. Signed-off-by: Andreas Klinger --- drivers/iio/pressure/bmp280-core.c | 27 ++++++++++++++++++++++++--- drivers/iio/pressure/bmp280.h | 5 +++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c index 4d18826ac63c..d434e2c23833 100644 --- a/drivers/iio/pressure/bmp280-core.c +++ b/drivers/iio/pressure/bmp280-core.c @@ -281,6 +281,11 @@ static int bmp280_read_temp(struct bmp280_data *data, } adc_temp = be32_to_cpu(tmp) >> 12; + if (adc_temp == BMP280_TEMP_SKIPPED) { + /* reading was skipped */ + dev_err(data->dev, "reading temperature skipped\n"); + return -EIO; + } comp_temp = bmp280_compensate_temp(data, adc_temp); /* @@ -316,6 +321,11 @@ static int bmp280_read_press(struct bmp280_data *data, } adc_press = be32_to_cpu(tmp) >> 12; + if (adc_press == BMP280_PRESS_SKIPPED) { + /* reading was skipped */ + dev_err(data->dev, "reading pressure skipped\n"); + return -EIO; + } comp_press = bmp280_compensate_press(data, adc_press); *val = comp_press; @@ -344,6 +354,11 @@ static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2) } adc_humidity = be16_to_cpu(tmp); + if (adc_humidity == BMP280_HUMIDITY_SKIPPED) { + /* reading was skipped */ + dev_err(data->dev, "reading humidity skipped\n"); + return -EIO; + } comp_humidity = bmp280_compensate_humidity(data, adc_humidity); *val = comp_humidity; @@ -596,14 +611,20 @@ static const struct bmp280_chip_info bmp280_chip_info = { static int bme280_chip_config(struct bmp280_data *data) { - int ret = bmp280_chip_config(data); + int ret; u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1); + /* + * Oversampling of humidity must be set before oversampling of + * temperature/pressure is set to become effective. + */ + ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY, + BMP280_OSRS_HUMIDITY_MASK, osrs); + if (ret < 0) return ret; - return regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY, - BMP280_OSRS_HUMIDITY_MASK, osrs); + return bmp280_chip_config(data); } static const struct bmp280_chip_info bme280_chip_info = { diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h index 2c770e13be0e..61347438b779 100644 --- a/drivers/iio/pressure/bmp280.h +++ b/drivers/iio/pressure/bmp280.h @@ -96,6 +96,11 @@ #define BME280_CHIP_ID 0x60 #define BMP280_SOFT_RESET_VAL 0xB6 +/* BMP280 register skipped special values */ +#define BMP280_TEMP_SKIPPED 0x80000 +#define BMP280_PRESS_SKIPPED 0x80000 +#define BMP280_HUMIDITY_SKIPPED 0x8000 + /* Regmap configurations */ extern const struct regmap_config bmp180_regmap_config; extern const struct regmap_config bmp280_regmap_config; -- 2.1.4