diff --git a/drivers/iio/magnetometer/ak8975.c b/drivers/iio/magnetometer/ak8975.c index 9511528cdd7b..dcb281149a9d 100644 --- a/drivers/iio/magnetometer/ak8975.c +++ b/drivers/iio/magnetometer/ak8975.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -132,13 +133,6 @@ #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;