[PATCH v3 7/7] iio: accel: kx022a: align with subsystem way

Matti Vaittinen
2024-11-28T09:03:50Z

[-- Attachment #1: Type: text/plain, Size: 7264 bytes --]

Many of the Kionix/ROHM accelerometers have a "PC1 - bit" which enables
the accelerometer. While a sensor configuration like ODR, g-range, FIFO
status etc. are changed, the PC1 bit must be cleared (sensor must be
disabled). (See the description for different CNTL registers [1])

In order to ensure this the kx022a driver uses a mutex, which is locked
when the PC1 bit is cleared, and held for the duration of the
configuration, and released after PC1 bit is set again (enabling the
sensor).

The locking and PC1 bit toggling was implemented using functions:
kx022a_turn_off_lock() and kx022a_turn_on_unlock().

Based on a discussions [2], the IIO subsystem prefers open-coding the
locking with scoped_guard() over these functions.

Drop the kx022a_turn_off_lock() and kx022a_turn_on_unlock() and use
scoped_guard() instead.

[1]: https://fscdn.rohm.com/kionix/en/datasheet/kx022acr-z-e.pdf
[2]: https://lore.kernel.org/all/20241126175550.4a8bedf3@jic23-huawei/

Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>

---
Revision history:
v2 => v3:
- New patch

NOTE: This patch uses the if_not_cond_guard() which is currently missing
the iio_testing.
https://lore.kernel.org/all/20241001-cleanup-if_not_cond_guard-v1-1-7753810b0f7a@baylibre.com/T/#m69982b23da9f71e72d84855b34e9b142cb3a1920
---
drivers/iio/accel/kionix-kx022a.c | 121 ++++++++++++------------------
1 file changed, 48 insertions(+), 73 deletions(-)

diff --git a/drivers/iio/accel/kionix-kx022a.c b/drivers/iio/accel/kionix-kx022a.c
index e3986dd65337..a34cf8da2860 100644
--- a/drivers/iio/accel/kionix-kx022a.c
+++ b/drivers/iio/accel/kionix-kx022a.c
@@ -458,7 +458,7 @@ static void kx022a_reg2scale(struct kx022a_data *data, unsigned int val,
*val2 = data->chip_info->scale_table[val][1];
}

-static int __kx022a_turn_on_off(struct kx022a_data *data, bool on)
+static int kx022a_turn_on_off(struct kx022a_data *data, bool on)
{
int ret;

@@ -474,28 +474,6 @@ static int __kx022a_turn_on_off(struct kx022a_data *data, bool on)
return ret;
}

-static int kx022a_turn_off_lock(struct kx022a_data *data)
-{
- int ret;
-
- mutex_lock(&data->mutex);
- ret = __kx022a_turn_on_off(data, false);
- if (ret)
- mutex_unlock(&data->mutex);
-
- return ret;
-}
-
-static int kx022a_turn_on_unlock(struct kx022a_data *data)
-{
- int ret;
-
- ret = __kx022a_turn_on_off(data, true);
- mutex_unlock(&data->mutex);
-
- return ret;
-}
-
static int kx022a_write_raw_get_fmt(struct iio_dev *idev,
struct iio_chan_spec const *chan,
long mask)
@@ -526,9 +504,8 @@ static int kx022a_write_raw(struct iio_dev *idev,
* issues if users trust the watermark to be reached within known
* time-limit).
*/
- ret = iio_device_claim_direct_mode(idev);
- if (ret)
- return ret;
+ if_not_cond_guard(iio_claim_direct_try, idev)
+ return -EBUSY;

switch (mask) {
case IIO_CHAN_INFO_SAMP_FREQ:
@@ -538,20 +515,20 @@ static int kx022a_write_raw(struct iio_dev *idev,
if (val == kx022a_accel_samp_freq_table[n][0] &&
val2 == kx022a_accel_samp_freq_table[n][1])
break;
- if (n < 0) {
- ret = -EINVAL;
- goto unlock_out;
- }
- ret = kx022a_turn_off_lock(data);
- if (ret)
- break;
+ if (n < 0)
+ return -EINVAL;

- ret = regmap_update_bits(data->regmap,
- data->chip_info->odcntl,
- KX022A_MASK_ODR, n);
- data->odr_ns = kx022a_odrs[n];
- kx022a_turn_on_unlock(data);
- break;
+ scoped_guard(mutex, &data->mutex) {
+ ret = kx022a_turn_on_off(data, false);
+ if (ret)
+ return ret;
+
+ ret = regmap_update_bits(data->regmap,
+ data->chip_info->odcntl,
+ KX022A_MASK_ODR, n);
+ data->odr_ns = kx022a_odrs[n];
+ return kx022a_turn_on_off(data, true);
+ }
case IIO_CHAN_INFO_SCALE:
n = data->chip_info->scale_table_size / 2;

@@ -559,29 +536,27 @@ static int kx022a_write_raw(struct iio_dev *idev,
if (val == data->chip_info->scale_table[n][0] &&
val2 == data->chip_info->scale_table[n][1])
break;
- if (n < 0) {
- ret = -EINVAL;
- goto unlock_out;
- }
+ if (n < 0)
+ return -EINVAL;

- ret = kx022a_turn_off_lock(data);
- if (ret)
- break;
+ scoped_guard(mutex, &data->mutex) {
+ ret = kx022a_turn_on_off(data, false);
+ if (ret)
+ return ret;

- ret = regmap_update_bits(data->regmap, data->chip_info->cntl,
- KX022A_MASK_GSEL,
- n << KX022A_GSEL_SHIFT);
- kx022a_turn_on_unlock(data);
- break;
+ ret = regmap_update_bits(data->regmap,
+ data->chip_info->cntl,
+ KX022A_MASK_GSEL,
+ n << KX022A_GSEL_SHIFT);
+ kx022a_turn_on_off(data, true);
+
+ return ret;
+ }
default:
- ret = -EINVAL;
break;
}

-unlock_out:
- iio_device_release_direct_mode(idev);
-
- return ret;
+ return -EINVAL;
}

static int kx022a_fifo_set_wmi(struct kx022a_data *data)
@@ -923,7 +898,7 @@ static int kx022a_fifo_disable(struct kx022a_data *data)
int ret = 0;

guard(mutex)(&data->mutex);
- ret = __kx022a_turn_on_off(data, false);
+ ret = kx022a_turn_on_off(data, false);
if (ret)
return ret;

@@ -942,7 +917,7 @@ static int kx022a_fifo_disable(struct kx022a_data *data)

kfree(data->fifo_buffer);

- return __kx022a_turn_on_off(data, true);
+ return kx022a_turn_on_off(data, true);
}

static int kx022a_buffer_predisable(struct iio_dev *idev)
@@ -966,7 +941,7 @@ static int kx022a_fifo_enable(struct kx022a_data *data)
return -ENOMEM;

guard(mutex)(&data->mutex);
- ret = __kx022a_turn_on_off(data, false);
+ ret = kx022a_turn_on_off(data, false);
if (ret)
return ret;

@@ -987,7 +962,7 @@ static int kx022a_fifo_enable(struct kx022a_data *data)
if (ret)
return ret;

- return __kx022a_turn_on_off(data, true);
+ return kx022a_turn_on_off(data, true);
}

