linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
To: Achim Gratz <Achim.Gratz@Stromeko.DE>
Cc: linux-iio@vger.kernel.org, "Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Nuno Sá" <nuno.sa@analog.com>
Subject: Re: [bmp280 v1 2/6] iio: pressure: bmp280: reduce overhead on read with MODE_FORCED
Date: Wed, 6 Aug 2025 16:58:59 +0100	[thread overview]
Message-ID: <20250806165859.000039d4@huawei.com> (raw)
In-Reply-To: <20250803140802.36888-3-Achim.Gratz@Stromeko.DE>

On Sun,  3 Aug 2025 16:07:58 +0200
Achim Gratz <Achim.Gratz@Stromeko.DE> wrote:

> When measuring with MODE_FORCED, each read through sysfs triggers a
> new measurement cycle through aLL channels with the current channel
> configuration, even though we can only access a single channel.
> Reduce the incurred overhead (especially for higher oversampling_ratio
> settings) by temporarily switching off the unused channels.  This
> savea about a third of the acquisition time when reading all three
> channels in succession:
> 
> | oversampling | max/full | max/skip | time/full | time/skip |
> |              |     [ms] |     [ms] |      [ms] |      [ms] |
> |--------------+----------+----------+-----------+-----------|
> |           16 |      339 |      120 |       315 |       127 |
> |            8 |      174 |       65 |       166 |        76 |
> |            4 |       90 |       38 |        94 |        53 |
> |            2 |       49 |       24 |        59 |        41 |
> |            1 |       28 |       17 |        36 |        33 |
> 
> The results are from an I²C connected sensor at 400kHz, so there is
> considerable overhead from the changing the channel configuration,
> most noticeably with low oversampling_ratio values.  Faster
> communication will reduce this overhead further; and since there is
> still a net reduction in acquisition time even for
> oversampling_ratio=1 switching off the channels is always done.
> 
> Note: The IIR filters will process a slightly noisier input signal.
> 
> Signed-off-by: Achim Gratz <Achim.Gratz@Stromeko.DE>
Hi Achim.

I'm not really sure what the algorithm implemented here is and what the
various local variables actually mean as state.  Please add some
more comments to the code.

