Skip to content
Permalink
Browse files

drivers/i2c/i2c_dw.c: templatize device configuration boilerplate

The 50-odd lines of boilerplate per I2C port is moved into a template
which is generated by CMake as needed at build-time.

Signed-off-by: Charles E. Youse <charles.youse@intel.com>
  • Loading branch information...
Charles E. Youse authored and nashif committed May 16, 2019
1 parent a8dcb8b commit 59ebe6a0726d59472c63f20e054db5d34b376cab
Showing with 80 additions and 317 deletions.
  1. +13 −1 drivers/i2c/CMakeLists.txt
  2. +8 −316 drivers/i2c/i2c_dw.c
  3. +59 −0 drivers/i2c/i2c_dw_port_x.h
@@ -4,7 +4,6 @@ zephyr_library()

zephyr_library_sources_ifdef(CONFIG_I2C_BITBANG i2c_bitbang.c)
zephyr_library_sources_ifdef(CONFIG_I2C_CC32XX i2c_cc32xx.c)
zephyr_library_sources_ifdef(CONFIG_I2C_DW i2c_dw.c)
zephyr_library_sources_ifdef(CONFIG_I2C_ESP32 i2c_esp32.c)
zephyr_library_sources_ifdef(CONFIG_I2C_GPIO i2c_gpio.c)
zephyr_library_sources_ifdef(CONFIG_I2C_IMX i2c_imx.c)
@@ -33,6 +32,19 @@ zephyr_library_sources_ifdef(CONFIG_I2C_STM32_V2
i2c_ll_stm32.c
)

if(CONFIG_I2C_DW)
zephyr_library_sources(i2c_dw.c)
foreach(NUM RANGE 0 7)
if(CONFIG_I2C_${NUM})
configure_file(
i2c_dw_port_x.h
${PROJECT_BINARY_DIR}/include/generated/i2c_dw_port_${NUM}.h
@ONLY
)
endif()
endforeach(NUM)
endif()

zephyr_library_sources_ifdef(CONFIG_USERSPACE i2c_handlers.c)

add_subdirectory_ifdef(CONFIG_I2C_SLAVE slave)
@@ -696,342 +696,34 @@ static int i2c_dw_initialize(struct device *dev)
return 0;
}

/* system bindings */
#if CONFIG_I2C_0
static void i2c_config_0(struct device *port);

static const struct i2c_dw_rom_config i2c_config_dw_0 = {
.config_func = i2c_config_0,

#ifdef CONFIG_GPIO_DW_0_IRQ_SHARED
.shared_irq_dev_name = DT_I2C_DW_0_IRQ_SHARED_NAME,
#include <i2c_dw_port_0.h>
#endif
.bitrate = DT_SNPS_DESIGNWARE_I2C_0_CLOCK_FREQUENCY,
};

static struct i2c_dw_dev_config i2c_0_runtime = {
.base_address = DT_SNPS_DESIGNWARE_I2C_0_BASE_ADDRESS,
#if CONFIG_PCI
.pci_dev.class_type = I2C_DW_0_PCI_CLASS,
.pci_dev.bus = I2C_DW_0_PCI_BUS,
.pci_dev.dev = I2C_DW_0_PCI_DEV,
.pci_dev.vendor_id = I2C_DW_0_PCI_VENDOR_ID,
.pci_dev.device_id = I2C_DW_0_PCI_DEVICE_ID,
.pci_dev.function = I2C_DW_0_PCI_FUNCTION,
.pci_dev.bar = I2C_DW_0_PCI_BAR,
#endif
};

DEVICE_AND_API_INIT(i2c_0, DT_SNPS_DESIGNWARE_I2C_0_LABEL, &i2c_dw_initialize,
&i2c_0_runtime, &i2c_config_dw_0,
POST_KERNEL, CONFIG_I2C_INIT_PRIORITY,
&funcs);

