mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/3] I2C: assign bus numbers from device tree
@ 2024-10-25  7:30 Sascha Hauer
  2024-10-25  7:30 ` [PATCH 1/3] i2c: set adapter.nr explicitly to -1 Sascha Hauer
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Sascha Hauer @ 2024-10-25  7:30 UTC (permalink / raw)
  To: open list:BAREBOX

Like done with SPI controllers already let's assign the I2C bus numbers
from device tree as well. This avoids confusing bus numbers when the
probe order doesn't match the intended bus number ordering or when
other I2C busses with dynamically assigned numbers are probed in
between.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
Sascha Hauer (3):
      i2c: set adapter.nr explicitly to -1
      i2c: get adapter.nr from device tree
      i2c: start dynamic I2C bus numbers at 32

 drivers/i2c/busses/i2c-at91.c       | 2 +-
 drivers/i2c/busses/i2c-bcm283x.c    | 2 +-
 drivers/i2c/busses/i2c-cadence.c    | 2 +-
 drivers/i2c/busses/i2c-designware.c | 2 +-
 drivers/i2c/busses/i2c-imx.c        | 2 +-
 drivers/i2c/busses/i2c-mv64xxx.c    | 2 +-
 drivers/i2c/busses/i2c-rockchip.c   | 2 +-
 drivers/i2c/busses/i2c-stm32.c      | 2 +-
 drivers/i2c/busses/i2c-tegra.c      | 2 +-
 drivers/i2c/i2c.c                   | 5 ++++-
 10 files changed, 13 insertions(+), 10 deletions(-)
---
base-commit: e55e492573e33823f25935ee00fe7fa7bf2c5c90
change-id: 20241025-i2c-busnum-bd3bdee6c0c3

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/3] i2c: set adapter.nr explicitly to -1
  2024-10-25  7:30 [PATCH 0/3] I2C: assign bus numbers from device tree Sascha Hauer
@ 2024-10-25  7:30 ` Sascha Hauer
  2024-10-25 13:43   ` Alexander Shiyan
  2024-10-25  7:30 ` [PATCH 2/3] i2c: get adapter.nr from device tree Sascha Hauer
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Sascha Hauer @ 2024-10-25  7:30 UTC (permalink / raw)
  To: open list:BAREBOX

dev->id will always be -1 for device tree instantiated devices. Instead
of setting adapter.nr to dev->id, set it to -1 explicitly for all I2C
bus drivers that are purely instantiated from device tree.

This is done for clarity just to show that all I2C bus numbers are
really dynamically assigned.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/i2c/busses/i2c-at91.c       | 2 +-
 drivers/i2c/busses/i2c-bcm283x.c    | 2 +-
 drivers/i2c/busses/i2c-cadence.c    | 2 +-
 drivers/i2c/busses/i2c-designware.c | 2 +-
 drivers/i2c/busses/i2c-imx.c        | 2 +-
 drivers/i2c/busses/i2c-mv64xxx.c    | 2 +-
 drivers/i2c/busses/i2c-rockchip.c   | 2 +-
 drivers/i2c/busses/i2c-stm32.c      | 2 +-
 drivers/i2c/busses/i2c-tegra.c      | 2 +-
 9 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/i2c/busses/i2c-at91.c b/drivers/i2c/busses/i2c-at91.c
index 5b1f456187..dbc5aa4ddf 100644
--- a/drivers/i2c/busses/i2c-at91.c
+++ b/drivers/i2c/busses/i2c-at91.c
@@ -558,7 +558,7 @@ static int at91_twi_probe(struct device *dev)
 
 	i2c_at91->adapter.master_xfer = at91_twi_xfer;
 	i2c_at91->adapter.dev.parent = dev;
-	i2c_at91->adapter.nr = dev->id;
+	i2c_at91->adapter.nr = -1;
 	i2c_at91->adapter.dev.of_node = dev->of_node;
 
 	rc = i2c_add_numbered_adapter(&i2c_at91->adapter);
