Skip to contentSkip to conversation

iio: magnetometer: ak8975: driver cleanup

PATCH Superseded Joshua Crofts 2 participants 10 messages May 18 – May 21 IIO

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v8 cover iio: magnetometer: ak8975: driver cleanup

This series is a continuation of the previous ak8975 driver cleanup effort, as most of the patches were picked.

Changes include:
- using BIT() and GENMASK() macros
- moving to devm_* resource management
- adding a scan index enum
- moving from using wait loops to iopoll functions
- various code style changes

---
Changes in v8:
- Code style changes
- Resent after existing issues with the driver were fixed in separate
patches
- Link to v7: https://lore.kernel.org/r/20260511-magnetometer-fixes-post-pickup-v7-0-9d910faa28b6@gmail.com

Changes in v7:
- Changed revision to v7 as series is a continuation of previous series
- Added Nuno's Reviewed-by tag
- PATCH 3: change order of pm_runtime initialization
- Link to v6: https://lore.kernel.org/r/20260507-magnetometer-fixes-post-pickup-v1-0-37827ca68fb3@gmail.com

---
Andy Shevchenko (4):
iio: magnetometer: ak8975: switch to using managed resources
iio: magnetometer: ak8975: unify messages with help of dev_err_probe()
iio: magnetometer: ak8975: use temporary variable for struct device
iio: magnetometer: ak8975: make use of the macros from bits.h

Joshua Crofts (1):
iio: magnetometer: ak8975: add scan mask index enum

 drivers/iio/magnetometer/ak8975.c | 208 +++++++++++++++++++-------------------
 1 file changed, 105 insertions(+), 103 deletions(-)

---
base-commit: 8678fb54958893818ddeccd05fea560a4e1fc759
change-id: 20260507-magnetometer-fixes-post-pickup-c6dabdd66f54

Best regards,

Signature · 1 line
Joshua Crofts <[email protected]>
#1 lore raw

PATCH v8 1/5 iio: magnetometer: ak8975: switch to using managed resources

From: Andy Shevchenko <[email protected]>

Switch the driver to use managed resources (devm_*) which simplifier error handling and allows removing ak8975_remove() method from the driver.

Note, on error path we now also set mode to POWER_DOWN state which is fine. Even if the device is in that mode, there is no problem to set that mode again, it should be no-op.

Additionally, remove any pm_runtime_get/put*() function calls that dummy cycled the counter to autosuspend the device.

---
 drivers/iio/magnetometer/ak8975.c | 77 ++++++++++++++++++++++-----------------
 1 file changed, 43 insertions(+), 34 deletions(-)
drivers/iio/magnetometer/ak8975.c +43 -34
index bb74abb648f138352576a68c49d3c7dce7cc068e..de4402201783430fc69369f1accaab35145e9f0c 100644
@@ -935,9 +935,25 @@ static const struct iio_buffer_setup_ops ak8975_buffer_setup_ops = {
 	.preenable = ak8975_buffer_preenable,
 	.postdisable = ak8975_buffer_postdisable,
 };
+
+static void devm_ak8975_power_off(void *data)
+{
+	struct ak8975_data *ak = data;
+	struct device *dev = &ak->client->dev;
+
+	/* Only power down if currently active */
+	if (pm_runtime_suspended(dev))
+		return;
+
+	/* Soft-stop the chip before hard-stopping the regulators */
+	ak8975_set_mode(data, POWER_DOWN);
+	ak8975_power_off(data);
+}
+
 static int ak8975_probe(struct i2c_client *client)
 {
 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
+	struct device *dev = &client->dev;
 	struct ak8975_data *data;
 	struct iio_dev *indio_dev;
 	struct gpio_desc *eoc_gpiod;
@@ -1005,10 +1021,21 @@ static int ak8975_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
+	/*
+	 * Set device as active early so pm_runtime_status_suspended() works
+	 * correctly if probe fails before pm_runtime is enabled. Do not attempt
+	 * to move this line lower.
+	 */
+	pm_runtime_set_active(dev);
+
+	ret = devm_add_action_or_reset(dev, devm_ak8975_power_off, data);
+	if (ret)
+		return ret;
+
 	ret = ak8975_who_i_am(data, data->def->type);
 	if (ret) {
 		dev_err(&client->dev, "Unexpected device\n");
-		goto power_off;
+		return ret;
 	}
 	dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
 
@@ -1016,10 +1043,13 @@ static int ak8975_probe(struct i2c_client *client)
 	ret = ak8975_setup(data);
 	if (ret) {
 		dev_err(&client->dev, "%s initialization fails\n", name);
-		goto power_off;
+		return ret;
 	}
 
-	mutex_init(&data->lock);
+	ret = devm_mutex_init(dev, &data->lock);
+	if (ret)
+		return ret;
+
 	indio_dev->channels = ak8975_channels;
 	indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
 	indio_dev->info = &ak8975_info;
@@ -1027,52 +1057,32 @@ static int ak8975_probe(struct i2c_client *client)
 	indio_dev->modes = INDIO_DIRECT_MODE;
 	indio_dev->name = name;
 
-	ret = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
-					 &ak8975_buffer_setup_ops);
+	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
+					      ak8975_handle_trigger,
+					      &ak8975_buffer_setup_ops);
 	if (ret) {
 		dev_err(&client->dev, "triggered buffer setup failed\n");
-		goto power_off;
+		return ret;
 	}
 
-	ret = iio_device_register(indio_dev);
+	ret = devm_pm_runtime_enable(dev);
+	if (ret)
+		return ret;
+
+	ret = devm_iio_device_register(dev, indio_dev);
 	if (ret) {
 		dev_err(&client->dev, "device register failed\n");
-		goto cleanup_buffer;
+		return ret;
 	}
 
-	/* Enable runtime PM */
-	pm_runtime_get_noresume(&client->dev);
-	pm_runtime_set_active(&client->dev);
-	pm_runtime_enable(&client->dev);
 	/*
 	 * The device comes online in 500us, so add two orders of magnitude
 	 * of delay before autosuspending: 50 ms.
 	 */
 	pm_runtime_set_autosuspend_delay(&client->dev, 50);
 	pm_runtime_use_autosuspend(&client->dev);
-	pm_runtime_put(&client->dev);
 
 	return 0;
-
-cleanup_buffer:
-	iio_triggered_buffer_cleanup(indio_dev);
-power_off:
-	ak8975_power_off(data);
-	return ret;
-}
-
-static void ak8975_remove(struct i2c_client *client)
-{
-	struct iio_dev *indio_dev = i2c_get_clientdata(client);
-	struct ak8975_data *data = iio_priv(indio_dev);
-
-	pm_runtime_get_sync(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
-	pm_runtime_disable(&client->dev);
-	iio_device_unregister(indio_dev);
-	iio_triggered_buffer_cleanup(indio_dev);
-	ak8975_set_mode(data, POWER_DOWN);
-	ak8975_power_off(data);
 }
 
 static int ak8975_runtime_suspend(struct device *dev)
@@ -1166,7 +1176,6 @@ static struct i2c_driver ak8975_driver = {
 		.acpi_match_table = ak_acpi_match,
 	},
 	.probe		= ak8975_probe,
-	.remove		= ak8975_remove,
 	.id_table	= ak8975_id,
 };
 module_i2c_driver(ak8975_driver);
Signature · 1 line
2.47.3
maintainer
#2 lore raw

Re: [PATCH v8 1/5] iio: magnetometer: ak8975: switch to using managed resources

Quoted 26 lines · On Mon, 18 May 2026 09:50:25 +0200 Joshua Crofts via B4 Relay…

From: Andy Shevchenko <[email protected]>

Switch the driver to use managed resources (devm_*) which simplifier error handling and allows removing ak8975_remove() method from the driver.

Note, on error path we now also set mode to POWER_DOWN state which is fine. Even if the device is in that mode, there is no problem to set that mode again, it should be no-op.

Additionally, remove any pm_runtime_get/put*() function calls that dummy cycled the counter to autosuspend the device.

---
 drivers/iio/magnetometer/ak8975.c | 77 ++++++++++++++++++++++-----------------
 1 file changed, 43 insertions(+), 34 deletions(-)
drivers/iio/magnetometer/ak8975.c +0 -0
index bb74abb648f138352576a68c49d3c7dce7cc068e..de4402201783430fc69369f1accaab35145e9f0c 100644
@@ -935,9 +935,25 @@ static const struct iio_buffer_setup_ops ak8975_buffer_setup_ops = {
 	.preenable = ak8975_buffer_preenable,

drivers/iio/magnetometer/ak8975.c static const struct iio_buffer_setup_ops ak8975_buffer_setup_ops = { · line 945

Quoted 10 lines
drivers/iio/magnetometer/ak8975.c +8 -0
 	.postdisable = ak8975_buffer_postdisable,
 };
+
+static void devm_ak8975_power_off(void *data)
+{
+	struct ak8975_data *ak = data;
+	struct device *dev = &ak->client->dev;
+
+	/* Only power down if currently active */
+	if (pm_runtime_suspended(dev))

I don't think we established that this actually works without underflowing the regular reference counts if in runtime pm suspsended state when we tear get to devm cleanup.

Sashiko correctly moans about this and points out you refer to a different check anyway on the comment below.
https://sashiko.dev/#/patchset/20260518-magnetometer-fixes-post-pickup-v8-0-088d610108a0%40gmail.com

I'm not convinced pm_runtime_status_suspended() is sufficient either.

The only path I'm 100% sure of is when we add tracking flag in the iio_priv() to indicate if we are already runtime suspended. Ugly but at least we know that will not run into these odd race conditions.

This is 'probably' a new bug rather than a latent one because of the unusual handling below. I think there is a race in original code but it is narrow so unlikely anyone ever hit it.

Whilst I do discuss what else you might do - I'd throw a flag in iio_priv() because it is a simpler solution.

Now, if you happen to have some sort of real hardware or emulation to test the different flows, then I'd be interested to see if there are advantages to doing something closer to what this driver originally did.

Quoted 72 lines
drivers/iio/magnetometer/ak8975.c +32 -7
+		return;
+
+	/* Soft-stop the chip before hard-stopping the regulators */
+	ak8975_set_mode(data, POWER_DOWN);
+	ak8975_power_off(data);
+}
+
 static int ak8975_probe(struct i2c_client *client)
 {
 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
+	struct device *dev = &client->dev;
 	struct ak8975_data *data;
 	struct iio_dev *indio_dev;
 	struct gpio_desc *eoc_gpiod;
@@ -1005,10 +1021,21 @@ static int ak8975_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
+	/*
+	 * Set device as active early so pm_runtime_status_suspended() works
+	 * correctly if probe fails before pm_runtime is enabled. Do not attempt
+	 * to move this line lower.
+	 */
+	pm_runtime_set_active(dev);
+
+	ret = devm_add_action_or_reset(dev, devm_ak8975_power_off, data);
+	if (ret)
+		return ret;
+
 	ret = ak8975_who_i_am(data, data->def->type);
 	if (ret) {
 		dev_err(&client->dev, "Unexpected device\n");
-		goto power_off;
+		return ret;
 	}
 	dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
 
@@ -1016,10 +1043,13 @@ static int ak8975_probe(struct i2c_client *client)
 	ret = ak8975_setup(data);
 	if (ret) {
 		dev_err(&client->dev, "%s initialization fails\n", name);
-		goto power_off;
+		return ret;
 	}
 
-	mutex_init(&data->lock);
+	ret = devm_mutex_init(dev, &data->lock);
+	if (ret)
+		return ret;
+
 	indio_dev->channels = ak8975_channels;
 	indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
 	indio_dev->info = &ak8975_info;
@@ -1027,52 +1057,32 @@ static int ak8975_probe(struct i2c_client *client)
 	indio_dev->modes = INDIO_DIRECT_MODE;
 	indio_dev->name = name;
 
-	ret = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
-					 &ak8975_buffer_setup_ops);
+	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
+					      ak8975_handle_trigger,
+					      &ak8975_buffer_setup_ops);
 	if (ret) {
 		dev_err(&client->dev, "triggered buffer setup failed\n");
-		goto power_off;
+		return ret;
 	}
 