#ifndef DT_SNPS_DESIGNWARE_I2C_0_IRQ_0_SENSE
#define DT_SNPS_DESIGNWARE_I2C_0_IRQ_0_SENSE 0
#endif
static void i2c_config_0(struct device *port)
{
#if defined(CONFIG_I2C_DW_0_IRQ_DIRECT)
IRQ_CONNECT(DT_SNPS_DESIGNWARE_I2C_0_IRQ_0, DT_SNPS_DESIGNWARE_I2C_0_IRQ_0_PRIORITY,
i2c_dw_isr, DEVICE_GET(i2c_0), DT_SNPS_DESIGNWARE_I2C_0_IRQ_0_SENSE);
irq_enable(DT_SNPS_DESIGNWARE_I2C_0_IRQ_0);
#elif defined(CONFIG_I2C_DW_0_IRQ_SHARED)
const struct i2c_dw_rom_config * const config =
port->config->config_info;
struct device *shared_irq_dev;

shared_irq_dev = device_get_binding(config->shared_irq_dev_name);
shared_irq_isr_register(shared_irq_dev, (isr_t)i2c_dw_isr, port);
shared_irq_enable(shared_irq_dev, port);
#endif
}
#endif /* CONFIG_I2C_0 */


/*
* Adding in I2C1
*/
#if CONFIG_I2C_1
static void i2c_config_1(struct device *port);

static const struct i2c_dw_rom_config i2c_config_dw_1 = {
.config_func = i2c_config_1,
.bitrate = DT_SNPS_DESIGNWARE_I2C_1_CLOCK_FREQUENCY,
};

static struct i2c_dw_dev_config i2c_1_runtime = {
.base_address = DT_SNPS_DESIGNWARE_I2C_1_BASE_ADDRESS,
#if CONFIG_PCI
.pci_dev.class_type = I2C_DW_1_PCI_CLASS,
.pci_dev.bus = I2C_DW_1_PCI_BUS,
.pci_dev.dev = I2C_DW_1_PCI_DEV,
.pci_dev.vendor_id = I2C_DW_1_PCI_VENDOR_ID,
.pci_dev.device_id = I2C_DW_1_PCI_DEVICE_ID,
.pci_dev.function = I2C_DW_1_PCI_FUNCTION,
.pci_dev.bar = I2C_DW_1_PCI_BAR,
#endif
};

DEVICE_AND_API_INIT(i2c_1, DT_SNPS_DESIGNWARE_I2C_1_LABEL, &i2c_dw_initialize,
&i2c_1_runtime, &i2c_config_dw_1,
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&funcs);

#ifndef DT_SNPS_DESIGNWARE_I2C_1_IRQ_0_SENSE
#define DT_SNPS_DESIGNWARE_I2C_1_IRQ_0_SENSE 0
#include <i2c_dw_port_1.h>
#endif
static void i2c_config_1(struct device *port)
{
IRQ_CONNECT(DT_SNPS_DESIGNWARE_I2C_1_IRQ_0, DT_SNPS_DESIGNWARE_I2C_1_IRQ_0_PRIORITY,
i2c_dw_isr, DEVICE_GET(i2c_1), DT_SNPS_DESIGNWARE_I2C_1_IRQ_0_SENSE);
irq_enable(DT_SNPS_DESIGNWARE_I2C_1_IRQ_0);
}

#endif /* CONFIG_I2C_1 */

/*
* Adding in I2C_2
*/
#if CONFIG_I2C_2
static void i2c_config_2(struct device *port);

static const struct i2c_dw_rom_config i2c_config_dw_2 = {
.config_func = i2c_config_2,
.bitrate = DT_SNPS_DESIGNWARE_I2C_2_CLOCK_FREQUENCY,
};

static struct i2c_dw_dev_config I2C_2_runtime = {
.base_address = DT_SNPS_DESIGNWARE_I2C_2_BASE_ADDRESS,
#if CONFIG_PCI
.pci_dev.class_type = I2C_DW_2_PCI_CLASS,
.pci_dev.bus = I2C_DW_2_PCI_BUS,
.pci_dev.dev = I2C_DW_2_PCI_DEV,
.pci_dev.vendor_id = I2C_DW_2_PCI_VENDOR_ID,
.pci_dev.device_id = I2C_DW_2_PCI_DEVICE_ID,
.pci_dev.function = I2C_DW_2_PCI_FUNCTION,
.pci_dev.bar = I2C_DW_2_PCI_BAR,
#endif
};

