diff --git a/drivers/espi/espi_emul.c b/drivers/espi/espi_emul.c index 7ed22bfb33..cedf774739 100644 --- a/drivers/espi/espi_emul.c +++ b/drivers/espi/espi_emul.c @@ -225,7 +225,7 @@ static struct emul_espi_driver_api emul_espi_driver_api = { #define EMUL_LINK_AND_COMMA(node_id) \ { \ - .label = DT_LABEL(node_id), \ + .dev = DEVICE_DT_GET(node_id), \ }, #define ESPI_EMUL_INIT(n) \ diff --git a/drivers/i2c/i2c_emul.c b/drivers/i2c/i2c_emul.c index 7ec8c3142f..34317adce4 100644 --- a/drivers/i2c/i2c_emul.c +++ b/drivers/i2c/i2c_emul.c @@ -139,7 +139,7 @@ static struct i2c_driver_api i2c_emul_api = { #define EMUL_LINK_AND_COMMA(node_id) \ { \ - .label = DT_LABEL(node_id), \ + .dev = DEVICE_DT_GET(node_id), \ }, #define I2C_EMUL_INIT(n) \ diff --git a/drivers/spi/spi_emul.c b/drivers/spi/spi_emul.c index 630fddc822..40d0e1aace 100644 --- a/drivers/spi/spi_emul.c +++ b/drivers/spi/spi_emul.c @@ -114,7 +114,7 @@ static struct spi_driver_api spi_emul_api = { #define EMUL_LINK_AND_COMMA(node_id) \ { \ - .label = DT_LABEL(node_id), \ + .dev = DEVICE_DT_GET(node_id), \ }, #define SPI_EMUL_INIT(n) \ diff --git a/include/zephyr/drivers/emul.h b/include/zephyr/drivers/emul.h index 8e7e5fdc8c..4c4ffa48ea 100644 --- a/include/zephyr/drivers/emul.h +++ b/include/zephyr/drivers/emul.h @@ -37,11 +37,9 @@ enum emul_bus_type { /** * Structure uniquely identifying a device to be emulated - * - * Currently this uses the device node label, but that will go away by 2.5. */ struct emul_link_for_bus { - const char *label; + const struct device *dev; }; /** List of emulators attached to a bus */ @@ -69,7 +67,7 @@ struct emul { /** function used to initialise the emulator state */ emul_init_t init; /** handle to the device for which this provides low-level emulation */ - const char *dev_label; + const struct device *dev; /** Emulator-specific configuration data */ const void *cfg; /** Emulator-specific data */ @@ -113,7 +111,8 @@ extern const struct emul __emul_list_end[]; * * @param init_ptr function to call to initialise the emulator (see emul_init * typedef) - * @param node_id Node ID of the driver to emulate (e.g. DT_DRV_INST(n)) + * @param node_id Node ID of the driver to emulate (e.g. DT_DRV_INST(n)); the node_id *MUST* have a + * corresponding DEVICE_DT_DEFINE(). * @param cfg_ptr emulator-specific configuration data * @param data_ptr emulator-specific data * @param bus_api emulator-specific bus api @@ -127,7 +126,7 @@ extern const struct emul __emul_list_end[]; static struct emul EMUL_REG_NAME(node_id) __attribute__((__section__(".emulators"))) \ __used = { \ .init = (init_ptr), \ - .dev_label = DT_LABEL(node_id), \ + .dev = DEVICE_DT_GET(node_id), \ .cfg = (cfg_ptr), \ .data = (data_ptr), \ .bus_type = Z_EMUL_BUS(node_id, EMUL_BUS_TYPE_I2C, EMUL_BUS_TYPE_ESPI, \ diff --git a/subsys/emul/emul.c b/subsys/emul/emul.c index 4e0f98d06f..8586251c82 100644 --- a/subsys/emul/emul.c +++ b/subsys/emul/emul.c @@ -18,7 +18,7 @@ const struct emul *emul_get_binding(const char *name) const struct emul *emul_it; for (emul_it = __emul_list_start; emul_it < __emul_list_end; emul_it++) { - if (strcmp(emul_it->dev_label, name) == 0) { + if (strcmp(emul_it->dev->name, name) == 0) { return emul_it; } } @@ -39,9 +39,9 @@ int emul_init_for_bus(const struct device *dev) LOG_INF("Registering %d emulator(s) for %s", cfg->num_children, dev->name); for (elp = cfg->children; elp < end; elp++) { - const struct emul *emul = emul_get_binding(elp->label); + const struct emul *emul = emul_get_binding(elp->dev->name); - __ASSERT(emul, "Cannot find emulator for '%s'", elp->label); + __ASSERT(emul, "Cannot find emulator for '%s'", elp->dev->name); switch (emul->bus_type) { case EMUL_BUS_TYPE_I2C: @@ -58,33 +58,33 @@ int emul_init_for_bus(const struct device *dev) if (rc != 0) { LOG_WRN("Init %s emulator failed: %d", - elp->label, rc); + elp->dev->name, rc); } switch (emul->bus_type) { #ifdef CONFIG_I2C_EMUL case EMUL_BUS_TYPE_I2C: - rc = i2c_emul_register(dev, emul->dev_label, emul->bus.i2c); + rc = i2c_emul_register(dev, emul->dev->name, emul->bus.i2c); break; #endif /* CONFIG_I2C_EMUL */ #ifdef CONFIG_ESPI_EMUL case EMUL_BUS_TYPE_ESPI: - rc = espi_emul_register(dev, emul->dev_label, emul->bus.espi); + rc = espi_emul_register(dev, emul->dev->name, emul->bus.espi); break; #endif /* CONFIG_ESPI_EMUL */ #ifdef CONFIG_SPI_EMUL case EMUL_BUS_TYPE_SPI: - rc = spi_emul_register(dev, emul->dev_label, emul->bus.spi); + rc = spi_emul_register(dev, emul->dev->name, emul->bus.spi); break; #endif /* CONFIG_SPI_EMUL */ default: rc = -EINVAL; LOG_WRN("Found no emulated bus enabled to register emulator %s", - elp->label); + elp->dev->name); } if (rc != 0) { - LOG_WRN("Failed to register emulator for %s: %d", elp->label, rc); + LOG_WRN("Failed to register emulator for %s: %d", elp->dev->name, rc); } } diff --git a/tests/drivers/espi/src/stub_espi_emul_host.c b/tests/drivers/espi/src/stub_espi_emul_host.c new file mode 100644 index 0000000000..330bae80f3 --- /dev/null +++ b/tests/drivers/espi/src/stub_espi_emul_host.c @@ -0,0 +1,39 @@ +/* + * Copyright 2022 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#define DT_DRV_COMPAT zephyr_espi_emul_espi_host + +/* Stub out an espi host device struct to use espi_host emulator. */ +static int emul_espi_host_init_stub(const struct device *dev) +{ + ARG_UNUSED(dev); + + return 0; +} + +struct emul_espi_host_stub_dev_data { + /* Stub */ +}; +struct emul_espi_host_stub_dev_config { + /* Stub */ +}; +struct emul_espi_host_stub_dev_api { + /* Stub */ +}; + +/* Since this is only stub, allocate the structs once. */ +static struct emul_espi_host_stub_dev_data stub_host_data; +static struct emul_espi_host_stub_dev_config stub_host_config; +static struct emul_espi_host_stub_dev_api stub_host_api; + +#define EMUL_ESPI_HOST_DEVICE_STUB(n) \ + DEVICE_DT_INST_DEFINE(n, &emul_espi_host_init_stub, NULL, &stub_host_data, \ + &stub_host_config, POST_KERNEL, CONFIG_ESPI_INIT_PRIORITY, \ + &stub_host_api) + +DT_INST_FOREACH_STATUS_OKAY(EMUL_ESPI_HOST_DEVICE_STUB);