emul: Use DEVICE_DT_GET instead of DT_LABEL

DT nodes aren't guaranteed to define a label property. But emulated bus
controllers currently make use of this property to dispatch to the
associated emulator.

Have emulated bus controllers use DEVICE_DT_GET(node_id) to dispatch to
right target peripheral emulator. This also change makes emul_get_binding
and device_get_binding synonymous in behavior with respect to their
parameters.

This also strictly enforces a 1:1 correspondence between invocations of
DEVICE_DT_DEFINE and EMUL_DEFINE.

Signed-off-by: Aaron Massey <aaronmassey@google.com>
This commit is contained in:
Aaron Massey 2022-07-07 14:09:30 -06:00 committed by Kumar Gala
parent 5e2678135d
commit fc98549952
6 changed files with 56 additions and 18 deletions

View file

@ -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) \

View file

@ -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) \

View file

@ -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) \

View file

@ -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, \

View file

@ -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);
}
}

View file

@ -0,0 +1,39 @@
/*
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#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);