Skip to contentSkip to conversation

spi: add devm_spi_optimize_message() helper

PATCH In review David Lechner 2 participants 4 messages Jun 24, 2024 – Jun 30, 2024

Subsystems (5)
lore
Share & tools
lore

David Lechner

author reviewer
#0 lore raw

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

---
Changes in v3:
- Fix commit hash in Fixes: tag in first patch.
- Link to v2: https://lore.kernel.org/r/20240624-devm_spi_optimize_message-v2-0-58155c0180c2@baylibre.com

Changes in v2:
- Dropped already applied SPI patches.
- Added patch to fix missing EXPORT_SYMBOL_GPL.
- Link to v1: https://lore.kernel.org/r/20240621-devm_spi_optimize_message-v1-0-3f9dcba6e95e@baylibre.com

---
David Lechner (2):
spi: add EXPORT_SYMBOL_GPL(devm_spi_optimize_message)
iio: adc: ad7944: use devm_spi_optimize_message()

 drivers/iio/adc/ad7944.c | 26 +++-----------------------
 drivers/spi/spi.c        |  1 +
 2 files changed, 4 insertions(+), 23 deletions(-)

---
base-commit: 17436001a6bc42c7f55dc547ca5b1a873208d91d
change-id: 20240621-devm_spi_optimize_message-ebbde029dd7a

Best regards,

Signature · 1 line
David Lechner <[email protected]>
author reviewer
#1 lore raw

PATCH v3 1/2 spi: add EXPORT_SYMBOL_GPL(devm_spi_optimize_message)

devm_spi_optimize_message() is a public function and needs EXPORT_SYMBOL_GPL.

---
 drivers/spi/spi.c | 1 +
 1 file changed, 1 insertion(+)
drivers/spi/spi.c +1 -0
index 3f953504244b..814d66fc9753 100644
@@ -4384,6 +4384,7 @@ int devm_spi_optimize_message(struct device *dev, struct spi_device *spi,
 
 	return devm_add_action_or_reset(dev, devm_spi_unoptimize_message, msg);
 }
+EXPORT_SYMBOL_GPL(devm_spi_optimize_message);
 
 /**
  * spi_async - asynchronous SPI transfer
Signature · 1 line
2.45.2
author reviewer
#2 lore raw

PATCH v3 2/2 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
#3 lore raw

Re: [PATCH v3 2/2] iio: adc: ad7944: use devm_spi_optimize_message()

Quoted 4 lines · On Mon, 24 Jun 2024 15:46:08 -0500 David Lechner <[email protected]> wrote:

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

Merged the updated tag from Mark and applied this on top.

Thanks,

Jonathan