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

From: Peng Fan

Date: Wed Dec 02 2026 - 01:50:46 EST


Hi Wolfram,
On Tue, Dec 01, 2026 at 11:50:27PM +0100, Wolfram Sang wrote:
>On Tue, Dec 01, 2026 at 05:45:51PM +0800, Peng Fan wrote:
>> 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>
>
>Acked-by: Wolfram Sang <wsa+renesas@xxxxxxxxxxxxxxxxxxxx>
>
>Applied to my for-andi branch, thanks!

Thanks for applying.

Sashiko reported minor issue:
https://sashiko.dev/#/message/sashiko-outbox-243018%40kernel.org

I am thinking to use below simplied version, do you expect a V2 or the applied
version is fine for you?

Thanks
Peng

------------------------------------------------------
Subject: [PATCH V2] i2c: add subsys_i2c_driver() macro

Add subsys_i2c_driver() so that I2C drivers registered at the
subsys_initcall() level can avoid the open-coded init/exit boilerplate.
It replaces subsys_initcall() plus module_exit() and is a parallel of
module_i2c_driver() with the init call promoted to subsys_initcall() when
built in.

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

diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 6a847fc88d25..c583fd1ab691 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -1020,6 +1020,19 @@ int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr);
#define builtin_i2c_driver(__i2c_driver) \
builtin_driver(__i2c_driver, i2c_add_driver)

+/**
+ * subsys_i2c_driver() - Helper macro for registering an I2C driver
+ * @__i2c_driver: i2c_driver struct
+ *
+ * Helper macro for I2C drivers which do not do anything special in their
+ * init/exit and need to be registered 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)
+
/* must call put_device() when done with returned i2c_client device */
struct i2c_client *i2c_find_device_by_fwnode(struct fwnode_handle *fwnode);

--
2.56.0