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>
This commit is contained in:
Benjamin Björnsson 2022-06-18 14:40:34 +02:00 committed by Maureen Helm
commit d4f00fe3c4
3 changed files with 39 additions and 44 deletions

View file

@ -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 @@ static int ccs811_init(const struct device *dev)
}
#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 @@ static int ccs811_init(const struct device *dev)
* 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 @@ static int ccs811_init(const struct device *dev)
* 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 struct ccs811_data ccs811_data_inst;
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,

View file

@ -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;
@ -71,12 +70,6 @@ struct ccs811_data {
uint16_t co2_l2m;
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;
@ -85,6 +78,15 @@ 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

@ -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 @@ int ccs811_attr_set(const struct device *dev,
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 @@ int ccs811_trigger_set(const struct device *dev,
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 @@ int ccs811_trigger_set(const struct device *dev,
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_trigger_set(const struct device *dev,
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;
}