drivers: sensor: bmc150_magn: 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>
diff --git a/drivers/sensor/bmc150_magn/bmc150_magn.c b/drivers/sensor/bmc150_magn/bmc150_magn.c
index 2cc130a..19ab6b6 100644
--- a/drivers/sensor/bmc150_magn/bmc150_magn.c
+++ b/drivers/sensor/bmc150_magn/bmc150_magn.c
@@ -52,36 +52,32 @@
enum bmc150_magn_power_modes mode,
int state)
{
- struct bmc150_magn_data *data = dev->data;
const struct bmc150_magn_config *config = dev->config;
switch (mode) {
case BMC150_MAGN_POWER_MODE_SUSPEND:
- if (i2c_reg_update_byte(data->i2c_master,
- config->i2c_slave_addr,
- BMC150_MAGN_REG_POWER,
- BMC150_MAGN_MASK_POWER_CTL,
- !state) < 0) {
+ if (i2c_reg_update_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_POWER,
+ BMC150_MAGN_MASK_POWER_CTL,
+ !state) < 0) {
return -EIO;
}
k_busy_wait(USEC_PER_MSEC * 5U);
return 0;
case BMC150_MAGN_POWER_MODE_SLEEP:
- return i2c_reg_update_byte(data->i2c_master,
- config->i2c_slave_addr,
- BMC150_MAGN_REG_OPMODE_ODR,
- BMC150_MAGN_MASK_OPMODE,
- BMC150_MAGN_MODE_SLEEP <<
- BMC150_MAGN_SHIFT_OPMODE);
+ return i2c_reg_update_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_OPMODE_ODR,
+ BMC150_MAGN_MASK_OPMODE,
+ BMC150_MAGN_MODE_SLEEP <<
+ BMC150_MAGN_SHIFT_OPMODE);
break;
case BMC150_MAGN_POWER_MODE_NORMAL:
- return i2c_reg_update_byte(data->i2c_master,
- config->i2c_slave_addr,
- BMC150_MAGN_REG_OPMODE_ODR,
- BMC150_MAGN_MASK_OPMODE,
- BMC150_MAGN_MODE_NORMAL <<
- BMC150_MAGN_SHIFT_OPMODE);
+ return i2c_reg_update_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_OPMODE_ODR,
+ BMC150_MAGN_MASK_OPMODE,
+ BMC150_MAGN_MODE_NORMAL <<
+ BMC150_MAGN_SHIFT_OPMODE);
break;
}
@@ -90,19 +86,16 @@
static int bmc150_magn_set_odr(const struct device *dev, uint8_t val)
{
- struct bmc150_magn_data *data = dev->data;
const struct bmc150_magn_config *config = dev->config;
uint8_t i;
for (i = 0U; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); ++i) {
if (val <= bmc150_magn_samp_freq_table[i].freq) {
- return i2c_reg_update_byte(data->i2c_master,
- config->i2c_slave_addr,
- BMC150_MAGN_REG_OPMODE_ODR,
- BMC150_MAGN_MASK_ODR,
- bmc150_magn_samp_freq_table[i].
- reg_val <<
- BMC150_MAGN_SHIFT_ODR);
+ return i2c_reg_update_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_OPMODE_ODR,
+ BMC150_MAGN_MASK_ODR,
+ bmc150_magn_samp_freq_table[i].reg_val
+ << BMC150_MAGN_SHIFT_ODR);
}
}
@@ -116,8 +109,8 @@
const struct bmc150_magn_config *config = dev->config;
uint8_t reg_val;
- if (i2c_reg_read_byte(data->i2c_master, config->i2c_slave_addr,
- BMC150_MAGN_REG_REP_XY, ®_val) < 0) {
+ if (i2c_reg_read_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_REP_XY, ®_val) < 0) {
return -EIO;
}
@@ -132,8 +125,8 @@
const struct bmc150_magn_config *config = dev->config;
uint8_t reg_val;
- if (i2c_reg_read_byte(data->i2c_master, config->i2c_slave_addr,
- BMC150_MAGN_REG_REP_Z, ®_val) < 0) {
+ if (i2c_reg_read_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_REP_Z, ®_val) < 0) {
return -EIO;
}
@@ -178,8 +171,8 @@
const struct bmc150_magn_config *config = dev->config;
uint8_t i, odr_val, reg_val;
- if (i2c_reg_read_byte(data->i2c_master, config->i2c_slave_addr,
- BMC150_MAGN_REG_OPMODE_ODR, ®_val) < 0) {
+ if (i2c_reg_read_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_OPMODE_ODR, ®_val) < 0) {
return -EIO;
}
@@ -202,10 +195,10 @@
struct bmc150_magn_data *data = dev->data;
const struct bmc150_magn_config *config = dev->config;
- if (i2c_reg_update_byte(data->i2c_master, config->i2c_slave_addr,
- BMC150_MAGN_REG_REP_XY,
- BMC150_MAGN_REG_REP_DATAMASK,
- BMC150_MAGN_REPXY_TO_REGVAL(val)) < 0) {
+ if (i2c_reg_update_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_REP_XY,
+ BMC150_MAGN_REG_REP_DATAMASK,
+ BMC150_MAGN_REPXY_TO_REGVAL(val)) < 0) {
return -EIO;
}
@@ -221,10 +214,10 @@
struct bmc150_magn_data *data = dev->data;
const struct bmc150_magn_config *config = dev->config;
- if (i2c_reg_update_byte(data->i2c_master, config->i2c_slave_addr,
- BMC150_MAGN_REG_REP_Z,
- BMC150_MAGN_REG_REP_DATAMASK,
- BMC150_MAGN_REPZ_TO_REGVAL(val)) < 0) {
+ if (i2c_reg_update_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_REP_Z,
+ BMC150_MAGN_REG_REP_DATAMASK,
+ BMC150_MAGN_REPZ_TO_REGVAL(val)) < 0) {
return -EIO;
}
@@ -300,9 +293,9 @@
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL ||
chan == SENSOR_CHAN_MAGN_XYZ);
- if (i2c_burst_read(data->i2c_master, config->i2c_slave_addr,
- BMC150_MAGN_REG_X_L, (uint8_t *)values,
- sizeof(values)) < 0) {
+ if (i2c_burst_read_dt(&config->i2c,
+ BMC150_MAGN_REG_X_L, (uint8_t *)values,
+ sizeof(values)) < 0) {
LOG_ERR("failed to read sample");
return -EIO;
}
@@ -498,8 +491,8 @@
return -EIO;
}
- if (i2c_reg_read_byte(data->i2c_master, config->i2c_slave_addr,
- BMC150_MAGN_REG_CHIP_ID, &chip_id) < 0) {
+ if (i2c_reg_read_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_CHIP_ID, &chip_id) < 0) {
LOG_ERR("failed reading chip id");
goto err_poweroff;
}
@@ -516,18 +509,18 @@
goto err_poweroff;
}
- if (i2c_reg_write_byte(data->i2c_master, config->i2c_slave_addr,
- BMC150_MAGN_REG_REP_XY,
- BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy))
- < 0) {
+ if (i2c_reg_write_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_REP_XY,
+ BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy))
+ < 0) {
LOG_ERR("failed to set REP XY to %d",
preset.rep_xy);
goto err_poweroff;
}
- if (i2c_reg_write_byte(data->i2c_master, config->i2c_slave_addr,
- BMC150_MAGN_REG_REP_Z,
- BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z)) < 0) {
+ if (i2c_reg_write_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_REP_Z,
+ BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z)) < 0) {
LOG_ERR("failed to set REP Z to %d",
preset.rep_z);
goto err_poweroff;
@@ -539,9 +532,9 @@
goto err_poweroff;
}
- if (i2c_burst_read(data->i2c_master, config->i2c_slave_addr,
- BMC150_MAGN_REG_TRIM_START, (uint8_t *)&data->tregs,
- sizeof(data->tregs)) < 0) {
+ if (i2c_burst_read_dt(&config->i2c,
+ BMC150_MAGN_REG_TRIM_START, (uint8_t *)&data->tregs,
+ sizeof(data->tregs)) < 0) {
LOG_ERR("failed to read trim regs");
goto err_poweroff;
}
@@ -572,13 +565,10 @@
{
const struct bmc150_magn_config * const config =
dev->config;
- struct bmc150_magn_data *data = dev->data;
- data->i2c_master = device_get_binding(config->i2c_master_dev_name);
- if (!data->i2c_master) {
- LOG_ERR("i2c master not found: %s",
- config->i2c_master_dev_name);
- return -EINVAL;
+ if (!device_is_ready(config->i2c.bus)) {
+ LOG_ERR("I2C bus device not ready");
+ return -ENODEV;
}
if (bmc150_magn_init_chip(dev) < 0) {
@@ -596,13 +586,12 @@
}
static const struct bmc150_magn_config bmc150_magn_config = {
+ .i2c = I2C_DT_SPEC_INST_GET(0),
#if defined(CONFIG_BMC150_MAGN_TRIGGER_DRDY)
.gpio_drdy_dev_name = DT_INST_GPIO_LABEL(0, drdy_gpios),
.gpio_drdy_int_pin = DT_INST_GPIO_PIN(0, drdy_gpios),
.gpio_drdy_int_flags = DT_INST_GPIO_FLAGS(0, drdy_gpios),
#endif
- .i2c_master_dev_name = DT_INST_BUS_LABEL(0),
- .i2c_slave_addr = BMC150_MAGN_I2C_ADDR,
};
static struct bmc150_magn_data bmc150_magn_data;
diff --git a/drivers/sensor/bmc150_magn/bmc150_magn.h b/drivers/sensor/bmc150_magn/bmc150_magn.h
index 3982f27..5c4e0a0 100644
--- a/drivers/sensor/bmc150_magn/bmc150_magn.h
+++ b/drivers/sensor/bmc150_magn/bmc150_magn.h
@@ -73,8 +73,6 @@
#define BMC150_MAGN_MASK_DRDY_LATCHING BIT(1)
#define BMC150_MAGN_MASK_DRDY_INT3_POLARITY BIT(0)
-#define BMC150_MAGN_I2C_ADDR DT_INST_REG_ADDR(0)
-
#if defined(CONFIG_BMC150_MAGN_SAMPLING_REP_XY) || \
defined(CONFIG_BMC150_MAGN_SAMPLING_REP_Z)
#define BMC150_MAGN_SET_ATTR_REP
@@ -86,13 +84,12 @@
#endif
struct bmc150_magn_config {
+ struct i2c_dt_spec i2c;
#if defined(CONFIG_BMC150_MAGN_TRIGGER_DRDY)
char *gpio_drdy_dev_name;
gpio_pin_t gpio_drdy_int_pin;
gpio_dt_flags_t gpio_drdy_int_flags;
#endif
- uint16_t i2c_slave_addr;
- char *i2c_master_dev_name;
};
struct bmc150_magn_trim_regs {
@@ -113,7 +110,6 @@
} __packed;
struct bmc150_magn_data {
- const struct device *i2c_master;
struct k_sem sem;
#if defined(CONFIG_BMC150_MAGN_TRIGGER)
diff --git a/drivers/sensor/bmc150_magn/bmc150_magn_trigger.c b/drivers/sensor/bmc150_magn/bmc150_magn_trigger.c
index 3a3dbbb..7920b27 100644
--- a/drivers/sensor/bmc150_magn/bmc150_magn_trigger.c
+++ b/drivers/sensor/bmc150_magn/bmc150_magn_trigger.c
@@ -51,12 +51,11 @@
data->handler_drdy = handler;
data->trigger_drdy = *trig;
- if (i2c_reg_update_byte(data->i2c_master,
- config->i2c_slave_addr,
- BMC150_MAGN_REG_INT_DRDY,
- BMC150_MAGN_MASK_DRDY_EN,
- state << BMC150_MAGN_SHIFT_DRDY_EN)
- < 0) {
+ if (i2c_reg_update_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_INT_DRDY,
+ BMC150_MAGN_MASK_DRDY_EN,
+ state << BMC150_MAGN_SHIFT_DRDY_EN)
+ < 0) {
LOG_DBG("failed to set DRDY interrupt");
return -EIO;
}
@@ -90,10 +89,9 @@
while (1) {
k_sem_take(&data->sem, K_FOREVER);
- while (i2c_reg_read_byte(data->i2c_master,
- config->i2c_slave_addr,
- BMC150_MAGN_REG_INT_STATUS,
- ®_val) < 0) {
+ while (i2c_reg_read_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_INT_STATUS,
+ ®_val) < 0) {
LOG_DBG("failed to clear data ready interrupt");
}
@@ -114,10 +112,10 @@
state = 1;
}
- return i2c_reg_update_byte(data->i2c_master, config->i2c_slave_addr,
- BMC150_MAGN_REG_INT_DRDY,
- BMC150_MAGN_MASK_DRDY_DR_POLARITY,
- state << BMC150_MAGN_SHIFT_DRDY_DR_POLARITY);
+ return i2c_reg_update_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_INT_DRDY,
+ BMC150_MAGN_MASK_DRDY_DR_POLARITY,
+ state << BMC150_MAGN_SHIFT_DRDY_DR_POLARITY);
}
int bmc150_magn_init_interrupt(const struct device *dev)
@@ -133,10 +131,10 @@
return -EIO;
}
- if (i2c_reg_update_byte(data->i2c_master, config->i2c_slave_addr,
- BMC150_MAGN_REG_INT_DRDY,
- BMC150_MAGN_MASK_DRDY_EN,
- 0 << BMC150_MAGN_SHIFT_DRDY_EN) < 0) {
+ if (i2c_reg_update_byte_dt(&config->i2c,
+ BMC150_MAGN_REG_INT_DRDY,
+ BMC150_MAGN_MASK_DRDY_EN,
+ 0 << BMC150_MAGN_SHIFT_DRDY_EN) < 0) {
LOG_DBG("failed to set data ready interrupt enabled bit");
return -EIO;
}