From: Jonathan Cameron <jic23@kernel.org>
To: Hungyu Lin <dennylin0707@gmail.com>
Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>
Subject: Re: [PATCH] iio: magnetometer: bmc150: simplify member access formatting
Date: Mon, 8 Jun 2026 18:50:46 +0100 [thread overview]
Message-ID: <20260608185046.14f326ea@jic23-huawei> (raw)
In-Reply-To: <20260607142120.60621-1-dennylin0707@gmail.com>
On Sun, 7 Jun 2026 14:21:20 +0000
Hungyu Lin <dennylin0707@gmail.com> wrote:
> Keep the structure member dereference on a single line to
> improve readability.
>
> Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
> ---
> drivers/iio/magnetometer/bmc150_magn.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/drivers/iio/magnetometer/bmc150_magn.c b/drivers/iio/magnetometer/bmc150_magn.c
> index bf2551988008..d2075a18e6a7 100644
> --- a/drivers/iio/magnetometer/bmc150_magn.c
> +++ b/drivers/iio/magnetometer/bmc150_magn.c
> @@ -311,8 +311,7 @@ static int bmc150_magn_set_odr(struct bmc150_magn_data *data, int val)
> ret = regmap_update_bits(data->regmap,
> BMC150_MAGN_REG_OPMODE_ODR,
> BMC150_MAGN_MASK_ODR,
> - bmc150_magn_samp_freq_table[i].
> - reg_val <<
> + bmc150_magn_samp_freq_table[i].reg_val <<
> BMC150_MAGN_SHIFT_ODR);
That is indeed ugly!
However, preferred route if we are touching the code is to use field prep for these rather
than explicit shifts (allowing the shift declaration to be dropped!) If you want
to make that change though it needs to be driver wide rather than just for this
one case.
FIELD_PREP(BMC150_MAGN_MASK_ODR,
bmc150_magn_samp_freq_table[i].reg_val),
Given that whole block is very long line, it may be worth looking to see if a broader
refactor can be done to make the whole thing more readable.
For this type of code there is a standard trick - invert the check in the loop
After a bit more tidying up (which is fine in one patch I think given this
is all about improving this function)
static int bmc150_magn_set_odr(struct bmc150_magn_data *data, int val)
{
for (unsigned int i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) {
if (bmc150_magn_samp_freq_table[i].freq != val)
continue;
return regmap_update_bits(data->regmap,
BMC150_MAGN_REG_OPMODE_ODR,
BMC150_MAGN_MASK_ODR,
FIELD_PREP(BMC150_MAGN_MASK_ODR,
bmc150_magn_samp_freq_table[i].reg_val));
}
return -EINVAL;
}
I don't really like the line going that near the absolute limit of 100 but
it seems to me that introducing a local variable would result in slightly
less readable code.
Anyhow, tidy it up along the lies of that code but also apply FIELD_PREP()
FIELD_GET() for all appropriate fields (and check for the relevant includes)
Thanks,
Jonathan
> if (ret < 0)
> return ret;
prev parent reply other threads:[~2026-06-08 17:50 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-07 14:21 [PATCH] iio: magnetometer: bmc150: simplify member access formatting Hungyu Lin
2026-06-08 17:50 ` Jonathan Cameron [this message]
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=20260608185046.14f326ea@jic23-huawei \
--to=jic23@kernel.org \
--cc=andy@kernel.org \
--cc=dennylin0707@gmail.com \
--cc=dlechner@baylibre.com \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.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