[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <f96694db-2ad5-46d3-a380-cba3eaa2de2f@baylibre.com>
Date: Wed, 3 Dec 2025 15:52:43 -0600
From: David Lechner <dlechner@...libre.com>
To: Kurt Borja <kuurtb@...il.com>,
Andy Shevchenko <andriy.shevchenko@...el.com>,
Lars-Peter Clausen <lars@...afoo.de>,
Michael Hennerich <Michael.Hennerich@...log.com>,
Jonathan Cameron <jic23@...nel.org>, Benson Leung <bleung@...omium.org>,
Antoniu Miclaus <antoniu.miclaus@...log.com>,
Gwendal Grignou <gwendal@...omium.org>,
Shrikant Raskar <raskar.shree97@...il.com>,
Per-Daniel Olsson <perdaniel.olsson@...s.com>
Cc: Nuno Sá <nuno.sa@...log.com>,
Andy Shevchenko <andy@...nel.org>, Guenter Roeck <groeck@...omium.org>,
Jonathan Cameron <Jonathan.Cameron@...wei.com>, linux-iio@...r.kernel.org,
linux-kernel@...r.kernel.org, chrome-platform@...ts.linux.dev
Subject: Re: [PATCH RFC 5/6] iio: health: max30102: Use cleanup.h for IIO
locks
On 12/3/25 1:18 PM, Kurt Borja wrote:
> Simplify and drop "hacky" busy-waiting code in max30102_read_raw() by
> using scoped_guard().
>
> Signed-off-by: Kurt Borja <kuurtb@...il.com>
> ---
> drivers/iio/health/max30102.c | 24 +++++++-----------------
> 1 file changed, 7 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/iio/health/max30102.c b/drivers/iio/health/max30102.c
> index 678720102f2b..c642842cb5fb 100644
> --- a/drivers/iio/health/max30102.c
> +++ b/drivers/iio/health/max30102.c
> @@ -24,6 +24,7 @@
> #include <linux/iio/iio.h>
> #include <linux/iio/buffer.h>
> #include <linux/iio/kfifo_buf.h>
> +#include <linux/cleanup.h>
>
> #define MAX30102_DRV_NAME "max30102"
> #define MAX30102_PART_NUMBER 0x15
> @@ -468,6 +469,7 @@ static int max30102_read_raw(struct iio_dev *indio_dev,
> {
> struct max30102_data *data = iio_priv(indio_dev);
> int ret = -EINVAL;
> + bool direct_en;
>
> switch (mask) {
> case IIO_CHAN_INFO_RAW:
> @@ -475,25 +477,13 @@ static int max30102_read_raw(struct iio_dev *indio_dev,
> * Temperature reading can only be acquired when not in
> * shutdown; leave shutdown briefly when buffer not running
> */
> -any_mode_retry:
> - if (!iio_device_claim_buffer(indio_dev)) {
> - /*
> - * This one is a *bit* hacky. If we cannot claim buffer
> - * mode, then try direct mode so that we make sure
> - * things cannot concurrently change. And we just keep
> - * trying until we get one of the modes...
> - */
> - if (!iio_device_claim_direct(indio_dev))
> - goto any_mode_retry;
> + scoped_guard(iio_device_claim, indio_dev) {
scoped_guard() is sketchy in switch statements because there is
a hidden for loop. If someone came along later and put a break;
inside of the scope, it would break out of the hidden for loop
rather than the apparent switch case!
Besides that, it adds extra indent that we could avoid.
> + direct_en = !iio_buffer_enabled(indio_dev);
>
> - ret = max30102_get_temp(data, val, true);
> - iio_device_release_direct(indio_dev);
> - } else {
> - ret = max30102_get_temp(data, val, false);
> - iio_device_release_buffer(indio_dev);
> + ret = max30102_get_temp(data, val, direct_en);
> + if (ret)
> + return ret;
> }
> - if (ret)
> - return ret;
>
> ret = IIO_VAL_INT;
> break;
>
I would write the whole function like this:
static int max30102_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
{
struct max30102_data *data = iio_priv(indio_dev);
int ret;
switch (mask) {
case IIO_CHAN_INFO_RAW: {
/*
* Temperature reading can only be acquired when not in
* shutdown; leave shutdown briefly when buffer not running
*/
guard(iio_device_claim)(indio_dev);
ret = max30102_get_temp(data, val, !iio_buffer_enabled(indio_dev));
if (ret)
return ret;
return IIO_VAL_INT;
}
case IIO_CHAN_INFO_SCALE:
*val = 1000; /* 62.5 */
*val2 = 16;
return IIO_VAL_FRACTIONAL;
default:
return -EINVAL;
}
}
Could also simplify things further by moving the call to iio_buffer_enabled()
into max30102_get_temp().
Powered by blists - more mailing lists