iio: magnetometer: ak8975: modernize and cleanup driver
v2 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
Joshua Crofts (4):
iio: magnetometer: ak8975: header cleanup
iio: magnetometer: ak8975: replace usleep_range() with fsleep()
iio: magnetometer: ak8975: change 'u8*' to 'u8 *' in cast
iio: magnetometer: ak8975: modernize polling loops with iopoll()
macros
drivers/iio/magnetometer/ak8975.c | 70 ++++++++++++++++--------------- 1 file changed, 37 insertions(+), 33 deletions(-)
base-commit: c65f27cae0deda3316ed49899df4492a3896e38c
Signature · 1 line
2.47.3
PATCH v2 1/4 iio: magnetometer: ak8975: header cleanup
Clean up headers by removing proxy kernel.h header and adding new headers to ensure atomicity (array_size.h, dev_printk.h, types.h, asm/byteorder.h). Removed unused headers (slab.h, iio/sysfs.h, iio/trigger.h) and added minmax.h, property.h, wait.h, irqreturn.h to mitigate transient dependencies during compilation.
Audited using the include-what-you-use tool.
--- drivers/iio/magnetometer/ak8975.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/iio/magnetometer/ak8975.c +10 -4
@@ -9,8 +9,6 @@#include <linux/module.h> #include <linux/mod_devicetable.h> -#include <linux/kernel.h> -#include <linux/slab.h> #include <linux/i2c.h> #include <linux/interrupt.h> #include <linux/err.h>
@@ -20,11 +18,19 @@#include <linux/gpio/consumer.h> #include <linux/regulator/consumer.h> #include <linux/pm_runtime.h> +#include <linux/array_size.h> +#include <linux/delay.h> +#include <linux/dev_printk.h> +#include <linux/irqreturn.h> +#include <linux/minmax.h> +#include <linux/property.h> +#include <linux/types.h> +#include <linux/wait.h> + +#include <asm/byteorder.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
Jonathan Cameron
maintainerRe: [PATCH v2 1/4] iio: magnetometer: ak8975: header cleanup
Quoted 9 lines · On Tue, 21 Apr 2026 10:07:19 +0000 Joshua Crofts <[email protected]>…
Clean up headers by removing proxy
kernel.hheader and adding new headers to ensure atomicity (array_size.h,dev_printk.h,types.h,asm/byteorder.h). Removed unused headers (slab.h,iio/sysfs.h,iio/trigger.h) and addedminmax.h,property.h,wait.h,irqreturn.hto mitigate transient dependencies during compilation.Audited using the include-what-you-use tool.
Signed-off-by Joshua Crofts <[email protected]>
I just saw your reply to v1 thread. General advice would be slow down a bit. Even if you have reviews, a new patch set should sit for at least a few days to gather multiple reviews before a revision.
For this sort of change I'd expect two patches.
1. Sort headers as is, not changes.
2. Add and remove headers.
Both are good to do, but if you do them together as you had in v1 it is annoyingly hard to review.
Quoted 19 lines
--- drivers/iio/magnetometer/ak8975.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)drivers/iio/magnetometer/ak8975.c +0
-2@@ -9,8 +9,6 @@#include <linux/module.h> #include <linux/mod_devicetable.h> -#include <linux/kernel.h> -#include <linux/slab.h> #include <linux/i2c.h> #include <linux/interrupt.h> #include <linux/err.h> @@ -20,11 +18,19 @@#include <linux/gpio/consumer.h>
drivers/iio/magnetometer/ak8975.c line 28
Quoted 10 lines
drivers/iio/magnetometer/ak8975.c +8
-0#include <linux/regulator/consumer.h> #include <linux/pm_runtime.h> +#include <linux/array_size.h> +#include <linux/delay.h> +#include <linux/dev_printk.h> +#include <linux/irqreturn.h> +#include <linux/minmax.h> +#include <linux/property.h> +#include <linux/types.h> +#include <linux/wait.h>
Even if you don't sort, these should be inserted as near as possible to where they'd end up in a sorted list.
Quoted 10 lines
drivers/iio/magnetometer/ak8975.c +2
-2+ +#include <asm/byteorder.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>
Re: [PATCH v2 1/4] iio: magnetometer: ak8975: header cleanup
Quoted 3 lines · Jonathan Cameron
I just saw your reply to v1 thread. General advice would be slow down a bit. Even if you have reviews, a new patch set should sit for at least a few days to gather multiple reviews before a revision.
Yeah, I might've sent a v2 a bit earlier, next time I'll let it sit longer before
sending a new version.
Quoted 6 lines
For this sort of change I'd expect two patches.
1. Sort headers as is, not changes.
2. Add and remove headers.Both are good to do, but if you do them together as you had in v1 it is annoyingly hard to review.
Fair, I can see why that would be annoying. I'll split it into two patches in the v3 version.
Signature · 3 lines
Kind regards CJD
PATCH v2 2/4 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
@@ -462,7 +462,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 ; }
@@ -552,7 +553,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 v2 3/4 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.
--- drivers/iio/magnetometer/ak8975.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/magnetometer/ak8975.c +4 -3
@@ -760,9 +760,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
Re: [PATCH v2 3/4] iio: magnetometer: ak8975: change 'u8*' to 'u8 *' in cast
Quoted 1 line · Joshua Crofts
Change 'u8*' cast to 'u8 *' as the former triggers a checkpatch error.
"While at it, fix the indentation of the parameters in ...func...() call."
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 3/4] iio: magnetometer: ak8975: change 'u8*' to 'u8 *' in cast
Quoted 1 line · Andy Shevchenko
"While at it, fix the indentation of the parameters in ...func...() call."
Indentation is fine, but the email client clipped it.
https://lore.kernel.org/linux-iio/[email protected]/
Signature · 3 lines
Kind regards CJD
Re: [PATCH v2 3/4] iio: magnetometer: ak8975: change 'u8*' to 'u8 *' in cast
Quoted 6 lines · Joshua Crofts
Quoted 1 line · Andy Shevchenko
"While at it, fix the indentation of the parameters in ...func...() call."
Indentation is fine, but the email client clipped it.
https://lore.kernel.org/linux-iio/[email protected]/
I was not clear enough. There are double quotes on purpose. They has to be interpreted as
Please also add to your commit message this sentence:
" ... the example of the sentence ... "
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 3/4] iio: magnetometer: ak8975: change 'u8*' to 'u8 *' in cast
Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Joshua Crofts
Quoted 1 line · Andy Shevchenko
"While at it, fix the indentation of the parameters in ...func...() call."
Indentation is fine, but the email client clipped it.
https://lore.kernel.org/linux-iio/[email protected]/
Quoted 6 lines
I was not clear enough. There are double quotes on purpose. They has to be interpreted as
Please also add to your commit message this sentence:
" ... the example of the sentence ... "
And to make it crystal clear, the double quotes should *not* appear in the commit message from this example :-)
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 3/4] iio: magnetometer: ak8975: change 'u8*' to 'u8 *' in cast
Quoted 6 lines · Andy Shevchenko
Quoted 6 lines
I was not clear enough. There are double quotes on purpose. They has to be interpreted as
Please also add to your commit message this sentence:
" ... the example of the sentence ... "
Right, my bad. And here I was analyzing where my indentation broke.
Quoted 2 lines
And to make it crystal clear, the double quotes should *not* appear in the commit message from this example :-)
This was unnecessary, lol.
Signature · 3 lines
Kind regards CJD
Re: [PATCH v2 3/4] iio: magnetometer: ak8975: change 'u8*' to 'u8 *' in cast
Quoted 2 lines · Joshua Crofts
On Wed, 22 Apr 2026 at 11:24, Andy Shevchenko
<[email protected]> wrote:
...
Quoted 4 lines
Quoted 2 lines
And to make it crystal clear, the double quotes should *not* appear in the commit message from this example :-)
This was unnecessary, lol.
I know, that's why smiley at the end.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 4/4 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.
--- drivers/iio/magnetometer/ak8975.c | 44 ++++++++++++++----------------- 1 file changed, 20 insertions(+), 24 deletions(-)
drivers/iio/magnetometer/ak8975.c +20 -24
@@ -21,9 +21,11 @@#include <linux/array_size.h> #include <linux/delay.h> #include <linux/dev_printk.h> +#include <linux/iopoll.h> #include <linux/irqreturn.h> #include <linux/minmax.h> #include <linux/property.h> +#include <linux/time.h> #include <linux/types.h> #include <linux/wait.h>
@@ -653,17 +655,15 @@ 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) { + 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) {dev_err (&client->dev,"Conversion timeout happened\n" );return -EINVAL ; }
@@ -678,30 +678,26 @@ 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 (val <0 ) { +dev_err (&client->dev,"Error in reading ST1\n" ); +return val; } -if (!timeout_ms) { + +if (ret == -ETIMEDOUT ) {dev_err (&client->dev,"Conversion timeout happened\n" );return -EINVAL ; } -return read_status; +return val; }/* Returns 0 if the end of conversion interrupt occurred or -ETIME otherwise */
Signature · 1 line
2.47.3
Jonathan Cameron
maintainerRe: [PATCH v2 4/4] iio: magnetometer: ak8975: modernize polling loops with iopoll() macros
Quoted 10 lines · On Tue, 21 Apr 2026 10:07:22 +0000 Joshua Crofts <[email protected]>…
The driver currently uses while loops and
msleep()for polling during conversion waits.Replace the custom polling loops with
readx_poll_timeout()andread_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.Assisted-by Gemini:3.1-ProSigned-off-by Joshua Crofts <[email protected]>
The rest of this series looks fine to me.
Jonathan
Re: [PATCH v2 4/4] iio: magnetometer: ak8975: modernize polling loops with iopoll() macros
In reply to Joshua CroftsQuoted 7 lines · Joshua Crofts
The driver currently uses while loops and
msleep()for polling during conversion waits.Replace the custom polling loops with
readx_poll_timeout()andread_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.
...
Quoted 12 lines
static int
wait_conversion_complete_gpio(struct ak8975_data *data){drivers/iio/magnetometer/ak8975.c +1
-5struct 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 ;
drivers/iio/magnetometer/ak8975.c static int ak8975_setup(struct i2c_client *client) · line 668
Quoted 10 lines
drivers/iio/magnetometer/ak8975.c +5
-3- timeout_ms -=AK8975_CONVERSION_DONE_POLL_TIME ; - } -if (!timeout_ms) { + 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) {dev_err (&client->dev,"Conversion timeout happened\n" );return -EINVAL ;
Side note: I would drop this message and return the correct error code (in a separate patch):
if (ret) return ret;
Quoted 1 line
}
...
Quoted 23 lines
static int
wait_conversion_complete_polled(struct ak8975_data *data){drivers/iio/magnetometer/ak8975.c +3
-14struct 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,
drivers/iio/magnetometer/ak8975.c static int wait_conversion_complete_gpio(struct ak8975_data *data)
Quoted 10 lines
drivers/iio/magnetometer/ak8975.c +7
-1+AK8975_MAX_CONVERSION_TIMEOUT *USEC_PER_MSEC , + true, + client, data->def->ctrl_regs[ST1]); +if (val <0 ) { +dev_err (&client->dev,"Error in reading ST1\n" ); +return val; } -if (!timeout_ms) { +
This blank line is not needed as the check coupled with the call (yes, I understand the need to check the val first).
drivers/iio/magnetometer/ak8975.c static int wait_conversion_complete_gpio(struct ak8975_data *data) · line 698
Quoted 4 lines
drivers/iio/magnetometer/ak8975.c +1
-0+if (ret == -ETIMEDOUT ) {dev_err (&client->dev,"Conversion timeout happened\n" );return -EINVAL ; }
This is ugly error code shadowing. See above. Preparatory patch, please, to avoid need of this piece of code altogether.
Quoted 4 lines
drivers/iio/magnetometer/ak8975.c +1
-1-return read_status; +return val; }
Signature · 2 lines
With Best Regards, Andy Shevchenko