diff mbox series

i2c-dev: Introduce "linux,i2c-dev-name" property for device tree of I2C controller.

Message ID 20240519165504.19627-1-egyszeregy@freemail.hu
State New
Headers show
Series i2c-dev: Introduce "linux,i2c-dev-name" property for device tree of I2C controller. | expand

Commit Message

Szőke Benjamin May 19, 2024, 4:55 p.m. UTC
From: Benjamin Szőke <egyszeregy@freemail.hu>

Optionally, an I2C controller may have a "linux,i2c-dev-name" property.
This is a string which is defining a custom suffix name for I2C device
in /dev/i2c-<name> format. It helps to improve software portability between
various SoCs and reduce complexities of hardware related codes in SWs.

Signed-off-by: Benjamin Szőke <egyszeregy@freemail.hu>
---
 drivers/i2c/i2c-dev.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

Comments

Wolfram Sang May 29, 2024, 10:28 a.m. UTC | #1
> Optionally, an I2C controller may have a "linux,i2c-dev-name" property.
> This is a string which is defining a custom suffix name for I2C device
> in /dev/i2c-<name> format. It helps to improve software portability between
> various SoCs and reduce complexities of hardware related codes in SWs.

(I thought I already replied to this?)

Highly similar to [1] from 2021. I don't have a super clear opinion
about this, so I'd need help from the DT maintainers. But the discussion
from back then stalled.

[1] http://patchwork.ozlabs.org/project/linux-i2c/list/?series=237908
Conor Dooley May 29, 2024, 4:10 p.m. UTC | #2
On Wed, May 29, 2024 at 12:28:03PM +0200, Wolfram Sang wrote:
> 
> > Optionally, an I2C controller may have a "linux,i2c-dev-name" property.
> > This is a string which is defining a custom suffix name for I2C device
> > in /dev/i2c-<name> format. It helps to improve software portability between
> > various SoCs and reduce complexities of hardware related codes in SWs.
> 
> (I thought I already replied to this?)
> 
> Highly similar to [1] from 2021. I don't have a super clear opinion
> about this, so I'd need help from the DT maintainers. But the discussion
> from back then stalled.
> 
> [1] http://patchwork.ozlabs.org/project/linux-i2c/list/?series=237908

This patch (or one very similar) got sent to the SPI and GPIO subsystems
too. The response was effectively "use udev":
https://lore.kernel.org/all/20240519144920.14804-1-egyszeregy@freemail.hu/
https://lore.kernel.org/all/20240519211346.30323-1-egyszeregy@freemail.hu/

This definitely is in "devicetree properties are not for software-policy"
territory to me, maybe Rob's changed his mind since 2021.
diff mbox series

Patch

diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
index 8b7e599f1674..df4cec88ea59 100644
--- a/drivers/i2c/i2c-dev.c
+++ b/drivers/i2c/i2c-dev.c
@@ -651,6 +651,7 @@  static void i2cdev_dev_release(struct device *dev)
 
 static int i2cdev_attach_adapter(struct device *dev)
 {
+	const char *name;
 	struct i2c_adapter *adap;
 	struct i2c_dev *i2c_dev;
 	int res;
@@ -672,7 +673,16 @@  static int i2cdev_attach_adapter(struct device *dev)
 	i2c_dev->dev.parent = &adap->dev;
 	i2c_dev->dev.release = i2cdev_dev_release;
 
-	res = dev_set_name(&i2c_dev->dev, "i2c-%d", adap->nr);
+	/*
+	 * If "linux,i2c-dev-name" is specified in device tree, use /dev/i2c-<name>
+	 * in Linux userspace, otherwise use /dev/i2c-<nr>.
+	 */
+	res = device_property_read_string(&adap->dev, "linux,i2c-dev-name", &name);
+	if (res < 0)
+		res = dev_set_name(&i2c_dev->dev, "i2c-%d", adap->nr);
+	else
+		res = dev_set_name(&i2c_dev->dev, "i2c-%s", name);
+
 	if (res)
 		goto err_put_i2c_dev;