diff --git a/drivers/sensor/ccs811/ccs811.c b/drivers/sensor/ccs811/ccs811.c index 67825d8dc37..274f99f49aa 100644 --- a/drivers/sensor/ccs811/ccs811.c +++ b/drivers/sensor/ccs811/ccs811.c @@ -1,4 +1,5 @@ /* + * Copyright (c) 2018 Peter Bigot Consulting, LLC * Copyright (c) 2018 Linaro Ltd. * * SPDX-License-Identifier: Apache-2.0 @@ -165,49 +166,57 @@ int ccs811_init(struct device *dev) return -EINVAL; } + struct device *gpio = NULL; + (void)gpio; #ifdef DT_INST_0_AMS_CCS811_WAKE_GPIOS_CONTROLLER - drv_data->gpio_wakeup = - device_get_binding(DT_INST_0_AMS_CCS811_WAKE_GPIOS_CONTROLLER); - if (drv_data->gpio_wakeup == NULL) { - LOG_ERR("Failed to get pointer to %s device!", + gpio = device_get_binding(DT_INST_0_AMS_CCS811_WAKE_GPIOS_CONTROLLER); + if (gpio == NULL) { + LOG_ERR("Failed to get pointer to WAKE device: %s", DT_INST_0_AMS_CCS811_WAKE_GPIOS_CONTROLLER); return -EINVAL; } -#endif + drv_data->gpio = gpio; +#endif #ifdef DT_INST_0_AMS_CCS811_RESET_GPIOS_CONTROLLER - drv_data->gpio_reset = - device_get_binding(DT_INST_0_AMS_CCS811_RESET_GPIOS_CONTROLLER); - if (drv_data->gpio_reset == NULL) { - LOG_ERR("Failed to get pointer to %s device!", + gpio = device_get_binding(DT_INST_0_AMS_CCS811_RESET_GPIOS_CONTROLLER); + if (gpio == NULL) { + LOG_ERR("Failed to get pointer to RESET device: %s", DT_INST_0_AMS_CCS811_RESET_GPIOS_CONTROLLER); return -EINVAL; } -#endif -#ifdef DT_INST_0_AMS_CCS811_RESET_GPIOS_CONTROLLER - gpio_pin_configure(drv_data->gpio_reset, - DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN, - GPIO_DIR_OUT); - gpio_pin_write(drv_data->gpio_reset, - DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN, 1); - k_sleep(K_MSEC(1)); + if (drv_data->gpio == NULL) { + drv_data->gpio = gpio; + } else if (drv_data->gpio != gpio) { + LOG_ERR("Crossing GPIO devices not supported"); + return -EINVAL; + } #endif - /* - * Wakeup pin should be pulled low before initiating any I2C transfer. - * If it has been tied to GND by default, skip this part. - */ -#ifdef DT_INST_0_AMS_CCS811_WAKE_GPIOS_CONTROLLER - gpio_pin_configure(drv_data->gpio_wakeup, - DT_INST_0_AMS_CCS811_WAKE_GPIOS_PIN, - GPIO_DIR_OUT); - gpio_pin_write(drv_data->gpio_wakeup, - DT_INST_0_AMS_CCS811_WAKE_GPIOS_PIN, 0); + if (drv_data->gpio) { +#ifdef DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN + gpio_pin_configure(drv_data->gpio, DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN, + GPIO_DIR_OUT); + gpio_pin_write(drv_data->gpio, DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN, 1); - k_sleep(K_MSEC(1)); + k_sleep(K_MSEC(1)); #endif + /* + * Wakeup pin should be pulled low before initiating + * any I2C transfer. If it has been tied to GND by + * default, skip this part. + */ +#ifdef DT_INST_0_AMS_CCS811_WAKE_GPIOS_PIN + gpio_pin_configure(drv_data->gpio, DT_INST_0_AMS_CCS811_WAKE_GPIOS_PIN, + GPIO_DIR_OUT); + gpio_pin_write(drv_data->gpio, DT_INST_0_AMS_CCS811_WAKE_GPIOS_PIN, 0); + + k_sleep(K_MSEC(1)); +#endif + } + /* Switch device to application mode */ ret = switch_to_app_mode(drv_data->i2c); if (ret) { diff --git a/drivers/sensor/ccs811/ccs811.h b/drivers/sensor/ccs811/ccs811.h index d8aaecfb602..17d7d648aae 100644 --- a/drivers/sensor/ccs811/ccs811.h +++ b/drivers/sensor/ccs811/ccs811.h @@ -1,4 +1,5 @@ /* + * Copyright (c) 2018 Peter Bigot Consulting, LLC * Copyright (c) 2018 Linaro Ltd. * * SPDX-License-Identifier: Apache-2.0 @@ -44,12 +45,7 @@ struct ccs811_data { struct device *i2c; -#ifdef DT_INST_0_AMS_CCS811_WAKE_GPIOS_CONTROLLER - struct device *gpio_wakeup; -#endif -#ifdef DT_INST_0_AMS_CCS811_RESET_GPIOS_CONTROLLER - struct device *gpio_reset; -#endif + struct device *gpio; u16_t co2; u16_t voc; u8_t status; diff --git a/dts/bindings/sensor/ams,ccs811.yaml b/dts/bindings/sensor/ams,ccs811.yaml index 6175b3a1170..8c4c3b0dde1 100644 --- a/dts/bindings/sensor/ams,ccs811.yaml +++ b/dts/bindings/sensor/ams,ccs811.yaml @@ -11,11 +11,14 @@ properties: wake-gpios: type: phandle-array required: false + description: WAKEn pin reset-gpios: type: phandle-array required: false + description: RESETn pin irq-gpios: type: phandle-array required: false + description: INTn pin