DEVICE_AND_API_INIT(I2C_2, DT_SNPS_DESIGNWARE_I2C_2_LABEL, &i2c_dw_initialize,
&I2C_2_runtime, &i2c_config_dw_2,
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&funcs);

#ifndef DT_SNPS_DESIGNWARE_I2C_2_IRQ_0_SENSE
#define DT_SNPS_DESIGNWARE_I2C_2_IRQ_0_SENSE 0
#include <i2c_dw_port_2.h>
#endif
static void i2c_config_2(struct device *port)
{
IRQ_CONNECT(DT_SNPS_DESIGNWARE_I2C_2_IRQ_0, DT_SNPS_DESIGNWARE_I2C_2_IRQ_0_PRIORITY,
i2c_dw_isr, DEVICE_GET(I2C_2), DT_SNPS_DESIGNWARE_I2C_2_IRQ_0_SENSE);
irq_enable(DT_SNPS_DESIGNWARE_I2C_2_IRQ_0);
}

#endif /* CONFIG_I2C_2 */

/*
* Adding in I2C_3
*/
#if CONFIG_I2C_3
static void i2c_config_3(struct device *port);

static const struct i2c_dw_rom_config i2c_config_dw_3 = {
.config_func = i2c_config_3,
.bitrate = DT_SNPS_DESIGNWARE_I2C_3_CLOCK_FREQUENCY,
};

static struct i2c_dw_dev_config I2C_3_runtime = {
.base_address = DT_SNPS_DESIGNWARE_I2C_3_BASE_ADDRESS,
#if CONFIG_PCI
.pci_dev.class_type = I2C_DW_3_PCI_CLASS,
.pci_dev.bus = I2C_DW_3_PCI_BUS,
.pci_dev.dev = I2C_DW_3_PCI_DEV,
.pci_dev.vendor_id = I2C_DW_3_PCI_VENDOR_ID,
.pci_dev.device_id = I2C_DW_3_PCI_DEVICE_ID,
.pci_dev.function = I2C_DW_3_PCI_FUNCTION,
.pci_dev.bar = I2C_DW_3_PCI_BAR,
#endif
};

DEVICE_AND_API_INIT(I2C_3, DT_SNPS_DESIGNWARE_I2C_3_LABEL, &i2c_dw_initialize,
&I2C_3_runtime, &i2c_config_dw_3,
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&funcs);

#ifndef DT_SNPS_DESIGNWARE_I2C_3_IRQ_0_SENSE
#define DT_SNPS_DESIGNWARE_I2C_3_IRQ_0_SENSE 0
#include <i2c_dw_port_3.h>
#endif
static void i2c_config_3(struct device *port)
{
IRQ_CONNECT(DT_SNPS_DESIGNWARE_I2C_3_IRQ_0, DT_SNPS_DESIGNWARE_I2C_3_IRQ_0_PRIORITY,
i2c_dw_isr, DEVICE_GET(I2C_3), DT_SNPS_DESIGNWARE_I2C_3_IRQ_0_SENSE);
irq_enable(DT_SNPS_DESIGNWARE_I2C_3_IRQ_0);
}

#endif /* CONFIG_I2C_3 */

/*
* Adding in I2C_4
*/
#if CONFIG_I2C_4
static void i2c_config_4(struct device *port);

static const struct i2c_dw_rom_config i2c_config_dw_4 = {
.config_func = i2c_config_4,
.bitrate = DT_SNPS_DESIGNWARE_I2C_4_CLOCK_FREQUENCY,
};

