From: Jonathan Cameron <jic23@kernel.org>
To: linux-iio@vger.kernel.org
Cc: "David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <noname.nuno@gmail.com>,
"Antoniu Miclaus" <antoniu.miclaus@analog.com>,
"Matti Vaittinen" <mazziesaccount@gmail.com>,
"Jonathan Cameron" <Jonathan.Cameron@huawei.com>
Subject: [PATCH 2/8] iio: accel: mma8452: Factor out guts of write_raw() to simplify locking
Date: Mon, 17 Feb 2025 14:01:29 +0000 [thread overview]
Message-ID: <20250217140135.896574-3-jic23@kernel.org> (raw)
In-Reply-To: <20250217140135.896574-1-jic23@kernel.org>
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Factoring out those parts of write_raw() in which direct mode is held
allows for direct returns on errors, simplifying the code.
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
drivers/iio/accel/mma8452.c | 78 ++++++++++++++++++-------------------
1 file changed, 37 insertions(+), 41 deletions(-)
diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
index 1b2014c4c4b4..8ce4ddadc559 100644
--- a/drivers/iio/accel/mma8452.c
+++ b/drivers/iio/accel/mma8452.c
@@ -707,55 +707,45 @@ static int mma8452_set_hp_filter_frequency(struct mma8452_data *data,
return mma8452_change_config(data, MMA8452_HP_FILTER_CUTOFF, reg);
}
-static int mma8452_write_raw(struct iio_dev *indio_dev,
+static int __mma8452_write_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int val, int val2, long mask)
{
struct mma8452_data *data = iio_priv(indio_dev);
int i, j, ret;
- ret = iio_device_claim_direct_mode(indio_dev);
- if (ret)
- return ret;
-
switch (mask) {
case IIO_CHAN_INFO_SAMP_FREQ:
i = mma8452_get_samp_freq_index(data, val, val2);
- if (i < 0) {
- ret = i;
- break;
- }
+ if (i < 0)
+ return i;
+
data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK;
data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT;
data->sleep_val = mma8452_calculate_sleep(data);
- ret = mma8452_change_config(data, MMA8452_CTRL_REG1,
- data->ctrl_reg1);
- break;
+ return mma8452_change_config(data, MMA8452_CTRL_REG1,
+ data->ctrl_reg1);
+
case IIO_CHAN_INFO_SCALE:
i = mma8452_get_scale_index(data, val, val2);
- if (i < 0) {
- ret = i;
- break;
- }
+ if (i < 0)
+ return i;
data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK;
data->data_cfg |= i;
- ret = mma8452_change_config(data, MMA8452_DATA_CFG,
- data->data_cfg);
- break;
+ return mma8452_change_config(data, MMA8452_DATA_CFG,
+ data->data_cfg);
+
case IIO_CHAN_INFO_CALIBBIAS:
- if (val < -128 || val > 127) {
- ret = -EINVAL;
- break;
- }
+ if (val < -128 || val > 127)
+ return -EINVAL;
- ret = mma8452_change_config(data,
- MMA8452_OFF_X + chan->scan_index,
- val);
- break;
+ return mma8452_change_config(data,
+ MMA8452_OFF_X + chan->scan_index,
+ val);
case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
if (val == 0 && val2 == 0) {
@@ -764,32 +754,38 @@ static int mma8452_write_raw(struct iio_dev *indio_dev,
data->data_cfg |= MMA8452_DATA_CFG_HPF_MASK;
ret = mma8452_set_hp_filter_frequency(data, val, val2);
if (ret < 0)
- break;
+ return ret;
}
- ret = mma8452_change_config(data, MMA8452_DATA_CFG,
+ return mma8452_change_config(data, MMA8452_DATA_CFG,
data->data_cfg);
- break;
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
j = mma8452_get_odr_index(data);
for (i = 0; i < ARRAY_SIZE(mma8452_os_ratio); i++) {
- if (mma8452_os_ratio[i][j] == val) {
- ret = mma8452_set_power_mode(data, i);
- break;
- }
+ if (mma8452_os_ratio[i][j] == val)
+ return mma8452_set_power_mode(data, i);
}
- if (i == ARRAY_SIZE(mma8452_os_ratio)) {
- ret = -EINVAL;
- break;
- }
- break;
+
+ return -EINVAL;
+
default:
- ret = -EINVAL;
- break;
+ return -EINVAL;
}
+}
+
+static int mma8452_write_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int val, int val2, long mask)
+{
+ int ret;
+
+ ret = iio_device_claim_direct_mode(indio_dev);
+ if (ret)
+ return ret;
+ ret = __mma8452_write_raw(indio_dev, chan, val, val2, mask);
iio_device_release_direct_mode(indio_dev);
return ret;
}
--
2.48.1
next prev parent reply other threads:[~2025-02-17 14:01 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-17 14:01 [PATCH 0/8] IIO: Accelerometers: Sparse friendly claim of direct mode Jonathan Cameron
2025-02-17 14:01 ` [PATCH 1/8] iio: accel: mma8452: Ensure error return on failure to matching oversampling ratio Jonathan Cameron
2025-02-17 14:01 ` Jonathan Cameron [this message]
2025-02-17 14:01 ` [PATCH 3/8] iio: accel: mma8452: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-17 14:01 ` [PATCH 4/8] iio: accel: kx022a: Factor out guts of write_raw() to allow direct returns Jonathan Cameron
2025-02-18 7:32 ` Matti Vaittinen
2025-02-17 14:01 ` [PATCH 5/8] iio: accel: kx022a: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-18 7:39 ` Matti Vaittinen
2025-02-18 15:42 ` David Lechner
2025-02-19 5:36 ` Matti Vaittinen
2025-02-19 10:51 ` Nuno Sá
2025-02-19 12:21 ` Matti Vaittinen
2025-02-19 15:25 ` David Lechner
2025-02-19 19:05 ` Jonathan Cameron
2025-02-20 6:31 ` Matti Vaittinen
2025-02-20 17:49 ` Jonathan Cameron
2025-02-20 6:26 ` Matti Vaittinen
2025-02-19 16:06 ` Jonathan Cameron
2025-02-17 14:01 ` [PATCH 6/8] iio: accel: msa311: Fix failure to release runtime pm if direct mode claim fails Jonathan Cameron
2025-02-17 14:01 ` [PATCH 7/8] iio: accel: msa311: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-17 14:01 ` [PATCH 8/8] iio: accel: " Jonathan Cameron
2025-02-17 22:21 ` [PATCH 0/8] IIO: Accelerometers: Sparse friendly claim of direct mode David Lechner
2025-02-22 12:42 ` Jonathan Cameron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250217140135.896574-3-jic23@kernel.org \
--to=jic23@kernel.org \
--cc=Jonathan.Cameron@huawei.com \
--cc=antoniu.miclaus@analog.com \
--cc=dlechner@baylibre.com \
--cc=linux-iio@vger.kernel.org \
--cc=mazziesaccount@gmail.com \
--cc=noname.nuno@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox