From: Jonathan Cameron <jic23@kernel.org>
To: linux-iio@vger.kernel.org
Cc: "David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Cosmin Tanislav" <demonsingur@gmail.com>,
"Roan van Dijk" <roan@protonic.nl>,
"Jyoti Bhayana" <jbhayana@google.com>,
"Nishant Malpani" <nish.malpani25@gmail.com>,
"Eugene Zaikonnikov" <ez@norphonic.com>,
"Jean-Baptiste Maneyrol" <jean-baptiste.maneyrol@tdk.com>,
"Shen Jianping" <Jianping.Shen@de.bosch.com>,
"Lorenzo Bianconi" <lorenzo@kernel.org>,
"Matti Vaittinen" <mazziesaccount@gmail.com>,
"Yasin Lee" <yasin.lee.x@gmail.com>,
"Andy Shevchenko" <andy.shevchenko@gmail.com>,
"Jonathan Cameron" <Jonathan.Cameron@huawei.com>
Subject: [PATCH 21/37] iio: magnetometer: mag3110: Factor out core of read/write_raw() and use guard() to simplify code flow.
Date: Mon, 31 Mar 2025 13:13:01 +0100 [thread overview]
Message-ID: <20250331121317.1694135-22-jic23@kernel.org> (raw)
In-Reply-To: <20250331121317.1694135-1-jic23@kernel.org>
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
The combination of guard(mutex) and factoring out sections of code
that occur with the device held in direct mode simplifies code flow.
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
drivers/iio/magnetometer/mag3110.c | 153 ++++++++++++++---------------
1 file changed, 75 insertions(+), 78 deletions(-)
diff --git a/drivers/iio/magnetometer/mag3110.c b/drivers/iio/magnetometer/mag3110.c
index 2fe8e97f2cf8..b633bdf793ed 100644
--- a/drivers/iio/magnetometer/mag3110.c
+++ b/drivers/iio/magnetometer/mag3110.c
@@ -9,6 +9,7 @@
* TODO: irq, user offset, oversampling, continuous mode
*/
+#include <linux/cleanup.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/iio/iio.h>
@@ -102,17 +103,12 @@ static int mag3110_read(struct mag3110_data *data, __be16 buf[3])
{
int ret;
- mutex_lock(&data->lock);
+ guard(mutex)(&data->lock);
ret = mag3110_request(data);
- if (ret < 0) {
- mutex_unlock(&data->lock);
+ if (ret < 0)
return ret;
- }
- ret = i2c_smbus_read_i2c_block_data(data->client,
- MAG3110_OUT_X, 3 * sizeof(__be16), (u8 *) buf);
- mutex_unlock(&data->lock);
-
- return ret;
+ return i2c_smbus_read_i2c_block_data(data->client, MAG3110_OUT_X,
+ 3 * sizeof(__be16), (u8 *) buf);
}
static ssize_t mag3110_show_int_plus_micros(char *buf,
@@ -231,19 +227,17 @@ static int mag3110_change_config(struct mag3110_data *data, u8 reg, u8 val)
int ret;
int is_active;
- mutex_lock(&data->lock);
+ guard(mutex)(&data->lock);
is_active = mag3110_is_active(data);
- if (is_active < 0) {
- ret = is_active;
- goto fail;
- }
+ if (is_active < 0)
+ return is_active;
/* config can only be changed when in standby */
if (is_active > 0) {
ret = mag3110_standby(data);
if (ret < 0)
- goto fail;
+ return ret;
}
/*
@@ -252,23 +246,52 @@ static int mag3110_change_config(struct mag3110_data *data, u8 reg, u8 val)
*/
ret = mag3110_wait_standby(data);
if (ret < 0)
- goto fail;
+ return ret;
ret = i2c_smbus_write_byte_data(data->client, reg, val);
if (ret < 0)
- goto fail;
+ return ret;
if (is_active > 0) {
ret = mag3110_active(data);
if (ret < 0)
- goto fail;
+ return ret;
}
- ret = 0;
-fail:
- mutex_unlock(&data->lock);
+ return 0;
+}
- return ret;
+static int __mag3110_read_info_raw(struct mag3110_data *data,
+ struct iio_chan_spec const *chan,
+ int *val)
+{
+ __be16 buffer[3];
+ int ret;
+
+ switch (chan->type) {
+ case IIO_MAGN: /* in 0.1 uT / LSB */
+ ret = mag3110_read(data, buffer);
+ if (ret < 0)
+ return ret;
+ *val = sign_extend32(be16_to_cpu(buffer[chan->scan_index]),
+ chan->scan_type.realbits - 1);
+ return IIO_VAL_INT;
+
+ case IIO_TEMP: { /* in 1 C / LSB */
+ guard(mutex)(&data->lock);
+ ret = mag3110_request(data);
+ if (ret < 0)
+ return ret;
+ ret = i2c_smbus_read_byte_data(data->client,
+ MAG3110_DIE_TEMP);
+ if (ret < 0)
+ return ret;
+ *val = sign_extend32(ret, chan->scan_type.realbits - 1);
+ return IIO_VAL_INT;
+ }
+ default:
+ return -EINVAL;
+ }
}
static int mag3110_read_raw(struct iio_dev *indio_dev,
@@ -276,7 +299,6 @@ static int mag3110_read_raw(struct iio_dev *indio_dev,
int *val, int *val2, long mask)
{
struct mag3110_data *data = iio_priv(indio_dev);
- __be16 buffer[3];
int i, ret;
switch (mask) {
@@ -284,37 +306,7 @@ static int mag3110_read_raw(struct iio_dev *indio_dev,
ret = iio_device_claim_direct_mode(indio_dev);
if (ret)
return ret;
-
- switch (chan->type) {
- case IIO_MAGN: /* in 0.1 uT / LSB */
- ret = mag3110_read(data, buffer);
- if (ret < 0)
- goto release;
- *val = sign_extend32(
- be16_to_cpu(buffer[chan->scan_index]),
- chan->scan_type.realbits - 1);
- ret = IIO_VAL_INT;
- break;
- case IIO_TEMP: /* in 1 C / LSB */
- mutex_lock(&data->lock);
- ret = mag3110_request(data);
- if (ret < 0) {
- mutex_unlock(&data->lock);
- goto release;
- }
- ret = i2c_smbus_read_byte_data(data->client,
- MAG3110_DIE_TEMP);
- mutex_unlock(&data->lock);
- if (ret < 0)
- goto release;
- *val = sign_extend32(ret,
- chan->scan_type.realbits - 1);
- ret = IIO_VAL_INT;
- break;
- default:
- ret = -EINVAL;
- }
-release:
+ ret = __mag3110_read_info_raw(data, chan, val);
iio_device_release_direct_mode(indio_dev);
return ret;
@@ -346,24 +338,18 @@ static int mag3110_read_raw(struct iio_dev *indio_dev,
return -EINVAL;
}
-static int mag3110_write_raw(struct iio_dev *indio_dev,
- struct iio_chan_spec const *chan,
- int val, int val2, long mask)
+static int __mag3110_write_raw(struct mag3110_data *data,
+ struct iio_chan_spec const *chan,
+ int val, int val2, long mask)
{
- struct mag3110_data *data = iio_priv(indio_dev);
- int rate, ret;
-
- ret = iio_device_claim_direct_mode(indio_dev);
- if (ret)
- return ret;
+ int rate;
switch (mask) {
case IIO_CHAN_INFO_SAMP_FREQ:
rate = mag3110_get_samp_freq_index(data, val, val2);
- if (rate < 0) {
- ret = -EINVAL;
- break;
- }
+ if (rate < 0)
+ return -EINVAL;
+
data->ctrl_reg1 &= 0xff & ~MAG3110_CTRL_DR_MASK
& ~MAG3110_CTRL_AC;
data->ctrl_reg1 |= rate << MAG3110_CTRL_DR_SHIFT;
@@ -371,22 +357,33 @@ static int mag3110_write_raw(struct iio_dev *indio_dev,
if (data->sleep_val < 40)
data->ctrl_reg1 |= MAG3110_CTRL_AC;
- ret = mag3110_change_config(data, MAG3110_CTRL_REG1,
- data->ctrl_reg1);
- break;
+ return mag3110_change_config(data, MAG3110_CTRL_REG1,
+ data->ctrl_reg1);
+
case IIO_CHAN_INFO_CALIBBIAS:
- if (val < -10000 || val > 10000) {
- ret = -EINVAL;
- break;
- }
- ret = i2c_smbus_write_word_swapped(data->client,
+ if (val < -10000 || val > 10000)
+ return -EINVAL;
+
+ return i2c_smbus_write_word_swapped(data->client,
MAG3110_OFF_X + 2 * chan->scan_index, val << 1);
- break;
default:
- ret = -EINVAL;
- break;
+ return -EINVAL;
}
+}
+
+static int mag3110_write_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int val, int val2, long mask)
+{
+ struct mag3110_data *data = iio_priv(indio_dev);
+ int ret;
+
+ ret = iio_device_claim_direct_mode(indio_dev);
+ if (ret)
+ return ret;
+ ret = __mag3110_write_raw(data, chan, val, val2, mask);
iio_device_release_direct_mode(indio_dev);
+
return ret;
}
--
2.48.1
next prev parent reply other threads:[~2025-03-31 12:15 UTC|newest]
Thread overview: 58+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-31 12:12 [PATCH 00/37] IIO: Sparse friendly claim of direct mode (the rest) Jonathan Cameron
2025-03-31 12:12 ` [PATCH 01/37] iio: addac: ad74115: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-03-31 12:12 ` [PATCH 02/37] iio: chemical: ccs811: Factor out handling of read of IIO_INFO_RAW to simplify error paths Jonathan Cameron
2025-03-31 12:12 ` [PATCH 03/37] iio: chemical: ccs811: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-03-31 12:12 ` [PATCH 04/37] iio: chemical: atlas-sensor: " Jonathan Cameron
2025-03-31 12:12 ` [PATCH 05/37] iio: chemical: scd4x: " Jonathan Cameron
2025-03-31 12:12 ` [PATCH 06/37] iio: common: scmi: " Jonathan Cameron
2025-03-31 12:12 ` [PATCH 07/37] iio: common: st_sensors: " Jonathan Cameron
2025-03-31 12:12 ` [PATCH 08/37] iio: gyro: adxrs290: " Jonathan Cameron
2025-03-31 12:12 ` [PATCH 09/37] iio: health: max30102: " Jonathan Cameron
2025-03-31 12:12 ` [PATCH 10/37] iio: humidity: hdc100x: Use guard(mutex) to simplify code flow Jonathan Cameron
2025-03-31 12:12 ` [PATCH 11/37] iio: humidity: hdc100x: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-03-31 12:12 ` [PATCH 12/37] iio: humidity: hdc2010: " Jonathan Cameron
2025-04-01 6:59 ` Eugene Zaikonnikov
2025-03-31 12:12 ` [PATCH 13/37] iio: humidity: hts211: Factor out everything under direct mode claim into helper functions Jonathan Cameron
2025-04-01 6:33 ` Lorenzo Bianconi
2025-03-31 12:12 ` [PATCH 14/37] iio: humidity: hts211: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-04-01 6:34 ` Lorenzo Bianconi
2025-03-31 12:12 ` [PATCH 15/37] iio: imu: inv_icm42600: " Jonathan Cameron
2025-03-31 14:27 ` Jean-Baptiste Maneyrol
2025-03-31 12:12 ` [PATCH 16/37] iio: imu: inv_mpu6050: " Jonathan Cameron
2025-03-31 13:57 ` Jean-Baptiste Maneyrol
2025-03-31 12:12 ` [PATCH 17/37] iio: imu: smi240: " Jonathan Cameron
2025-03-31 12:12 ` [PATCH 18/37] iio: imu: st_lsm6dsx: Factor out parts of st_lsm6dsx_shub_write_raw() to allow direct returns Jonathan Cameron
2025-03-31 14:31 ` David Lechner
2025-04-01 6:28 ` Lorenzo Bianconi
2025-04-06 13:15 ` Jonathan Cameron
2025-04-01 6:27 ` Lorenzo Bianconi
2025-03-31 12:12 ` [PATCH 19/37] iio: imu: st_lsm6dsx: Switch to sparse friendly claim/release_direct() Jonathan Cameron
2025-04-01 6:31 ` Lorenzo Bianconi
2025-03-31 12:13 ` [PATCH 20/37] iio: imu: st_lsm6dsx: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-04-01 6:29 ` Lorenzo Bianconi
2025-03-31 12:13 ` Jonathan Cameron [this message]
2025-03-31 12:13 ` [PATCH 22/37] iio: magnetometer: mag3110: " Jonathan Cameron
2025-03-31 12:13 ` [PATCH 23/37] iio: magnetometer: rm3100: " Jonathan Cameron
2025-03-31 12:13 ` [PATCH 24/37] iio: pressure: dlhl60d: " Jonathan Cameron
2025-03-31 12:13 ` [PATCH 25/37] iio: pressure: icp10100: " Jonathan Cameron
2025-03-31 14:03 ` Jean-Baptiste Maneyrol
2025-03-31 12:13 ` [PATCH 26/37] iio: pressure: mpl3115: factor out core of IIO_INFO_RAW read to simplify code flow Jonathan Cameron
2025-03-31 12:13 ` [PATCH 27/37] iio: pressure: mpl3115: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-03-31 12:13 ` [PATCH 28/37] iio: pressure: ms5611: " Jonathan Cameron
2025-03-31 12:13 ` [PATCH 29/37] iio: pressure: rohm-bm1390: " Jonathan Cameron
2025-04-01 13:14 ` Matti Vaittinen
2025-03-31 12:13 ` [PATCH 30/37] iio: pressure: zpa2326: " Jonathan Cameron
2025-03-31 12:13 ` [PATCH 31/37] iio: proximity: hx9023s: " Jonathan Cameron
2025-03-31 12:13 ` [PATCH 32/37] iio: proximity: pulsed-light: " Jonathan Cameron
2025-03-31 12:13 ` [PATCH 33/37] iio: proximity: sx9500: " Jonathan Cameron
2025-03-31 12:13 ` [PATCH 34/37] iio: temp: maxim_thermocouple: " Jonathan Cameron
2025-03-31 12:13 ` [PATCH 35/37] iio: temp: maxim_thermocouple: Drop unused mutex.h include Jonathan Cameron
2025-03-31 12:13 ` [PATCH 36/37] staging: iio: ad5933: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-03-31 12:13 ` [PATCH 37/37] iio: Adjust internals of handling of direct mode claiming to suit new API Jonathan Cameron
2025-03-31 13:41 ` Andy Shevchenko
2025-03-31 14:07 ` David Lechner
2025-04-05 16:34 ` Jonathan Cameron
2025-04-05 16:46 ` Andy Shevchenko
2025-04-01 19:13 ` [PATCH 00/37] IIO: Sparse friendly claim of direct mode (the rest) David Lechner
2025-04-06 13:20 ` Jonathan Cameron
2025-04-12 18:54 ` Marcelo Schmitt
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=20250331121317.1694135-22-jic23@kernel.org \
--to=jic23@kernel.org \
--cc=Jianping.Shen@de.bosch.com \
--cc=Jonathan.Cameron@huawei.com \
--cc=andy.shevchenko@gmail.com \
--cc=demonsingur@gmail.com \
--cc=dlechner@baylibre.com \
--cc=ez@norphonic.com \
--cc=jbhayana@google.com \
--cc=jean-baptiste.maneyrol@tdk.com \
--cc=linux-iio@vger.kernel.org \
--cc=lorenzo@kernel.org \
--cc=mazziesaccount@gmail.com \
--cc=nish.malpani25@gmail.com \
--cc=nuno.sa@analog.com \
--cc=roan@protonic.nl \
--cc=yasin.lee.x@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