Skip to content

Commit 05f8dd9

Browse files
Johan Hedbergnashif
authored andcommitted
drivers: i2c_dw: Remove hard-coded instance count assumption
Introduce a Kconfig variable that the SoC can set to indicate the number of instances in Device Tree. This also fixes the accuracy of the Elkhart Lake instance count where the code was previously assuming up to 12 instances even though DT lists 15 nodes. Signed-off-by: Johan Hedberg <[email protected]>
1 parent 704ddaa commit 05f8dd9

File tree

6 files changed

+35
-48
lines changed

6 files changed

+35
-48
lines changed

drivers/i2c/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ zephyr_library_sources_ifdef(CONFIG_I2C_STM32_V2
4343

4444
if(CONFIG_I2C_DW)
4545
zephyr_library_sources(i2c_dw.c)
46-
foreach(NUM RANGE 0 11)
46+
math(EXPR max_index "${CONFIG_I2C_DW_MAX_INSTANCES} - 1")
47+
foreach(NUM RANGE 0 ${max_index})
48+
math(EXPR NEXT_NUM "${NUM} + 1")
4749
configure_file(
4850
i2c_dw_port_x.h
4951
${PROJECT_BINARY_DIR}/include/generated/i2c_dw_port_${NUM}.h

drivers/i2c/Kconfig.dw

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ menuconfig I2C_DW
1212
help
1313
Enable the Design Ware I2C driver
1414

15+
if I2C_DW
16+
1517
config I2C_DW_CLOCK_SPEED
1618
int "Set the clock speed for I2C"
1719
default 32
18-
depends on I2C_DW
20+
21+
config I2C_DW_MAX_INSTANCES
22+
int "Maximum number of supported driver instances"
23+
range 1 32
24+
default 12
25+
help
26+
The maximum number of supported driver instances in device tree.
27+
28+
endif # I2C_DW

drivers/i2c/i2c_dw.c

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -665,50 +665,8 @@ static int i2c_dw_initialize(const struct device *dev)
665665
return 0;
666666
}
667667

668-
#if DT_NODE_HAS_STATUS(DT_DRV_INST(0), okay)
668+
/* The instance-specific header files are chained together (each instance
669+
* includes the next one, unless it's the last instance) so we only need to
670+
* include the first instance.
671+
*/
669672
#include <i2c_dw_port_0.h>
670-
#endif
671-
672-
#if DT_NODE_HAS_STATUS(DT_DRV_INST(1), okay)
673-
#include <i2c_dw_port_1.h>
674-
#endif
675-
676-
#if DT_NODE_HAS_STATUS(DT_DRV_INST(2), okay)
677-
#include <i2c_dw_port_2.h>
678-
#endif
679-
680-
#if DT_NODE_HAS_STATUS(DT_DRV_INST(3), okay)
681-
#include <i2c_dw_port_3.h>
682-
#endif
683-
684-
#if DT_NODE_HAS_STATUS(DT_DRV_INST(4), okay)
685-
#include <i2c_dw_port_4.h>
686-
#endif
687-
688-
#if DT_NODE_HAS_STATUS(DT_DRV_INST(5), okay)
689-
#include <i2c_dw_port_5.h>
690-
#endif
691-
692-
#if DT_NODE_HAS_STATUS(DT_DRV_INST(6), okay)
693-
#include <i2c_dw_port_6.h>
694-
#endif
695-
696-
#if DT_NODE_HAS_STATUS(DT_DRV_INST(7), okay)
697-
#include <i2c_dw_port_7.h>
698-
#endif
699-
700-
#if DT_NODE_HAS_STATUS(DT_DRV_INST(8), okay)
701-
#include <i2c_dw_port_8.h>
702-
#endif
703-
704-
#if DT_NODE_HAS_STATUS(DT_DRV_INST(9), okay)
705-
#include <i2c_dw_port_9.h>
706-
#endif
707-
708-
#if DT_NODE_HAS_STATUS(DT_DRV_INST(10), okay)
709-
#include <i2c_dw_port_10.h>
710-
#endif
711-
712-
#if DT_NODE_HAS_STATUS(DT_DRV_INST(11), okay)
713-
#include <i2c_dw_port_11.h>
714-
#endif

drivers/i2c/i2c_dw_port_x.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* This file is a template for cmake and is not meant to be used directly!
77
*/
88

9+
#if DT_NODE_HAS_STATUS(DT_DRV_INST(@NUM@), okay)
10+
911
static void i2c_config_@NUM@(const struct device *port);
1012

1113
static const struct i2c_dw_rom_config i2c_config_dw_@NUM@ = {
@@ -82,3 +84,10 @@ static void i2c_config_@NUM@(const struct device *port)
8284

8385
#endif
8486
}
87+
88+
#endif /* DT_NODE_HAS_STATUS(DT_DRV_INST(@NUM@), okay) */
89+
90+
/* Include subsequent instances */
91+
#if @NEXT_NUM@ < CONFIG_I2C_DW_MAX_INSTANCES
92+
#include <i2c_dw_port_@[email protected]>
93+
#endif

soc/x86/apollo_lake/Kconfig.defconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ config I2C_DW
5252
default y
5353
depends on I2C
5454

55+
config I2C_DW_MAX_INSTANCES
56+
default 8
57+
depends on I2C_DW
58+
5559
config GPIO_INTEL_APL
5660
default y
5761
depends on GPIO

soc/x86/elkhart_lake/Kconfig.defconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ config I2C_DW
5252
default y
5353
depends on I2C
5454

55+
config I2C_DW_MAX_INSTANCES
56+
default 15
57+
depends on I2C_DW
58+
5559
config UART_NS16550_MAX_INSTANCES
5660
default 11
5761
depends on UART_NS16550

0 commit comments

Comments
 (0)