]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
iio: magnetometer: ak8975: pass conversion timeouts as arguments
authorJoshua Crofts <joshua.crofts1@gmail.com>
Tue, 5 May 2026 11:46:02 +0000 (13:46 +0200)
committerJonathan Cameron <jic23@kernel.org>
Sun, 31 May 2026 09:59:36 +0000 (10:59 +0100)
Refactor wait_conversion_complete*_() helper function to accept poll
and timeout values directly as parameters. This improves the
readability of the code and does not rely on hardcoded macros.

Besides that, fix the home grown and obviously wrong in some cases the
jiffy-based timeout.

Co-developed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Joshua Crofts <joshua.crofts1@gmail.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
drivers/iio/magnetometer/ak8975.c

index 9511528cdd7b2fc456274f19a9132f302ccf0c3c..dcb281149a9dfd6b2d936031e33bbcada7b225da 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/iopoll.h>
+#include <linux/jiffies.h>
 #include <linux/minmax.h>
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
 
 #define AK09912_MAX_REGS               AK09912_REG_ASAZ
 
-/*
- * Miscellaneous values.
- */
-#define AK8975_MAX_CONVERSION_TIMEOUT  500
-#define AK8975_CONVERSION_DONE_POLL_TIME 10
-#define AK8975_DATA_READY_TIMEOUT      ((100*HZ)/1000)
-
 /*
  * Precalculate scale factor (in Gauss units) for each axis and
  * store in the device data.
@@ -649,18 +643,19 @@ static int ak8975_setup(struct i2c_client *client)
        return 0;
 }
 
-static int wait_conversion_complete_gpio(struct ak8975_data *data)
+static int wait_conversion_complete_gpio(struct ak8975_data *data,
+                                        unsigned int poll_ms,
+                                        unsigned int timeout_ms)
 {
        struct i2c_client *client = data->client;
-       u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
        int ret;
 
        /* Wait for the conversion to complete. */
        while (timeout_ms) {
-               msleep(AK8975_CONVERSION_DONE_POLL_TIME);
+               msleep(poll_ms);
                if (gpiod_get_value(data->eoc_gpiod))
                        break;
-               timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
+               timeout_ms -= poll_ms;
        }
        if (!timeout_ms)
                return -ETIMEDOUT;
@@ -672,16 +667,17 @@ static int wait_conversion_complete_gpio(struct ak8975_data *data)
        return ret;
 }
 
-static int wait_conversion_complete_polled(struct ak8975_data *data)
+static int wait_conversion_complete_polled(struct ak8975_data *data,
+                                          unsigned int poll_ms,
+                                          unsigned int timeout_ms)
 {
        struct i2c_client *client = data->client;
        u8 read_status;
-       u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
        int ret;
 
        /* Wait for the conversion to complete. */
        while (timeout_ms) {
-               msleep(AK8975_CONVERSION_DONE_POLL_TIME);
+               msleep(poll_ms);
                ret = i2c_smbus_read_byte_data(client,
                                               data->def->ctrl_regs[ST1]);
                if (ret < 0) {
@@ -691,7 +687,7 @@ static int wait_conversion_complete_polled(struct ak8975_data *data)
                read_status = ret;
                if (read_status)
                        break;
-               timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
+               timeout_ms -= poll_ms;
        }
        if (!timeout_ms)
                return -ETIMEDOUT;
@@ -700,13 +696,14 @@ static int wait_conversion_complete_polled(struct ak8975_data *data)
 }
 
 /* Returns 0 if the end of conversion interrupt occurred or -ETIMEDOUT otherwise */
-static int wait_conversion_complete_interrupt(struct ak8975_data *data)
+static int wait_conversion_complete_interrupt(struct ak8975_data *data,
+                                             unsigned int timeout_ms)
 {
        int ret;
 
        ret = wait_event_timeout(data->data_ready_queue,
                                 test_bit(0, &data->flags),
-                                AK8975_DATA_READY_TIMEOUT);
+                                msecs_to_jiffies(timeout_ms));
        clear_bit(0, &data->flags);
 
        return ret > 0 ? 0 : -ETIMEDOUT;
@@ -725,11 +722,11 @@ static int ak8975_start_read_axis(struct ak8975_data *data,
 
        /* Wait for the conversion to complete. */
        if (data->eoc_irq)
-               ret = wait_conversion_complete_interrupt(data);
+               ret = wait_conversion_complete_interrupt(data, 100);
        else if (data->eoc_gpiod)
-               ret = wait_conversion_complete_gpio(data);
+               ret = wait_conversion_complete_gpio(data, 10, 500);
        else
-               ret = wait_conversion_complete_polled(data);
+               ret = wait_conversion_complete_polled(data, 10, 500);
        if (ret < 0)
                return ret;