drivers: sensor: ccs811: Update driver to use i2c_dt_spec

Simplify driver by using i2c_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 11:39:23 +02:00 committed by Maureen Helm
commit 3ac870b03b
2 changed files with 48 additions and 54 deletions

View file

@ -46,12 +46,11 @@ static void set_wake(const struct device *dev, bool enable)
*/
static int fetch_status(const struct device *dev)
{
struct ccs811_data *drv_data = dev->data;
const struct ccs811_config *config = dev->config;
uint8_t status;
int rv;
if (i2c_reg_read_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
CCS811_REG_STATUS, &status) < 0) {
if (i2c_reg_read_byte_dt(&config->i2c, CCS811_REG_STATUS, &status) < 0) {
LOG_ERR("Failed to read Status register");
return -EIO;
}
@ -60,8 +59,7 @@ static int fetch_status(const struct device *dev)
if (status & CCS811_STATUS_ERROR) {
uint8_t error_id;
if (i2c_reg_read_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
CCS811_REG_ERROR_ID, &error_id) < 0) {
if (i2c_reg_read_byte_dt(&config->i2c, CCS811_REG_ERROR_ID, &error_id) < 0) {
LOG_ERR("Failed to read ERROR_ID register");
return -EIO;
}
@ -88,6 +86,7 @@ int ccs811_configver_fetch(const struct device *dev,
struct ccs811_configver_type *ptr)
{
struct ccs811_data *drv_data = dev->data;
const struct ccs811_config *config = dev->config;
uint8_t cmd;
int rc;
@ -97,24 +96,21 @@ int ccs811_configver_fetch(const struct device *dev,
set_wake(dev, true);
cmd = CCS811_REG_HW_VERSION;
rc = i2c_write_read(drv_data->i2c, DT_INST_REG_ADDR(0),
&cmd, sizeof(cmd),
&ptr->hw_version, sizeof(ptr->hw_version));
rc = i2c_write_read_dt(&config->i2c, &cmd, sizeof(cmd), &ptr->hw_version,
sizeof(ptr->hw_version));
if (rc == 0) {
cmd = CCS811_REG_FW_BOOT_VERSION;
rc = i2c_write_read(drv_data->i2c, DT_INST_REG_ADDR(0),
&cmd, sizeof(cmd),
(uint8_t *)&ptr->fw_boot_version,
sizeof(ptr->fw_boot_version));
rc = i2c_write_read_dt(&config->i2c, &cmd, sizeof(cmd),
(uint8_t *)&ptr->fw_boot_version,
sizeof(ptr->fw_boot_version));
ptr->fw_boot_version = sys_be16_to_cpu(ptr->fw_boot_version);
}
if (rc == 0) {
cmd = CCS811_REG_FW_APP_VERSION;
rc = i2c_write_read(drv_data->i2c, DT_INST_REG_ADDR(0),
&cmd, sizeof(cmd),
(uint8_t *)&ptr->fw_app_version,
sizeof(ptr->fw_app_version));
rc = i2c_write_read_dt(&config->i2c, &cmd, sizeof(cmd),
(uint8_t *)&ptr->fw_app_version,
sizeof(ptr->fw_app_version));
ptr->fw_app_version = sys_be16_to_cpu(ptr->fw_app_version);
}
if (rc == 0) {
@ -132,15 +128,14 @@ int ccs811_configver_fetch(const struct device *dev,
int ccs811_baseline_fetch(const struct device *dev)
{
const uint8_t cmd = CCS811_REG_BASELINE;
struct ccs811_data *drv_data = dev->data;
const struct ccs811_config *config = dev->config;
int rc;
uint16_t baseline;
set_wake(dev, true);
rc = i2c_write_read(drv_data->i2c, DT_INST_REG_ADDR(0),
&cmd, sizeof(cmd),
(uint8_t *)&baseline, sizeof(baseline));
rc = i2c_write_read_dt(&config->i2c, &cmd, sizeof(cmd), (uint8_t *)&baseline,
sizeof(baseline));
set_wake(dev, false);
if (rc <= 0) {
rc = baseline;
@ -152,14 +147,14 @@ int ccs811_baseline_fetch(const struct device *dev)
int ccs811_baseline_update(const struct device *dev,
uint16_t baseline)
{
struct ccs811_data *drv_data = dev->data;
const struct ccs811_config *config = dev->config;
uint8_t buf[1 + sizeof(baseline)];
int rc;
buf[0] = CCS811_REG_BASELINE;
memcpy(buf + 1, &baseline, sizeof(baseline));
set_wake(dev, true);
rc = i2c_write(drv_data->i2c, buf, sizeof(buf), DT_INST_REG_ADDR(0));
rc = i2c_write_dt(&config->i2c, buf, sizeof(buf));
set_wake(dev, false);
return rc;
}
@ -168,7 +163,7 @@ int ccs811_envdata_update(const struct device *dev,
const struct sensor_value *temperature,
const struct sensor_value *humidity)
{
struct ccs811_data *drv_data = dev->data;
const struct ccs811_config *config = dev->config;
int rc;
uint8_t buf[5] = { CCS811_REG_ENV_DATA };
@ -227,7 +222,7 @@ int ccs811_envdata_update(const struct device *dev,
}
set_wake(dev, true);
rc = i2c_write(drv_data->i2c, buf, sizeof(buf), DT_INST_REG_ADDR(0));
rc = i2c_write_dt(&config->i2c, buf, sizeof(buf));
set_wake(dev, false);
return rc;
}
@ -236,6 +231,7 @@ static int ccs811_sample_fetch(const struct device *dev,
enum sensor_channel chan)
{
struct ccs811_data *drv_data = dev->data;
const struct ccs811_config *config = dev->config;
struct ccs811_result_type *rp = &drv_data->result;
const uint8_t cmd = CCS811_REG_ALG_RESULT_DATA;
int rc;
@ -243,9 +239,7 @@ static int ccs811_sample_fetch(const struct device *dev,
unsigned int status;
set_wake(dev, true);
rc = i2c_write_read(drv_data->i2c, DT_INST_REG_ADDR(0),
&cmd, sizeof(cmd),
(uint8_t *)buf, sizeof(buf));
rc = i2c_write_read_dt(&config->i2c, &cmd, sizeof(cmd), (uint8_t *)buf, sizeof(buf));
set_wake(dev, false);
if (rc < 0) {
return -EIO;
@ -328,7 +322,7 @@ static const struct sensor_driver_api ccs811_driver_api = {
static int switch_to_app_mode(const struct device *dev)
{
struct ccs811_data *drv_data = dev->data;
const struct ccs811_config *config = dev->config;
uint8_t buf;
int status;
@ -353,7 +347,7 @@ static int switch_to_app_mode(const struct device *dev)
buf = CCS811_REG_APP_START;
/* Set the device to application mode */
if (i2c_write(drv_data->i2c, &buf, 1, DT_INST_REG_ADDR(0)) < 0) {
if (i2c_write_dt(&config->i2c, &buf, 1) < 0) {
LOG_ERR("Failed to set Application mode");
return -EIO;
}
@ -382,6 +376,7 @@ int ccs811_mutate_meas_mode(const struct device *dev,
uint8_t clear)
{
struct ccs811_data *drv_data = dev->data;
const struct ccs811_config *config = dev->config;
int rc = 0;
uint8_t mode = set | (drv_data->mode & ~clear);
@ -395,9 +390,7 @@ int ccs811_mutate_meas_mode(const struct device *dev,
if (mode != drv_data->mode) {
set_wake(dev, true);
rc = i2c_reg_write_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
CCS811_REG_MEAS_MODE,
mode);
rc = i2c_reg_write_byte_dt(&config->i2c, CCS811_REG_MEAS_MODE, mode);
LOG_DBG("CCS811 meas mode change %02x to %02x got %d",
drv_data->mode, mode, rc);
if (rc < 0) {
@ -417,6 +410,7 @@ int ccs811_mutate_meas_mode(const struct device *dev,
int ccs811_set_thresholds(const struct device *dev)
{
struct ccs811_data *drv_data = dev->data;
const struct ccs811_config *config = dev->config;
const uint8_t buf[5] = {
CCS811_REG_THRESHOLDS,
drv_data->co2_l2m >> 8,
@ -427,7 +421,7 @@ int ccs811_set_thresholds(const struct device *dev)
int rc;
set_wake(dev, true);
rc = i2c_write(drv_data->i2c, buf, sizeof(buf), DT_INST_REG_ADDR(0));
rc = i2c_write_dt(&config->i2c, buf, sizeof(buf));
set_wake(dev, false);
return rc;
}
@ -437,18 +431,16 @@ int ccs811_set_thresholds(const struct device *dev)
static int ccs811_init(const struct device *dev)
{
struct ccs811_data *drv_data = dev->data;
const struct ccs811_config *config = dev->config;
int ret = 0;
int status;
uint16_t fw_ver;
uint8_t cmd;
uint8_t hw_id;
*drv_data = (struct ccs811_data){ 0 };
drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0));
if (drv_data->i2c == NULL) {
LOG_ERR("Failed to get pointer to %s device!",
DT_INST_BUS_LABEL(0));
return -EINVAL;
if (!device_is_ready(config->i2c.bus)) {
LOG_ERR("I2C bus device not ready");
return -ENODEV;
}
#if DT_INST_NODE_HAS_PROP(0, wake_gpios)
@ -510,8 +502,7 @@ static int ccs811_init(const struct device *dev)
0xFF, 0x11, 0xE5, 0x72, 0x8A,
};
if (i2c_write(drv_data->i2c, reset_seq, sizeof(reset_seq),
DT_INST_REG_ADDR(0)) < 0) {
if (i2c_write_dt(&config->i2c, reset_seq, sizeof(reset_seq)) < 0) {
LOG_ERR("Failed to issue SW reset");
ret = -EIO;
goto out;
@ -527,8 +518,7 @@ static int ccs811_init(const struct device *dev)
}
/* Check Hardware ID */
if (i2c_reg_read_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
CCS811_REG_HW_ID, &hw_id) < 0) {
if (i2c_reg_read_byte_dt(&config->i2c, CCS811_REG_HW_ID, &hw_id) < 0) {
LOG_ERR("Failed to read Hardware ID register");
ret = -EIO;
goto out;
@ -542,9 +532,7 @@ static int ccs811_init(const struct device *dev)
/* Check application firmware version (first byte) */
cmd = CCS811_REG_FW_APP_VERSION;
if (i2c_write_read(drv_data->i2c, DT_INST_REG_ADDR(0),
&cmd, sizeof(cmd),
&fw_ver, sizeof(fw_ver)) < 0) {
if (i2c_write_read_dt(&config->i2c, &cmd, sizeof(cmd), &fw_ver, sizeof(fw_ver)) < 0) {
LOG_ERR("Failed to read App Firmware Version register");
ret = -EIO;
goto out;
@ -564,9 +552,7 @@ static int ccs811_init(const struct device *dev)
#elif defined(CONFIG_CCS811_DRIVE_MODE_4)
meas_mode = CCS811_MODE_IAQ_250MSEC;
#endif
if (i2c_reg_write_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
CCS811_REG_MEAS_MODE,
meas_mode) < 0) {
if (i2c_reg_write_byte_dt(&config->i2c, CCS811_REG_MEAS_MODE, meas_mode) < 0) {
LOG_ERR("Failed to set Measurement mode");
ret = -EIO;
goto out;
@ -597,9 +583,13 @@ out:
return ret;
}
static struct ccs811_data ccs811_driver;
static struct ccs811_data ccs811_data_inst;
static const struct ccs811_config ccs811_config_inst = {
.i2c = I2C_DT_SPEC_INST_GET(0),
};
DEVICE_DT_INST_DEFINE(0, ccs811_init, NULL,
&ccs811_driver, NULL,
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&ccs811_driver_api);
&ccs811_data_inst, &ccs811_config_inst,
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&ccs811_driver_api);

View file

@ -9,6 +9,7 @@
#define ZEPHYR_DRIVERS_SENSOR_CCS811_CCS811_H_
#include <zephyr/device.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/util.h>
#include <zephyr/drivers/sensor/ccs811.h>
@ -48,7 +49,6 @@
#define CCS811_CO2_MAX_PPM 32767
struct ccs811_data {
const struct device *i2c;
#if DT_INST_NODE_HAS_PROP(0, irq_gpios)
const struct device *irq_gpio;
#ifdef CONFIG_CCS811_TRIGGER
@ -83,6 +83,10 @@ struct ccs811_data {
uint8_t app_fw_ver;
};
struct ccs811_config {
struct i2c_dt_spec i2c;
};
#ifdef CONFIG_CCS811_TRIGGER
int ccs811_mutate_meas_mode(const struct device *dev,