diff --git a/cmd/i2c.c b/cmd/i2c.c index 56df8eb3bca589e28c5bf23976a41a5652f342f7..738d68603e0a1c24f5cd64fdc75a121ef2d4d005 100644 --- a/cmd/i2c.c +++ b/cmd/i2c.c @@ -1823,11 +1823,9 @@ static int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc, printf("Current bus is %d\n", bus_no); } else { bus_no = simple_strtoul(argv[1], NULL, 10); -#if defined(CONFIG_SYS_I2C) - if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) { - printf("Invalid bus %d\n", bus_no); +#ifndef CONFIG_DM_I2C + if (!i2c_get_adapter(bus_no)) return -1; - } #endif printf("Setting bus to %d\n", bus_no); #ifdef CONFIG_DM_I2C diff --git a/drivers/i2c/i2c_core.c b/drivers/i2c/i2c_core.c index 234277a299c76fe648fecd3f472a003b8b24bab8..cf0d76718597b929ceb81f97d05697d1c4c62d32 100644 --- a/drivers/i2c/i2c_core.c +++ b/drivers/i2c/i2c_core.c @@ -10,25 +10,42 @@ #include <common.h> #include <i2c.h> -struct i2c_adapter *i2c_get_adapter(int index) +struct i2c_adapter *i2c_get_adapter(int bus) { - struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter, - i2c); - int max = ll_entry_count(struct i2c_adapter, i2c); - int i; + struct i2c_adapter *i2cp = ll_entry_start(struct i2c_adapter, i2c); - if (index >= max) { - printf("Error, wrong i2c adapter %d max %d possible\n", - index, max); - return i2c_adap_p; + bus = I2C_ADAPTER(bus); +#ifndef CONFIG_SYS_I2C_DIRECT_BUS + if (bus >= CONFIG_SYS_NUM_I2C_BUSES) { + printf("Invalid bus %d, max is %d\n", bus, + CONFIG_SYS_NUM_I2C_BUSES - 1); + return NULL; } - if (index == 0) - return i2c_adap_p; +#endif - for (i = 0; i < index; i++) - i2c_adap_p++; +#ifdef CONFIG_SYS_I2C_MASK + if (!(BIT(bus) & CONFIG_SYS_I2C_MASK)) { + printf("Invalid bus %d\n", bus); + return NULL; + } +#else + { + int max = ll_entry_count(struct i2c_adapter, i2c); + if (bus >= max) { + printf("Error, wrong i2c adapter %d max %d possible\n", + bus, max); + return NULL; + } + } +#endif + return i2cp + bus; +} - return i2c_adap_p; +struct i2c_adapter *i2c_get_adapter_valid(int bus) +{ + struct i2c_adapter *i2cp = i2c_get_adapter(bus); + + return i2cp ? i2cp : ll_entry_start(struct i2c_adapter, i2c); } #if !defined(CONFIG_SYS_I2C_DIRECT_BUS) @@ -235,22 +252,11 @@ unsigned int i2c_get_bus_num(void) */ int i2c_set_bus_num(unsigned int bus) { - int max; - if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0)) return 0; -#ifndef CONFIG_SYS_I2C_DIRECT_BUS - if (bus >= CONFIG_SYS_NUM_I2C_BUSES) + if (!i2c_get_adapter(bus)) return -1; -#endif - - max = ll_entry_count(struct i2c_adapter, i2c); - if (I2C_ADAPTER(bus) >= max) { - printf("Error, wrong i2c adapter %d max %d possible\n", - I2C_ADAPTER(bus), max); - return -2; - } #ifndef CONFIG_SYS_I2C_DIRECT_BUS i2c_mux_disconnect_all(); diff --git a/include/i2c.h b/include/i2c.h index d33f827500b17dbbf7f9e722b760b3b3fc27c4aa..622dd316c68d06658f15a205b7a4acd846798305 100644 --- a/include/i2c.h +++ b/include/i2c.h @@ -615,6 +615,7 @@ struct i2c_adapter { _set_speed, _speed, _slaveaddr, _hwadapnr, _name); struct i2c_adapter *i2c_get_adapter(int index); +struct i2c_adapter *i2c_get_adapter_valid(int index); #ifndef CONFIG_SYS_I2C_DIRECT_BUS struct i2c_mux { @@ -641,7 +642,7 @@ extern struct i2c_bus_hose i2c_bus[]; #endif #define I2C_BUS gd->cur_i2c_bus -#define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus)) +#define I2C_ADAP_NR(bus) i2c_get_adapter_valid(bus) #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus) #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)