From 3e4e4eaf75e77fb72df1730461920ad9ea43d2fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Bj=C3=B6rnsson?= Date: Mon, 27 Jun 2022 06:11:54 +0200 Subject: [PATCH] drivers: sensor: si7060: Update driver to use i2c_dt_spec MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplify driver by using i2c_dt_spec for bus access. Signed-off-by: Benjamin Björnsson --- drivers/sensor/si7060/si7060.c | 35 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/drivers/sensor/si7060/si7060.c b/drivers/sensor/si7060/si7060.c index fff071c2825..6bb315bbcfb 100644 --- a/drivers/sensor/si7060/si7060.c +++ b/drivers/sensor/si7060/si7060.c @@ -18,16 +18,18 @@ LOG_MODULE_REGISTER(si7060, CONFIG_SENSOR_LOG_LEVEL); struct si7060_data { - const struct device *i2c_dev; uint16_t temperature; }; +struct si7060_config { + struct i2c_dt_spec i2c; +}; + static int si7060_reg_read(const struct device *dev, uint8_t reg, uint8_t *val) { - struct si7060_data *drv_data = dev->data; + const struct si7060_config *config = dev->config; - if (i2c_reg_read_byte(drv_data->i2c_dev, - DT_INST_REG_ADDR(0), reg, val)) { + if (i2c_reg_read_byte_dt(&config->i2c, reg, val)) { return -EIO; } @@ -36,10 +38,9 @@ static int si7060_reg_read(const struct device *dev, uint8_t reg, uint8_t *val) static int si7060_reg_write(const struct device *dev, uint8_t reg, uint8_t val) { - struct si7060_data *drv_data = dev->data; + const struct si7060_config *config = dev->config; - return i2c_reg_write_byte(drv_data->i2c_dev, - DT_INST_REG_ADDR(0), reg, val); + return i2c_reg_write_byte_dt(&config->i2c, reg, val); } static int si7060_sample_fetch(const struct device *dev, @@ -98,16 +99,12 @@ static const struct sensor_driver_api si7060_api = { static int si7060_chip_init(const struct device *dev) { - struct si7060_data *drv_data = dev->data; + const struct si7060_config *config = dev->config; uint8_t value; - drv_data->i2c_dev = device_get_binding( - DT_INST_BUS_LABEL(0)); - - if (!drv_data->i2c_dev) { - 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("Bus device is not ready"); + return -ENODEV; } if (si7060_reg_read(dev, SI7060_REG_CHIP_INFO, &value) != 0) { @@ -133,5 +130,9 @@ static int si7060_init(const struct device *dev) static struct si7060_data si_data; -DEVICE_DT_INST_DEFINE(0, si7060_init, NULL, - &si_data, NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &si7060_api); +static const struct si7060_config si7060_config_inst = { + .i2c = I2C_DT_SPEC_INST_GET(0), +}; + +DEVICE_DT_INST_DEFINE(0, si7060_init, NULL, &si_data, &si7060_config_inst, + POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &si7060_api);