drivers: sensor: fxas21002: Add multi-instance support

Move driver to use DT_INST_FOREACH_STATUS_OKAY to add
multi-instance support.

Signed-off-by: Benjamin Björnsson <benjamin.bjornsson@gmail.com>
This commit is contained in:
Benjamin Björnsson 2022-07-11 20:32:59 +02:00 committed by Carles Cufí
commit 2259c6771f
2 changed files with 30 additions and 22 deletions

View file

@ -245,9 +245,11 @@ static int fxas21002_init(const struct device *dev)
k_sem_init(&data->sem, 0, K_SEM_MAX_LIMIT); k_sem_init(&data->sem, 0, K_SEM_MAX_LIMIT);
#if CONFIG_FXAS21002_TRIGGER #if CONFIG_FXAS21002_TRIGGER
if (fxas21002_trigger_init(dev)) { if (config->int_gpio.port) {
LOG_ERR("Could not initialize interrupts"); if (fxas21002_trigger_init(dev)) {
return -EIO; LOG_ERR("Could not initialize interrupts");
return -EIO;
}
} }
#endif #endif
@ -277,23 +279,25 @@ static const struct sensor_driver_api fxas21002_driver_api = {
#endif #endif
}; };
static const struct fxas21002_config fxas21002_config = { #define FXAS21002_DEFINE(inst) \
.i2c = I2C_DT_SPEC_INST_GET(0), static struct fxas21002_data fxas21002_data_##inst; \
.whoami = CONFIG_FXAS21002_WHOAMI, \
.range = CONFIG_FXAS21002_RANGE, static const struct fxas21002_config fxas21002_config_##inst = { \
.dr = CONFIG_FXAS21002_DR, .i2c = I2C_DT_SPEC_INST_GET(inst), \
#ifdef CONFIG_FXAS21002_TRIGGER .whoami = CONFIG_FXAS21002_WHOAMI, \
#ifdef CONFIG_FXAS21002_DRDY_INT1 .range = CONFIG_FXAS21002_RANGE, \
.int_gpio = GPIO_DT_SPEC_INST_GET(0, int1_gpios), .dr = CONFIG_FXAS21002_DR, \
#else IF_ENABLED(CONFIG_FXAS21002_TRIGGER, \
.int_gpio = GPIO_DT_SPEC_INST_GET(0, int2_gpios), (COND_CODE_1(CONFIG_FXAS21002_DRDY_INT1, \
#endif (.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int1_gpios, \
#endif { 0 }),), \
}; (.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int2_gpios, \
{ 0 }),)))) \
}; \
\
DEVICE_DT_INST_DEFINE(inst, fxas21002_init, NULL, \
&fxas21002_data_##inst, &fxas21002_config_##inst, \
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
&fxas21002_driver_api); \
static struct fxas21002_data fxas21002_data; DT_INST_FOREACH_STATUS_OKAY(FXAS21002_DEFINE)
DEVICE_DT_INST_DEFINE(0, fxas21002_init, NULL,
&fxas21002_data, &fxas21002_config,
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&fxas21002_driver_api);

View file

@ -101,6 +101,10 @@ int fxas21002_trigger_set(const struct device *dev,
uint8_t mask; uint8_t mask;
int ret = 0; int ret = 0;
if (!config->int_gpio.port) {
return -ENOTSUP;
}
k_sem_take(&data->sem, K_FOREVER); k_sem_take(&data->sem, K_FOREVER);
switch (trig->type) { switch (trig->type) {