From: Jonathan Cameron <jic23@kernel.org>
To: Marcelo Schmitt <marcelo.schmitt@analog.com>
Cc: <linux-iio@vger.kernel.org>, <devicetree@vger.kernel.org>,
<linux-gpio@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<lars@metafoo.de>, <Michael.Hennerich@analog.com>,
<dlechner@baylibre.com>, <nuno.sa@analog.com>, <andy@kernel.org>,
<robh@kernel.org>, <krzk+dt@kernel.org>, <conor+dt@kernel.org>,
<linus.walleij@linaro.org>, <brgl@bgdev.pl>,
<marcelo.schmitt1@gmail.com>
Subject: Re: [PATCH v3 05/10] iio: adc: ad4170: Add digital filter and sample frequency config support
Date: Sun, 25 May 2025 11:41:19 +0100 [thread overview]
Message-ID: <20250525114119.655e24ec@jic23-huawei> (raw)
In-Reply-To: <c61fc067989f7ceda8ab38d38805839b9912c82e.1747083143.git.marcelo.schmitt@analog.com>
On Tue, 13 May 2025 09:35:03 -0300
Marcelo Schmitt <marcelo.schmitt@analog.com> wrote:
> Add support for sinc3, sinc5, and averaged sinc5 digital filters along with
> sample frequency configuration.
>
> Signed-off-by: Marcelo Schmitt <marcelo.schmitt@analog.com>
> +static int ad4170_set_filter_type(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + unsigned int val)
> +{
> + struct ad4170_state *st = iio_priv(indio_dev);
> + struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
> + struct ad4170_setup *setup = &chan_info->setup;
> + unsigned int old_filter_fs, old_filter, filter_type_conf;
> + int ret;
> +
> + switch (val) {
> + case AD4170_SINC5_AVG:
> + filter_type_conf = AD4170_FILTER_FILTER_TYPE_SINC5_AVG;
> + break;
> + case AD4170_SINC5:
> + filter_type_conf = AD4170_FILTER_FILTER_TYPE_SINC5;
> + break;
> + case AD4170_SINC3:
> + filter_type_conf = AD4170_FILTER_FILTER_TYPE_SINC3;
> + break;
> + default:
> + return -EINVAL;
> + }
> +
> + if (!iio_device_claim_direct(indio_dev))
> + return -EBUSY;
> +
> + guard(mutex)(&st->lock);
You need to scope this because otherwise locks are released in a different
order to how they are taken.
iio_device_claim_direct (takes mlock in the core)
guard(mutex)(&st->lock)
iio_device_release_direct (releases mlock in the core.
... scope finishes
guard(mutex)(&st->lock) cleanup happens.
Current code might be fine, but it's harder to reason about.
> + /*
> + * The filters provide the same ODR for a given filter_fs value but
> + * there are different minimum and maximum filter_fs limits for each
> + * filter. The filter_fs value will be adjusted if the current filter_fs
> + * is out of the limits of the just requested filter. Since the
> + * filter_fs value affects the ODR (sampling_frequency), changing the
> + * filter may lead to a change in the sampling frequency.
> + */
> + old_filter = setup->filter;
> + old_filter_fs = setup->filter_fs;
> + if (val == AD4170_SINC5_AVG || val == AD4170_SINC3)
> + setup->filter_fs = clamp(val, AD4170_SINC3_MIN_FS,
> + AD4170_SINC3_MAX_FS);
> + else
> + setup->filter_fs = clamp(val, AD4170_SINC5_MIN_FS,
> + AD4170_SINC5_MAX_FS);
> +
> + setup->filter &= ~AD4170_FILTER_FILTER_TYPE_MSK;
> + setup->filter |= FIELD_PREP(AD4170_FILTER_FILTER_TYPE_MSK,
> + filter_type_conf);
> +
> + ret = ad4170_write_channel_setup(st, chan->address, false);
> + if (ret) {
> + setup->filter = old_filter;
> + setup->filter_fs = old_filter_fs;
> + }
> +
> + iio_device_release_direct(indio_dev);
> + return ret;
> +}
next prev parent reply other threads:[~2025-05-25 10:41 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-13 12:32 [PATCH v3 00/10] Add support for AD4170 series of ADCs Marcelo Schmitt
2025-05-13 12:33 ` [PATCH v3 01/10] dt-bindings: iio: adc: Add AD4170 Marcelo Schmitt
2025-05-13 15:47 ` David Lechner
2025-05-16 15:45 ` Marcelo Schmitt
2025-05-16 16:06 ` David Lechner
2025-05-21 8:33 ` Krzysztof Kozlowski
2025-05-21 8:41 ` Krzysztof Kozlowski
2025-05-22 15:07 ` Marcelo Schmitt
2025-05-25 10:05 ` Jonathan Cameron
2025-05-25 10:11 ` Jonathan Cameron
2025-05-26 21:59 ` Marcelo Schmitt
2025-05-31 15:50 ` Jonathan Cameron
2025-05-13 12:34 ` [PATCH v3 02/10] iio: adc: Add basic support for AD4170 Marcelo Schmitt
2025-05-25 10:36 ` Jonathan Cameron
2025-05-26 10:21 ` Nuno Sá
2025-05-13 12:34 ` [PATCH v3 03/10] iio: adc: ad4170: Add support for calibration gain Marcelo Schmitt
2025-05-26 10:24 ` Nuno Sá
2025-05-13 12:34 ` [PATCH v3 04/10] iio: adc: ad4170: Add support for calibration bias Marcelo Schmitt
2025-05-26 10:27 ` Nuno Sá
2025-05-13 12:35 ` [PATCH v3 05/10] iio: adc: ad4170: Add digital filter and sample frequency config support Marcelo Schmitt
2025-05-25 10:41 ` Jonathan Cameron [this message]
2025-05-13 12:35 ` [PATCH v3 06/10] iio: adc: ad4170: Add support for buffered data capture Marcelo Schmitt
2025-05-25 10:46 ` Jonathan Cameron
2025-05-13 12:35 ` [PATCH v3 07/10] iio: adc: ad4170: Add clock provider support Marcelo Schmitt
2025-05-13 16:59 ` David Lechner
2025-05-13 12:36 ` [PATCH v3 08/10] iio: adc: ad4170: Add GPIO controller support Marcelo Schmitt
2025-05-20 17:06 ` Bartosz Golaszewski
2025-05-13 12:36 ` [PATCH v3 09/10] iio: adc: ad4170: Add support for internal temperature sensor Marcelo Schmitt
2025-05-13 12:36 ` [PATCH v3 10/10] iio: adc: ad4170: Add support for weigh scale and RTD sensors Marcelo Schmitt
2025-05-25 10:57 ` 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=20250525114119.655e24ec@jic23-huawei \
--to=jic23@kernel.org \
--cc=Michael.Hennerich@analog.com \
--cc=andy@kernel.org \
--cc=brgl@bgdev.pl \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=marcelo.schmitt1@gmail.com \
--cc=marcelo.schmitt@analog.com \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
/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