static int kx022a_buffer_postenable(struct iio_dev *idev)
@@ -1089,7 +1064,7 @@ static int kx022a_trigger_set_state(struct iio_trigger *trig,
return -EBUSY;
}

- ret = __kx022a_turn_on_off(data, false);
+ ret = kx022a_turn_on_off(data, false);
if (ret)
return ret;

@@ -1098,7 +1073,7 @@ static int kx022a_trigger_set_state(struct iio_trigger *trig,
if (ret)
return ret;

- return __kx022a_turn_on_off(data, true);
+ return kx022a_turn_on_off(data, true);
}

static const struct iio_trigger_ops kx022a_trigger_ops = {
@@ -1379,19 +1354,19 @@ int kx022a_probe_internal(struct device *dev, const struct kx022a_chip_info *chi
return ret;

/* The sensor must be turned off for configuration */
- ret = kx022a_turn_off_lock(data);
- if (ret)
- return ret;
+ scoped_guard(mutex, &data->mutex) {
+ ret = kx022a_turn_on_off(data, false);
+ if (ret)
+ return ret;

- ret = kx022a_chip_init(data);
- if (ret) {
- mutex_unlock(&data->mutex);
- return ret;
- }
+ ret = kx022a_chip_init(data);
+ if (ret)
+ return ret;

- ret = kx022a_turn_on_unlock(data);
- if (ret)
- return ret;
+ ret = kx022a_turn_on_off(data, true);
+ if (ret)
+ return ret;
+ }

ret = devm_iio_triggered_buffer_setup_ext(dev, idev,
&iio_pollfunc_store_time,
--
2.47.0

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]