> ---
>  drivers/iio/pressure/bmp280-core.c | 138 ++++++++++++++++++++++++++---
>  drivers/iio/pressure/bmp280.h      |   7 +-
>  2 files changed, 129 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
> index 3213dcadba28..858974a64306 100644
> --- a/drivers/iio/pressure/bmp280-core.c
> +++ b/drivers/iio/pressure/bmp280-core.c
> @@ -619,18 +619,66 @@ static int bmp280_read_raw_impl(struct iio_dev *indio_dev,
>  	struct bmp280_data *data = iio_priv(indio_dev);
>  	int chan_value;
>  	int ret;
> +	int prev_oversampling_humid, prev_oversampling_press, prev_oversampling_temp;
> +	int temp_oversampling_humid, temp_oversampling_press, temp_oversampling_temp;
> +	int switch_off, switch_threshold = -1;
>  
>  	guard(mutex)(&data->lock);
>  
> +	prev_oversampling_humid = temp_oversampling_humid = data->oversampling_humid;
> +	prev_oversampling_press = temp_oversampling_press = data->oversampling_press;
> +	prev_oversampling_temp  = temp_oversampling_temp  = data->oversampling_temp;
> +
>  	switch (mask) {
>  	case IIO_CHAN_INFO_PROCESSED:
> +		/* switch off unused channels */
> +		switch_off = 0;

So this is saying if 'anything can be switched off'? 
switch_off_something or some naming like that.

> +		switch (chan->type) {
> +		case IIO_HUMIDITYRELATIVE:
> +			temp_oversampling_press = 0-1;

0 - 1
etc. So always spaces around operators.
I'm not at all sure on the logic behind this maths though.
Seems it ultimately gets store into a u8.  Add some comments on what the algorithm
is doing.


> +			switch_off |= (prev_oversampling_press > switch_threshold);
> +			/* can't be switched off as it is needed for compensation */
What can't be switched off?  The temperature I guess.
> +			temp_oversampling_temp  = 1-1;
1 - 1
> +			break;
> +		case IIO_PRESSURE:
> +			temp_oversampling_humid = 0-1;
> +			switch_off |= (prev_oversampling_humid > switch_threshold);
> +			/* can't be switched off as it is needed for compensation */
> +			temp_oversampling_temp  = 1-1;
> +			break;
> +		case IIO_TEMP:
> +			temp_oversampling_humid = 0-1;
> +			temp_oversampling_press = 0-1;
> +			switch_off = (prev_oversampling_humid > switch_threshold) |
> +				     (prev_oversampling_press > switch_threshold);
> +			break;
> +		default:
> +			return -EINVAL;
> +		}
> +		if (switch_off) {
> +			data->oversampling_humid = temp_oversampling_humid;
> +			data->oversampling_press = temp_oversampling_press;
> +			data->oversampling_temp  = temp_oversampling_temp;
> +			ret = data->chip_info->chip_config(data);
> +			if (ret)
> +				goto restore;
> +		}
> +
>  		ret = data->chip_info->set_mode(data, BMP280_FORCED);
>  		if (ret)
> -			return ret;
> -
> +			goto restore;
>  		ret = data->chip_info->wait_conv(data);
>  		if (ret)
> -			return ret;
> +			goto restore;
> +
> +		if (switch_off) {
> +			data->oversampling_humid = prev_oversampling_humid;
> +			data->oversampling_press = prev_oversampling_press;
> +			data->oversampling_temp  = prev_oversampling_temp;
> +			ret = data->chip_info->chip_config(data);
> +			if (ret)
> +				return ret;
> +		}
>  
>  		switch (chan->type) {
>  		case IIO_HUMIDITYRELATIVE:
> @@ -661,13 +709,55 @@ static int bmp280_read_raw_impl(struct iio_dev *indio_dev,
>  			return -EINVAL;
>  		}
>  	case IIO_CHAN_INFO_RAW:
> +		/* switch off unused channels */
> +		switch_off = 0;
> +		switch (chan->type) {
> +		case IIO_HUMIDITYRELATIVE:
> +			temp_oversampling_press = 0-1;
> +			switch_off |= (prev_oversampling_press > switch_threshold);
> +			/* can't be switched off as it is needed for compensation */
> +			temp_oversampling_temp  = 1-1;
> +			break;
> +		case IIO_PRESSURE:
> +			temp_oversampling_humid = 0-1;
> +			switch_off |= (prev_oversampling_humid > switch_threshold);
> +			/* can't be switched off as it is needed for compensation */
> +			temp_oversampling_temp  = 1-1;
> +			break;
> +		case IIO_TEMP:
> +			temp_oversampling_humid = 0-1;
> +			temp_oversampling_press = 0-1;
> +			switch_off = (prev_oversampling_humid > switch_threshold) |
> +				     (prev_oversampling_press > switch_threshold);
> +			break;
> +		default:
> +			return -EINVAL;
> +		}
> +		if (switch_off) {
> +			data->oversampling_humid = temp_oversampling_humid;
> +			data->oversampling_press = temp_oversampling_press;
> +			data->oversampling_temp  = temp_oversampling_temp;
> +			ret = data->chip_info->chip_config(data);
> +			if (ret)
> +				goto restore;
> +		}
> +
>  		ret = data->chip_info->set_mode(data, BMP280_FORCED);
>  		if (ret)
> -			return ret;
> -
> +			goto restore;
>  		ret = data->chip_info->wait_conv(data);
>  		if (ret)
> -			return ret;
> +			goto restore;
> +
> +		if (switch_off) {
> +			data->oversampling_humid = prev_oversampling_humid;
> +			data->oversampling_press = prev_oversampling_press;
> +			data->oversampling_temp  = prev_oversampling_temp;
> +			data->chip_info->chip_config(data);
> +			if (ret) {
> +				return ret;
> +			}
> +		}
>  
>  		switch (chan->type) {
>  		case IIO_HUMIDITYRELATIVE:
> @@ -741,6 +831,15 @@ static int bmp280_read_raw_impl(struct iio_dev *indio_dev,
>  	default:
>  		return -EINVAL;
>  	}
> +restore:
Maybe worth factoring out he contents of the switch case that has goto restore
into a separate function so that the scope of the restore is same as the code
that is calling it.  (i.e. avoid a goto jumping out of a switch.

> +	if (switch_off) {
> +	/* restore channel configuration */
> +		data->oversampling_humid = prev_oversampling_humid;
> +		data->oversampling_press = prev_oversampling_press;
> +		data->oversampling_temp  = prev_oversampling_temp;
> +		data->chip_info->chip_config(data);
> +	}
> +	return ret;
>  }

  parent reply	other threads:[~2025-08-06 15:59 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-17 23:30 [PATCH v9 0/4] pressure: bmp280: Minor cleanup and interrupt support Vasileios Amoiridis
2024-10-17 23:30 ` [PATCH v9 1/4] iio: pressure: bmp280: Use sleep and forced mode for oneshot captures Vasileios Amoiridis
2025-06-28 18:45   ` ASSI
2025-06-28 20:57     ` David Lechner
2025-06-29  7:43       ` ASSI
2025-07-11 19:17       ` ASSI
2025-07-12 14:49         ` ASSI
2025-07-21 19:15           ` ASSI
2025-07-26 18:34           ` ASSI
2025-07-27 19:08             ` ASSI
2024-10-17 23:30 ` [PATCH v9 2/4] dt-bindings: iio: pressure: bmp085: Add interrupts for BMP3xx and BMP5xx devices Vasileios Amoiridis
2024-10-17 23:30 ` [PATCH v9 3/4] iio: pressure: bmp280: Add data ready trigger support Vasileios Amoiridis
2024-10-17 23:30 ` [PATCH v9 4/4] iio: pressure: bmp280: Move bmp085 interrupt to new configuration Vasileios Amoiridis
2024-10-19 13:55 ` [PATCH v9 0/4] pressure: bmp280: Minor cleanup and interrupt support Jonathan Cameron
2025-08-03 14:07 ` [bmp280 v1 0/6] Fixes and enhancements for the bmp280 driver Achim Gratz
2025-08-03 14:07   ` [bmp280 v1 1/6] iio: pressure: bmp280: correct meas_time_us calculation Achim Gratz
2025-08-06 15:46     ` Jonathan Cameron
2025-08-06 17:53       ` ASSI
2025-08-10 18:04         ` Jonathan Cameron
2025-08-03 14:07   ` [bmp280 v1 2/6] iio: pressure: bmp280: reduce overhead on read with MODE_FORCED Achim Gratz
2025-08-03 20:12     ` Andy Shevchenko
2025-08-06 15:58     ` Jonathan Cameron [this message]
2025-08-06 18:00       ` ASSI
2025-08-03 14:07   ` [bmp280 v1 3/6] iio: pressure: bmp280: implement sampling_frequency for BMx280 Achim Gratz
2025-08-03 20:26     ` Andy Shevchenko
2025-08-04 17:29       ` ASSI
2025-08-10 18:11         ` Jonathan Cameron
2025-08-10 19:12           ` ASSI
2025-08-11 19:48             ` Jonathan Cameron
2025-08-12 19:53               ` ASSI
2025-08-17 15:10                 ` Jonathan Cameron
2025-08-17 16:36                   ` ASSI
2025-08-03 14:08   ` [bmp280 v1 4/6] iio: pressure: bmp280: enable filter settings " Achim Gratz
2025-08-03 20:28     ` Andy Shevchenko
2025-08-04 17:14       ` ASSI
2025-08-10 18:13     ` Jonathan Cameron
2025-08-10 19:01       ` ASSI
2025-08-11 20:14         ` Jonathan Cameron
2025-08-12 19:34           ` ASSI
2025-08-17 14:51             ` Jonathan Cameron
2025-08-03 14:08   ` [bmp280 v1 5/6] iio: pressure: bmp280: remove code duplication Achim Gratz
2025-08-03 20:30     ` Andy Shevchenko
2025-08-10 18:19     ` Jonathan Cameron
2025-08-03 14:08   ` [bmp280 v1 6/6] iio: pressure: bmp280: implement sampling_frequency calculation for BMx280 Achim Gratz
2025-08-03 20:37     ` Andy Shevchenko
2025-08-04 17:20       ` ASSI
2025-08-03 19:20   ` [bmp280 v1 0/6] Fixes and enhancements for the bmp280 driver Andy Shevchenko
2025-08-10 18:58 ` [RFC PATCH v2 0/9] " Achim Gratz
2025-08-10 18:58   ` [RFC PATCH v2 1/9] iio: pressure: bmp280: correct meas_time_us calculation Achim Gratz
2025-08-17 15:16     ` Jonathan Cameron
2025-08-10 18:58   ` [RFC PATCH v2 2/9] iio: pressure: bmp280: implement adaptive wait for BMx280 devices Achim Gratz
2025-08-10 19:49     ` Andy Shevchenko
2025-08-16 18:42       ` ASSI
2025-08-10 18:58   ` [RFC PATCH v2 3/9] iio: pressure: bmp280: implement adaptive wait for BMP380 devices Achim Gratz
2025-08-10 18:58   ` [RFC PATCH v2 4/9] iio: pressure: bmp280: refactoring Achim Gratz
2025-08-17 15:23     ` Jonathan Cameron
2025-08-10 18:58   ` [RFC PATCH v2 5/9] iio: pressure: bmp280: remove code duplication Achim Gratz
2025-08-10 18:58   ` [RFC PATCH v2 6/9] iio: pressure: bmp280: enable filter settings for BMx280 Achim Gratz
2025-08-17 16:37     ` Jonathan Cameron
2025-08-10 18:58   ` [RFC PATCH v2 7/9] iio: pressure: bmp280: implement sampling_frequency " Achim Gratz
2025-08-17 16:53     ` Jonathan Cameron
2025-08-17 17:36       ` ASSI
2025-08-10 18:58   ` [RFC PATCH v2 8/9] iio: pressure: bmp280: implement sampling_frequency calculation " Achim Gratz
2025-08-17 17:04     ` Jonathan Cameron
2025-08-17 17:40       ` ASSI
2025-08-18 17:52         ` Jonathan Cameron
2025-08-10 18:58   ` [RFC PATCH v2 9/9] iio: pressure: bmp280: test longer autosuspend (WIP) Achim Gratz
2025-08-17 17:05     ` Jonathan Cameron
2025-08-17 17:44       ` ASSI
2025-08-11 12:14   ` [RFC PATCH v2 0/9] Fixes and enhancements for the bmp280 driver Andy Shevchenko
2025-08-11 20:17   ` 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=20250806165859.000039d4@huawei.com \
    --to=jonathan.cameron@huawei.com \
    --cc=Achim.Gratz@Stromeko.DE \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    /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).