diff --git a/drivers/i2c/busses/i2c-bcm283x.c b/drivers/i2c/busses/i2c-bcm283x.c
index b40918932f..fb69515825 100644
--- a/drivers/i2c/busses/i2c-bcm283x.c
+++ b/drivers/i2c/busses/i2c-bcm283x.c
@@ -306,7 +306,7 @@ static int bcm283x_i2c_probe(struct device *dev)
 	}
 
 	bcm_i2c->adapter.master_xfer = bcm283x_i2c_xfer;
-	bcm_i2c->adapter.nr = dev->id;
+	bcm_i2c->adapter.nr = -1;
 	bcm_i2c->adapter.dev.parent = dev;
 	bcm_i2c->adapter.dev.of_node = np;
 
diff --git a/drivers/i2c/busses/i2c-cadence.c b/drivers/i2c/busses/i2c-cadence.c
index bf9ec30994..8551cd05da 100644
--- a/drivers/i2c/busses/i2c-cadence.c
+++ b/drivers/i2c/busses/i2c-cadence.c
@@ -422,7 +422,7 @@ static int cdns_i2c_probe(struct device *dev)
 		return err;
 
 	i2c->adapter.master_xfer = cdns_i2c_xfer;
-	i2c->adapter.nr = dev->id;
+	i2c->adapter.nr = -1;
 	i2c->adapter.dev.parent = dev;
 	i2c->adapter.dev.of_node = np;
 
diff --git a/drivers/i2c/busses/i2c-designware.c b/drivers/i2c/busses/i2c-designware.c
index 152b795c37..3ba10e172a 100644
--- a/drivers/i2c/busses/i2c-designware.c
+++ b/drivers/i2c/busses/i2c-designware.c
@@ -525,7 +525,7 @@ static int i2c_dw_probe(struct device *pdev)
 	}
 
 	dw->adapter.master_xfer = i2c_dw_xfer;
-	dw->adapter.nr = pdev->id;
+	dw->adapter.nr = -1;
 	dw->adapter.dev.parent = pdev;
 	dw->adapter.dev.of_node = pdev->of_node;
 
diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index 981db015ea..eabb8252ae 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -592,7 +592,7 @@ static int __init i2c_fsl_probe(struct device *pdev)
 
 	/* Setup i2c_fsl driver structure */
 	i2c_fsl->adapter.master_xfer = i2c_fsl_xfer;
-	i2c_fsl->adapter.nr = pdev->id;
+	i2c_fsl->adapter.nr = -1;
 	i2c_fsl->adapter.dev.parent = pdev;
 	i2c_fsl->adapter.dev.of_node = pdev->of_node;
 	iores = dev_request_mem_resource(pdev, 0);
diff --git a/drivers/i2c/busses/i2c-mv64xxx.c b/drivers/i2c/busses/i2c-mv64xxx.c
index 44f1fd4ce6..6629e65b48 100644
--- a/drivers/i2c/busses/i2c-mv64xxx.c
+++ b/drivers/i2c/busses/i2c-mv64xxx.c
@@ -625,7 +625,7 @@ mv64xxx_i2c_probe(struct device *pd)
 
 	drv_data->adapter.master_xfer = mv64xxx_i2c_xfer;
 	drv_data->adapter.dev.parent = pd;
-	drv_data->adapter.nr = pd->id;
+	drv_data->adapter.nr = -1;
 	drv_data->adapter.dev.of_node = pd->of_node;
 
 	mv64xxx_i2c_hw_init(drv_data);
diff --git a/drivers/i2c/busses/i2c-rockchip.c b/drivers/i2c/busses/i2c-rockchip.c
index ce029d148f..a7bd66781f 100644
--- a/drivers/i2c/busses/i2c-rockchip.c
+++ b/drivers/i2c/busses/i2c-rockchip.c
@@ -432,7 +432,7 @@ static int rk_i2c_probe(struct device *dev)
 		return dev_err_probe(dev, PTR_ERR(i2c->clk), "Can't get bus clk\n");
 
 	i2c->adapter.master_xfer = rockchip_i2c_xfer;
-	i2c->adapter.nr = dev->id;
+	i2c->adapter.nr = -1;
 	i2c->adapter.dev.parent = dev;
 	i2c->adapter.dev.of_node = np;
 
