drivers: sensor: ccs811: 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 19:52:02 +02:00 committed by Carles Cufí
commit 6c82570f62
3 changed files with 63 additions and 63 deletions

View file

@ -21,7 +21,6 @@
LOG_MODULE_REGISTER(CCS811, CONFIG_SENSOR_LOG_LEVEL);
#if DT_INST_NODE_HAS_PROP(0, wake_gpios)
static void set_wake(const struct device *dev, bool enable)
{
const struct ccs811_config *config = dev->config;
@ -33,9 +32,6 @@ static void set_wake(const struct device *dev, bool enable)
k_busy_wait(20); /* t_DWAKE = 20 us */
}
}
#else
#define set_wake(...)
#endif
/* Get STATUS register in low 8 bits, and if ERROR is set put ERROR_ID
* in bits 8..15. These registers are available in both boot and
@ -440,7 +436,7 @@ static int ccs811_init(const struct device *dev)
return -ENODEV;
}
#if DT_INST_NODE_HAS_PROP(0, wake_gpios)
if (config->wake_gpio.port) {
if (!device_is_ready(config->wake_gpio.port)) {
LOG_ERR("GPIO device not ready");
return -ENODEV;
@ -455,8 +451,9 @@ static int ccs811_init(const struct device *dev)
set_wake(dev, true);
k_msleep(1);
#endif
#if DT_INST_NODE_HAS_PROP(0, reset_gpios)
}
if (config->reset_gpio.port) {
if (!device_is_ready(config->reset_gpio.port)) {
LOG_ERR("GPIO device not ready");
return -ENODEV;
@ -465,14 +462,14 @@ static int ccs811_init(const struct device *dev)
gpio_pin_configure_dt(&config->reset_gpio, GPIO_OUTPUT_ACTIVE);
k_msleep(1);
#endif
}
#if DT_INST_NODE_HAS_PROP(0, irq_gpios)
if (config->irq_gpio.port) {
if (!device_is_ready(config->irq_gpio.port)) {
LOG_ERR("GPIO device not ready");
return -ENODEV;
}
#endif
}
k_msleep(20); /* t_START assuming recent power-on */
@ -480,12 +477,11 @@ static int ccs811_init(const struct device *dev)
* and validating any errors or configuration inconsistencies
* after a reset that left the device running.
*/
#if DT_INST_NODE_HAS_PROP(0, reset_gpios)
if (config->reset_gpio.port) {
gpio_pin_set_dt(&config->reset_gpio, 1);
k_busy_wait(15); /* t_RESET */
gpio_pin_set_dt(&config->reset_gpio, 0);
#else
{
} else {
static uint8_t const reset_seq[] = {
0xFF, 0x11, 0xE5, 0x72, 0x8A,
};
@ -496,7 +492,7 @@ static int ccs811_init(const struct device *dev)
goto out;
}
}
#endif
k_msleep(2); /* t_START after reset */
/* Switch device to application mode */
@ -562,8 +558,10 @@ static int ccs811_init(const struct device *dev)
}
#ifdef CONFIG_CCS811_TRIGGER
if (config->irq_gpio.port) {
ret = ccs811_init_interrupt(dev);
LOG_DBG("CCS811 interrupt init got %d", ret);
}
#endif
out:
@ -571,19 +569,20 @@ out:
return ret;
}
static struct ccs811_data ccs811_data_inst;
#define CCS811_DEFINE(inst) \
static struct ccs811_data ccs811_data_##inst; \
\
static const struct ccs811_config ccs811_config_##inst = { \
.i2c = I2C_DT_SPEC_INST_GET(inst), \
IF_ENABLED(CONFIG_CCS811_TRIGGER, \
(.irq_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, irq_gpios, { 0 }),)) \
.reset_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, reset_gpios, { 0 }), \
.wake_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, wake_gpios, { 0 }), \
}; \
\
DEVICE_DT_INST_DEFINE(0, ccs811_init, NULL, \
&ccs811_data_##inst, &ccs811_config_##inst, \
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
&ccs811_driver_api); \
static const struct ccs811_config ccs811_config_inst = {
.i2c = I2C_DT_SPEC_INST_GET(0),
IF_ENABLED(CONFIG_CCS811_TRIGGER,
(.irq_gpio = GPIO_DT_SPEC_INST_GET(0, irq_gpios),))
IF_ENABLED(DT_INST_NODE_HAS_PROP(0, reset_gpios),
(.reset_gpio = GPIO_DT_SPEC_INST_GET(0, reset_gpios),))
IF_ENABLED(DT_INST_NODE_HAS_PROP(0, wake_gpios),
(.wake_gpio = GPIO_DT_SPEC_INST_GET(0, wake_gpios),))
};
DEVICE_DT_INST_DEFINE(0, ccs811_init, NULL,
&ccs811_data_inst, &ccs811_config_inst,
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&ccs811_driver_api);
DT_INST_FOREACH_STATUS_OKAY(CCS811_DEFINE)

View file

@ -49,7 +49,6 @@
#define CCS811_CO2_MAX_PPM 32767
struct ccs811_data {
#if DT_INST_NODE_HAS_PROP(0, irq_gpios)
#ifdef CONFIG_CCS811_TRIGGER
const struct device *dev;
@ -70,7 +69,6 @@ struct ccs811_data {
uint16_t co2_l2m;
uint16_t co2_m2h;
#endif /* CONFIG_CCS811_TRIGGER */
#endif
struct ccs811_result_type result;
uint8_t mode;
uint8_t app_fw_ver;
@ -78,15 +76,9 @@ struct ccs811_data {
struct ccs811_config {
struct i2c_dt_spec i2c;
#if DT_INST_NODE_HAS_PROP(0, irq_gpios)
struct gpio_dt_spec irq_gpio;
#endif
#if DT_INST_NODE_HAS_PROP(0, reset_gpios)
struct gpio_dt_spec reset_gpio;
#endif
#if DT_INST_NODE_HAS_PROP(0, wake_gpios)
struct gpio_dt_spec wake_gpio;
#endif
};
#ifdef CONFIG_CCS811_TRIGGER

View file

@ -19,8 +19,13 @@ int ccs811_attr_set(const struct device *dev,
const struct sensor_value *thr)
{
struct ccs811_data *drv_data = dev->data;
const struct ccs811_config *config = dev->config;
int rc;
if (!config->irq_gpio.port) {
return -ENOTSUP;
}
if (chan != SENSOR_CHAN_CO2) {
rc = -ENOTSUP;
} else if (attr == SENSOR_ATTR_LOWER_THRESH) {
@ -122,6 +127,10 @@ int ccs811_trigger_set(const struct device *dev,
uint8_t drdy_thresh = CCS811_MODE_THRESH | CCS811_MODE_DATARDY;
int rc;
if (!config->irq_gpio.port) {
return -ENOTSUP;
}
LOG_DBG("CCS811 trigger set");
setup_irq(dev, false);