iio: magnetometer: ak8975: modernize and cleanup driver
v3 cover iio: magnetometer: ak8975: modernize and cleanup driver
This series modernizes and cleans up the ak8975 driver, migrating to more modern kernel APIs and cleaning up legacy code.
Changes include:
- replacing usleep_range() with fsleep() for optimization
- migrating to iopoll macros for better device polling
- removing unused headers and adding new ones
- checkpatch.pl fixes
v2:
- PATCH 1: reverted alphabetical header ordering per logical change separation
- PATCH 2: added newline between fsleep() and return
- PATCH 3: fixed open parenthesis formatting
- PATCH 4: fixed whitespace issues, added <linux/time.h> for USEC_PER_MSEC macro
v3:
- PATCH 1 & 2: split alphabetical ordering and header removal into two separate patches
- PATCH 4: commit message fix
- PATCH 5 & 6: split error handling and polling loop replacement into two separate patches
Joshua Crofts (6):
iio: magnetometer: ak8975: sort headers alphabetically
iio: magnetometer: ak8975: remove unused headers
iio: magnetometer: ak8975: replace usleep_range() with fsleep()
iio: magnetometer: ak8975: change 'u8*' to 'u8 *' in cast
iio: magnetometer: ak8975: fix wrong errno on return
iio: magnetometer: ak8975: modernize polling loops with iopoll()
macros
drivers/iio/magnetometer/ak8975.c | 92 +++++++++++++++---------------- 1 file changed, 45 insertions(+), 47 deletions(-)
base-commit: c65f27cae0deda3316ed49899df4492a3896e38c
Signature · 1 line
2.47.3
PATCH v3 1/6 iio: magnetometer: ak8975: sort headers alphabetically
Sort include headers alphabetically to improve coding style and readability.
No functional change.
--- drivers/iio/magnetometer/ak8975.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/iio/magnetometer/ak8975.c +10 -10
@@ -7,23 +7,23 @@* Copyright (c) 2010, NVIDIA Corporation. */ -#include <linux/module.h> -#include <linux/mod_devicetable.h> -#include <linux/kernel.h> -#include <linux/slab.h> +#include <linux/bitops.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/gpio/consumer.h> #include <linux/i2c.h> #include <linux/interrupt.h> -#include <linux/err.h> +#include <linux/kernel.h> +#include <linux/mod_devicetable.h> +#include <linux/module.h> #include <linux/mutex.h> -#include <linux/delay.h> -#include <linux/bitops.h> -#include <linux/gpio/consumer.h> -#include <linux/regulator/consumer.h> #include <linux/pm_runtime.h> +#include <linux/regulator/consumer.h> +#include <linux/slab.h> +#include <linux/iio/buffer.h> #include <linux/iio/iio.h> #include <linux/iio/sysfs.h> -#include <linux/iio/buffer.h> #include <linux/iio/trigger.h> #include <linux/iio/trigger_consumer.h> #include <linux/iio/triggered_buffer.h>
Signature · 1 line
2.47.3
PATCH v3 2/6 iio: magnetometer: ak8975: remove unused headers
In reply to Joshua CroftsRemove kernel.h proxy header and unused headers (slab.h, iio/sysfs.h, iio/trigger.h). Add missing headers to ensure atomicity (array_size.h, dev_printk.h, asm/byteorder.h, irqreturn.h, minmax.h, property.h, types.h, wait.h).
Audited using the include-what-you-use tool.
--- drivers/iio/magnetometer/ak8975.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/iio/magnetometer/ak8975.c +9 -4
@@ -7,24 +7,29 @@* Copyright (c) 2010, NVIDIA Corporation. */ +#include <linux/array_size.h> #include <linux/bitops.h> #include <linux/delay.h> +#include <linux/dev_printk.h> #include <linux/err.h> #include <linux/gpio/consumer.h> #include <linux/i2c.h> #include <linux/interrupt.h> -#include <linux/kernel.h> +#include <linux/irqreturn.h> +#include <linux/minmax.h> #include <linux/mod_devicetable.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/pm_runtime.h> +#include <linux/property.h> #include <linux/regulator/consumer.h> -#include <linux/slab.h> +#include <linux/types.h> +#include <linux/wait.h> + +#include <asm/byteorder.h> #include <linux/iio/buffer.h> #include <linux/iio/iio.h> -#include <linux/iio/sysfs.h> -#include <linux/iio/trigger.h> #include <linux/iio/trigger_consumer.h> #include <linux/iio/triggered_buffer.h>
Signature · 1 line
2.47.3
Jonathan Cameron
maintainerRe: [PATCH v3 2/6] iio: magnetometer: ak8975: remove unused headers
Quoted 4 lines · On Wed, 22 Apr 2026 11:22:03 +0000 Joshua Crofts <[email protected]>…
Remove
kernel.hproxy header and unused headers (slab.h,iio/sysfs.h,iio/trigger.h). Add missing headers to ensure atomicity (array_size.h,dev_printk.h,asm/byteorder.h,irqreturn.h,minmax.h,property.h,types.h,wait.h).
Patch title should not focus on just removing headers. Maybe "update header to reflect what is actually used"
Otherwise LGTM
Quoted 46 lines
Audited using the include-what-you-use tool.
Signed-off-by Joshua Crofts <[email protected]>--- drivers/iio/magnetometer/ak8975.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)drivers/iio/magnetometer/ak8975.c +9
-4@@ -7,24 +7,29 @@* Copyright (c) 2010, NVIDIA Corporation. */ +#include <linux/array_size.h> #include <linux/bitops.h> #include <linux/delay.h> +#include <linux/dev_printk.h> #include <linux/err.h> #include <linux/gpio/consumer.h> #include <linux/i2c.h> #include <linux/interrupt.h> -#include <linux/kernel.h> +#include <linux/irqreturn.h> +#include <linux/minmax.h> #include <linux/mod_devicetable.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/pm_runtime.h> +#include <linux/property.h> #include <linux/regulator/consumer.h> -#include <linux/slab.h> +#include <linux/types.h> +#include <linux/wait.h> + +#include <asm/byteorder.h> #include <linux/iio/buffer.h> #include <linux/iio/iio.h> -#include <linux/iio/sysfs.h> -#include <linux/iio/trigger.h> #include <linux/iio/trigger_consumer.h> #include <linux/iio/triggered_buffer.h>
Re: [PATCH v3 2/6] iio: magnetometer: ak8975: remove unused headers
Quoted 2 lines · Jonathan Cameron
Patch title should not focus on just removing headers. Maybe "update header to reflect what is actually used"
Yeah, I realized that it's not fully descriptive of the actual changes.
Quoted 1 line
Otherwise LGTM
Thanks!
Signature · 3 lines
Kind regards CJD
PATCH v3 3/6 iio: magnetometer: ak8975: replace usleep_range() with fsleep()
In reply to Joshua CroftsReplace usleep_range() calls with fsleep(), passing the minimum value required by the sensor for hardware delays.
fsleep() automatically selects the optimal sleep mechanism, simplifying driver code and time management.
--- drivers/iio/magnetometer/ak8975.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/iio/magnetometer/ak8975.c +3 -2
@@ -461,7 +461,8 @@ static int ak8975_power_on(const struct ak8975_data *data)* and the minimum wait time before mode setting is 100us, in * total 300us. Add some margin and say minimum 500us here. */ -usleep_range (500 ,1000 ); +fsleep (500 ); +return 0 ; }
@@ -551,7 +552,7 @@ static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode) data->cntl_cache = regval;/* After mode change wait at least 100us */ -usleep_range (100 ,500 ); +fsleep (100 );return 0 ; }
Signature · 1 line
2.47.3
PATCH v3 4/6 iio: magnetometer: ak8975: change 'u8*' to 'u8 *' in cast
In reply to Joshua CroftsChange 'u8*' cast to 'u8 *' as the former triggers a checkpatch error. Also fix the indentation of parameters in i2c_smbus_read_i2c_block_data_or_emulated() function.
No functional change.
--- drivers/iio/magnetometer/ak8975.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/magnetometer/ak8975.c +4 -3
@@ -759,9 +759,10 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)if (ret) goto exit; - ret =i2c_smbus_read_i2c_block_data_or_emulated ( - client, def->data_regs[index], -sizeof (rval), (u8 *)&rval); + ret =i2c_smbus_read_i2c_block_data_or_emulated (client, + def->data_regs[index], +sizeof (rval), + (u8 *)&rval);if (ret <0 ) goto exit;
Signature · 1 line
2.47.3
PATCH v3 5/6 iio: magnetometer: ak8975: fix wrong errno on return
In reply to Joshua CroftsThe driver currently returns -EINVAL on polling timeout instead of -ETIMEDOUT.
Replace return code for -ETIMEDOUT and remove unnecessary error message as error is described enough by error code.
--- drivers/iio/magnetometer/ak8975.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/iio/magnetometer/ak8975.c +4 -8
@@ -662,10 +662,8 @@ static int wait_conversion_complete_gpio(struct ak8975_data *data)break ; timeout_ms -=AK8975_CONVERSION_DONE_POLL_TIME ; } -if (!timeout_ms) { -dev_err (&client->dev,"Conversion timeout happened\n" ); -return -EINVAL ; - } +if (!timeout_ms) +return -ETIMEDOUT ; ret =i2c_smbus_read_byte_data (client, data->def->ctrl_regs[ST1 ]);if (ret <0 )
@@ -695,10 +693,8 @@ static int wait_conversion_complete_polled(struct ak8975_data *data)break ; timeout_ms -=AK8975_CONVERSION_DONE_POLL_TIME ; } -if (!timeout_ms) { -dev_err (&client->dev,"Conversion timeout happened\n" ); -return -EINVAL ; - } +if (!timeout_ms) +return -ETIMEDOUT ;return read_status; }
Signature · 1 line
2.47.3
Re: [PATCH v3 5/6] iio: magnetometer: ak8975: fix wrong errno on return
Quoted 5 lines · Joshua Crofts
The driver currently returns
-EINVALon polling timeout instead of-ETIMEDOUT.Replace return code for
-ETIMEDOUTand remove unnecessary error message as error is described enough by error code.
The idea is to drop messages because this is well established error code and we have a POSIX message being associated with that (meaning all libc implementations should follow). That said, it's not described by the error code, it can simply be retrieved from libc *based on the error code*.
Can you rephrase that?
...
You also want to align the code in
https://elixir.bootlin.com/linux/v7.0/source/drivers/iio/magnetometer/ak8975.c#L700
The all three wait_conv...*() are called by the same user in a single if-else-if chain.
There are two definitions:
#define ETIME 62 /* Timer expired */
#define ETIMEDOUT 110 /* Connection timed out */
For IO the first one makes no sense.
The driver says
` /* Wait for the conversion to complete. */
which implies the IO with the device, and "Connection timed out" maybe not the clearest form of it but better than "Timer expired" which rather misleading.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v3 5/6] iio: magnetometer: ak8975: fix wrong errno on return
Quoted 6 lines · Andy Shevchenko
The idea is to drop messages because this is well established error code and we have a POSIX message being associated with that (meaning all libc implementations should follow). That said, it's not described by the error code, it can simply be retrieved from libc *based on the error code*.
Can you rephrase that?
Sure, I know that these are just regular errno.h codes, I could've elaborated a bit more in the commit message.
Quoted 6 lines
You also want to align the code in
https://elixir.bootlin.com/linux/v7.0/source/drivers/iio/magnetometer/ak8975.c#L700The all three wait_conv...*() are called by the same user in a single if-else-if chain.
Quoted 6 lines
There are two definitions:
#define ETIME 62 /* Timer expired */ #define ETIMEDOUT 110 /* Connection timed out */For IO the first one makes no sense.
That is odd, it definitely should be -ETIMEDOUT.
Quoted 6 lines
The driver says
` /* Wait for the conversion to complete. */
which implies the IO with the device, and "Connection timed out" maybe not the clearest form of it but better than "Timer expired" which rather misleading.
This driver has more issues than I expected...
Signature · 3 lines
Kind regards CJD
PATCH v3 6/6 iio: magnetometer: ak8975: modernize polling loops with iopoll() macros
In reply to Joshua CroftsThe driver currently uses while loops and msleep() for polling during conversion waits.
Replace the custom polling loops with readx_poll_timeout() and read_poll_timeout() macros from <linux/iopoll.h>. This reduces boilerplate, standardizes timeout handling and improves overall code readability, keeping the original timing and error behaviour. Add <linux/time.h> for USEC_PER_MSEC macro instead of using magic numbers.
--- drivers/iio/magnetometer/ak8975.c | 47 ++++++++++++++----------------- 1 file changed, 21 insertions(+), 26 deletions(-)
drivers/iio/magnetometer/ak8975.c +21 -26
@@ -15,6 +15,7 @@#include <linux/gpio/consumer.h> #include <linux/i2c.h> #include <linux/interrupt.h> +#include <linux/iopoll.h> #include <linux/irqreturn.h> #include <linux/minmax.h> #include <linux/mod_devicetable.h>
@@ -23,6 +24,7 @@#include <linux/pm_runtime.h> #include <linux/property.h> #include <linux/regulator/consumer.h> +#include <linux/time.h> #include <linux/types.h> #include <linux/wait.h>
@@ -652,18 +654,16 @@ static int ak8975_setup(struct i2c_client *client)static int wait_conversion_complete_gpio (struct ak8975_data *data) {struct i2c_client *client = data->client; -u32 timeout_ms =AK8975_MAX_CONVERSION_TIMEOUT ;int ret; +int val;/* Wait for the conversion to complete. */ -while (timeout_ms) { -msleep (AK8975_CONVERSION_DONE_POLL_TIME ); -if (gpiod_get_value (data->eoc_gpiod)) -break ; - timeout_ms -=AK8975_CONVERSION_DONE_POLL_TIME ; - } -if (!timeout_ms) -return -ETIMEDOUT ; + ret =readx_poll_timeout (gpiod_get_value, data->eoc_gpiod, val, + val !=0 , +AK8975_CONVERSION_DONE_POLL_TIME *USEC_PER_MSEC , +AK8975_MAX_CONVERSION_TIMEOUT *USEC_PER_MSEC ); +if (ret) +return ret; ret =i2c_smbus_read_byte_data (client, data->def->ctrl_regs[ST1 ]);if (ret <0 )
@@ -675,28 +675,23 @@ static int wait_conversion_complete_gpio(struct ak8975_data *data)static int wait_conversion_complete_polled (struct ak8975_data *data) {struct i2c_client *client = data->client; -u8 read_status; -u32 timeout_ms =AK8975_MAX_CONVERSION_TIMEOUT ;int ret; +int val;/* Wait for the conversion to complete. */ -while (timeout_ms) { -msleep (AK8975_CONVERSION_DONE_POLL_TIME ); - ret =i2c_smbus_read_byte_data (client, - data->def->ctrl_regs[ST1 ]); -if (ret <0 ) { -dev_err (&client->dev,"Error in reading ST1\n" ); -return ret; - } - read_status = ret; -if (read_status) -break ; - timeout_ms -=AK8975_CONVERSION_DONE_POLL_TIME ; + ret =read_poll_timeout (i2c_smbus_read_byte_data, val, val !=0 , +AK8975_CONVERSION_DONE_POLL_TIME *USEC_PER_MSEC , +AK8975_MAX_CONVERSION_TIMEOUT *USEC_PER_MSEC , + true, + client, data->def->ctrl_regs[ST1 ]); +if (ret) +return ret; +if (val <0 ) { +dev_err (&client->dev,"Error in reading ST1\n" ); +return val; } -if (!timeout_ms) -return -ETIMEDOUT ; -return read_status; +return val; }/* Returns 0 if the end of conversion interrupt occurred or -ETIME otherwise */
Signature · 1 line
2.47.3
Quoted 8 lines · Joshua Crofts
This series modernizes and cleans up the ak8975 driver, migrating to more modern kernel APIs and cleaning up legacy code.
Changes include:
- replacingusleep_range()withfsleep()for optimization
- migrating to iopoll macros for better device polling
- removing unused headers and adding new ones
- checkpatch.pl fixes
5 out of 6 are good, one needs a bit more as it's a new patch in this version. Also, wait a bit I'll send a couple more that you may incorporate in your series. Btw, are you familiar with b4 tool? If not, good time to start playing with it.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 1 line · Andy Shevchenko
5 out of 6 are good, one needs a bit more as it's a new patch in this version.
Considering this is my second patch series, I'll take this as a compliment :)
Quoted 3 lines
Also, wait a bit I'll send a couple more that you may incorporate in your series. Btw, are you familiar with
b4tool? If not, good time to start playing with it.
Only a little bit, I only really do 'b4 am' on patches but I'll check out the manual
a bit more.
Signature · 3 lines
Kind regards CJD
Quoted 4 lines · Andy Shevchenko
5 out of 6 are good, one needs a bit more as it's a new patch in this version. Also, wait a bit I'll send a couple more that you may incorporate in your series. Btw, are you familiar with
b4tool? If not, good time to start playing with it.
Just checking in on this, since I haven't seen any changes on lore. Btw, b4 really does save *a lot* of time.
Signature · 3 lines
Kind regards CJD
Quoted 8 lines · Joshua Crofts
Quoted 4 lines · Andy Shevchenko
5 out of 6 are good, one needs a bit more as it's a new patch in this version. Also, wait a bit I'll send a couple more that you may incorporate in your series. Btw, are you familiar with
b4tool? If not, good time to start playing with it.Just checking in on this, since I haven't seen any changes on lore.
Sorry for the delay (if you want to help, start reviewing others' patches in IIO mailing list, you also learn from that). Now when I got my email pipeline clean, I am about to start doing what's been promised.
Quoted 1 line
Btw, b4 really does save *a lot* of time.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 3 lines · Andy Shevchenko
Sorry for the delay (if you want to help, start reviewing others' patches in IIO mailing list, you also learn from that). Now when I got my email pipeline clean, I am about to start doing what's been promised.
I wasn't sure that I can just start reviewing, so good to know. No worries about the delay.
Signature · 3 lines
Kind regards CJD
Jonathan Cameron
maintainerQuoted 9 lines · On Fri, 24 Apr 2026 12:23:09 +0200 Joshua Crofts <[email protected]>…
Quoted 3 lines · Andy Shevchenko
Sorry for the delay (if you want to help, start reviewing others' patches in IIO mailing list, you also learn from that). Now when I got my email pipeline clean, I am about to start doing what's been promised.
I wasn't sure that I can just start reviewing, so good to know. No worries about the delay.
FWIW I took a look through and have nothing to add. Looking forward to v4. How Andy manage to catch up with is backlog is beyond me :)
Jonathan
Quoted 2 lines · Jonathan Cameron
FWIW I took a look through and have nothing to add. Looking forward to v4. How Andy manage to catch up with is backlog is beyond me :)
Sure is impressive judging by the amount of patches that come in daily :)
Signature · 3 lines
Kind regards CJD
Quoted 8 lines · Joshua Crofts
Quoted 3 lines · Andy Shevchenko
Sorry for the delay (if you want to help, start reviewing others' patches in IIO mailing list, you also learn from that). Now when I got my email pipeline clean, I am about to start doing what's been promised.
I wasn't sure that I can just start reviewing, so good to know. No worries about the delay.
FWIW, just send what I promised to send, this driver is a rabbit hole... See [email protected].
Signature · 2 lines
With Best Regards, Andy Shevchenko