From: David Frey <dpfrey@gmail.com>
To: linux-iio@vger.kernel.org
Cc: jic23@kernel.org, himanshujha199640@gmail.com,
David Frey <dpfrey@gmail.com>
Subject: [PATCH v3 7/7] iio: chemical: bme680: simplify oversampling handling
Date: Fri, 17 Aug 2018 12:03:19 -0700 [thread overview]
Message-ID: <20180817190319.13119-8-dpfrey@gmail.com> (raw)
In-Reply-To: <20180817190319.13119-1-dpfrey@gmail.com>
Temperature, pressure and humidity all expose and oversampling setting
that works in the same way. Provide common handling for the
oversampling sysfs attributes.
Signed-off-by: David Frey <dpfrey@gmail.com>
---
drivers/iio/chemical/bme680_core.c | 99 ++++++++++++++------------------------
1 file changed, 36 insertions(+), 63 deletions(-)
diff --git a/drivers/iio/chemical/bme680_core.c b/drivers/iio/chemical/bme680_core.c
index 46afc93041f5..30614ef043ca 100644
--- a/drivers/iio/chemical/bme680_core.c
+++ b/drivers/iio/chemical/bme680_core.c
@@ -91,8 +91,6 @@ static const struct iio_chan_spec bme680_channels[] = {
},
};
-static const int bme680_oversampling_avail[] = { 1, 2, 4, 8, 16 };
-
static int bme680_read_calib(struct bme680_data *data,
struct bme680_calib *calib)
{
@@ -503,12 +501,20 @@ static int bme680_set_mode(struct bme680_data *data, bool mode)
return ret;
}
+static u8 bme680_oversampling_to_reg(u8 val)
+{
+ return ilog2(val) + 1;
+}
+
static int bme680_chip_config(struct bme680_data *data)
{
struct device *dev = regmap_get_device(data->regmap);
int ret;
- u8 osrs = FIELD_PREP(BME680_OSRS_HUMIDITY_MASK,
- data->oversampling_humid + 1);
+ u8 osrs;
+
+ osrs = FIELD_PREP(
+ BME680_OSRS_HUMIDITY_MASK,
+ bme680_oversampling_to_reg(data->oversampling_humid));
/*
* Highly recommended to set oversampling of humidity before
* temperature/pressure oversampling.
@@ -529,12 +535,12 @@ static int bme680_chip_config(struct bme680_data *data)
return ret;
}
- osrs = FIELD_PREP(BME680_OSRS_TEMP_MASK, data->oversampling_temp + 1) |
- FIELD_PREP(BME680_OSRS_PRESS_MASK, data->oversampling_press + 1);
-
+ osrs = FIELD_PREP(BME680_OSRS_TEMP_MASK,
+ bme680_oversampling_to_reg(data->oversampling_temp)) |
+ FIELD_PREP(BME680_OSRS_PRESS_MASK,
+ bme680_oversampling_to_reg(data->oversampling_press));
ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS,
- BME680_OSRS_TEMP_MASK |
- BME680_OSRS_PRESS_MASK,
+ BME680_OSRS_TEMP_MASK | BME680_OSRS_PRESS_MASK,
osrs);
if (ret < 0)
dev_err(dev, "failed to write ctrl_meas register\n");
@@ -767,13 +773,13 @@ static int bme680_read_raw(struct iio_dev *indio_dev,
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
switch (chan->type) {
case IIO_TEMP:
- *val = 1 << data->oversampling_temp;
+ *val = data->oversampling_temp;
return IIO_VAL_INT;
case IIO_PRESSURE:
- *val = 1 << data->oversampling_press;
+ *val = data->oversampling_press;
return IIO_VAL_INT;
case IIO_HUMIDITYRELATIVE:
- *val = 1 << data->oversampling_humid;
+ *val = data->oversampling_humid;
return IIO_VAL_INT;
default:
return -EINVAL;
@@ -783,52 +789,9 @@ static int bme680_read_raw(struct iio_dev *indio_dev,
}
}
-static int bme680_write_oversampling_ratio_temp(struct bme680_data *data,
- int val)
-{
- int i;
-
- for (i = 0; i < ARRAY_SIZE(bme680_oversampling_avail); i++) {
- if (bme680_oversampling_avail[i] == val) {
- data->oversampling_temp = ilog2(val);
-
- return bme680_chip_config(data);
- }
- }
-
- return -EINVAL;
-}
-
-static int bme680_write_oversampling_ratio_press(struct bme680_data *data,
- int val)
-{
- int i;
-
- for (i = 0; i < ARRAY_SIZE(bme680_oversampling_avail); i++) {
- if (bme680_oversampling_avail[i] == val) {
- data->oversampling_press = ilog2(val);
-
- return bme680_chip_config(data);
- }
- }
-
- return -EINVAL;
-}
-
-static int bme680_write_oversampling_ratio_humid(struct bme680_data *data,
- int val)
+static bool bme680_is_valid_oversampling(int rate)
{
- int i;
-
- for (i = 0; i < ARRAY_SIZE(bme680_oversampling_avail); i++) {
- if (bme680_oversampling_avail[i] == val) {
- data->oversampling_humid = ilog2(val);
-
- return bme680_chip_config(data);
- }
- }
-
- return -EINVAL;
+ return (rate > 0 && rate <= 16 && is_power_of_2(rate));
}
static int bme680_write_raw(struct iio_dev *indio_dev,
@@ -842,16 +805,26 @@ static int bme680_write_raw(struct iio_dev *indio_dev,
switch (mask) {
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
+ {
+ if (!bme680_is_valid_oversampling(val))
+ return -EINVAL;
+
switch (chan->type) {
case IIO_TEMP:
- return bme680_write_oversampling_ratio_temp(data, val);
+ data->oversampling_temp = val;
+ break;
case IIO_PRESSURE:
- return bme680_write_oversampling_ratio_press(data, val);
+ data->oversampling_press = val;
+ break;
case IIO_HUMIDITYRELATIVE:
- return bme680_write_oversampling_ratio_humid(data, val);
+ data->oversampling_humid = val;
+ break;
default:
return -EINVAL;
}
+
+ return bme680_chip_config(data);
+ }
default:
return -EINVAL;
}
@@ -913,9 +886,9 @@ int bme680_core_probe(struct device *dev, struct regmap *regmap,
indio_dev->modes = INDIO_DIRECT_MODE;
/* default values for the sensor */
- data->oversampling_humid = ilog2(2); /* 2X oversampling rate */
- data->oversampling_press = ilog2(4); /* 4X oversampling rate */
- data->oversampling_temp = ilog2(8); /* 8X oversampling rate */
+ data->oversampling_humid = 2; /* 2X oversampling rate */
+ data->oversampling_press = 4; /* 4X oversampling rate */
+ data->oversampling_temp = 8; /* 8X oversampling rate */
data->heater_temp = 320; /* degree Celsius */
data->heater_dur = 150; /* milliseconds */
--
2.11.0
next prev parent reply other threads:[~2018-08-17 22:07 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-17 19:03 [PATCH v3 0/7] bme680 cleanup David Frey
2018-08-17 19:03 ` [PATCH v3 1/7] iio: chemical: bme680: use clamp macro David Frey
2018-08-18 11:03 ` Himanshu Jha
2018-08-19 15:47 ` Jonathan Cameron
2018-08-20 17:18 ` David Frey
2018-08-20 17:55 ` Himanshu Jha
2018-08-20 17:58 ` Jonathan Cameron
2018-08-17 19:03 ` [PATCH v3 2/7] iio: chemical: bme680: cleanup bme680_read_calib formatting David Frey
2018-08-18 11:06 ` Himanshu Jha
2018-08-19 15:54 ` Jonathan Cameron
2018-08-19 17:18 ` Himanshu Jha
2018-08-20 19:24 ` David Frey
2018-08-21 18:46 ` Himanshu Jha
2018-08-17 19:03 ` [PATCH v3 3/7] iio: chemical: bme680: indent #defines consistently David Frey
2018-08-18 11:07 ` Himanshu Jha
2018-08-19 16:02 ` Jonathan Cameron
2018-08-19 17:28 ` Himanshu Jha
2018-08-19 19:14 ` Jonathan Cameron
2018-08-20 15:37 ` Himanshu Jha
2018-08-20 17:39 ` [PATCH] iio: chemical: bme680: Remove field value defines David Frey
2018-08-22 10:44 ` Himanshu Jha
2018-08-25 8:14 ` Jonathan Cameron
2018-08-17 19:03 ` [PATCH v3 4/7] iio: chemical: bme680: change MSK->MASK in #defines David Frey
2018-08-18 11:09 ` Himanshu Jha
2018-08-19 16:05 ` Jonathan Cameron
2018-08-17 19:03 ` [PATCH v3 5/7] iio: chemical: bme680: use GENMASK macro David Frey
2018-08-18 11:09 ` Himanshu Jha
2018-08-19 16:07 ` Jonathan Cameron
2018-08-17 19:03 ` [PATCH v3 6/7] iio: chemical: bme680: use FIELD_GET macro David Frey
2018-08-18 11:10 ` Himanshu Jha
2018-08-19 16:08 ` Jonathan Cameron
2018-08-17 19:03 ` David Frey [this message]
2018-08-18 11:17 ` [PATCH v3 7/7] iio: chemical: bme680: simplify oversampling handling Himanshu Jha
2018-08-19 16:14 ` Jonathan Cameron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180817190319.13119-8-dpfrey@gmail.com \
--to=dpfrey@gmail.com \
--cc=himanshujha199640@gmail.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).