drivers: sensor: ccs811: Update driver to use gpio_dt_spec
Simplify driver by using gpio_dt_spec for bus access.
Signed-off-by: Benjamin Björnsson <benjamin.bjornsson@gmail.com>
diff --git a/drivers/sensor/ccs811/ccs811.c b/drivers/sensor/ccs811/ccs811.c
index 1b71e8b..c3d55a6 100644
--- a/drivers/sensor/ccs811/ccs811.c
+++ b/drivers/sensor/ccs811/ccs811.c
@@ -19,17 +19,14 @@
#include "ccs811.h"
-#define WAKE_PIN DT_INST_GPIO_PIN(0, wake_gpios)
-#define RESET_PIN DT_INST_GPIO_PIN(0, reset_gpios)
-
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)
{
- struct ccs811_data *drv_data = dev->data;
+ const struct ccs811_config *config = dev->config;
- gpio_pin_set(drv_data->wake_gpio, WAKE_PIN, enable);
+ gpio_pin_set_dt(&config->wake_gpio, enable);
if (enable) {
k_busy_wait(50); /* t_WAKE = 50 us */
} else {
@@ -444,11 +441,9 @@
}
#if DT_INST_NODE_HAS_PROP(0, wake_gpios)
- drv_data->wake_gpio = device_get_binding(DT_INST_GPIO_LABEL(0, wake_gpios));
- if (drv_data->wake_gpio == NULL) {
- LOG_ERR("Failed to get pointer to WAKE device: %s",
- DT_INST_GPIO_LABEL(0, wake_gpios));
- return -EINVAL;
+ if (!device_is_ready(config->wake_gpio.port)) {
+ LOG_ERR("GPIO device not ready");
+ return -ENODEV;
}
/*
@@ -456,33 +451,26 @@
* any I2C transfer. If it has been tied to GND by
* default, skip this part.
*/
- gpio_pin_configure(drv_data->wake_gpio, WAKE_PIN,
- GPIO_OUTPUT_INACTIVE
- | DT_INST_GPIO_FLAGS(0, wake_gpios));
+ gpio_pin_configure_dt(&config->wake_gpio, GPIO_OUTPUT_INACTIVE);
set_wake(dev, true);
k_msleep(1);
#endif
#if DT_INST_NODE_HAS_PROP(0, reset_gpios)
- drv_data->reset_gpio = device_get_binding(DT_INST_GPIO_LABEL(0, reset_gpios));
- if (drv_data->reset_gpio == NULL) {
- LOG_ERR("Failed to get pointer to RESET device: %s",
- DT_INST_GPIO_LABEL(0, reset_gpios));
- return -EINVAL;
+ if (!device_is_ready(config->reset_gpio.port)) {
+ LOG_ERR("GPIO device not ready");
+ return -ENODEV;
}
- gpio_pin_configure(drv_data->reset_gpio, RESET_PIN,
- GPIO_OUTPUT_ACTIVE
- | DT_INST_GPIO_FLAGS(0, reset_gpios));
+
+ gpio_pin_configure_dt(&config->reset_gpio, GPIO_OUTPUT_ACTIVE);
k_msleep(1);
#endif
#if DT_INST_NODE_HAS_PROP(0, irq_gpios)
- drv_data->irq_gpio = device_get_binding(DT_INST_GPIO_LABEL(0, irq_gpios));
- if (drv_data->irq_gpio == NULL) {
- LOG_ERR("Failed to get pointer to INT device: %s",
- DT_INST_GPIO_LABEL(0, irq_gpios));
- return -EINVAL;
+ if (!device_is_ready(config->irq_gpio.port)) {
+ LOG_ERR("GPIO device not ready");
+ return -ENODEV;
}
#endif
@@ -493,9 +481,9 @@
* after a reset that left the device running.
*/
#if DT_INST_NODE_HAS_PROP(0, reset_gpios)
- gpio_pin_set(drv_data->reset_gpio, RESET_PIN, 1);
+ gpio_pin_set_dt(&config->reset_gpio, 1);
k_busy_wait(15); /* t_RESET */
- gpio_pin_set(drv_data->reset_gpio, RESET_PIN, 0);
+ gpio_pin_set_dt(&config->reset_gpio, 0);
#else
{
static uint8_t const reset_seq[] = {
@@ -587,6 +575,12 @@
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,
diff --git a/drivers/sensor/ccs811/ccs811.h b/drivers/sensor/ccs811/ccs811.h
index c5471c1..d882614 100644
--- a/drivers/sensor/ccs811/ccs811.h
+++ b/drivers/sensor/ccs811/ccs811.h
@@ -50,7 +50,6 @@
struct ccs811_data {
#if DT_INST_NODE_HAS_PROP(0, irq_gpios)
- const struct device *irq_gpio;
#ifdef CONFIG_CCS811_TRIGGER
const struct device *dev;
@@ -72,12 +71,6 @@
uint16_t co2_m2h;
#endif /* CONFIG_CCS811_TRIGGER */
#endif
-#if DT_INST_NODE_HAS_PROP(0, reset_gpios)
- const struct device *reset_gpio;
-#endif
-#if DT_INST_NODE_HAS_PROP(0, wake_gpios)
- const struct device *wake_gpio;
-#endif
struct ccs811_result_type result;
uint8_t mode;
uint8_t app_fw_ver;
@@ -85,6 +78,15 @@
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
diff --git a/drivers/sensor/ccs811/ccs811_trigger.c b/drivers/sensor/ccs811/ccs811_trigger.c
index b7d2017..3467b01 100644
--- a/drivers/sensor/ccs811/ccs811_trigger.c
+++ b/drivers/sensor/ccs811/ccs811_trigger.c
@@ -13,8 +13,6 @@
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(CCS811);
-#define IRQ_PIN DT_INST_GPIO_PIN(0, irq_gpios)
-
int ccs811_attr_set(const struct device *dev,
enum sensor_channel chan,
enum sensor_attribute attr,
@@ -48,12 +46,12 @@
static inline void setup_irq(const struct device *dev,
bool enable)
{
- struct ccs811_data *data = dev->data;
+ const struct ccs811_config *config = dev->config;
unsigned int flags = enable
? GPIO_INT_LEVEL_ACTIVE
: GPIO_INT_DISABLE;
- gpio_pin_interrupt_configure(data->irq_gpio, IRQ_PIN, flags);
+ gpio_pin_interrupt_configure_dt(&config->irq_gpio, flags);
}
static inline void handle_irq(const struct device *dev)
@@ -120,6 +118,7 @@
sensor_trigger_handler_t handler)
{
struct ccs811_data *drv_data = dev->data;
+ const struct ccs811_config *config = dev->config;
uint8_t drdy_thresh = CCS811_MODE_THRESH | CCS811_MODE_DATARDY;
int rc;
@@ -154,7 +153,7 @@
drv_data->trigger = *trig;
setup_irq(dev, true);
- if (gpio_pin_get(drv_data->irq_gpio, IRQ_PIN) > 0) {
+ if (gpio_pin_get_dt(&config->irq_gpio) > 0) {
handle_irq(dev);
}
} else {
@@ -168,15 +167,15 @@
int ccs811_init_interrupt(const struct device *dev)
{
struct ccs811_data *drv_data = dev->data;
+ const struct ccs811_config *config = dev->config;
drv_data->dev = dev;
- gpio_pin_configure(drv_data->irq_gpio, IRQ_PIN,
- GPIO_INPUT | DT_INST_GPIO_FLAGS(0, irq_gpios));
+ gpio_pin_configure_dt(&config->irq_gpio, GPIO_INPUT);
- gpio_init_callback(&drv_data->gpio_cb, gpio_callback, BIT(IRQ_PIN));
+ gpio_init_callback(&drv_data->gpio_cb, gpio_callback, BIT(config->irq_gpio.pin));
- if (gpio_add_callback(drv_data->irq_gpio, &drv_data->gpio_cb) < 0) {
+ if (gpio_add_callback(config->irq_gpio.port, &drv_data->gpio_cb) < 0) {
LOG_DBG("Failed to set gpio callback!");
return -EIO;
}