drivers: sensor: bmg160: 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-03 21:21:32 +02:00 committed by Maureen Helm
commit 6e62bd070f
2 changed files with 28 additions and 12 deletions

View file

@ -20,8 +20,6 @@
LOG_MODULE_REGISTER(BMG160, CONFIG_SENSOR_LOG_LEVEL); LOG_MODULE_REGISTER(BMG160, CONFIG_SENSOR_LOG_LEVEL);
struct bmg160_device_data bmg160_data;
static inline int bmg160_bus_config(const struct device *dev) static inline int bmg160_bus_config(const struct device *dev)
{ {
const struct bmg160_device_config *dev_cfg = dev->config; const struct bmg160_device_config *dev_cfg = dev->config;
@ -136,6 +134,7 @@ static int bmg160_attr_set(const struct device *dev, enum sensor_channel chan,
const struct sensor_value *val) const struct sensor_value *val)
{ {
struct bmg160_device_data *bmg160 = dev->data; struct bmg160_device_data *bmg160 = dev->data;
const struct bmg160_device_config *config = dev->config;
int idx; int idx;
uint16_t range_dps; uint16_t range_dps;
@ -184,6 +183,10 @@ static int bmg160_attr_set(const struct device *dev, enum sensor_channel chan,
#ifdef CONFIG_BMG160_TRIGGER #ifdef CONFIG_BMG160_TRIGGER
case SENSOR_ATTR_SLOPE_TH: case SENSOR_ATTR_SLOPE_TH:
case SENSOR_ATTR_SLOPE_DUR: case SENSOR_ATTR_SLOPE_DUR:
if (!config->int_gpio.port) {
return -ENOTSUP;
}
return bmg160_slope_config(dev, attr, val); return bmg160_slope_config(dev, attr, val);
#endif #endif
default: default:
@ -327,19 +330,26 @@ int bmg160_init(const struct device *dev)
} }
#ifdef CONFIG_BMG160_TRIGGER #ifdef CONFIG_BMG160_TRIGGER
bmg160_trigger_init(dev); if (cfg->int_gpio.port) {
bmg160_trigger_init(dev);
}
#endif #endif
return 0; return 0;
} }
static const struct bmg160_device_config bmg160_config = { #define BMG160_DEFINE(inst) \
.i2c = I2C_DT_SPEC_INST_GET(0), static struct bmg160_device_data bmg160_data_##inst; \
IF_ENABLED(CONFIG_BMG160_TRIGGER, \
(.int_gpio = GPIO_DT_SPEC_INST_GET(0, int_gpios),)) static const struct bmg160_device_config bmg160_config_##inst = { \
}; .i2c = I2C_DT_SPEC_INST_GET(inst), \
IF_ENABLED(CONFIG_BMG160_TRIGGER, \
(.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, { 0 }),)) \
\
}; \
\
DEVICE_DT_INST_DEFINE(inst, bmg160_init, NULL, \
&bmg160_data_##inst, &bmg160_config_##inst, \
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &bmg160_api); \
DEVICE_DT_INST_DEFINE(0, bmg160_init, NULL, DT_INST_FOREACH_STATUS_OKAY(BMG160_DEFINE)
&bmg160_data,
&bmg160_config, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&bmg160_api);

View file

@ -121,6 +121,12 @@ int bmg160_trigger_set(const struct device *dev,
const struct sensor_trigger *trig, const struct sensor_trigger *trig,
sensor_trigger_handler_t handler) sensor_trigger_handler_t handler)
{ {
const struct bmg160_device_config *config = dev->config;
if (!config->int_gpio.port) {
return -ENOTSUP;
}
if (trig->type == SENSOR_TRIG_DELTA) { if (trig->type == SENSOR_TRIG_DELTA) {
return bmg160_anymotion_set(dev, handler); return bmg160_anymotion_set(dev, handler);
} else if (trig->type == SENSOR_TRIG_DATA_READY) { } else if (trig->type == SENSOR_TRIG_DATA_READY) {