[PATCH 13/15] iio: health: max30100: do not use internal iio_dev lock

Nuno Sá
2022-09-20T11:36:41Z

The pattern used in this device does not quite fit in the
iio_device_claim_direct_mode() typical usage. In this case,
iio_buffer_enabled() was being used not to prevent the raw access but to
allow it. Hence to get rid of the 'mlock' we need to:

1. Use iio_device_claim_direct_mode() to check if direct mode can be
claimed and if we can return -EINVAL (as the original code);
2. Make sure that buffering is not disabled while doing a raw read. For
that, we can make use of the local lock that already exists.

While at it, fixed a minor coding style complain...

Signed-off-by: Nuno Sá <nuno.sa@analog.com>
---
drivers/iio/health/max30100.c | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/drivers/iio/health/max30100.c b/drivers/iio/health/max30100.c
index ad5717965223..aa494cad5df0 100644
--- a/drivers/iio/health/max30100.c
+++ b/drivers/iio/health/max30100.c
@@ -185,8 +185,19 @@ static int max30100_buffer_postenable(struct iio_dev *indio_dev)
static int max30100_buffer_predisable(struct iio_dev *indio_dev)
{
struct max30100_data *data = iio_priv(indio_dev);
+ int ret;
+
+ /*
+ * As stated in the comment in the read_raw() function, temperature
+ * can only be acquired if the engine is running. As such the mutex
+ * is used to make sure we do not power down while doing a temperature
+ * reading.
+ */
+ mutex_lock(&data->lock);
+ ret = max30100_set_powermode(data, false);
+ mutex_unlock(&data->lock);

- return max30100_set_powermode(data, false);
+ return ret;
}

static const struct iio_buffer_setup_ops max30100_buffer_setup_ops = {
@@ -387,18 +398,17 @@ static int max30100_read_raw(struct iio_dev *indio_dev,
* Temperature reading can only be acquired while engine
* is running
*/
- mutex_lock(&indio_dev->mlock);
-
- if (!iio_buffer_enabled(indio_dev))
+ if (!iio_device_claim_direct_mode(indio_dev)) {
ret = -EAGAIN;
- else {
+ iio_device_release_direct_mode(indio_dev);
+ } else {
+ mutex_lock(&data->lock);
ret = max30100_get_temp(data, val);
if (!ret)
ret = IIO_VAL_INT;
-
+ mutex_unlock(&data->lock);
}

- mutex_unlock(&indio_dev->mlock);
break;
case IIO_CHAN_INFO_SCALE:
*val = 1; /* 0.0625 */
--
2.37.3