diff --git a/drivers/i2c/busses/i2c-stm32.c b/drivers/i2c/busses/i2c-stm32.c
index 1be52b3dd9..4d40ca3b51 100644
--- a/drivers/i2c/busses/i2c-stm32.c
+++ b/drivers/i2c/busses/i2c-stm32.c
@@ -941,7 +941,7 @@ static int __init stm32_i2c_probe(struct device *dev)
 
 	/* Setup stm32_i2c driver structure */
 	stm32_i2c->adapter.master_xfer = stm32_i2c_xfer;
-	stm32_i2c->adapter.nr = dev->id;
+	stm32_i2c->adapter.nr = -1;
 	stm32_i2c->adapter.dev.parent = dev;
 	stm32_i2c->adapter.dev.of_node = dev->of_node;
 
diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
index f86f64f573..4271a22f83 100644
--- a/drivers/i2c/busses/i2c-tegra.c
+++ b/drivers/i2c/busses/i2c-tegra.c
@@ -654,7 +654,7 @@ static int tegra_i2c_probe(struct device *dev)
 
 	i2c_dev->adapter.master_xfer = tegra_i2c_xfer;
 	i2c_dev->adapter.dev.parent = dev;
-	i2c_dev->adapter.nr = dev->id;
+	i2c_dev->adapter.nr = -1;
 	i2c_dev->adapter.dev.of_node = dev->of_node;
 
 	ret = i2c_add_numbered_adapter(&i2c_dev->adapter);

-- 
2.39.5




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/3] i2c: get adapter.nr from device tree
  2024-10-25  7:30 [PATCH 0/3] I2C: assign bus numbers from device tree Sascha Hauer
  2024-10-25  7:30 ` [PATCH 1/3] i2c: set adapter.nr explicitly to -1 Sascha Hauer
@ 2024-10-25  7:30 ` Sascha Hauer
  2024-10-25  7:30 ` [PATCH 3/3] i2c: start dynamic I2C bus numbers at 32 Sascha Hauer
  2024-10-28 12:11 ` [PATCH 0/3] I2C: assign bus numbers from device tree Sascha Hauer
  3 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2024-10-25  7:30 UTC (permalink / raw)
  To: open list:BAREBOX

In case we are instantiating a I2C bus driver from device tree let's
try to get the bus number from the device tree instead of dynamically
assigning a free bus number.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/i2c/i2c.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c
index 5471519045..4eed7fb5c6 100644
--- a/drivers/i2c/i2c.c
+++ b/drivers/i2c/i2c.c
@@ -705,6 +705,9 @@ int i2c_add_numbered_adapter(struct i2c_adapter *adapter)
 	struct device *hw_dev;
 	int ret;
 
+	if (adapter->nr < 0 && adapter->dev.parent && adapter->dev.parent->of_node)
+		adapter->nr = of_alias_get_id(adapter->dev.parent->of_node, "i2c");
+
 	if (adapter->nr < 0) {
 		int nr;
 

-- 
2.39.5




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 3/3] i2c: start dynamic I2C bus numbers at 32
  2024-10-25  7:30 [PATCH 0/3] I2C: assign bus numbers from device tree Sascha Hauer
  2024-10-25  7:30 ` [PATCH 1/3] i2c: set adapter.nr explicitly to -1 Sascha Hauer
  2024-10-25  7:30 ` [PATCH 2/3] i2c: get adapter.nr from device tree Sascha Hauer
@ 2024-10-25  7:30 ` Sascha Hauer
  2024-10-28 12:11 ` [PATCH 0/3] I2C: assign bus numbers from device tree Sascha Hauer
  3 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2024-10-25  7:30 UTC (permalink / raw)
  To: open list:BAREBOX

To avoid clashes with bus numbers derived from device tree let's start
the dynamically assigned bus numbers at 32.

