Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: health: max30102: do not use internal iio_dev lock

The pattern used in this device does not quite fit in the
iio_device_claim_direct_mode() typical usage. In this case, we want to
know if we are in buffered mode or not to know if the device is powered
(buffer mode) or not. And depending on that max30102_get_temp() will
power on the device if needed. Hence, in order to keep the same
functionality, we try to:

1. Claim Buffered mode;
2: If 1) succeeds call max30102_get_temp() without powering on the
device;
3: Release Buffered mode;
4: If 1) fails, Claim Direct mode;
5: If 4) succeeds call max30102_get_temp() with powering on the device;
6: Release Direct mode;
7: If 4) fails, goto to 1) and try again.

This dance between buffered and direct mode is not particularly pretty
(as well as the loop introduced by the goto statement) but it does allow
us to get rid of the mlock usage while keeping the same behavior.

Signed-off-by: Nuno Sá <nuno.sa@analog.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Link: https://lore.kernel.org/r/20221012151620.1725215-4-nuno.sa@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Nuno Sá and committed by
Jonathan Cameron
6b701cda 1555790c

+15 -4
+15 -4
drivers/iio/health/max30102.c
··· 477 477 * Temperature reading can only be acquired when not in 478 478 * shutdown; leave shutdown briefly when buffer not running 479 479 */ 480 - mutex_lock(&indio_dev->mlock); 481 - if (!iio_buffer_enabled(indio_dev)) 480 + any_mode_retry: 481 + if (iio_device_claim_buffer_mode(indio_dev)) { 482 + /* 483 + * This one is a *bit* hacky. If we cannot claim buffer 484 + * mode, then try direct mode so that we make sure 485 + * things cannot concurrently change. And we just keep 486 + * trying until we get one of the modes... 487 + */ 488 + if (iio_device_claim_direct_mode(indio_dev)) 489 + goto any_mode_retry; 490 + 482 491 ret = max30102_get_temp(data, val, true); 483 - else 492 + iio_device_release_direct_mode(indio_dev); 493 + } else { 484 494 ret = max30102_get_temp(data, val, false); 485 - mutex_unlock(&indio_dev->mlock); 495 + iio_device_release_buffer_mode(indio_dev); 496 + } 486 497 if (ret) 487 498 return ret; 488 499