Skip to contentSkip to conversation

iio: magnetometer: ak8975: modernize and cleanup driver

PATCH Superseded Joshua Crofts 3 participants 13 messages Apr 20 – Apr 21 IIO

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

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

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 | 77 ++++++++++++++++---------------
 1 file changed, 39 insertions(+), 38 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 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 | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
drivers/iio/magnetometer/ak8975.c +16 -11
index 44782c26698..f45dca0a6f0 100644
@@ -7,24 +7,29 @@
  * Copyright (c) 2010, NVIDIA Corporation.
  */
 
-#include <linux/module.h>
-#include <linux/mod_devicetable.h>
-#include <linux/kernel.h>
-#include <linux/slab.h>
+#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/err.h>
+#include <linux/irqreturn.h>
+#include <linux/minmax.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/property.h>
+#include <linux/regulator/consumer.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
#2 lore raw

Re: [PATCH 1/4] iio: magnetometer: ak8975: header cleanup

Quoted 7 lines · Joshua Crofts

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.

Seems this implied sorting as well. As Jonathan says somewhere else he prefers to split sorting and IWYU patches.

...

drivers/iio/magnetometer/ak8975.c line 34

Quoted 6 lines

#include <linux/iio/iio.h>

drivers/iio/magnetometer/ak8975.c +0 -2
-#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>

Also note that this group left unsorted.

Quoted 4 lines
Signature · 2 lines
-- 
2.47.3
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH 1/4] iio: magnetometer: ak8975: header cleanup

Quoted 2 lines · Andy Shevchenko

Seems this implied sorting as well. As Jonathan says somewhere else he prefers to split sorting and IWYU patches.

Okay, I'll return it pre-sort.

Signature · 3 lines
Kind regards

CJD
maintainer
#4 lore raw

Re: [PATCH 1/4] iio: magnetometer: ak8975: header cleanup

Quoted 7 lines · On Tue, 21 Apr 2026 08:16:42 +0200 Joshua Crofts <[email protected]>…
Quoted 2 lines · Andy Shevchenko

Seems this implied sorting as well. As Jonathan says somewhere else he prefers to split sorting and IWYU patches.

Okay, I'll return it pre-sort.

What I'd prefer is sort, then changes.

That way it's easy to see what is removed or added rather than moved.

Thanks,

Jonathan

#5 lore raw

PATCH 2/4 iio: magnetometer: ak8975: replace usleep_range() with fsleep()

In reply to Joshua Crofts

Replace 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 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/magnetometer/ak8975.c +2 -2
index f45dca0a6f0..d4ba96a5630 100644
@@ -461,7 +461,7 @@ 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 +551,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
#6 lore raw

Re: [PATCH 2/4] iio: magnetometer: ak8975: replace usleep_range() with fsleep()

Quoted 5 lines · Joshua Crofts

Replace 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 static int ak8975_power_on(const struct ak8975_data *data) · line 464

Quoted 2 lines
drivers/iio/magnetometer/ak8975.c +1 -1
-	usleep_range(500, 1000);
+	fsleep(500);

Seems code is inconsistent in usage of blank lines before return statements. I would add one while at it.

Quoted 1 line

return 0;

...

drivers/iio/magnetometer/ak8975.c static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode) · line 556

Quoted 5 lines
drivers/iio/magnetometer/ak8975.c +1 -1
 	/* After mode change wait at least 100us */
-	usleep_range(100, 500);
+	fsleep(100);
 
 	return 0;

(Left for the context for the above.)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

PATCH 3/4 iio: magnetometer: ak8975: change 'u8*' to 'u8 *' in cast

In reply to Joshua Crofts

Change 'u8*' cast to 'u8 *' as the former triggers a checkpatch error.

---
 drivers/iio/magnetometer/ak8975.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/magnetometer/ak8975.c +1 -1
index d4ba96a5630..c57ff6d99f3 100644
@@ -760,7 +760,7 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
 
 	ret = i2c_smbus_read_i2c_block_data_or_emulated(
 			client, def->data_regs[index],
-			sizeof(rval), (u8*)&rval);
+			sizeof(rval), (u8 *)&rval);
 	if (ret < 0)
 		goto exit;
 