static struct i2c_dw_dev_config I2C_4_runtime = {
.base_address = DT_SNPS_DESIGNWARE_I2C_4_BASE_ADDRESS,
#if CONFIG_PCI
.pci_dev.class_type = I2C_DW_4_PCI_CLASS,
.pci_dev.bus = I2C_DW_4_PCI_BUS,
.pci_dev.dev = I2C_DW_4_PCI_DEV,
.pci_dev.vendor_id = I2C_DW_4_PCI_VENDOR_ID,
.pci_dev.device_id = I2C_DW_4_PCI_DEVICE_ID,
.pci_dev.function = I2C_DW_4_PCI_FUNCTION,
.pci_dev.bar = I2C_DW_4_PCI_BAR,
#endif
};

DEVICE_AND_API_INIT(I2C_4, DT_SNPS_DESIGNWARE_I2C_4_LABEL, &i2c_dw_initialize,
&I2C_4_runtime, &i2c_config_dw_4,
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&funcs);

#ifndef DT_SNPS_DESIGNWARE_I2C_4_IRQ_0_SENSE
#define DT_SNPS_DESIGNWARE_I2C_4_IRQ_0_SENSE 0
#include <i2c_dw_port_4.h>
#endif
static void i2c_config_4(struct device *port)
{
IRQ_CONNECT(DT_SNPS_DESIGNWARE_I2C_4_IRQ_0, DT_SNPS_DESIGNWARE_I2C_4_IRQ_0_PRIORITY,
i2c_dw_isr, DEVICE_GET(I2C_4), DT_SNPS_DESIGNWARE_I2C_4_IRQ_0_SENSE);
irq_enable(DT_SNPS_DESIGNWARE_I2C_4_IRQ_0);
}

#endif /* CONFIG_I2C_4 */

/*
* Adding in I2C_5
*/
#if CONFIG_I2C_5
static void i2c_config_5(struct device *port);

static const struct i2c_dw_rom_config i2c_config_dw_5 = {
.config_func = i2c_config_5,
.bitrate = DT_SNPS_DESIGNWARE_I2C_5_CLOCK_FREQUENCY,
};

static struct i2c_dw_dev_config I2C_5_runtime = {
.base_address = DT_SNPS_DESIGNWARE_I2C_5_BASE_ADDRESS,
#if CONFIG_PCI
.pci_dev.class_type = I2C_DW_5_PCI_CLASS,
.pci_dev.bus = I2C_DW_5_PCI_BUS,
.pci_dev.dev = I2C_DW_5_PCI_DEV,
.pci_dev.vendor_id = I2C_DW_5_PCI_VENDOR_ID,
.pci_dev.device_id = I2C_DW_5_PCI_DEVICE_ID,
.pci_dev.function = I2C_DW_5_PCI_FUNCTION,
.pci_dev.bar = I2C_DW_5_PCI_BAR,
#endif
};

DEVICE_AND_API_INIT(I2C_5, DT_SNPS_DESIGNWARE_I2C_5_LABEL, &i2c_dw_initialize,
&I2C_5_runtime, &i2c_config_dw_5,
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&funcs);

#ifndef DT_SNPS_DESIGNWARE_I2C_5_IRQ_0_SENSE
#define DT_SNPS_DESIGNWARE_I2C_5_IRQ_0_SENSE 0
#include <i2c_dw_port_5.h>
#endif
static void i2c_config_5(struct device *port)
{
IRQ_CONNECT(DT_SNPS_DESIGNWARE_I2C_5_IRQ_0, DT_SNPS_DESIGNWARE_I2C_5_IRQ_0_PRIORITY,
i2c_dw_isr, DEVICE_GET(I2C_5), DT_SNPS_DESIGNWARE_I2C_5_IRQ_0_SENSE);
irq_enable(DT_SNPS_DESIGNWARE_I2C_5_IRQ_0);
}

#endif /* CONFIG_I2C_5 */

/*
* Adding in I2C_6
*/
#if CONFIG_I2C_6
static void i2c_config_6(struct device *port);

static const struct i2c_dw_rom_config i2c_config_dw_6 = {
.config_func = i2c_config_6,
.bitrate = DT_SNPS_DESIGNWARE_I2C_6_CLOCK_FREQUENCY,
};

