drivers: sensor: bmm150: 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:
parent
d63c2c44b5
commit
534988ffcf
2 changed files with 52 additions and 67 deletions
|
@ -40,13 +40,11 @@ static int bmm150_set_power_mode(const struct device *dev,
|
|||
enum bmm150_power_modes mode,
|
||||
int state)
|
||||
{
|
||||
struct bmm150_data *data = dev->data;
|
||||
const struct bmm150_config *config = dev->config;
|
||||
|
||||
switch (mode) {
|
||||
case BMM150_POWER_MODE_SUSPEND:
|
||||
if (i2c_reg_update_byte(data->i2c,
|
||||
config->i2c_slave_addr,
|
||||
if (i2c_reg_update_byte_dt(&config->i2c,
|
||||
BMM150_REG_POWER,
|
||||
BMM150_MASK_POWER_CTL,
|
||||
!state) < 0) {
|
||||
|
@ -56,16 +54,14 @@ static int bmm150_set_power_mode(const struct device *dev,
|
|||
|
||||
return 0;
|
||||
case BMM150_POWER_MODE_SLEEP:
|
||||
return i2c_reg_update_byte(data->i2c,
|
||||
config->i2c_slave_addr,
|
||||
return i2c_reg_update_byte_dt(&config->i2c,
|
||||
BMM150_REG_OPMODE_ODR,
|
||||
BMM150_MASK_OPMODE,
|
||||
BMM150_MODE_SLEEP <<
|
||||
BMM150_SHIFT_OPMODE);
|
||||
break;
|
||||
case BMM150_POWER_MODE_NORMAL:
|
||||
return i2c_reg_update_byte(data->i2c,
|
||||
config->i2c_slave_addr,
|
||||
return i2c_reg_update_byte_dt(&config->i2c,
|
||||
BMM150_REG_OPMODE_ODR,
|
||||
BMM150_MASK_OPMODE,
|
||||
BMM150_MODE_NORMAL <<
|
||||
|
@ -79,18 +75,15 @@ static int bmm150_set_power_mode(const struct device *dev,
|
|||
|
||||
static int bmm150_set_odr(const struct device *dev, uint8_t val)
|
||||
{
|
||||
struct bmm150_data *data = dev->data;
|
||||
const struct bmm150_config *config = dev->config;
|
||||
uint8_t i;
|
||||
|
||||
for (i = 0U; i < ARRAY_SIZE(bmm150_samp_freq_table); ++i) {
|
||||
if (val <= bmm150_samp_freq_table[i].freq) {
|
||||
return i2c_reg_update_byte(data->i2c,
|
||||
config->i2c_slave_addr,
|
||||
return i2c_reg_update_byte_dt(&config->i2c,
|
||||
BMM150_REG_OPMODE_ODR,
|
||||
BMM150_MASK_ODR,
|
||||
(bmm150_samp_freq_table[i].
|
||||
reg_val <<
|
||||
(bmm150_samp_freq_table[i].reg_val <<
|
||||
BMM150_SHIFT_ODR));
|
||||
}
|
||||
}
|
||||
|
@ -104,7 +97,7 @@ static int bmm150_read_rep_xy(const struct device *dev)
|
|||
const struct bmm150_config *config = dev->config;
|
||||
uint8_t reg_val;
|
||||
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_slave_addr,
|
||||
if (i2c_reg_read_byte_dt(&config->i2c,
|
||||
BMM150_REG_REP_XY, ®_val) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -120,7 +113,7 @@ static int bmm150_read_rep_z(const struct device *dev)
|
|||
const struct bmm150_config *config = dev->config;
|
||||
uint8_t reg_val;
|
||||
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_slave_addr,
|
||||
if (i2c_reg_read_byte_dt(&config->i2c,
|
||||
BMM150_REG_REP_Z, ®_val) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -167,7 +160,7 @@ static int bmm150_read_odr(const struct device *dev)
|
|||
const struct bmm150_config *config = dev->config;
|
||||
uint8_t i, odr_val, reg_val;
|
||||
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_slave_addr,
|
||||
if (i2c_reg_read_byte_dt(&config->i2c,
|
||||
BMM150_REG_OPMODE_ODR, ®_val) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -191,7 +184,7 @@ static int bmm150_write_rep_xy(const struct device *dev, int val)
|
|||
struct bmm150_data *data = dev->data;
|
||||
const struct bmm150_config *config = dev->config;
|
||||
|
||||
if (i2c_reg_update_byte(data->i2c, config->i2c_slave_addr,
|
||||
if (i2c_reg_update_byte_dt(&config->i2c,
|
||||
BMM150_REG_REP_XY,
|
||||
BMM150_REG_REP_DATAMASK,
|
||||
BMM150_REPXY_TO_REGVAL(val)) < 0) {
|
||||
|
@ -210,7 +203,7 @@ static int bmm150_write_rep_z(const struct device *dev, int val)
|
|||
struct bmm150_data *data = dev->data;
|
||||
const struct bmm150_config *config = dev->config;
|
||||
|
||||
if (i2c_reg_update_byte(data->i2c, config->i2c_slave_addr,
|
||||
if (i2c_reg_update_byte_dt(&config->i2c,
|
||||
BMM150_REG_REP_Z,
|
||||
BMM150_REG_REP_DATAMASK,
|
||||
BMM150_REPZ_TO_REGVAL(val)) < 0) {
|
||||
|
@ -301,7 +294,7 @@ static int bmm150_sample_fetch(const struct device *dev,
|
|||
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL ||
|
||||
chan == SENSOR_CHAN_MAGN_XYZ);
|
||||
|
||||
if (i2c_burst_read(drv_data->i2c, config->i2c_slave_addr,
|
||||
if (i2c_burst_read_dt(&config->i2c,
|
||||
BMM150_REG_X_L, (uint8_t *)values,
|
||||
sizeof(values)) < 0) {
|
||||
LOG_ERR("failed to read sample");
|
||||
|
@ -510,7 +503,7 @@ static int bmm150_init_chip(const struct device *dev)
|
|||
return -EIO;
|
||||
}
|
||||
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_slave_addr,
|
||||
if (i2c_reg_read_byte_dt(&config->i2c,
|
||||
BMM150_REG_CHIP_ID, &chip_id) < 0) {
|
||||
LOG_ERR("failed reading chip id");
|
||||
goto err_poweroff;
|
||||
|
@ -528,7 +521,7 @@ static int bmm150_init_chip(const struct device *dev)
|
|||
goto err_poweroff;
|
||||
}
|
||||
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_slave_addr,
|
||||
if (i2c_reg_write_byte_dt(&config->i2c,
|
||||
BMM150_REG_REP_XY,
|
||||
BMM150_REPXY_TO_REGVAL(preset.rep_xy))
|
||||
< 0) {
|
||||
|
@ -537,7 +530,7 @@ static int bmm150_init_chip(const struct device *dev)
|
|||
goto err_poweroff;
|
||||
}
|
||||
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_slave_addr,
|
||||
if (i2c_reg_write_byte_dt(&config->i2c,
|
||||
BMM150_REG_REP_Z,
|
||||
BMM150_REPZ_TO_REGVAL(preset.rep_z)) < 0) {
|
||||
LOG_ERR("failed to set REP Z to %d",
|
||||
|
@ -550,7 +543,7 @@ static int bmm150_init_chip(const struct device *dev)
|
|||
LOG_ERR("failed to power on device");
|
||||
}
|
||||
|
||||
if (i2c_burst_read(data->i2c, config->i2c_slave_addr,
|
||||
if (i2c_burst_read_dt(&config->i2c,
|
||||
BMM150_REG_TRIM_START, (uint8_t *)&data->tregs,
|
||||
sizeof(data->tregs)) < 0) {
|
||||
LOG_ERR("failed to read trim regs");
|
||||
|
@ -583,13 +576,10 @@ static int bmm150_init(const struct device *dev)
|
|||
{
|
||||
const struct bmm150_config *const config =
|
||||
dev->config;
|
||||
struct bmm150_data *data = dev->data;
|
||||
|
||||
data->i2c = device_get_binding(config->i2c_master_dev_name);
|
||||
if (!data->i2c) {
|
||||
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 (bmm150_init_chip(dev) < 0) {
|
||||
|
@ -601,8 +591,7 @@ static int bmm150_init(const struct device *dev)
|
|||
}
|
||||
|
||||
static const struct bmm150_config bmm150_config = {
|
||||
.i2c_master_dev_name = DT_INST_BUS_LABEL(0),
|
||||
.i2c_slave_addr = BMM150_I2C_ADDR,
|
||||
.i2c = I2C_DT_SPEC_INST_GET(0),
|
||||
};
|
||||
|
||||
static struct bmm150_data bmm150_data;
|
||||
|
|
|
@ -82,8 +82,6 @@
|
|||
#define BMM150_MASK_DRDY_LATCHING BIT(1)
|
||||
#define BMM150_MASK_DRDY_INT3_POLARITY BIT(0)
|
||||
|
||||
#define BMM150_I2C_ADDR DT_INST_REG_ADDR(0)
|
||||
|
||||
#if defined(CONFIG_BMM150_SAMPLING_REP_XY) || \
|
||||
defined(CONFIG_BMM150_SAMPLING_REP_Z)
|
||||
#define BMM150_SET_ATTR_REP
|
||||
|
@ -96,8 +94,7 @@
|
|||
|
||||
|
||||
struct bmm150_config {
|
||||
char *i2c_master_dev_name;
|
||||
uint16_t i2c_slave_addr;
|
||||
struct i2c_dt_spec i2c;
|
||||
};
|
||||
|
||||
struct bmm150_trim_regs {
|
||||
|
@ -118,7 +115,6 @@ struct bmm150_trim_regs {
|
|||
} __packed;
|
||||
|
||||
struct bmm150_data {
|
||||
const struct device *i2c;
|
||||
struct k_sem sem;
|
||||
struct bmm150_trim_regs tregs;
|
||||
int rep_xy, rep_z, odr, max_odr;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue