]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
iio: accel: kx022a: Factor out guts of write_raw() to allow direct returns
authorJonathan Cameron <Jonathan.Cameron@huawei.com>
Mon, 17 Feb 2025 14:01:31 +0000 (14:01 +0000)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Tue, 4 Mar 2025 13:17:48 +0000 (13:17 +0000)
Create a new utility function for the actions taken when direct mode
is held. This allows for direct returns, simplifying the code flow.

Cc: Matti Vaittinen <mazziesaccount@gmail.com>
Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com>
Reviewed-by: David Lechner <dlechner@baylibre.com>
Link: https://patch.msgid.link/20250217140135.896574-5-jic23@kernel.org
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/accel/kionix-kx022a.c

index 3a56ab00791aeda7cd9e3459c0f7ce2ede664889..727e007c5fc109b35862ce1f3866c9a778742206 100644 (file)
@@ -510,26 +510,13 @@ static int kx022a_write_raw_get_fmt(struct iio_dev *idev,
        }
 }
 
-static int kx022a_write_raw(struct iio_dev *idev,
-                           struct iio_chan_spec const *chan,
-                           int val, int val2, long mask)
+static int __kx022a_write_raw(struct iio_dev *idev,
+                             struct iio_chan_spec const *chan,
+                             int val, int val2, long mask)
 {
        struct kx022a_data *data = iio_priv(idev);
        int ret, n;
 
-       /*
-        * We should not allow changing scale or frequency when FIFO is running
-        * as it will mess the timestamp/scale for samples existing in the
-        * buffer. If this turns out to be an issue we can later change logic
-        * to internally flush the fifo before reconfiguring so the samples in
-        * fifo keep matching the freq/scale settings. (Such setup could cause
-        * issues if users trust the watermark to be reached within known
-        * time-limit).
-        */
-       ret = iio_device_claim_direct_mode(idev);
-       if (ret)
-               return ret;
-
        switch (mask) {
        case IIO_CHAN_INFO_SAMP_FREQ:
                n = ARRAY_SIZE(kx022a_accel_samp_freq_table);
@@ -538,20 +525,19 @@ 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;
-               }
+               if (n < 0)
+                       return -EINVAL;
+
                ret = kx022a_turn_off_lock(data);
                if (ret)
-                       break;
+                       return ret;
 
                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;
+               return ret;
        case IIO_CHAN_INFO_SCALE:
                n = data->chip_info->scale_table_size / 2;
 
@@ -559,26 +545,44 @@ 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;
+                       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;
+               return ret;
        default:
-               ret = -EINVAL;
-               break;
+               return -EINVAL;
        }
+}
+
+static int kx022a_write_raw(struct iio_dev *idev,
+                           struct iio_chan_spec const *chan,
+                           int val, int val2, long mask)
+{
+       int ret;
+
+       /*
+        * We should not allow changing scale or frequency when FIFO is running
+        * as it will mess the timestamp/scale for samples existing in the
+        * buffer. If this turns out to be an issue we can later change logic
+        * to internally flush the fifo before reconfiguring so the samples in
+        * fifo keep matching the freq/scale settings. (Such setup could cause
+        * issues if users trust the watermark to be reached within known
+        * time-limit).
+        */
+       ret = iio_device_claim_direct_mode(idev);
+       if (ret)
+               return ret;
+
+       ret = __kx022a_write_raw(idev, chan, val, val2, mask);
 
-unlock_out:
        iio_device_release_direct_mode(idev);
 
        return ret;