Skip to contentSkip to conversation

i2c: core: Move client towards fwnode

PATCH Superseded Andy Shevchenko 3 participants 15 messages Apr 7, 2025 – Apr 8, 2025

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover i2c: core: Move client towards fwnode

The struct i2c_board_info has of_node and fwnode members. This is quite confusing as they are of the same semantics and it's tend to have an issue if user assigns both. Luckily there is only a single driver that does this and fix is provided in the last patch. Nevertheless the series moves the client handling code to use fwnode and deprecates the of_node member in the respective documentation.

Tomi tested the last patch, but since it was separate there is no tag (yet).

In v3:
- fixed compile issues with i2c-core-slave.c (LKP)
- fixed compile issues with IRQ APIs, i.e. missing header (LKP)
- added patch for the only user which assigns two fields (Tomi)
- added tags (Tomi)

In v2:
- covered i2c-core-slave.c where it makes sense
- covered i2c-core-of.c where it makes sense
- rebased on top of the latest code base

Andy Shevchenko (7):
i2c: core: Drop duplicate check before calling OF APIs
i2c: core: Unify the firmware node type check
i2c: core: Switch to fwnode APIs to get IRQ
i2c: core: Reuse fwnode variable where it makes sense
i2c: core: Do not dereference fwnode in struct device
i2c: core: Deprecate of_node in struct i2c_board_info
media: i2c: ds90ub960: Remove of_node assignment

 drivers/i2c/i2c-core-base.c   | 61 +++++++++++++++++------------------
 drivers/i2c/i2c-core-of.c     |  1 -
 drivers/i2c/i2c-core-slave.c  | 12 ++++---
 drivers/media/i2c/ds90ub960.c |  1 -
 include/linux/i2c.h           |  2 +-
 5 files changed, 39 insertions(+), 38 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH v3 1/7 i2c: core: Drop duplicate check before calling OF APIs

OF APIs are usually NULL-aware and returns an error in case when device node is not present or supported. We already have a check for the returned value, no need to check for the parameter.

---
 drivers/i2c/i2c-core-base.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/i2c/i2c-core-base.c +7 -11
