drivers: sensor: si7060: 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-27 06:11:54 +02:00 committed by Maureen Helm
parent 0f7751049f
commit 3e4e4eaf75

View file

@ -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);