Signature · 1 line
2.47.3
#8 lore raw

Re: [PATCH 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.

...

drivers/iio/magnetometer/ak8975.c static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val) · line 763

Quoted 4 lines
drivers/iio/magnetometer/ak8975.c +1 -1
 	ret = i2c_smbus_read_i2c_block_data_or_emulated(
 			client, def->data_regs[index],
-			sizeof(rval), (u8*)&rval);
+			sizeof(rval), (u8 *)&rval);

Strange no warnings on the open-ended function call on the first line (trailing "("). That said, I would rather see the result as

	ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
							def->data_regs[index],
							sizeof(rval),
							(u8 *)&rval);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

Re: [PATCH 3/4] iio: magnetometer: ak8975: change 'u8*' to 'u8 *' in cast

Quoted 7 lines · Andy Shevchenko

Strange no warnings on the open-ended function call on the first line (trailing "("). That said, I would rather see the result as

        ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
                                                        def->data_regs[index],
                                                        sizeof(rval),
                                                        (u8 *)&rval);

Checkpatch seems to miss quite a few issues. Will fix.

Signature · 3 lines
Kind regards

CJD
#10 lore raw

PATCH 4/4 iio: magnetometer: ak8975: modernize polling loops with iopoll() macros

In reply to Joshua Crofts

The 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.

Assisted-by Gemini:3.1-Pro
---
 drivers/iio/magnetometer/ak8975.c | 44 ++++++++++++++-----------------
 1 file changed, 20 insertions(+), 24 deletions(-)
drivers/iio/magnetometer/ak8975.c +20 -24
index c57ff6d99f3..86b4149e77b 100644
@@ -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>
@@ -651,17 +652,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 * 1000,
+							 AK8975_MAX_CONVERSION_TIMEOUT * 1000);
+
+	if (ret) {
 		dev_err(&client->dev, "Conversion timeout happened\n");
 		return -EINVAL;
 	}
@@ -676,30 +675,27 @@ 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 * 1000,
+							AK8975_MAX_CONVERSION_TIMEOUT * 1000,
+							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
#11 lore raw

Re: [PATCH 4/4] iio: magnetometer: ak8975: modernize polling loops with iopoll() macros

Quoted 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() 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 static int ak8975_setup(struct i2c_client *client) · line 659

Quoted 9 lines
drivers/iio/magnetometer/ak8975.c +1 -7
 	/* 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,

' != 0' is not needed, but perhaps okay in this case to explicitly hint that this is used as boolean conditional.

drivers/iio/magnetometer/ak8975.c static int ak8975_setup(struct i2c_client *client) · line 661

Quoted 2 lines
drivers/iio/magnetometer/ak8975.c +2 -0
+							 AK8975_CONVERSION_DONE_POLL_TIME * 1000,
+							 AK8975_MAX_CONVERSION_TIMEOUT * 1000);

Somebody broke the indentation...

Quoted 1 line

+

...and added unneeded blank line.

Also, use multipliers from time.h, id est USEC_PER_MSEC.

Quoted 4 lines
drivers/iio/magnetometer/ak8975.c +1 -0
+	if (ret) {
 		dev_err(&client->dev, "Conversion timeout happened\n");
 		return -EINVAL;
 	}

...

Quoted 1 line

static int wait_conversion_complete_polled(struct ak8975_data *data)

Same comments as per above.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw

Re: [PATCH 4/4] iio: magnetometer: ak8975: modernize polling loops with iopoll() macros

Quoted 2 lines · Andy Shevchenko

' != 0' is not needed, but perhaps okay in this case to explicitly hint that this is used as boolean conditional.

Yeah, that was my intention, I thought that (...val, val, ...) was a bit confusing.

Quoted 5 lines

Somebody broke the indentation...

Quoted 1 line

+

...and added unneeded blank line.

I should probably just stick to one editor.

Quoted 1 line

Also, use multipliers from time.h, id est USEC_PER_MSEC.

Will do.

Signature · 3 lines
Kind regards

CJD