[PATCH] gpio: pca953x: add bus option to select the i2c bus.

Joseph Liu maxdog988 at gmail.com
Tue Dec 8 08:32:26 CET 2020


From: Joseph Liu <kwliu at nuvoton.com>

If DM_I2C is used, the command "i2c dev" will not reassign
gd->cur_i2c_bus.

Add this bus option to change the active I2C bus.

Signed-off-by: Joseph Liu <kwliu at nuvoton.com>
---
 drivers/gpio/pca953x.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/gpio/pca953x.c b/drivers/gpio/pca953x.c
index 4ab8cee2d1..b64f6ddcea 100644
--- a/drivers/gpio/pca953x.c
+++ b/drivers/gpio/pca953x.c
@@ -20,6 +20,7 @@
 
 enum {
 	PCA953X_CMD_INFO,
+	PCA953X_CMD_BUS,
 	PCA953X_CMD_DEVICE,
 	PCA953X_CMD_OUTPUT,
 	PCA953X_CMD_INPUT,
@@ -194,6 +195,7 @@ static int pca953x_info(uint8_t chip)
 }
 
 static struct cmd_tbl cmd_pca953x[] = {
+	U_BOOT_CMD_MKENT(bus, 3, 0, (void *)PCA953X_CMD_BUS, "", ""),
 	U_BOOT_CMD_MKENT(device, 3, 0, (void *)PCA953X_CMD_DEVICE, "", ""),
 	U_BOOT_CMD_MKENT(output, 4, 0, (void *)PCA953X_CMD_OUTPUT, "", ""),
 	U_BOOT_CMD_MKENT(input, 3, 0, (void *)PCA953X_CMD_INPUT, "", ""),
@@ -205,6 +207,7 @@ static int do_pca953x(struct cmd_tbl *cmdtp, int flag, int argc,
 		      char *const argv[])
 {
 	static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR;
+	uint8_t bus = 0;
 	int ret = CMD_RET_USAGE, val;
 	ulong ul_arg2 = 0;
 	ulong ul_arg3 = 0;
@@ -214,7 +217,7 @@ static int do_pca953x(struct cmd_tbl *cmdtp, int flag, int argc,
 
 	/* All commands but "device" require 'maxargs' arguments */
 	if (!c || !((argc == (c->maxargs)) ||
-		(((long)c->cmd == PCA953X_CMD_DEVICE) &&
+		((((long)c->cmd == PCA953X_CMD_DEVICE) || ((long)c->cmd == PCA953X_CMD_BUS)) &&
 		 (argc == (c->maxargs - 1))))) {
 		return CMD_RET_USAGE;
 	}
@@ -233,7 +236,14 @@ static int do_pca953x(struct cmd_tbl *cmdtp, int flag, int argc,
 		if (ret)
 			ret = CMD_RET_FAILURE;
 		break;
+	case PCA953X_CMD_BUS:
+		if (argc == 3)
+			i2c_set_bus_num((uint8_t)ul_arg2);
 
+		bus = i2c_get_bus_num();
+		printf("Current i2c bus: 0x%x\n", bus);
+		ret = CMD_RET_SUCCESS;
+		break;
 	case PCA953X_CMD_DEVICE:
 		if (argc == 3)
 			chip = (uint8_t)ul_arg2;
@@ -280,6 +290,8 @@ static int do_pca953x(struct cmd_tbl *cmdtp, int flag, int argc,
 U_BOOT_CMD(
 	pca953x,	5,	1,	do_pca953x,
 	"pca953x gpio access",
+	"bus [num]\n"
+	"	- show or set current i2c bus\n"
 	"device [dev]\n"
 	"	- show or set current device address\n"
 	"pca953x info\n"
-- 
2.17.1



More information about the U-Boot mailing list