From b9ff20355040d4bece8357a0af31bc5fca191cee Mon Sep 17 00:00:00 2001
From: Peng Fan <peng.fan@nxp.com>
Date: Wed, 27 Feb 2019 09:35:35 +0800
Subject: [PATCH] MLK-20993 i2c: rpmsg: add a BUG_ON to catch alias id issue

The alias ID must be defined in device tree, because
that will be used as BUS ID to Cortex M4. If the alias ID
not defined, linux kernel will automatically allocate one
ID which might not be the same number used in Cortex M4 and
Cortex M4 will not send msg to I2C controller.

So let's add BUG_ON to catch issue as earlier as possible to avoid
wasting efforts.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Reviewed-by: Clark Wang <xiaoning.wang@nxp.com>
---
 drivers/i2c/busses/i2c-rpmsg-imx.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/drivers/i2c/busses/i2c-rpmsg-imx.c b/drivers/i2c/busses/i2c-rpmsg-imx.c
index 2c13056167f118..37f39609da2ea5 100644
--- a/drivers/i2c/busses/i2c-rpmsg-imx.c
+++ b/drivers/i2c/busses/i2c-rpmsg-imx.c
@@ -369,6 +369,16 @@ static int i2c_rpbus_probe(struct platform_device *pdev)
 	adapter->dev.parent = dev;
 	adapter->dev.of_node = np;
 	adapter->nr = of_alias_get_id(np, "i2c");
+	/*
+	 * The driver will send the adapter->nr as BUS ID to the other
+	 * side, and the other side will check the BUS ID to see whether
+	 * the BUS has been registered. If there is alias id for this
+	 * virtual adapter, linux kernel will automatically allocate one
+	 * id which might be not the same number used in the other side,
+	 * cause i2c slave probe failure under this virtual I2C bus.
+	 * So let's add a BUG_ON to catch this issue earlier.
+	 */
+	BUG_ON(adapter->nr < 0);
 	adapter->quirks = &i2c_rpbus_quirks;
 	snprintf(rdata->adapter.name, sizeof(rdata->adapter.name), "%s",
 							"i2c-rpmsg-adapter");
-- 
GitLab