Re: [PATCH v8 4/5] iio: magnetometer: ak8975: add scan mask index enum

From: Jonathan Cameron

Date: Wed May 20 2026 - 12:15:00 EST


On Mon, 18 May 2026 09:50:28 +0200
Joshua Crofts via B4 Relay <devnull+joshua.crofts1.gmail.com@xxxxxxxxxx> wrote:

> From: Joshua Crofts <joshua.crofts1@xxxxxxxxx>
>
> Add an enum to explicitly define scan mask indexes for the X, Y, Z and
> timestamp channels. Also, update the struct iio_chan_spec to use said
> enum for the .scan_index parameter.
>
> This prevents magic numbers from obscuring the hardware channel mapping
> and improves code style.
>
> No functional change.
>
> Suggested-by: Jonathan Cameron <jic23@xxxxxxxxxx>
> Reviewed-by: Nuno Sá <nuno.sa@xxxxxxxxxx>
> Signed-off-by: Joshua Crofts <joshua.crofts1@xxxxxxxxx>
There is one other place we can use this to improve the code:

Replace:
static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
with
static const unsigned long ak8975_scan_masks[] = {
BIT(AK8975_SCAN_X) | BIT(AK8975_SCAN_Y) | BIT(AK8975_SCAN_Z),
0
};

That makes it obvious we are dealing with all the channels.

I only noticed this because I couldn't work out why there weren't
more per things indexing the channel / addresses stored in channels
or that sort of thing. Answer was because the driver only does things
one way.

Also, maybe rename....

Jonathan



> ---
> drivers/iio/magnetometer/ak8975.c | 13 +++++++++++--
> 1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/magnetometer/ak8975.c b/drivers/iio/magnetometer/ak8975.c
> index 7ab4c4d69d8b73c6f367f6e62fee3017d8691b67..a140d352bad663a138295dc0c44084a9a18a4c24 100644
> --- a/drivers/iio/magnetometer/ak8975.c
> +++ b/drivers/iio/magnetometer/ak8975.c
> @@ -238,6 +238,13 @@ enum ak_ctrl_mode {
> MODE_END,
> };
>
> +enum ak_scan_index {
> + AK8975_SCAN_X,
> + AK8975_SCAN_Y,
> + AK8975_SCAN_Z,
> + AK8975_SCAN_TS,
Can we call these
AK8975_CHAN_X,
etc because they are used both for scan indexes and as chan->address
and putting something called scan index in there feels wrong.

Hence use a vaguer naming.

> +};
> +
> struct ak_def {
> enum asahi_compass_chipset type;
> long (*raw_to_gauss)(u16 data);
> @@ -845,8 +852,10 @@ static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
> }
>
> static const struct iio_chan_spec ak8975_channels[] = {
> - AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
> - IIO_CHAN_SOFT_TIMESTAMP(3),
> + AK8975_CHANNEL(X, AK8975_SCAN_X),
> + AK8975_CHANNEL(Y, AK8975_SCAN_Y),
> + AK8975_CHANNEL(Z, AK8975_SCAN_Z),
> + IIO_CHAN_SOFT_TIMESTAMP(AK8975_SCAN_TS),
> };
>
> static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
>