[PATCH 1/2] i2c: add subsys_i2c_driver() macro

From: Peng Fan

Date: Tue Dec 01 2026 - 04:46:27 EST


From: Peng Fan <peng.fan@xxxxxxx>

Add subsys_i2c_driver() and subsys_i2c_driver_probe() macros so that
I2C drivers registered at the subsys_initcall() level can avoid the
open-coded init/exit boilerplate.

subsys_i2c_driver() replaces subsys_initcall() plus module_exit() for
drivers that need an unregister path. subsys_i2c_driver_probe() replaces
subsys_initcall() for drivers without an exit callback.

Signed-off-by: Peng Fan <peng.fan@xxxxxxx>
---
include/linux/i2c.h | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)

diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 6a847fc88d25..f26bb2e0ba0d 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -915,6 +915,29 @@ void i2c_del_driver(struct i2c_driver *driver);
#define i2c_add_driver(driver) \
i2c_register_driver(THIS_MODULE, driver)

+/*
+ * subsys_i2c_driver() - Helper macro for i2c drivers that don't do anything
+ * special in module init/exit but need to register earlier, at
+ * subsys_initcall level, when built in. This eliminates a lot of
+ * boilerplate. Each driver may only use this macro once, and calling it
+ * replaces subsys_initcall() and module_exit().
+ */
+#define subsys_i2c_driver(__i2c_driver) \
+ subsys_driver(__i2c_driver, i2c_add_driver, i2c_del_driver)
+
+/*
+ * subsys_i2c_driver_probe() - Helper macro for drivers that don't do
+ * anything special in driver register and have no exit, but need to register
+ * earlier, at subsys_initcall level. This eliminates some boilerplate. Each
+ * driver may only use this macro once, and using it replaces subsys_initcall.
+ */
+#define subsys_i2c_driver_probe(__i2c_driver) \
+static int __init __i2c_driver##_init(void) \
+{ \
+ return i2c_add_driver(&__i2c_driver); \
+} \
+subsys_initcall(__i2c_driver##_init)
+
static inline bool i2c_client_has_driver(struct i2c_client *client)
{
return !IS_ERR_OR_NULL(client) && client->dev.driver;

--
2.56.0