Usually the SoC internal I2C controllers with fixed bus numbers are
registered before any other controllers, but with deep probe support
this is no longer always true. In my case I saw a HDMI internal I2C
controller registered before some SoC controller messing up the
numbering.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/i2c/i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c
index 4eed7fb5c6..d19506af26 100644
--- a/drivers/i2c/i2c.c
+++ b/drivers/i2c/i2c.c
@@ -711,7 +711,7 @@ int i2c_add_numbered_adapter(struct i2c_adapter *adapter)
 	if (adapter->nr < 0) {
 		int nr;
 
-		for (nr = 0;; nr++)
+		for (nr = 32;; nr++)
 			if (!i2c_get_adapter(nr))
 				break;
 		adapter->nr = nr;

-- 
2.39.5




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/3] i2c: set adapter.nr explicitly to -1
  2024-10-25  7:30 ` [PATCH 1/3] i2c: set adapter.nr explicitly to -1 Sascha Hauer
@ 2024-10-25 13:43   ` Alexander Shiyan
  2024-10-28 12:11     ` Sascha Hauer
  0 siblings, 1 reply; 7+ messages in thread
From: Alexander Shiyan @ 2024-10-25 13:43 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: open list:BAREBOX

Hello.

Last time I didn't do it completely :)
https://lore.barebox.org/barebox/20230714051510.2304-4-eagle.alexander923@gmail.com/

Thanks!

...

> dev->id will always be -1 for device tree instantiated devices. Instead
> of setting adapter.nr to dev->id, set it to -1 explicitly for all I2C
> bus drivers that are purely instantiated from device tree.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/3] i2c: set adapter.nr explicitly to -1
  2024-10-25 13:43   ` Alexander Shiyan
@ 2024-10-28 12:11     ` Sascha Hauer
  0 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2024-10-28 12:11 UTC (permalink / raw)
  To: Alexander Shiyan; +Cc: open list:BAREBOX

On Fri, Oct 25, 2024 at 04:43:35PM +0300, Alexander Shiyan wrote:
> Hello.
> 
> Last time I didn't do it completely :)
> https://lore.barebox.org/barebox/20230714051510.2304-4-eagle.alexander923@gmail.com/

Oh, I wasn't aware of this anymore. I am feeling lucky, none of the
comments I made back then seems to apply to my own patchset ;)

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/3] I2C: assign bus numbers from device tree
  2024-10-25  7:30 [PATCH 0/3] I2C: assign bus numbers from device tree Sascha Hauer
                   ` (2 preceding siblings ...)
  2024-10-25  7:30 ` [PATCH 3/3] i2c: start dynamic I2C bus numbers at 32 Sascha Hauer
@ 2024-10-28 12:11 ` Sascha Hauer
  3 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2024-10-28 12:11 UTC (permalink / raw)
  To: open list:BAREBOX, Sascha Hauer


On Fri, 25 Oct 2024 09:30:23 +0200, Sascha Hauer wrote:
> Like done with SPI controllers already let's assign the I2C bus numbers
> from device tree as well. This avoids confusing bus numbers when the
> probe order doesn't match the intended bus number ordering or when
> other I2C busses with dynamically assigned numbers are probed in
> between.
> 
> 
> [...]

Applied, thanks!

[1/3] i2c: set adapter.nr explicitly to -1
      https://git.pengutronix.de/cgit/barebox/commit/?id=5b167ad520c4 (link may not be stable)
[2/3] i2c: get adapter.nr from device tree
      https://git.pengutronix.de/cgit/barebox/commit/?id=b4746725454c (link may not be stable)
[3/3] i2c: start dynamic I2C bus numbers at 32
      https://git.pengutronix.de/cgit/barebox/commit/?id=79357f8b965a (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-10-28 12:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-25  7:30 [PATCH 0/3] I2C: assign bus numbers from device tree Sascha Hauer
2024-10-25  7:30 ` [PATCH 1/3] i2c: set adapter.nr explicitly to -1 Sascha Hauer
2024-10-25 13:43   ` Alexander Shiyan
2024-10-28 12:11     ` Sascha Hauer
2024-10-25  7:30 ` [PATCH 2/3] i2c: get adapter.nr from device tree Sascha Hauer
2024-10-25  7:30 ` [PATCH 3/3] i2c: start dynamic I2C bus numbers at 32 Sascha Hauer
2024-10-28 12:11 ` [PATCH 0/3] I2C: assign bus numbers from device tree Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox