Skip to contentSkip to conversation

spi: add devm_spi_optimize_message() helper

PATCH Superseded David Lechner 3 participants 10 messages Jun 21, 2024 – Jun 24, 2024

Subsystems (6)
Documentation SPI IIO Driver core +2 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

David Lechner

author reviewer
#0 lore raw

cover spi: add devm_spi_optimize_message() helper

In the IIO subsystem, we are finding that it is common to call spi_optimize_message() during driver probe since the SPI message doesn't change for the lifetime of the driver. This patch adds a devm_spi_optimize_message() helper to simplify this common pattern.

---
David Lechner (3):
Documentation: devres: add missing SPI helpers
spi: add devm_spi_optimize_message() helper
iio: adc: ad7944: use devm_spi_optimize_message()

 Documentation/driver-api/driver-model/devres.rst |  3 +++
 drivers/iio/adc/ad7944.c                         | 26 +++--------------------
 drivers/spi/spi.c                                | 27 ++++++++++++++++++++++++
 include/linux/spi/spi.h                          |  2 ++
 4 files changed, 35 insertions(+), 23 deletions(-)

---
base-commit: 0ca645ab5b1528666f6662a0e620140355b5aea3
change-id: 20240621-devm_spi_optimize_message-ebbde029dd7a

author reviewer
#1 lore raw

PATCH 1/3 Documentation: devres: add missing SPI helpers

A few SPI devm_* helpers were missing from the devres documentation. This patch adds them.

---
 Documentation/driver-api/driver-model/devres.rst | 2 ++
 1 file changed, 2 insertions(+)
Documentation/driver-api/driver-model/devres.rst +2 -0
index 18caebad7376..a1c17bcae68d 100644
@@ -465,6 +465,8 @@ SPI
   devm_spi_alloc_master()
   devm_spi_alloc_slave()
   devm_spi_register_controller()
+  devm_spi_register_host()
+  devm_spi_register_target()
 
 WATCHDOG
   devm_watchdog_register_device()
Signature · 1 line
2.45.2
author reviewer
#2 lore raw

PATCH 2/3 spi: add devm_spi_optimize_message() helper

In reply to David Lechner

This adds a new helper function devm_spi_optimize_message() that automatically registers spi_unoptimize_message() to be called when the device is removed.

---
 Documentation/driver-api/driver-model/devres.rst |  1 +
 drivers/spi/spi.c                                | 27 ++++++++++++++++++++++++
 include/linux/spi/spi.h                          |  2 ++
 3 files changed, 30 insertions(+)
Documentation/driver-api/driver-model/devres.rst +1 -0
index a1c17bcae68d..ac9ee7441887 100644
@@ -464,6 +464,7 @@ SLAVE DMA ENGINE
 SPI
   devm_spi_alloc_master()
   devm_spi_alloc_slave()
+  devm_spi_optimize_message()
   devm_spi_register_controller()
   devm_spi_register_host()
   devm_spi_register_target()
drivers/spi/spi.c +27 -0
index 54cbe652a4df..3f953504244b 100644
@@ -4358,6 +4358,33 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
 	return ctlr->transfer(spi, message);
 }
 
+static void devm_spi_unoptimize_message(void *msg)
+{
+	spi_unoptimize_message(msg);
+}
+
+/**
+ * devm_spi_optimize_message - managed version of spi_optimize_message()
+ * @dev: the device that manages @msg (usually @spi->dev)
+ * @spi: the device that will be used for the message
+ * @msg: the message to optimize
+ * Return: zero on success, else a negative error code
+ *
+ * spi_unoptimize_message() will automatically be called when the device is
+ * removed.
+ */
+int devm_spi_optimize_message(struct device *dev, struct spi_device *spi,
+			      struct spi_message *msg)
+{
+	int ret;
+
+	ret = spi_optimize_message(spi, msg);
+	if (ret)
+		return ret;
+
+	return devm_add_action_or_reset(dev, devm_spi_unoptimize_message, msg);
+}
+
 /**
  * spi_async - asynchronous SPI transfer
  * @spi: device with which data will be exchanged
include/linux/spi/spi.h +2 -0
index 85785bcd20c1..a9388714e7e7 100644
@@ -1272,6 +1272,8 @@ static inline void spi_message_free(struct spi_message *m)
 
 extern int spi_optimize_message(struct spi_device *spi, struct spi_message *msg);
 extern void spi_unoptimize_message(struct spi_message *msg);
+extern int devm_spi_optimize_message(struct device *dev, struct spi_device *spi,
+				     struct spi_message *msg);
 
 extern int spi_setup(struct spi_device *spi);
 extern int spi_async(struct spi_device *spi, struct spi_message *message);
Signature · 1 line
2.45.2
maintainer
#3 lore raw
Quoted 5 lines · On Fri, 21 Jun 2024 15:51:31 -0500 David Lechner <[email protected]> wrote:

This adds a new helper function devm_spi_optimize_message() that automatically registers spi_unoptimize_message() to be called when the device is removed.

The new function can be called from modules and doesn't have an EXPORT_SYMBOL_GPL()

I failed to spot this but a build test immediately noticed!

Jonathan

Quoted 42 lines
---
 Documentation/driver-api/driver-model/devres.rst |  1 +
 drivers/spi/spi.c                                | 27 ++++++++++++++++++++++++
 include/linux/spi/spi.h                          |  2 ++
 3 files changed, 30 insertions(+)
Documentation/driver-api/driver-model/devres.rst +1 -0
index a1c17bcae68d..ac9ee7441887 100644
@@ -464,6 +464,7 @@ SLAVE DMA ENGINE
 SPI
   devm_spi_alloc_master()
   devm_spi_alloc_slave()
+  devm_spi_optimize_message()
   devm_spi_register_controller()
   devm_spi_register_host()
   devm_spi_register_target()
drivers/spi/spi.c +16 -0
index 54cbe652a4df..3f953504244b 100644
@@ -4358,6 +4358,33 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
 	return ctlr->transfer(spi, message);
 }
 
+static void devm_spi_unoptimize_message(void *msg)
+{
+	spi_unoptimize_message(msg);
+}
+
+/**
+ * devm_spi_optimize_message - managed version of spi_optimize_message()
+ * @dev: the device that manages @msg (usually @spi->dev)
+ * @spi: the device that will be used for the message
+ * @msg: the message to optimize
+ * Return: zero on success, else a negative error code
+ *
+ * spi_unoptimize_message() will automatically be called when the device is
+ * removed.
+ */
+int devm_spi_optimize_message(struct device *dev, struct spi_device *spi,

drivers/spi/spi.c static int __spi_async(struct spi_device *spi, struct spi_message *message) · line 4386

Quoted 10 lines
drivers/spi/spi.c +10 -0
+			      struct spi_message *msg)
+{
+	int ret;
+
+	ret = spi_optimize_message(spi, msg);
+	if (ret)
+		return ret;
+
+	return devm_add_action_or_reset(dev, devm_spi_unoptimize_message, msg);
+}

Missing an export.

Quoted 18 lines
drivers/spi/spi.c +1 -0
+
 /**
  * spi_async - asynchronous SPI transfer
  * @spi: device with which data will be exchanged
include/linux/spi/spi.h +2 -0
index 85785bcd20c1..a9388714e7e7 100644
@@ -1272,6 +1272,8 @@ static inline void spi_message_free(struct spi_message *m)
 
 extern int spi_optimize_message(struct spi_device *spi, struct spi_message *msg);
 extern void spi_unoptimize_message(struct spi_message *msg);
+extern int devm_spi_optimize_message(struct device *dev, struct spi_device *spi,
+				     struct spi_message *msg);
 
 extern int spi_setup(struct spi_device *spi);
 extern int spi_async(struct spi_device *spi, struct spi_message *message);
author reviewer
#4 lore raw

PATCH 3/3 iio: adc: ad7944: use devm_spi_optimize_message()

In reply to David Lechner

Use new devm_spi_optimize_message() helper to simplify repeated code in the ad7944 driver.

---
 drivers/iio/adc/ad7944.c | 26 +++-----------------------
 1 file changed, 3 insertions(+), 23 deletions(-)
drivers/iio/adc/ad7944.c +3 -23
index 4602ab5ed2a6..a6ac17c13a29 100644
@@ -134,18 +134,12 @@ AD7944_DEFINE_CHIP_INFO(ad7985, ad7944, 16, 0);
 /* fully differential */
 AD7944_DEFINE_CHIP_INFO(ad7986, ad7986, 18, 1);
 
-static void ad7944_unoptimize_msg(void *msg)
-{
-	spi_unoptimize_message(msg);
-}
-
 static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
 					 const struct iio_chan_spec *chan)
 {
 	unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns
 						   : adc->timing_spec->conv_ns;
 	struct spi_transfer *xfers = adc->xfers;
-	int ret;
 
 	/*
 	 * NB: can get better performance from some SPI controllers if we use
@@ -175,11 +169,7 @@ static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944_adc *
 
 	spi_message_init_with_transfers(&adc->msg, xfers, 3);
 
-	ret = spi_optimize_message(adc->spi, &adc->msg);
-	if (ret)
-		return ret;
-
-	return devm_add_action_or_reset(dev, ad7944_unoptimize_msg, &adc->msg);
+	return devm_spi_optimize_message(dev, adc->spi, &adc->msg);
 }
 
 static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
@@ -188,7 +178,6 @@ static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc
 	unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns
 						   : adc->timing_spec->conv_ns;
 	struct spi_transfer *xfers = adc->xfers;
-	int ret;
 
 	/*
 	 * NB: can get better performance from some SPI controllers if we use
@@ -209,11 +198,7 @@ static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc
 
 	spi_message_init_with_transfers(&adc->msg, xfers, 2);
 
-	ret = spi_optimize_message(adc->spi, &adc->msg);
-	if (ret)
-		return ret;
-
-	return devm_add_action_or_reset(dev, ad7944_unoptimize_msg, &adc->msg);
+	return devm_spi_optimize_message(dev, adc->spi, &adc->msg);
 }
 
 static int ad7944_chain_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
@@ -221,7 +206,6 @@ static int ad7944_chain_mode_init_msg(struct device *dev, struct ad7944_adc *adc
 				      u32 n_chain_dev)
 {
 	struct spi_transfer *xfers = adc->xfers;
-	int ret;
 
 	/*
 	 * NB: SCLK has to be low before we toggle CS to avoid triggering the
@@ -249,11 +233,7 @@ static int ad7944_chain_mode_init_msg(struct device *dev, struct ad7944_adc *adc
 
 	spi_message_init_with_transfers(&adc->msg, xfers, 2);
 
-	ret = spi_optimize_message(adc->spi, &adc->msg);
-	if (ret)
-		return ret;
-
-	return devm_add_action_or_reset(dev, ad7944_unoptimize_msg, &adc->msg);
+	return devm_spi_optimize_message(dev, adc->spi, &adc->msg);
 }
 
 /**
Signature · 1 line
2.45.2
maintainer
#5 lore raw
In reply to David Lechner
Quoted 4 lines · On Fri, 21 Jun 2024 15:51:29 -0500 David Lechner <[email protected]> wrote:

In the IIO subsystem, we are finding that it is common to call spi_optimize_message() during driver probe since the SPI message doesn't change for the lifetime of the driver. This patch adds a devm_spi_optimize_message() helper to simplify this common pattern.

Looks good to me. Obviously I need Mark's ack or an immutable branch to pick these up though.

Thanks

Jonathan

Quoted 15 lines

---
David Lechner (3):
Documentation: devres: add missing SPI helpers
spi: add devm_spi_optimize_message() helper
iio: adc: ad7944: use devm_spi_optimize_message()

 Documentation/driver-api/driver-model/devres.rst |  3 +++
 drivers/iio/adc/ad7944.c                         | 26 +++--------------------
 drivers/spi/spi.c                                | 27 ++++++++++++++++++++++++
 include/linux/spi/spi.h                          |  2 ++
 4 files changed, 35 insertions(+), 23 deletions(-)

---
base-commit: 0ca645ab5b1528666f6662a0e620140355b5aea3
change-id: 20240621-devm_spi_optimize_message-ebbde029dd7a

maintainer
#6 lore raw

Re: (subset) [PATCH 0/3] spi: add devm_spi_optimize_message() helper

In reply to David Lechner
Quoted 5 lines · David Lechner

In the IIO subsystem, we are finding that it is common to call spi_optimize_message() during driver probe since the SPI message doesn't change for the lifetime of the driver. This patch adds a devm_spi_optimize_message() helper to simplify this common pattern.

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

[1/3] Documentation: devres: add missing SPI helpers

      commit: 9b894d65e9788ece0d51e76d2c184524900f5ec9
[2/3] spi: add devm_spi_optimize_message() helper
      commit: 17436001a6bc42c7f55dc547ca5b1a873208d91d

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark

maintainer
#7 lore raw
In reply to David Lechner
Quoted 4 lines · David Lechner

In the IIO subsystem, we are finding that it is common to call spi_optimize_message() during driver probe since the SPI message doesn't change for the lifetime of the driver. This patch adds a devm_spi_optimize_message() helper to simplify this common pattern.

The following changes since commit 6ba59ff4227927d3a8530fc2973b80e94b54d58f:

Linux 6.10-rc4 (2024-06-16 13:40:16 -0700)

are available in the Git repository at:

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git tags/spi-devm-optimize

for you to fetch changes up to d4a0055fdc22381fa256e345095e88d134e354c5:

spi: add devm_spi_optimize_message() helper (2024-06-22 12:14:33 +0100)

----------------------------------------------------------------
spi: add devm_spi_optimize_message() helper

Helper from David Lechner <[email protected]>:

In the IIO subsystem, we are finding that it is common to call spi_optimize_message() during driver probe since the SPI message doesn't change for the lifetime of the driver. This patch adds a devm_spi_optimize_message() helper to simplify this common pattern.

----------------------------------------------------------------
David Lechner (2):
Documentation: devres: add missing SPI helpers
spi: add devm_spi_optimize_message() helper

 Documentation/driver-api/driver-model/devres.rst |  3 +++
 drivers/spi/spi.c                                | 27 ++++++++++++++++++++++++
 include/linux/spi/spi.h                          |  2 ++
 3 files changed, 32 insertions(+)
maintainer
#8 lore raw
Quoted 6 lines · On Sun, 23 Jun 2024 18:20:39 +0100 Mark Brown <[email protected]> wrote:
Quoted 4 lines · David Lechner

In the IIO subsystem, we are finding that it is common to call spi_optimize_message() during driver probe since the SPI message doesn't change for the lifetime of the driver. This patch adds a devm_spi_optimize_message() helper to simplify this common pattern.

Quoted 7 lines

The following changes since commit 6ba59ff4227927d3a8530fc2973b80e94b54d58f:

Linux 6.10-rc4 (2024-06-16 13:40:16 -0700)

are available in the Git repository at:

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git tags/spi-devm-optimize

Thanks. Applied patch 3 on a merge of that tag into the togreg branch of iio.git which I've pushed out as testing to see if anything went horribly wrong ;)

Jonathan

Quoted 24 lines

for you to fetch changes up to d4a0055fdc22381fa256e345095e88d134e354c5:

spi: add devm_spi_optimize_message() helper (2024-06-22 12:14:33 +0100)

----------------------------------------------------------------
spi: add devm_spi_optimize_message() helper

Helper from David Lechner <[email protected]>:

In the IIO subsystem, we are finding that it is common to call spi_optimize_message() during driver probe since the SPI message doesn't change for the lifetime of the driver. This patch adds a devm_spi_optimize_message() helper to simplify this common pattern.

----------------------------------------------------------------
David Lechner (2):
Documentation: devres: add missing SPI helpers
spi: add devm_spi_optimize_message() helper

 Documentation/driver-api/driver-model/devres.rst |  3 +++
 drivers/spi/spi.c                                | 27 ++++++++++++++++++++++++
 include/linux/spi/spi.h                          |  2 ++
 3 files changed, 32 insertions(+)
maintainer
#9 lore raw
Quoted 13 lines · On Mon, 24 Jun 2024 20:39:18 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 10 lines · On Sun, 23 Jun 2024 18:20:39 +0100 Mark Brown <[email protected]> wrote:
Quoted 4 lines · David Lechner

In the IIO subsystem, we are finding that it is common to call spi_optimize_message() during driver probe since the SPI message doesn't change for the lifetime of the driver. This patch adds a devm_spi_optimize_message() helper to simplify this common pattern.

The following changes since commit 6ba59ff4227927d3a8530fc2973b80e94b54d58f:

Linux 6.10-rc4 (2024-06-16 13:40:16 -0700)

Quoted 8 lines
Quoted 3 lines

are available in the Git repository at:

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git tags/spi-devm-optimize

Thanks. Applied patch 3 on a merge of that tag into the togreg branch of iio.git which I've pushed out as testing to see if anything went horribly wrong ;)

Hit send too soon. The new devm_spi_optmize_message() is missing an EXPORT_SYMBOL_* so my local build test with modules failed.

Jonathan

Quoted 27 lines

Jonathan

Quoted 24 lines

for you to fetch changes up to d4a0055fdc22381fa256e345095e88d134e354c5:

spi: add devm_spi_optimize_message() helper (2024-06-22 12:14:33 +0100)

----------------------------------------------------------------
spi: add devm_spi_optimize_message() helper

Helper from David Lechner <[email protected]>:

In the IIO subsystem, we are finding that it is common to call spi_optimize_message() during driver probe since the SPI message doesn't change for the lifetime of the driver. This patch adds a devm_spi_optimize_message() helper to simplify this common pattern.

----------------------------------------------------------------
David Lechner (2):
Documentation: devres: add missing SPI helpers
spi: add devm_spi_optimize_message() helper

 Documentation/driver-api/driver-model/devres.rst |  3 +++
 drivers/spi/spi.c                                | 27 ++++++++++++++++++++++++
 include/linux/spi/spi.h                          |  2 ++
 3 files changed, 32 insertions(+)