-	ret = iio_device_register(indio_dev);
+	ret = devm_pm_runtime_enable(dev);
+	if (ret)
+		return ret;

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

Quoted 10 lines
drivers/iio/magnetometer/ak8975.c +3 -3
+
+	ret = devm_iio_device_register(dev, indio_dev);
 	if (ret) {
 		dev_err(&client->dev, "device register failed\n");
-		goto cleanup_buffer;
+		return ret;
 	}
 
-	/* Enable runtime PM */
-	pm_runtime_get_noresume(&client->dev);

See below for comment on this.

Quoted 15 lines
drivers/iio/magnetometer/ak8975.c +0 -7
-	pm_runtime_set_active(&client->dev);
-	pm_runtime_enable(&client->dev);
 	/*
 	 * The device comes online in 500us, so add two orders of magnitude
 	 * of delay before autosuspending: 50 ms.
 	 */
 	pm_runtime_set_autosuspend_delay(&client->dev, 50);
 	pm_runtime_use_autosuspend(&client->dev);
-	pm_runtime_put(&client->dev);
 
 	return 0;
-
-cleanup_buffer:
-	iio_triggered_buffer_cleanup(indio_dev);
-power_off:

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

Quoted 10 lines
drivers/iio/magnetometer/ak8975.c +0 -10
-	ak8975_power_off(data);
-	return ret;
-}
-
-static void ak8975_remove(struct i2c_client *client)
-{
-	struct iio_dev *indio_dev = i2c_get_clientdata(client);
-	struct ak8975_data *data = iio_priv(indio_dev);
-
-	pm_runtime_get_sync(&client->dev);

This forces device to wake up.

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

Quoted 1 line
drivers/iio/magnetometer/ak8975.c +0 -1
-	pm_runtime_put_noidle(&client->dev);

This forces the counter down to 0 but doesn't trigger a runtime
pm suspend. I'm nervous though as userspace is still in place
(before iio_device_register() - so it's possible another get / put cycle can occur before the next call).

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

Quoted 1 line
drivers/iio/magnetometer/ak8975.c +0 -1
-	pm_runtime_disable(&client->dev);

Hence when we disable runtime pm the device is powered up and the rest won't underflow.

Now interestingly there is a :
devm_pm_runtime_get_noresume() which calls
pm_runtime_get_noresume() and schedules the
pm_runtime_put_noidle() in the tear down path..

However it is vanishingly rarely used and the sequence to me is non obvious - the two users are inconsistent :(

Quoted 16 lines
drivers/iio/magnetometer/ak8975.c +0 -5
-	iio_device_unregister(indio_dev);
-	iio_triggered_buffer_cleanup(indio_dev);
-	ak8975_set_mode(data, POWER_DOWN);
-	ak8975_power_off(data);
 }
 
 static int ak8975_runtime_suspend(struct device *dev)
@@ -1166,7 +1176,6 @@ static struct i2c_driver ak8975_driver = {
 		.acpi_match_table = ak_acpi_match,
 	},
 	.probe		= ak8975_probe,
-	.remove		= ak8975_remove,
 	.id_table	= ak8975_id,
 };
 module_i2c_driver(ak8975_driver);
#3 lore raw

Re: [PATCH v8 1/5] iio: magnetometer: ak8975: switch to using managed resources

Quoted 58 lines · Jonathan Cameron
Quoted 26 lines · On Mon, 18 May 2026 09:50:25 +0200 Joshua Crofts via B4 Relay…

From: Andy Shevchenko <[email protected]>

Switch the driver to use managed resources (devm_*) which simplifier error handling and allows removing ak8975_remove() method from the driver.

Note, on error path we now also set mode to POWER_DOWN state which is fine. Even if the device is in that mode, there is no problem to set that mode again, it should be no-op.

Additionally, remove any pm_runtime_get/put*() function calls that dummy cycled the counter to autosuspend the device.

---
 drivers/iio/magnetometer/ak8975.c | 77 ++++++++++++++++++++++-----------------
 1 file changed, 43 insertions(+), 34 deletions(-)
drivers/iio/magnetometer/ak8975.c +0 -0
index bb74abb648f138352576a68c49d3c7dce7cc068e..de4402201783430fc69369f1accaab35145e9f0c 100644
@@ -935,9 +935,25 @@ static const struct iio_buffer_setup_ops ak8975_buffer_setup_ops = {
      .preenable = ak8975_buffer_preenable,

drivers/iio/magnetometer/ak8975.c static const struct iio_buffer_setup_ops ak8975_buffer_setup_ops = { · line 945

Quoted 10 lines
drivers/iio/magnetometer/ak8975.c +8 -0
      .postdisable = ak8975_buffer_postdisable,
 };
+
+static void devm_ak8975_power_off(void *data)
+{
+     struct ak8975_data *ak = data;
+     struct device *dev = &ak->client->dev;
+
+     /* Only power down if currently active */
+     if (pm_runtime_suspended(dev))

I don't think we established that this actually works without underflowing the regular reference counts if in runtime pm suspsended state when we tear get to devm cleanup.

Sashiko correctly moans about this and points out you refer to a different check anyway on the comment below.
https://sashiko.dev/#/patchset/20260518-magnetometer-fixes-post-pickup-v8-0-088d610108a0%40gmail.com

I'm not convinced pm_runtime_status_suspended() is sufficient either.

The only path I'm 100% sure of is when we add tracking flag in the iio_priv() to indicate if we are already runtime suspended. Ugly but at least we know that will not run into these odd race conditions.

This is 'probably' a new bug rather than a latent one because of the unusual handling below. I think there is a race in original code but it is narrow so unlikely anyone ever hit it.

Quoted 6 lines

Whilst I do discuss what else you might do - I'd throw a flag in iio_priv() because it is a simpler solution.

Now, if you happen to have some sort of real hardware or emulation to test the different flows, then I'd be interested to see if there are advantages to doing something closer to what this driver originally did.

I've thought about dropping this patch as power management in this driver seems to be a complete rabbit hole, and that I don't have the actual hardware to test this (IMO stubbing won't really help). Although the flag solution is a bit
hacky, it could work.

Quoted 122 lines
Quoted 72 lines
drivers/iio/magnetometer/ak8975.c +32 -7
+             return;
+
+     /* Soft-stop the chip before hard-stopping the regulators */
+     ak8975_set_mode(data, POWER_DOWN);
+     ak8975_power_off(data);
+}
+
 static int ak8975_probe(struct i2c_client *client)
 {
      const struct i2c_device_id *id = i2c_client_get_device_id(client);
+     struct device *dev = &client->dev;
      struct ak8975_data *data;
      struct iio_dev *indio_dev;
      struct gpio_desc *eoc_gpiod;
@@ -1005,10 +1021,21 @@ static int ak8975_probe(struct i2c_client *client)
      if (ret)
              return ret;
 
+     /*
+      * Set device as active early so pm_runtime_status_suspended() works
+      * correctly if probe fails before pm_runtime is enabled. Do not attempt
+      * to move this line lower.
+      */
+     pm_runtime_set_active(dev);
+
+     ret = devm_add_action_or_reset(dev, devm_ak8975_power_off, data);
+     if (ret)
+             return ret;
+
      ret = ak8975_who_i_am(data, data->def->type);
      if (ret) {
              dev_err(&client->dev, "Unexpected device\n");
-             goto power_off;
+             return ret;
      }
      dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
 
@@ -1016,10 +1043,13 @@ static int ak8975_probe(struct i2c_client *client)
      ret = ak8975_setup(data);
      if (ret) {
              dev_err(&client->dev, "%s initialization fails\n", name);
-             goto power_off;
+             return ret;
      }
 
-     mutex_init(&data->lock);
+     ret = devm_mutex_init(dev, &data->lock);
+     if (ret)
+             return ret;
+
      indio_dev->channels = ak8975_channels;
      indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
      indio_dev->info = &ak8975_info;
@@ -1027,52 +1057,32 @@ static int ak8975_probe(struct i2c_client *client)
      indio_dev->modes = INDIO_DIRECT_MODE;
      indio_dev->name = name;
 
-     ret = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
-                                      &ak8975_buffer_setup_ops);
+     ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
+                                           ak8975_handle_trigger,
+                                           &ak8975_buffer_setup_ops);
      if (ret) {
              dev_err(&client->dev, "triggered buffer setup failed\n");
-             goto power_off;
+             return ret;
      }
 
-     ret = iio_device_register(indio_dev);
+     ret = devm_pm_runtime_enable(dev);
+     if (ret)
+             return ret;

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

Quoted 10 lines
drivers/iio/magnetometer/ak8975.c +3 -3
+
+     ret = devm_iio_device_register(dev, indio_dev);
      if (ret) {
              dev_err(&client->dev, "device register failed\n");
-             goto cleanup_buffer;
+             return ret;
      }
 
-     /* Enable runtime PM */
-     pm_runtime_get_noresume(&client->dev);

See below for comment on this.

Quoted 15 lines
drivers/iio/magnetometer/ak8975.c +0 -7
-     pm_runtime_set_active(&client->dev);
-     pm_runtime_enable(&client->dev);
      /*
       * The device comes online in 500us, so add two orders of magnitude
       * of delay before autosuspending: 50 ms.
       */
      pm_runtime_set_autosuspend_delay(&client->dev, 50);
      pm_runtime_use_autosuspend(&client->dev);
-     pm_runtime_put(&client->dev);
 
      return 0;
-
-cleanup_buffer:
-     iio_triggered_buffer_cleanup(indio_dev);
-power_off:

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

Quoted 10 lines
drivers/iio/magnetometer/ak8975.c +0 -10
-     ak8975_power_off(data);
-     return ret;
-}
-
-static void ak8975_remove(struct i2c_client *client)
-{
-     struct iio_dev *indio_dev = i2c_get_clientdata(client);
-     struct ak8975_data *data = iio_priv(indio_dev);
-
-     pm_runtime_get_sync(&client->dev);

This forces device to wake up.

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

Quoted 1 line
drivers/iio/magnetometer/ak8975.c +0 -1
-     pm_runtime_put_noidle(&client->dev);

This forces the counter down to 0 but doesn't trigger a runtime
pm suspend. I'm nervous though as userspace is still in place
(before iio_device_register() - so it's possible another get / put cycle can occur before the next call).

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

Quoted 1 line
drivers/iio/magnetometer/ak8975.c +0 -1
-     pm_runtime_disable(&client->dev);

Hence when we disable runtime pm the device is powered up and the rest won't underflow.

Quoted 7 lines

Now interestingly there is a :
devm_pm_runtime_get_noresume() which calls
pm_runtime_get_noresume() and schedules the
pm_runtime_put_noidle() in the tear down path..

However it is vanishingly rarely used and the sequence to me is non obvious - the two users are inconsistent :(

Hmm, I guess I could reorder the set to get things moving a bit more, the rest of the patches are trivial.

Signature · 3 lines
Kind regards

CJD
#4 lore raw

PATCH v8 2/5 iio: magnetometer: ak8975: unify messages with help of dev_err_probe()

In reply to Joshua Crofts

From: Andy Shevchenko <[email protected]>

Unify error messages that might appear during probe phase by switching to use dev_err_probe().

---
 drivers/iio/magnetometer/ak8975.c | 37 +++++++++++++------------------------
 1 file changed, 13 insertions(+), 24 deletions(-)
drivers/iio/magnetometer/ak8975.c +13 -24
index de4402201783430fc69369f1accaab35145e9f0c..e7cd28ab37aaeb2b68758a42ba40a87c71bd1e24 100644
@@ -495,14 +495,10 @@ static int ak8975_who_i_am(const struct ak8975_data *data,
 							AK09912_REG_WIA1,
 							sizeof(wia_val),
 							wia_val);
-	if (ret < 0) {
-		dev_err(&client->dev, "Error reading WIA\n");
-		return ret;
-	}
-	if (ret != sizeof(wia_val)) {
-		dev_err(&client->dev, "Error reading WIA\n");
-		return -EIO;
-	}
+	if (ret < 0)
+		return dev_err_probe(&client->dev, ret, "Error reading WIA\n");
+	if (ret != sizeof(wia_val))
+		return dev_err_probe(&client->dev, -EIO, "Error reading WIA\n");
 
 	if (wia_val[0] != AK8975_DEVICE_ID)
 		return -ENODEV;
@@ -1033,18 +1029,15 @@ static int ak8975_probe(struct i2c_client *client)
 		return ret;
 
 	ret = ak8975_who_i_am(data, data->def->type);
-	if (ret) {
-		dev_err(&client->dev, "Unexpected device\n");
-		return ret;
-	}
+	if (ret)
+		return dev_err_probe(dev, ret, "Unexpected device\n");
+
 	dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
 
 	/* Perform some basic start-of-day setup of the device. */
 	ret = ak8975_setup(data);
-	if (ret) {
-		dev_err(&client->dev, "%s initialization fails\n", name);
-		return ret;
-	}
+	if (ret)
+		return dev_err_probe(dev, ret, "%s initialization fails\n", name);
 
 	ret = devm_mutex_init(dev, &data->lock);
 	if (ret)
@@ -1060,20 +1053,16 @@ static int ak8975_probe(struct i2c_client *client)
 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
 					      ak8975_handle_trigger,
 					      &ak8975_buffer_setup_ops);
-	if (ret) {
-		dev_err(&client->dev, "triggered buffer setup failed\n");
-		return ret;
-	}
+	if (ret)
+		return dev_err_probe(dev, ret, "triggered buffer setup failed\n");
 
 	ret = devm_pm_runtime_enable(dev);
 	if (ret)
 		return ret;
 
 	ret = devm_iio_device_register(dev, indio_dev);
-	if (ret) {
-		dev_err(&client->dev, "device register failed\n");
-		return ret;
-	}
+	if (ret)
+		return dev_err_probe(dev, ret, "device register failed\n");
 
 	/*
 	 * The device comes online in 500us, so add two orders of magnitude
Signature · 1 line
2.47.3
#5 lore raw

PATCH v8 3/5 iio: magnetometer: ak8975: use temporary variable for struct device

In reply to Joshua Crofts

From: Andy Shevchenko <[email protected]>

Use temporary variable for struct device to make code neater.

---
 drivers/iio/magnetometer/ak8975.c | 63 +++++++++++++++++++--------------------
 1 file changed, 31 insertions(+), 32 deletions(-)
drivers/iio/magnetometer/ak8975.c +31 -32
index e7cd28ab37aaeb2b68758a42ba40a87c71bd1e24..7ab4c4d69d8b73c6f367f6e62fee3017d8691b67 100644
@@ -433,18 +433,17 @@ struct ak8975_data {
 /* Enable attached power regulator if any. */
 static int ak8975_power_on(const struct ak8975_data *data)
 {
+	struct device *dev = &data->client->dev;
 	int ret;
 
 	ret = regulator_enable(data->vdd);
 	if (ret) {
-		dev_warn(&data->client->dev,
-			 "Failed to enable specified Vdd supply\n");
+		dev_warn(dev, "Failed to enable specified Vdd supply\n");
 		return ret;
 	}
 	ret = regulator_enable(data->vid);
 	if (ret) {
-		dev_warn(&data->client->dev,
-			 "Failed to enable specified Vid supply\n");
+		dev_warn(dev, "Failed to enable specified Vid supply\n");
 		regulator_disable(data->vdd);
 		return ret;
 	}
@@ -574,6 +573,7 @@ static irqreturn_t ak8975_irq_handler(int irq, void *data)
 static int ak8975_setup_irq(struct ak8975_data *data)
 {
 	struct i2c_client *client = data->client;
+	struct device *dev = &client->dev;
 	int irq;
 	int ret;
 
@@ -584,9 +584,8 @@ static int ak8975_setup_irq(struct ak8975_data *data)
 	else
 		irq = gpiod_to_irq(data->eoc_gpiod);
 
-	ret = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
-			       IRQF_TRIGGER_RISING,
-			       dev_name(&client->dev), data);
+	ret = devm_request_irq(dev, irq, ak8975_irq_handler, IRQF_TRIGGER_RISING,
+			       dev_name(dev), data);
 	if (ret)
 		return ret;
 
@@ -602,12 +601,13 @@ static int ak8975_setup_irq(struct ak8975_data *data)
 static int ak8975_setup(struct ak8975_data *data)
 {
 	struct i2c_client *client = data->client;
+	struct device *dev = &client->dev;
 	int ret;
 
 	/* Write the fused rom access mode. */
 	ret = ak8975_set_mode(data, FUSE_ROM);
 	if (ret < 0) {
-		dev_err(&client->dev, "Error in setting fuse access mode\n");
+		dev_err(dev, "Error in setting fuse access mode\n");
 		return ret;
 	}
 
@@ -617,7 +617,7 @@ static int ak8975_setup(struct ak8975_data *data)
 							sizeof(data->asa),
 							data->asa);
 	if (ret < 0) {
-		dev_err(&client->dev, "Not able to read asa data\n");
+		dev_err(dev, "Not able to read asa data\n");
 		return ret;
 	}
 	if (ret != sizeof(data->asa)) {
@@ -628,15 +628,14 @@ static int ak8975_setup(struct ak8975_data *data)
 	/* After reading fuse ROM data set power-down mode */
 	ret = ak8975_set_mode(data, POWER_DOWN);
 	if (ret < 0) {
-		dev_err(&client->dev, "Error in setting power-down mode\n");
+		dev_err(dev, "Error in setting power-down mode\n");
 		return ret;
 	}
 
 	if (data->eoc_gpiod || client->irq > 0) {
 		ret = ak8975_setup_irq(data);
 		if (ret < 0) {
-			dev_err(&client->dev,
-				"Error setting data ready interrupt\n");
+			dev_err(dev, "Error setting data ready interrupt\n");
 			return ret;
 		}
 	}
@@ -742,10 +741,11 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
 	struct ak8975_data *data = iio_priv(indio_dev);
 	const struct i2c_client *client = data->client;
 	const struct ak_def *def = data->def;
+	struct device *dev = &data->client->dev;
 	__le16 rval;
 	int ret;
 
-	pm_runtime_get_sync(&data->client->dev);
+	pm_runtime_get_sync(dev);
 
 	mutex_lock(&data->lock);
 
@@ -767,20 +767,20 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
 	/* Read out ST2 for release lock on measurement data. */
 	ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST2]);
 	if (ret < 0) {
-		dev_err(&client->dev, "Error in reading ST2\n");
+		dev_err(dev, "Error in reading ST2\n");
 		goto exit;
 	}
 
 	if (ret & (data->def->ctrl_masks[ST2_DERR] |
 		   data->def->ctrl_masks[ST2_HOFL])) {
-		dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
+		dev_err(dev, "ST2 status error 0x%x\n", ret);
 		ret = -EINVAL;
 		goto exit;
 	}
 
 	mutex_unlock(&data->lock);
 
-	pm_runtime_put_autosuspend(&data->client->dev);
+	pm_runtime_put_autosuspend(dev);
 
 	/* Swap bytes and convert to valid range. */
 	*val = clamp_t(s16, le16_to_cpu(rval), -def->range, def->range);
@@ -789,8 +789,8 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
 
 exit:
 	mutex_unlock(&data->lock);
-	pm_runtime_put_autosuspend(&data->client->dev);
-	dev_err(&client->dev, "Error in reading axis\n");
+	pm_runtime_put_autosuspend(dev);
+	dev_err(dev, "Error in reading axis\n");
 	return ret;
 }
 
@@ -962,7 +962,7 @@ static int ak8975_probe(struct i2c_client *client)
 	 * We may not have a GPIO based IRQ to scan, that is fine, we will
 	 * poll if so.
 	 */
-	eoc_gpiod = devm_gpiod_get_optional(&client->dev, NULL, GPIOD_IN);
+	eoc_gpiod = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
 	if (IS_ERR(eoc_gpiod))
 		return PTR_ERR(eoc_gpiod);
 	gpiod_set_consumer_name(eoc_gpiod, "ak_8975");
@@ -972,13 +972,12 @@ static int ak8975_probe(struct i2c_client *client)
 	 * deassert reset on ak8975_power_on() and assert reset on
 	 * ak8975_power_off().
 	 */
-	reset_gpiod = devm_gpiod_get_optional(&client->dev,
-					      "reset", GPIOD_OUT_HIGH);
+	reset_gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
 	if (IS_ERR(reset_gpiod))
 		return PTR_ERR(reset_gpiod);
 
 	/* Register with IIO */
-	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
 	if (indio_dev == NULL)
 		return -ENOMEM;
 
@@ -990,7 +989,7 @@ static int ak8975_probe(struct i2c_client *client)
 	data->reset_gpiod = reset_gpiod;
 	data->eoc_irq = 0;
 
-	ret = iio_read_mount_matrix(&client->dev, &data->orientation);
+	ret = iio_read_mount_matrix(dev, &data->orientation);
 	if (ret)
 		return ret;
 
@@ -1000,16 +999,16 @@ static int ak8975_probe(struct i2c_client *client)
 		return -ENODEV;
 
 	/* If enumerated via firmware node, fix the ABI */
-	if (dev_fwnode(&client->dev))
-		name = dev_name(&client->dev);
+	if (dev_fwnode(dev))
+		name = dev_name(dev);
 	else
 		name = id->name;
 
 	/* Fetch the regulators */
-	data->vdd = devm_regulator_get(&client->dev, "vdd");
+	data->vdd = devm_regulator_get(dev, "vdd");
 	if (IS_ERR(data->vdd))
 		return PTR_ERR(data->vdd);
-	data->vid = devm_regulator_get(&client->dev, "vid");
+	data->vid = devm_regulator_get(dev, "vid");
 	if (IS_ERR(data->vid))
 		return PTR_ERR(data->vid);
 
@@ -1032,7 +1031,7 @@ static int ak8975_probe(struct i2c_client *client)
 	if (ret)
 		return dev_err_probe(dev, ret, "Unexpected device\n");
 
-	dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
+	dev_dbg(dev, "Asahi compass chip %s\n", name);
 
 	/* Perform some basic start-of-day setup of the device. */
 	ret = ak8975_setup(data);
@@ -1068,8 +1067,8 @@ static int ak8975_probe(struct i2c_client *client)
 	 * The device comes online in 500us, so add two orders of magnitude
 	 * of delay before autosuspending: 50 ms.
 	 */
-	pm_runtime_set_autosuspend_delay(&client->dev, 50);
-	pm_runtime_use_autosuspend(&client->dev);
+	pm_runtime_set_autosuspend_delay(dev, 50);
+	pm_runtime_use_autosuspend(dev);
 
 	return 0;
 }
@@ -1084,7 +1083,7 @@ static int ak8975_runtime_suspend(struct device *dev)
 	/* Set the device in power down if it wasn't already */
 	ret = ak8975_set_mode(data, POWER_DOWN);
 	if (ret < 0) {
-		dev_err(&client->dev, "Error in setting power-down mode\n");
+		dev_err(dev, "Error in setting power-down mode\n");
 		return ret;
 	}
 	/* Next cut the regulators */
@@ -1108,7 +1107,7 @@ static int ak8975_runtime_resume(struct device *dev)
 	 */
 	ret = ak8975_set_mode(data, POWER_DOWN);
 	if (ret < 0) {
-		dev_err(&client->dev, "Error in setting power-down mode\n");
+		dev_err(dev, "Error in setting power-down mode\n");
 		return ret;
 	}
 
Signature · 1 line
2.47.3
#6 lore raw

PATCH v8 4/5 iio: magnetometer: ak8975: add scan mask index enum

In reply to Joshua Crofts

Add an enum to explicitly define scan mask indexes for the X, Y, Z and timestamp channels. Also, update the struct iio_chan_spec to use said enum for the .scan_index parameter.

This prevents magic numbers from obscuring the hardware channel mapping and improves code style.

No functional change.

---
 drivers/iio/magnetometer/ak8975.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/iio/magnetometer/ak8975.c +11 -2
index 7ab4c4d69d8b73c6f367f6e62fee3017d8691b67..a140d352bad663a138295dc0c44084a9a18a4c24 100644
@@ -238,6 +238,13 @@ enum ak_ctrl_mode {
 	MODE_END,
 };
 
+enum ak_scan_index {
+	AK8975_SCAN_X,
+	AK8975_SCAN_Y,
+	AK8975_SCAN_Z,
+	AK8975_SCAN_TS,
+};
+
 struct ak_def {
 	enum asahi_compass_chipset type;
 	long (*raw_to_gauss)(u16 data);
@@ -845,8 +852,10 @@ static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
 	}
 
 static const struct iio_chan_spec ak8975_channels[] = {
-	AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
-	IIO_CHAN_SOFT_TIMESTAMP(3),
+	AK8975_CHANNEL(X, AK8975_SCAN_X),
+	AK8975_CHANNEL(Y, AK8975_SCAN_Y),
+	AK8975_CHANNEL(Z, AK8975_SCAN_Z),
+	IIO_CHAN_SOFT_TIMESTAMP(AK8975_SCAN_TS),
 };
 
 static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
Signature · 1 line
2.47.3
maintainer
#7 lore raw

Re: [PATCH v8 4/5] iio: magnetometer: ak8975: add scan mask index enum

Quoted 6 lines · On Mon, 18 May 2026 09:50:28 +0200 Joshua Crofts via B4 Relay…

From: Joshua Crofts <[email protected]>

Add an enum to explicitly define scan mask indexes for the X, Y, Z and timestamp channels. Also, update the struct iio_chan_spec to use said enum for the .scan_index parameter.

Quoted 8 lines

This prevents magic numbers from obscuring the hardware channel mapping and improves code style.

No functional change.

There is one other place we can use this to improve the code:

Replace:
static const unsigned long ak8975_scan_masks[] = { 0x7, 0 }; with

static const unsigned long ak8975_scan_masks[] = {
	BIT(AK8975_SCAN_X) | BIT(AK8975_SCAN_Y) | BIT(AK8975_SCAN_Z),
	0
};

That makes it obvious we are dealing with all the channels.

I only noticed this because I couldn't work out why there weren't more per things indexing the channel / addresses stored in channels or that sort of thing. Answer was because the driver only does things one way.

Also, maybe rename....

Jonathan

Quoted 9 lines
---
 drivers/iio/magnetometer/ak8975.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/iio/magnetometer/ak8975.c +0 -0
index 7ab4c4d69d8b73c6f367f6e62fee3017d8691b67..a140d352bad663a138295dc0c44084a9a18a4c24 100644

drivers/iio/magnetometer/ak8975.c enum ak_ctrl_mode { · line 245

Quoted 8 lines
drivers/iio/magnetometer/ak8975.c +5 -0
@@ -238,6 +238,13 @@ enum ak_ctrl_mode {
 	MODE_END,
 };
 
+enum ak_scan_index {
+	AK8975_SCAN_X,
+	AK8975_SCAN_Y,
+	AK8975_SCAN_Z,
+	AK8975_SCAN_TS,

Can we call these

	AK8975_CHAN_X,

etc because they are used both for scan indexes and as chan->address and putting something called scan index in there feels wrong.

Hence use a vaguer naming.

Quoted 19 lines
drivers/iio/magnetometer/ak8975.c +6 -2
+};
+
 struct ak_def {
 	enum asahi_compass_chipset type;
 	long (*raw_to_gauss)(u16 data);
@@ -845,8 +852,10 @@ static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
 	}
 
 static const struct iio_chan_spec ak8975_channels[] = {
-	AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
-	IIO_CHAN_SOFT_TIMESTAMP(3),
+	AK8975_CHANNEL(X, AK8975_SCAN_X),
+	AK8975_CHANNEL(Y, AK8975_SCAN_Y),
+	AK8975_CHANNEL(Z, AK8975_SCAN_Z),
+	IIO_CHAN_SOFT_TIMESTAMP(AK8975_SCAN_TS),
 };
 
 static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
#8 lore raw

PATCH v8 5/5 iio: magnetometer: ak8975: make use of the macros from bits.h

In reply to Joshua Crofts

From: Andy Shevchenko <[email protected]>

Make use of BIT() and GENMASK() where it makes sense.

---
 drivers/iio/magnetometer/ak8975.c | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)
drivers/iio/magnetometer/ak8975.c +11 -15
index a140d352bad663a138295dc0c44084a9a18a4c24..366d56a3f0355f9db3711e17e1215b1210f0c9b5 100644
@@ -45,8 +45,7 @@
 #define AK8975_REG_INFO			0x01
 
 #define AK8975_REG_ST1			0x02
-#define AK8975_REG_ST1_DRDY_SHIFT	0
-#define AK8975_REG_ST1_DRDY_MASK	(1 << AK8975_REG_ST1_DRDY_SHIFT)
+#define AK8975_REG_ST1_DRDY_MASK	BIT(0)
 
 #define AK8975_REG_HXL			0x03
 #define AK8975_REG_HXH			0x04
@@ -55,15 +54,12 @@
 #define AK8975_REG_HZL			0x07
 #define AK8975_REG_HZH			0x08
 #define AK8975_REG_ST2			0x09
-#define AK8975_REG_ST2_DERR_SHIFT	2
-#define AK8975_REG_ST2_DERR_MASK	(1 << AK8975_REG_ST2_DERR_SHIFT)
+#define AK8975_REG_ST2_DERR_MASK	BIT(2)
 
-#define AK8975_REG_ST2_HOFL_SHIFT	3
-#define AK8975_REG_ST2_HOFL_MASK	(1 << AK8975_REG_ST2_HOFL_SHIFT)
+#define AK8975_REG_ST2_HOFL_MASK	BIT(3)
 
 #define AK8975_REG_CNTL			0x0A
-#define AK8975_REG_CNTL_MODE_SHIFT	0
-#define AK8975_REG_CNTL_MODE_MASK	(0xF << AK8975_REG_CNTL_MODE_SHIFT)
+#define AK8975_REG_CNTL_MODE_MASK	GENMASK(3, 0)
 #define AK8975_REG_CNTL_MODE_POWER_DOWN	0x00
 #define AK8975_REG_CNTL_MODE_ONCE	0x01
 #define AK8975_REG_CNTL_MODE_SELF_TEST	0x08
@@ -95,8 +91,7 @@
 
 #define AK09912_REG_ST1			0x10
 
-#define AK09912_REG_ST1_DRDY_SHIFT	0
-#define AK09912_REG_ST1_DRDY_MASK	(1 << AK09912_REG_ST1_DRDY_SHIFT)
+#define AK09912_REG_ST1_DRDY_MASK	BIT(0)
 
 #define AK09912_REG_HXL			0x11
 #define AK09912_REG_HXH			0x12
@@ -107,8 +102,7 @@
 #define AK09912_REG_TMPS		0x17
 
 #define AK09912_REG_ST2			0x18
-#define AK09912_REG_ST2_HOFL_SHIFT	3
-#define AK09912_REG_ST2_HOFL_MASK	(1 << AK09912_REG_ST2_HOFL_SHIFT)
+#define AK09912_REG_ST2_HOFL_MASK	BIT(3)
 
 #define AK09912_REG_CNTL1		0x30
 
@@ -117,8 +111,7 @@
 #define AK09912_REG_CNTL_MODE_ONCE	0x01
 #define AK09912_REG_CNTL_MODE_SELF_TEST	0x10
 #define AK09912_REG_CNTL_MODE_FUSE_ROM	0x1F
-#define AK09912_REG_CNTL2_MODE_SHIFT	0
-#define AK09912_REG_CNTL2_MODE_MASK	(0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
+#define AK09912_REG_CNTL2_MODE_MASK	GENMASK(4, 0)
 
 #define AK09912_REG_CNTL3		0x32
 
@@ -858,7 +851,10 @@ static const struct iio_chan_spec ak8975_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(AK8975_SCAN_TS),
 };
 
-static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
+static const unsigned long ak8975_scan_masks[] = {
+	BIT(AK8975_SCAN_X) | BIT(AK8975_SCAN_Y) | BIT(AK8975_SCAN_Z),
+	0
+};
 
 static const struct iio_info ak8975_info = {
 	.read_raw = &ak8975_read_raw,
Signature · 1 line
2.47.3
maintainer
#9 lore raw

Re: [PATCH v8 5/5] iio: magnetometer: ak8975: make use of the macros from bits.h

Quoted 11 lines · On Mon, 18 May 2026 09:50:29 +0200 Joshua Crofts via B4 Relay…

From: Andy Shevchenko <[email protected]>

Make use of BIT() and GENMASK() where it makes sense.

drivers/iio/magnetometer/ak8975.c static const struct iio_chan_spec ak8975_channels[] = { · line 855

Quoted 6 lines
drivers/iio/magnetometer/ak8975.c +2 -1
@@ -858,7 +851,10 @@ static const struct iio_chan_spec ak8975_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(AK8975_SCAN_TS),
 };
 
-static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
+static const unsigned long ak8975_scan_masks[] = {
+	BIT(AK8975_SCAN_X) | BIT(AK8975_SCAN_Y) | BIT(AK8975_SCAN_Z),

Ah. Here it is. Fair enough.
Maybe I'll remember it next time!

Quoted 6 lines
drivers/iio/magnetometer/ak8975.c +2 -0
+	0
+};
 
 static const struct iio_info ak8975_info = {
 	.read_raw = &ak8975_read_raw,