[PATCH] spi: Add spi_for_each_controller() helper

958028483@qq.com posted 1 patch 4 weeks, 1 day ago
There is a newer version of this series
drivers/spi/spi.c       | 17 +++++++++++++++++
include/linux/spi/spi.h |  2 ++
2 files changed, 19 insertions(+)
[PATCH] spi: Add spi_for_each_controller() helper
Posted by 958028483@qq.com 4 weeks, 1 day ago
From: huangchao <huangchao@kylinos.cn>

Add a new helper function to iterate over all SPI controllers.
This provides a convenient way for SPI core code and drivers
to perform operations on all registered controllers.

The implementation follows existing SPI subsystem patterns:
- Uses spi_controller_list with board_lock protection
- Directly operates on spi_controller objects
- Breaks iteration on callback error
- Follows kernel naming conventions (spi_for_each_*)

This helper is useful for scenarios like statistics gathering,
configuration updates, or debugging operations that need to
process all SPI controllers in the system.

Signed-off-by: huangchao <huangchao@kylinos.cn>
---
 drivers/spi/spi.c       | 17 +++++++++++++++++
 include/linux/spi/spi.h |  2 ++
 2 files changed, 19 insertions(+)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 104279858f56..7cd07b390a4b 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -5116,6 +5116,23 @@ static struct notifier_block spi_acpi_notifier = {
 extern struct notifier_block spi_acpi_notifier;
 #endif
 
+int spi_for_each_controller(int (*fn)(struct spi_controller *, void *), void *data)
+{
+	struct spi_controller *ctlr;
+	int ret = 0;
+
+	mutex_lock(&board_lock);
+	list_for_each_entry(ctlr, &spi_controller_list, list) {
+		ret = fn(ctlr, data);
+		if (ret)
+			break;
+	}
+	mutex_unlock(&board_lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(spi_for_each_controller);
+
 static int __init spi_init(void)
 {
 	int	status;
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index 79513f5941cc..8fcf3faf9739 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -934,6 +934,8 @@ static inline int acpi_spi_count_resources(struct acpi_device *adev)
 }
 #endif
 
+int spi_for_each_controller(int (*fn)(struct spi_controller *, void *), void *data);
+
 /*
  * SPI resource management while processing a SPI message
  */
-- 
2.25.1
Re: [PATCH] spi: Add spi_for_each_controller() helper
Posted by Mark Brown 4 weeks, 1 day ago
On Thu, May 14, 2026 at 02:13:08PM +0800, 958028483@qq.com wrote:
> From: huangchao <huangchao@kylinos.cn>

> Signed-off-by: huangchao <huangchao@kylinos.cn>

Your email address and (non-)name don't match your signoff at all so I
can't really verify that you're the same person, could you please bring
the two in line somehow so I can verify that there's a valid signoff?