static struct i2c_dw_dev_config I2C_6_runtime = {
.base_address = DT_SNPS_DESIGNWARE_I2C_6_BASE_ADDRESS,
#if CONFIG_PCI
.pci_dev.class_type = I2C_DW_6_PCI_CLASS,
.pci_dev.bus = I2C_DW_6_PCI_BUS,
.pci_dev.dev = I2C_DW_6_PCI_DEV,
.pci_dev.vendor_id = I2C_DW_6_PCI_VENDOR_ID,
.pci_dev.device_id = I2C_DW_6_PCI_DEVICE_ID,
.pci_dev.function = I2C_DW_6_PCI_FUNCTION,
.pci_dev.bar = I2C_DW_6_PCI_BAR,
#endif
};

DEVICE_AND_API_INIT(I2C_6, DT_SNPS_DESIGNWARE_I2C_6_LABEL, &i2c_dw_initialize,
&I2C_6_runtime, &i2c_config_dw_6,
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&funcs);

#ifndef DT_SNPS_DESIGNWARE_I2C_6_IRQ_0_SENSE
#define DT_SNPS_DESIGNWARE_I2C_6_IRQ_0_SENSE 0
#include <i2c_dw_port_6.h>
#endif
static void i2c_config_6(struct device *port)
{
IRQ_CONNECT(DT_SNPS_DESIGNWARE_I2C_6_IRQ_0, DT_SNPS_DESIGNWARE_I2C_6_IRQ_0_PRIORITY,
i2c_dw_isr, DEVICE_GET(I2C_6), DT_SNPS_DESIGNWARE_I2C_6_IRQ_0_SENSE);
irq_enable(DT_SNPS_DESIGNWARE_I2C_6_IRQ_0);
}

#endif /* CONFIG_I2C_6 */

/*
* Adding in I2C_7
*/
#if CONFIG_I2C_7
static void i2c_config_7(struct device *port);

static const struct i2c_dw_rom_config i2c_config_dw_7 = {
.config_func = i2c_config_7,
.bitrate = DT_SNPS_DESIGNWARE_I2C_7_CLOCK_FREQUENCY,
};

static struct i2c_dw_dev_config I2C_7_runtime = {
.base_address = DT_SNPS_DESIGNWARE_I2C_7_BASE_ADDRESS,
#if CONFIG_PCI
.pci_dev.class_type = I2C_DW_7_PCI_CLASS,
.pci_dev.bus = I2C_DW_7_PCI_BUS,
.pci_dev.dev = I2C_DW_7_PCI_DEV,
.pci_dev.vendor_id = I2C_DW_7_PCI_VENDOR_ID,
.pci_dev.device_id = I2C_DW_7_PCI_DEVICE_ID,
.pci_dev.function = I2C_DW_7_PCI_FUNCTION,
.pci_dev.bar = I2C_DW_7_PCI_BAR,
#endif
};

DEVICE_AND_API_INIT(I2C_7, DT_SNPS_DESIGNWARE_I2C_7_LABEL, &i2c_dw_initialize,
&I2C_7_runtime, &i2c_config_dw_7,
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&funcs);

#ifndef DT_SNPS_DESIGNWARE_I2C_7_IRQ_0_SENSE
#define DT_SNPS_DESIGNWARE_I2C_7_IRQ_0_SENSE 0
#include <i2c_dw_port_7.h>
#endif
static void i2c_config_7(struct device *port)
{
IRQ_CONNECT(DT_SNPS_DESIGNWARE_I2C_7_IRQ_0, DT_SNPS_DESIGNWARE_I2C_7_IRQ_0_PRIORITY,
i2c_dw_isr, DEVICE_GET(I2C_7), DT_SNPS_DESIGNWARE_I2C_7_IRQ_0_SENSE);
irq_enable(DT_SNPS_DESIGNWARE_I2C_7_IRQ_0);
}

#endif /* CONFIG_I2C_7 */

0 comments on commit 59ebe6a

Please sign in to comment.
You can’t perform that action at this time.