From: Jonathan Cameron <jic23@kernel.org>
To: linux-iio@vger.kernel.org
Cc: "Mudit Sharma" <muditsharma.info@gmail.com>,
"Julien Stephan" <jstephan@baylibre.com>,
"Mariel Tinaco" <Mariel.Tinaco@analog.com>,
"Angelo Dureghello" <adureghello@baylibre.com>,
"Gustavo Silva" <gustavograzs@gmail.com>,
"Nuno Sa" <nuno.sa@analog.com>,
"João Paulo Gonçalves" <joao.goncalves@toradex.com>,
"ChiYuan Huang" <cy_huang@richtek.com>,
"Ramona Alexandra Nechita" <ramona.nechita@analog.com>,
"Trevor Gamblin" <tgamblin@baylibre.com>,
"Guillaume Stols" <gstols@baylibre.com>,
"David Lechner" <dlechner@baylibre.com>,
"Cosmin Tanislav" <demonsingur@gmail.com>,
"Marcelo Schmitt" <marcelo.schmitt@analog.com>,
"Gwendal Grignou" <gwendal@chromium.org>,
"Antoni Pokusinski" <apokusinski01@gmail.com>,
"Tomasz Duszynski" <tomasz.duszynski@octakon.com>,
"Jonathan Cameron" <Jonathan.Cameron@huawei.com>
Subject: [PATCH v2 11/27] iio: adc: ad4695: Stop using iio_device_claim_direct_scoped()
Date: Sun, 9 Feb 2025 18:06:08 +0000 [thread overview]
Message-ID: <20250209180624.701140-12-jic23@kernel.org> (raw)
In-Reply-To: <20250209180624.701140-1-jic23@kernel.org>
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
This complex cleanup.h use case of conditional guards has proved
to be more trouble that it is worth in terms of false positive compiler
warnings and hard to read code.
Move directly to the new claim/release_direct() that allow sparse
to check for unbalanced context. In some cases code is factored
out to utility functions that can do a direct return with the
claim and release around the call.
Reviewed-by: David Lechner <dlechner@baylibre.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
v2: Typo in commit description (David).
Note there are several sets current in flight that touch this driver.
I'll rebase as necessary depending on what order the dependencies resolve.
---
drivers/iio/adc/ad4695.c | 240 ++++++++++++++++++++++-----------------
1 file changed, 133 insertions(+), 107 deletions(-)
diff --git a/drivers/iio/adc/ad4695.c b/drivers/iio/adc/ad4695.c
index 13cf01d35301..4bb22f4d739b 100644
--- a/drivers/iio/adc/ad4695.c
+++ b/drivers/iio/adc/ad4695.c
@@ -738,6 +738,25 @@ static int ad4695_read_one_sample(struct ad4695_state *st, unsigned int address)
return spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
}
+static int __ad4695_read_info_raw(struct ad4695_state *st,
+ struct iio_chan_spec const *chan,
+ int *val)
+{
+ u8 realbits = chan->scan_type.realbits;
+ int ret;
+
+ ret = ad4695_read_one_sample(st, chan->address);
+ if (ret)
+ return ret;
+
+ if (chan->scan_type.sign == 's')
+ *val = sign_extend32(st->raw_data, realbits - 1);
+ else
+ *val = st->raw_data;
+
+ return IIO_VAL_INT;
+}
+
static int ad4695_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
@@ -750,19 +769,12 @@ static int ad4695_read_raw(struct iio_dev *indio_dev,
switch (mask) {
case IIO_CHAN_INFO_RAW:
- iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
- ret = ad4695_read_one_sample(st, chan->address);
- if (ret)
- return ret;
-
- if (chan->scan_type.sign == 's')
- *val = sign_extend32(st->raw_data, realbits - 1);
- else
- *val = st->raw_data;
- return IIO_VAL_INT;
- }
- unreachable();
+ if (!iio_device_claim_direct(indio_dev))
+ return -EBUSY;
+ ret = __ad4695_read_info_raw(st, chan, val);
+ iio_device_release_direct(indio_dev);
+ return ret;
case IIO_CHAN_INFO_SCALE:
switch (chan->type) {
case IIO_VOLTAGE:
@@ -800,45 +812,45 @@ static int ad4695_read_raw(struct iio_dev *indio_dev,
case IIO_CHAN_INFO_CALIBSCALE:
switch (chan->type) {
case IIO_VOLTAGE:
- iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
- ret = regmap_read(st->regmap16,
- AD4695_REG_GAIN_IN(chan->scan_index),
- ®_val);
- if (ret)
- return ret;
+ if (!iio_device_claim_direct(indio_dev))
+ return -EBUSY;
+ ret = regmap_read(st->regmap16,
+ AD4695_REG_GAIN_IN(chan->scan_index),
+ ®_val);
+ iio_device_release_direct(indio_dev);
+ if (ret)
+ return ret;
- *val = reg_val;
- *val2 = 15;
+ *val = reg_val;
+ *val2 = 15;
- return IIO_VAL_FRACTIONAL_LOG2;
- }
- unreachable();
+ return IIO_VAL_FRACTIONAL_LOG2;
default:
return -EINVAL;
}
case IIO_CHAN_INFO_CALIBBIAS:
switch (chan->type) {
case IIO_VOLTAGE:
- iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
- ret = regmap_read(st->regmap16,
- AD4695_REG_OFFSET_IN(chan->scan_index),
- ®_val);
- if (ret)
- return ret;
-
- tmp = sign_extend32(reg_val, 15);
+ if (!iio_device_claim_direct(indio_dev))
+ return -EBUSY;
+ ret = regmap_read(st->regmap16,
+ AD4695_REG_OFFSET_IN(chan->scan_index),
+ ®_val);
+ iio_device_release_direct(indio_dev);
+ if (ret)
+ return ret;
- *val = tmp / 4;
- *val2 = abs(tmp) % 4 * MICRO / 4;
+ tmp = sign_extend32(reg_val, 15);
- if (tmp < 0 && *val2) {
- *val *= -1;
- *val2 *= -1;
- }
+ *val = tmp / 4;
+ *val2 = abs(tmp) % 4 * MICRO / 4;
- return IIO_VAL_INT_PLUS_MICRO;
+ if (tmp < 0 && *val2) {
+ *val *= -1;
+ *val2 *= -1;
}
- unreachable();
+
+ return IIO_VAL_INT_PLUS_MICRO;
default:
return -EINVAL;
}
@@ -847,64 +859,75 @@ static int ad4695_read_raw(struct iio_dev *indio_dev,
}
}
-static int ad4695_write_raw(struct iio_dev *indio_dev,
- struct iio_chan_spec const *chan,
- int val, int val2, long mask)
+static int __ad4695_write_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int val, int val2, long mask)
{
struct ad4695_state *st = iio_priv(indio_dev);
unsigned int reg_val;
- iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
- switch (mask) {
- case IIO_CHAN_INFO_CALIBSCALE:
- switch (chan->type) {
- case IIO_VOLTAGE:
- if (val < 0 || val2 < 0)
- reg_val = 0;
- else if (val > 1)
- reg_val = U16_MAX;
- else
- reg_val = (val * (1 << 16) +
- mul_u64_u32_div(val2, 1 << 16,
- MICRO)) / 2;
-
- return regmap_write(st->regmap16,
- AD4695_REG_GAIN_IN(chan->scan_index),
- reg_val);
- default:
- return -EINVAL;
- }
- case IIO_CHAN_INFO_CALIBBIAS:
- switch (chan->type) {
- case IIO_VOLTAGE:
- if (val2 >= 0 && val > S16_MAX / 4)
- reg_val = S16_MAX;
- else if ((val2 < 0 ? -val : val) < S16_MIN / 4)
- reg_val = S16_MIN;
- else if (val2 < 0)
- reg_val = clamp_t(int,
- -(val * 4 + -val2 * 4 / MICRO),
- S16_MIN, S16_MAX);
- else if (val < 0)
- reg_val = clamp_t(int,
- val * 4 - val2 * 4 / MICRO,
- S16_MIN, S16_MAX);
- else
- reg_val = clamp_t(int,
- val * 4 + val2 * 4 / MICRO,
- S16_MIN, S16_MAX);
-
- return regmap_write(st->regmap16,
- AD4695_REG_OFFSET_IN(chan->scan_index),
- reg_val);
- default:
- return -EINVAL;
- }
+ switch (mask) {
+ case IIO_CHAN_INFO_CALIBSCALE:
+ switch (chan->type) {
+ case IIO_VOLTAGE:
+ if (val < 0 || val2 < 0)
+ reg_val = 0;
+ else if (val > 1)
+ reg_val = U16_MAX;
+ else
+ reg_val = (val * (1 << 16) +
+ mul_u64_u32_div(val2, 1 << 16,
+ MICRO)) / 2;
+
+ return regmap_write(st->regmap16,
+ AD4695_REG_GAIN_IN(chan->scan_index),
+ reg_val);
+ default:
+ return -EINVAL;
+ }
+ case IIO_CHAN_INFO_CALIBBIAS:
+ switch (chan->type) {
+ case IIO_VOLTAGE:
+ if (val2 >= 0 && val > S16_MAX / 4)
+ reg_val = S16_MAX;
+ else if ((val2 < 0 ? -val : val) < S16_MIN / 4)
+ reg_val = S16_MIN;
+ else if (val2 < 0)
+ reg_val = clamp_t(int,
+ -(val * 4 + -val2 * 4 / MICRO),
+ S16_MIN, S16_MAX);
+ else if (val < 0)
+ reg_val = clamp_t(int,
+ val * 4 - val2 * 4 / MICRO,
+ S16_MIN, S16_MAX);
+ else
+ reg_val = clamp_t(int,
+ val * 4 + val2 * 4 / MICRO,
+ S16_MIN, S16_MAX);
+
+ return regmap_write(st->regmap16,
+ AD4695_REG_OFFSET_IN(chan->scan_index),
+ reg_val);
default:
return -EINVAL;
}
+ default:
+ return -EINVAL;
}
- unreachable();
+}
+
+static int ad4695_write_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int val, int val2, long mask)
+{
+ int ret;
+
+ if (!iio_device_claim_direct(indio_dev))
+ return -EBUSY;
+ ret = __ad4695_write_raw(indio_dev, chan, val, val2, mask);
+ iio_device_release_direct(indio_dev);
+
+ return ret;
}
static int ad4695_read_avail(struct iio_dev *indio_dev,
@@ -954,26 +977,29 @@ static int ad4695_debugfs_reg_access(struct iio_dev *indio_dev,
unsigned int *readval)
{
struct ad4695_state *st = iio_priv(indio_dev);
-
- iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
- if (readval) {
- if (regmap_check_range_table(st->regmap, reg,
- &ad4695_regmap_rd_table))
- return regmap_read(st->regmap, reg, readval);
- if (regmap_check_range_table(st->regmap16, reg,
- &ad4695_regmap16_rd_table))
- return regmap_read(st->regmap16, reg, readval);
- } else {
- if (regmap_check_range_table(st->regmap, reg,
- &ad4695_regmap_wr_table))
- return regmap_write(st->regmap, reg, writeval);
- if (regmap_check_range_table(st->regmap16, reg,
- &ad4695_regmap16_wr_table))
- return regmap_write(st->regmap16, reg, writeval);
- }
+ int ret = -EINVAL;
+
+ if (!iio_device_claim_direct(indio_dev))
+ return -EBUSY;
+
+ if (readval) {
+ if (regmap_check_range_table(st->regmap, reg,
+ &ad4695_regmap_rd_table))
+ ret = regmap_read(st->regmap, reg, readval);
+ if (regmap_check_range_table(st->regmap16, reg,
+ &ad4695_regmap16_rd_table))
+ ret = regmap_read(st->regmap16, reg, readval);
+ } else {
+ if (regmap_check_range_table(st->regmap, reg,
+ &ad4695_regmap_wr_table))
+ ret = regmap_write(st->regmap, reg, writeval);
+ if (regmap_check_range_table(st->regmap16, reg,
+ &ad4695_regmap16_wr_table))
+ ret = regmap_write(st->regmap16, reg, writeval);
}
+ iio_device_release_direct(indio_dev);
- return -EINVAL;
+ return ret;
}
static const struct iio_info ad4695_info = {
--
2.48.1
next prev parent reply other threads:[~2025-02-09 18:08 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-09 18:05 [PATCH v2 00/27] iio: improve handling of direct mode claim and release Jonathan Cameron
2025-02-09 18:05 ` [PATCH v2 01/27] iio: core: Rework claim and release of direct mode to work with sparse Jonathan Cameron
2025-02-17 10:38 ` Nuno Sá
2025-02-17 12:57 ` Jonathan Cameron
2025-02-22 15:51 ` Andy Shevchenko
2025-02-22 17:23 ` Jonathan Cameron
2025-02-22 20:58 ` Andy Shevchenko
2025-02-25 6:25 ` Jonathan Cameron
2025-02-25 7:09 ` Andy Shevchenko
2025-02-09 18:05 ` [PATCH v2 02/27] iio: chemical: scd30: Use guard(mutex) to allow early returns Jonathan Cameron
2025-02-17 10:56 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 03/27] iio: chemical: scd30: Switch to sparse friendly claim/release_direct() Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 04/27] iio: temperature: tmp006: Stop using iio_device_claim_direct_scoped() Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 05/27] iio: proximity: sx9310: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 06/27] iio: proximity: sx9324: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 07/27] iio: proximity: sx9360: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 08/27] iio: accel: adxl367: " Jonathan Cameron
2025-02-17 10:44 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 09/27] iio: adc: ad4000: " Jonathan Cameron
2025-02-17 10:45 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 10/27] iio: adc: ad4130: " Jonathan Cameron
2025-02-17 10:45 ` Nuno Sá
2025-02-09 18:06 ` Jonathan Cameron [this message]
2025-02-16 18:19 ` [PATCH v2 11/27] iio: adc: ad4695: " Jonathan Cameron
2025-02-16 19:00 ` David Lechner
2025-02-17 10:48 ` Nuno Sá
2025-02-17 13:04 ` Jonathan Cameron
2025-02-17 10:48 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 12/27] iio: adc: ad7606: " Jonathan Cameron
2025-02-17 10:49 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 13/27] iio: adc: ad7625: " Jonathan Cameron
2025-02-17 10:49 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 14/27] iio: adc: ad7779: " Jonathan Cameron
2025-02-17 10:50 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 15/27] iio: adc: ad9467: " Jonathan Cameron
2025-02-17 10:50 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 16/27] iio: adc: max1363: " Jonathan Cameron
2025-02-17 10:51 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 17/27] iio: adc: rtq6056: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 18/27] iio: adc: ti-adc161s626: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 19/27] iio: adc: ti-ads1119: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 20/27] iio: addac: ad74413r: " Jonathan Cameron
2025-02-17 10:52 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 21/27] iio: chemical: ens160: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 22/27] iio: dac: ad3552r-hs: " Jonathan Cameron
2025-02-17 10:53 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 23/27] iio: dac: ad8460: " Jonathan Cameron
2025-02-17 10:52 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 24/27] iio: dummy: " Jonathan Cameron
2025-02-17 10:55 ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 25/27] iio: imu: bmi323: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 26/27] iio: light: bh1745: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 27/27] iio: Drop iio_device_claim_direct_scoped() and related infrastructure Jonathan Cameron
2025-02-17 10:57 ` Nuno Sá
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=20250209180624.701140-12-jic23@kernel.org \
--to=jic23@kernel.org \
--cc=Jonathan.Cameron@huawei.com \
--cc=Mariel.Tinaco@analog.com \
--cc=adureghello@baylibre.com \
--cc=apokusinski01@gmail.com \
--cc=cy_huang@richtek.com \
--cc=demonsingur@gmail.com \
--cc=dlechner@baylibre.com \
--cc=gstols@baylibre.com \
--cc=gustavograzs@gmail.com \
--cc=gwendal@chromium.org \
--cc=joao.goncalves@toradex.com \
--cc=jstephan@baylibre.com \
--cc=linux-iio@vger.kernel.org \
--cc=marcelo.schmitt@analog.com \
--cc=muditsharma.info@gmail.com \
--cc=nuno.sa@analog.com \
--cc=ramona.nechita@analog.com \
--cc=tgamblin@baylibre.com \
--cc=tomasz.duszynski@octakon.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