index ebda3926acf8..0d850d425734 100644
@@ -1213,11 +1213,9 @@ struct i2c_client *i2c_new_ancillary_device(struct i2c_client *client,
 	u32 addr = default_addr;
 	int i;
 
-	if (np) {
-		i = of_property_match_string(np, "reg-names", name);
-		if (i >= 0)
-			of_property_read_u32_index(np, "reg", i, &addr);
-	}
+	i = of_property_match_string(np, "reg-names", name);
+	if (i >= 0)
+		of_property_read_u32_index(np, "reg", i, &addr);
 
 	dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
 	return i2c_new_dummy_device(client->adapter, addr);
@@ -1655,12 +1653,10 @@ int i2c_add_adapter(struct i2c_adapter *adapter)
 	struct device *dev = &adapter->dev;
 	int id;
 
-	if (dev->of_node) {
-		id = of_alias_get_id(dev->of_node, "i2c");
-		if (id >= 0) {
-			adapter->nr = id;
-			return __i2c_add_numbered_adapter(adapter);
-		}
+	id = of_alias_get_id(dev->of_node, "i2c");
+	if (id >= 0) {
+		adapter->nr = id;
+		return __i2c_add_numbered_adapter(adapter);
 	}
 
 	mutex_lock(&core_lock);
Signature · 1 line
2.47.2
#2 lore raw

Re: [PATCH v3 1/7] i2c: core: Drop duplicate check before calling OF APIs

Quoted 1 line · Andy Shevchenko

OF APIs are usually NULL-aware and returns an error in case when

s/return\K/s//

Signature · 1 line
Sakari Ailus
#3 lore raw

PATCH v3 2/7 i2c: core: Unify the firmware node type check

In reply to Andy Shevchenko

OF and ACPI currently are using asymmetrical APIs to check for the firmware node type. Unify them by using is_*_node() against struct fwnode_handle pointer.

---
 drivers/i2c/i2c-core-base.c  | 14 ++++++++------
 drivers/i2c/i2c-core-slave.c | 12 ++++++++----
 2 files changed, 16 insertions(+), 10 deletions(-)
drivers/i2c/i2c-core-base.c +8 -6
index 0d850d425734..b320a20957ed 100644
@@ -494,6 +494,7 @@ static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
 
 static int i2c_device_probe(struct device *dev)
 {
+	struct fwnode_handle	*fwnode = dev_fwnode(dev);
 	struct i2c_client	*client = i2c_verify_client(dev);
 	struct i2c_driver	*driver;
 	bool do_power_on;
@@ -512,11 +513,11 @@ static int i2c_device_probe(struct device *dev)
 			/* Keep adapter active when Host Notify is required */
 			pm_runtime_get_sync(&client->adapter->dev);
 			irq = i2c_smbus_host_notify_to_irq(client);
-		} else if (dev->of_node) {
+		} else if (is_of_node(fwnode)) {
 			irq = of_irq_get_byname(dev->of_node, "irq");
 			if (irq == -EINVAL || irq == -ENODATA)
 				irq = of_irq_get(dev->of_node, 0);
-		} else if (ACPI_COMPANION(dev)) {
+		} else if (is_acpi_device_node(fwnode)) {
 			bool wake_capable;
 
 			irq = i2c_acpi_get_irq(client, &wake_capable);
@@ -1058,15 +1059,16 @@ EXPORT_SYMBOL_GPL(i2c_new_client_device);
  */
 void i2c_unregister_device(struct i2c_client *client)
 {
+	struct fwnode_handle *fwnode;
+
 	if (IS_ERR_OR_NULL(client))
 		return;
 
-	if (client->dev.of_node) {
+	fwnode = dev_fwnode(&client->dev);
+	if (is_of_node(fwnode)) {
 		of_node_clear_flag(client->dev.of_node, OF_POPULATED);
 		of_node_put(client->dev.of_node);
-	}
-
-	if (ACPI_COMPANION(&client->dev))
+	} else if (is_acpi_device_node(fwnode))
 		acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
 
 	device_remove_software_node(&client->dev);
drivers/i2c/i2c-core-slave.c +8 -4
index faefe1dfa8e5..7ee6b992b835 100644
@@ -11,6 +11,7 @@
 #include <linux/err.h>
 #include <linux/i2c.h>
 #include <linux/of.h>
+#include <linux/property.h>
 
 #include "i2c-core.h"
 
@@ -108,15 +109,18 @@ EXPORT_SYMBOL_GPL(i2c_slave_event);
  */
 bool i2c_detect_slave_mode(struct device *dev)
 {
-	if (IS_BUILTIN(CONFIG_OF) && dev->of_node) {
+	struct fwnode_handle *fwnode = dev_fwnode(dev);
+
+	if (is_of_node(fwnode)) {
+		struct fwnode_handle *child __free(fwnode_handle) = NULL;
 		u32 reg;
 
-		for_each_child_of_node_scoped(dev->of_node, child) {
-			of_property_read_u32(child, "reg", &reg);
+		fwnode_for_each_child_node(fwnode, child) {
+			fwnode_property_read_u32(child, "reg", &reg);
 			if (reg & I2C_OWN_SLAVE_ADDRESS)
 				return true;
 		}
-	} else if (IS_BUILTIN(CONFIG_ACPI) && ACPI_HANDLE(dev)) {
+	} else if (is_acpi_device_node(fwnode)) {
 		dev_dbg(dev, "ACPI slave is not supported yet\n");
 	}
 	return false;
Signature · 1 line
2.47.2
#4 lore raw

PATCH v3 3/7 i2c: core: Switch to fwnode APIs to get IRQ

In reply to Andy Shevchenko

Switch to fwnode APIs to get IRQ. In particular this enables a support of the separate wakeup IRQ. The rest is converted just for the sake of consistency and fwnode reuse.

---
 drivers/i2c/i2c-core-base.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/i2c/i2c-core-base.c +4 -5
index b320a20957ed..e2fdfbdb1bd7 100644
@@ -26,14 +26,13 @@
 #include <linux/idr.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
-#include <linux/irqflags.h>
+#include <linux/irq.h>
 #include <linux/jump_label.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/of_device.h>
 #include <linux/of.h>
-#include <linux/of_irq.h>
 #include <linux/pinctrl/consumer.h>
 #include <linux/pinctrl/devinfo.h>
 #include <linux/pm_domain.h>
@@ -514,9 +513,9 @@ static int i2c_device_probe(struct device *dev)
 			pm_runtime_get_sync(&client->adapter->dev);
 			irq = i2c_smbus_host_notify_to_irq(client);
 		} else if (is_of_node(fwnode)) {
-			irq = of_irq_get_byname(dev->of_node, "irq");
+			irq = fwnode_irq_get_byname(fwnode, "irq");
 			if (irq == -EINVAL || irq == -ENODATA)
-				irq = of_irq_get(dev->of_node, 0);
+				irq = fwnode_irq_get(fwnode, 0);
 		} else if (is_acpi_device_node(fwnode)) {
 			bool wake_capable;
 
@@ -551,7 +550,7 @@ static int i2c_device_probe(struct device *dev)
 	if (client->flags & I2C_CLIENT_WAKE) {
 		int wakeirq;
 
-		wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
+		wakeirq = fwnode_irq_get_byname(fwnode, "wakeup");
 		if (wakeirq == -EPROBE_DEFER) {
 			status = wakeirq;
 			goto put_sync_adapter;
Signature · 1 line
2.47.2
#5 lore raw

PATCH v3 4/7 i2c: core: Reuse fwnode variable where it makes sense

In reply to Andy Shevchenko

Reuse fwnode variable where it makes sense. This avoids unneeded duplication hidden in some macros and unifies the code for different types of fwnode.

---
 drivers/i2c/i2c-core-base.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/i2c/i2c-core-base.c +3 -3
index e2fdfbdb1bd7..c92badadd47a 100644
@@ -571,7 +571,7 @@ static int i2c_device_probe(struct device *dev)
 
 	dev_dbg(dev, "probe\n");
 
-	status = of_clk_set_defaults(dev->of_node, false);
+	status = of_clk_set_defaults(to_of_node(fwnode), false);
 	if (status < 0)
 		goto err_clear_wakeup_irq;
 
@@ -1065,10 +1065,10 @@ void i2c_unregister_device(struct i2c_client *client)
 
 	fwnode = dev_fwnode(&client->dev);
 	if (is_of_node(fwnode)) {
-		of_node_clear_flag(client->dev.of_node, OF_POPULATED);
+		of_node_clear_flag(to_of_node(fwnode), OF_POPULATED);
 		of_node_put(client->dev.of_node);
 	} else if (is_acpi_device_node(fwnode))
-		acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
+		acpi_device_clear_enumerated(to_acpi_device_node(fwnode));
 
 	device_remove_software_node(&client->dev);
 	device_unregister(&client->dev);
Signature · 1 line
2.47.2
#6 lore raw

PATCH v3 5/7 i2c: core: Do not dereference fwnode in struct device

In reply to Andy Shevchenko

In order to make the underneath API easier to change in the future, prevent users from dereferencing fwnode from struct device. Instead, use the specific device_set_node() API for that.

---
 drivers/i2c/i2c-core-base.c | 18 ++++++++++--------
 drivers/i2c/i2c-core-of.c   |  1 -
 2 files changed, 10 insertions(+), 9 deletions(-)
drivers/i2c/i2c-core-base.c +10 -8
index c92badadd47a..d9e2f9559ce4 100644
@@ -965,6 +965,7 @@ static void i2c_unlock_addr(struct i2c_adapter *adap, unsigned short addr,
 struct i2c_client *
 i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
 {
+	struct fwnode_handle *fwnode;
 	struct i2c_client *client;
 	bool need_put = false;
 	int status;
@@ -1005,18 +1006,19 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
 	client->dev.parent = &client->adapter->dev;
 	client->dev.bus = &i2c_bus_type;
 	client->dev.type = &i2c_client_type;
-	client->dev.of_node = of_node_get(info->of_node);
-	client->dev.fwnode = info->fwnode;
 
 	device_enable_async_suspend(&client->dev);
 
+	fwnode = info->fwnode ?: of_fwnode_handle(info->of_node);
+	device_set_node(&client->dev, fwnode_handle_get(fwnode));
+
 	if (info->swnode) {
 		status = device_add_software_node(&client->dev, info->swnode);
 		if (status) {
 			dev_err(&adap->dev,
 				"Failed to add software node to client %s: %d\n",
 				client->name, status);
-			goto out_err_put_of_node;
+			goto out_err_put_fwnode;
 		}
 	}
 
@@ -1035,8 +1037,8 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
 out_remove_swnode:
 	device_remove_software_node(&client->dev);
 	need_put = true;
-out_err_put_of_node:
-	of_node_put(info->of_node);
+out_err_put_fwnode:
+	fwnode_handle_put(fwnode);
 out_err:
 	dev_err(&adap->dev,
 		"Failed to register i2c client %s at 0x%02x (%d)\n",
@@ -1064,11 +1066,11 @@ void i2c_unregister_device(struct i2c_client *client)
 		return;
 
 	fwnode = dev_fwnode(&client->dev);
-	if (is_of_node(fwnode)) {
+	if (is_of_node(fwnode))
 		of_node_clear_flag(to_of_node(fwnode), OF_POPULATED);
-		of_node_put(client->dev.of_node);
-	} else if (is_acpi_device_node(fwnode))
+	else if (is_acpi_device_node(fwnode))
 		acpi_device_clear_enumerated(to_acpi_device_node(fwnode));
+	fwnode_handle_put(fwnode);
 
 	device_remove_software_node(&client->dev);
 	device_unregister(&client->dev);
drivers/i2c/i2c-core-of.c +0 -1
index 02feee6c9ba9..eb7fb202355f 100644
@@ -49,7 +49,6 @@ int of_i2c_get_board_info(struct device *dev, struct device_node *node,
 	}
 
 	info->addr = addr;
-	info->of_node = node;
 	info->fwnode = of_fwnode_handle(node);
 
 	if (of_property_read_bool(node, "host-notify"))
Signature · 1 line
2.47.2
#7 lore raw

PATCH v3 6/7 i2c: core: Deprecate of_node in struct i2c_board_info

In reply to Andy Shevchenko

Two members of the same or similar semantics is quite confusing to begin with. Moreover, the fwnode covers all possible firmware descriptions that Linux kernel supports. Deprecate of_node in struct i2c_board_info, so users will be warned and in the future remote it completely.

---
 include/linux/i2c.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/i2c.h +1 -1
index 2e4903b7f7bc..cc1437f29823 100644
@@ -405,7 +405,7 @@ static inline bool i2c_detect_slave_mode(struct device *dev) { return false; }
  * @addr: stored in i2c_client.addr
  * @dev_name: Overrides the default <busnr>-<addr> dev_name if set
  * @platform_data: stored in i2c_client.dev.platform_data
- * @of_node: pointer to OpenFirmware device node
+ * @of_node: **DEPRECATED** - use @fwnode for this
  * @fwnode: device node supplied by the platform firmware
  * @swnode: software node for the device
  * @resources: resources associated with the device
Signature · 1 line
2.47.2
#8 lore raw

Re: [PATCH v3 6/7] i2c: core: Deprecate of_node in struct i2c_board_info

Hi Andy,

Quoted 4 lines · Andy Shevchenko

Two members of the same or similar semantics is quite confusing to begin with. Moreover, the fwnode covers all possible firmware descriptions that Linux kernel supports. Deprecate of_node in struct i2c_board_info, so users will be warned and in the future remote it completely.

Too long lines, should be up to 75 characters long only.

Quoted 20 lines
---
 include/linux/i2c.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/i2c.h +1 -1
index 2e4903b7f7bc..cc1437f29823 100644
@@ -405,7 +405,7 @@ static inline bool i2c_detect_slave_mode(struct device *dev) { return false; }
  * @addr: stored in i2c_client.addr
  * @dev_name: Overrides the default <busnr>-<addr> dev_name if set
  * @platform_data: stored in i2c_client.dev.platform_data
- * @of_node: pointer to OpenFirmware device node
+ * @of_node: **DEPRECATED** - use @fwnode for this
  * @fwnode: device node supplied by the platform firmware
  * @swnode: software node for the device
  * @resources: resources associated with the device
Signature · 3 lines
Regards,

Sakari Ailus
#9 lore raw

Re: [PATCH v3 6/7] i2c: core: Deprecate of_node in struct i2c_board_info

Quoted 7 lines · Sakari Ailus
Quoted 4 lines · Andy Shevchenko

Two members of the same or similar semantics is quite confusing to begin with. Moreover, the fwnode covers all possible firmware descriptions that Linux kernel supports. Deprecate of_node in struct i2c_board_info, so users will be warned and in the future remote it completely.

Too long lines, should be up to 75 characters long only.

It's media CI complains, but this code is for I²C :-) But in _this_ case I agree with you. It's more for the external users of Git, rather than for us.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

PATCH v3 7/7 media: i2c: ds90ub960: Remove of_node assignment

In reply to Andy Shevchenko

Remove of_node assignment which duplicates fwnode in struct i2c_board_info. In general drivers must not set both, it's quite confusing. The I²C core will consider fwnode with a priority and of_node is subject to remove from above mentioned data structure.

---
 drivers/media/i2c/ds90ub960.c | 1 -
 1 file changed, 1 deletion(-)
drivers/media/i2c/ds90ub960.c +0 -1
index 5dde8452739b..5afdbbad9ff4 100644
@@ -1682,7 +1682,6 @@ static int ub960_rxport_add_serializer(struct ub960_data *priv, u8 nport)
 	struct device *dev = &priv->client->dev;
 	struct ds90ub9xx_platform_data *ser_pdata = &rxport->ser.pdata;
 	struct i2c_board_info ser_info = {
-		.of_node = to_of_node(rxport->ser.fwnode),
 		.fwnode = rxport->ser.fwnode,
 		.platform_data = ser_pdata,
 	};
Signature · 1 line
2.47.2
Reviewed-by on the v3 series
#11 lore raw
In reply to Andy Shevchenko

Hi Andy,

Quoted 8 lines · Andy Shevchenko

The struct i2c_board_info has of_node and fwnode members. This is quite confusing as they are of the same semantics and it's tend to have an issue if user assigns both. Luckily there is only a single driver that does this and fix is provided in the last patch. Nevertheless the series moves the client handling code to use fwnode and deprecates the of_node member in the respective documentation.

Tomi tested the last patch, but since it was separate there is no tag (yet).

Apart from the two minor commit message comments:

Signature · 1 line
Sakari Ailus
#12 lore raw
Quoted 8 lines · Sakari Ailus
Quoted 7 lines · Andy Shevchenko

The struct i2c_board_info has of_node and fwnode members. This is quite confusing as they are of the same semantics and it's tend to have an issue if user assigns both. Luckily there is only a single driver that does this and fix is provided in the last patch. Nevertheless the series moves the client handling code to use fwnode and deprecates the of_node member in the respective documentation.

Quoted 5 lines
Quoted 1 line

Tomi tested the last patch, but since it was separate there is no tag (yet).

Apart from the two minor commit message comments:

Thank you for your review!
Does it imply that media patch can go via I²C subsystem?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Acked-by
#13 lore raw
Quoted 9 lines · Andy Shevchenko
Quoted 8 lines · Sakari Ailus
Quoted 7 lines · Andy Shevchenko

The struct i2c_board_info has of_node and fwnode members. This is quite confusing as they are of the same semantics and it's tend to have an issue if user assigns both. Luckily there is only a single driver that does this and fix is provided in the last patch. Nevertheless the series moves the client handling code to use fwnode and deprecates the of_node member in the respective documentation.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Tomi tested the last patch, but since it was separate there is no tag (yet).

Apart from the two minor commit message comments:

Thank you for your review!
Does it imply that media patch can go via I²C subsystem?

Good point. Yes, and you can use this for the last patch:

I wonder if Tomi would still be able to test (or at least ack) it. I see he has tested the rest but this one is missing hist Tested-by:.

Signature · 1 line
Sakari Ailus
maintainer
#14 lore raw
Quoted 19 lines · Sakari Ailus
Quoted 9 lines · Andy Shevchenko
Quoted 8 lines · Sakari Ailus
Quoted 7 lines · Andy Shevchenko

The struct i2c_board_info has of_node and fwnode members. This is quite confusing as they are of the same semantics and it's tend to have an issue if user assigns both. Luckily there is only a single driver that does this and fix is provided in the last patch. Nevertheless the series moves the client handling code to use fwnode and deprecates the of_node member in the respective documentation.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Tomi tested the last patch, but since it was separate there is no tag (yet).

Apart from the two minor commit message comments:

Thank you for your review!
Does it imply that media patch can go via I²C subsystem?

Quoted 6 lines

Good point. Yes, and you can use this for the last patch:

I wonder if Tomi would still be able to test (or at least ack) it. I see he has tested the rest but this one is missing hist Tested-by:.

I think Andy just missed it, as it wasn't explicit. I did test the v2, with the 7th patch from this series on top (it was not included in v2). I haven't tested v3, but I don't think anything really has changed.

Tomi