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:
Benjamin Björnsson 2022-06-16 20:52:17 +02:00 committed by Maureen Helm
commit 534988ffcf
2 changed files with 52 additions and 67 deletions

View file

@ -40,36 +40,32 @@ static int bmm150_set_power_mode(const struct device *dev,
enum bmm150_power_modes mode, enum bmm150_power_modes mode,
int state) int state)
{ {
struct bmm150_data *data = dev->data;
const struct bmm150_config *config = dev->config; const struct bmm150_config *config = dev->config;
switch (mode) { switch (mode) {
case BMM150_POWER_MODE_SUSPEND: case BMM150_POWER_MODE_SUSPEND:
if (i2c_reg_update_byte(data->i2c, if (i2c_reg_update_byte_dt(&config->i2c,
config->i2c_slave_addr, BMM150_REG_POWER,
BMM150_REG_POWER, BMM150_MASK_POWER_CTL,
BMM150_MASK_POWER_CTL, !state) < 0) {
!state) < 0) {
return -EIO; return -EIO;
} }
k_busy_wait(USEC_PER_MSEC * 5U); k_busy_wait(USEC_PER_MSEC * 5U);
return 0; return 0;
case BMM150_POWER_MODE_SLEEP: case BMM150_POWER_MODE_SLEEP:
return i2c_reg_update_byte(data->i2c, return i2c_reg_update_byte_dt(&config->i2c,
config->i2c_slave_addr, BMM150_REG_OPMODE_ODR,
BMM150_REG_OPMODE_ODR, BMM150_MASK_OPMODE,
BMM150_MASK_OPMODE, BMM150_MODE_SLEEP <<
BMM150_MODE_SLEEP << BMM150_SHIFT_OPMODE);
BMM150_SHIFT_OPMODE);
break; break;
case BMM150_POWER_MODE_NORMAL: case BMM150_POWER_MODE_NORMAL:
return i2c_reg_update_byte(data->i2c, return i2c_reg_update_byte_dt(&config->i2c,
config->i2c_slave_addr, BMM150_REG_OPMODE_ODR,
BMM150_REG_OPMODE_ODR, BMM150_MASK_OPMODE,
BMM150_MASK_OPMODE, BMM150_MODE_NORMAL <<
BMM150_MODE_NORMAL << BMM150_SHIFT_OPMODE);
BMM150_SHIFT_OPMODE);
break; break;
} }
@ -79,19 +75,16 @@ static int bmm150_set_power_mode(const struct device *dev,
static int bmm150_set_odr(const struct device *dev, uint8_t val) 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; const struct bmm150_config *config = dev->config;
uint8_t i; uint8_t i;
for (i = 0U; i < ARRAY_SIZE(bmm150_samp_freq_table); ++i) { for (i = 0U; i < ARRAY_SIZE(bmm150_samp_freq_table); ++i) {
if (val <= bmm150_samp_freq_table[i].freq) { if (val <= bmm150_samp_freq_table[i].freq) {
return i2c_reg_update_byte(data->i2c, return i2c_reg_update_byte_dt(&config->i2c,
config->i2c_slave_addr, BMM150_REG_OPMODE_ODR,
BMM150_REG_OPMODE_ODR, BMM150_MASK_ODR,
BMM150_MASK_ODR, (bmm150_samp_freq_table[i].reg_val <<
(bmm150_samp_freq_table[i]. BMM150_SHIFT_ODR));
reg_val <<
BMM150_SHIFT_ODR));
} }
} }
return -ENOTSUP; return -ENOTSUP;
@ -104,8 +97,8 @@ static int bmm150_read_rep_xy(const struct device *dev)
const struct bmm150_config *config = dev->config; const struct bmm150_config *config = dev->config;
uint8_t reg_val; 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, &reg_val) < 0) { BMM150_REG_REP_XY, &reg_val) < 0) {
return -EIO; return -EIO;
} }
@ -120,8 +113,8 @@ static int bmm150_read_rep_z(const struct device *dev)
const struct bmm150_config *config = dev->config; const struct bmm150_config *config = dev->config;
uint8_t reg_val; 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, &reg_val) < 0) { BMM150_REG_REP_Z, &reg_val) < 0) {
return -EIO; return -EIO;
} }
@ -167,8 +160,8 @@ static int bmm150_read_odr(const struct device *dev)
const struct bmm150_config *config = dev->config; const struct bmm150_config *config = dev->config;
uint8_t i, odr_val, reg_val; 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, &reg_val) < 0) { BMM150_REG_OPMODE_ODR, &reg_val) < 0) {
return -EIO; return -EIO;
} }
@ -191,10 +184,10 @@ static int bmm150_write_rep_xy(const struct device *dev, int val)
struct bmm150_data *data = dev->data; struct bmm150_data *data = dev->data;
const struct bmm150_config *config = dev->config; 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_XY,
BMM150_REG_REP_DATAMASK, BMM150_REG_REP_DATAMASK,
BMM150_REPXY_TO_REGVAL(val)) < 0) { BMM150_REPXY_TO_REGVAL(val)) < 0) {
return -EIO; return -EIO;
} }
@ -210,10 +203,10 @@ static int bmm150_write_rep_z(const struct device *dev, int val)
struct bmm150_data *data = dev->data; struct bmm150_data *data = dev->data;
const struct bmm150_config *config = dev->config; 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_Z,
BMM150_REG_REP_DATAMASK, BMM150_REG_REP_DATAMASK,
BMM150_REPZ_TO_REGVAL(val)) < 0) { BMM150_REPZ_TO_REGVAL(val)) < 0) {
return -EIO; return -EIO;
} }
@ -301,9 +294,9 @@ static int bmm150_sample_fetch(const struct device *dev,
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL ||
chan == SENSOR_CHAN_MAGN_XYZ); 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, BMM150_REG_X_L, (uint8_t *)values,
sizeof(values)) < 0) { sizeof(values)) < 0) {
LOG_ERR("failed to read sample"); LOG_ERR("failed to read sample");
return -EIO; return -EIO;
} }
@ -510,8 +503,8 @@ static int bmm150_init_chip(const struct device *dev)
return -EIO; 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) { BMM150_REG_CHIP_ID, &chip_id) < 0) {
LOG_ERR("failed reading chip id"); LOG_ERR("failed reading chip id");
goto err_poweroff; goto err_poweroff;
} }
@ -528,18 +521,18 @@ static int bmm150_init_chip(const struct device *dev)
goto err_poweroff; 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_REG_REP_XY,
BMM150_REPXY_TO_REGVAL(preset.rep_xy)) BMM150_REPXY_TO_REGVAL(preset.rep_xy))
< 0) { < 0) {
LOG_ERR("failed to set REP XY to %d", LOG_ERR("failed to set REP XY to %d",
preset.rep_xy); preset.rep_xy);
goto err_poweroff; 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_REG_REP_Z,
BMM150_REPZ_TO_REGVAL(preset.rep_z)) < 0) { BMM150_REPZ_TO_REGVAL(preset.rep_z)) < 0) {
LOG_ERR("failed to set REP Z to %d", LOG_ERR("failed to set REP Z to %d",
preset.rep_z); preset.rep_z);
goto err_poweroff; goto err_poweroff;
@ -550,9 +543,9 @@ static int bmm150_init_chip(const struct device *dev)
LOG_ERR("failed to power on device"); 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, BMM150_REG_TRIM_START, (uint8_t *)&data->tregs,
sizeof(data->tregs)) < 0) { sizeof(data->tregs)) < 0) {
LOG_ERR("failed to read trim regs"); LOG_ERR("failed to read trim regs");
goto err_poweroff; goto err_poweroff;
} }
@ -583,13 +576,10 @@ static int bmm150_init(const struct device *dev)
{ {
const struct bmm150_config *const config = const struct bmm150_config *const config =
dev->config; dev->config;
struct bmm150_data *data = dev->data;
data->i2c = device_get_binding(config->i2c_master_dev_name); if (!device_is_ready(config->i2c.bus)) {
if (!data->i2c) { LOG_ERR("I2C bus device not ready");
LOG_ERR("i2c master not found: %s", return -ENODEV;
config->i2c_master_dev_name);
return -EINVAL;
} }
if (bmm150_init_chip(dev) < 0) { 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 = { static const struct bmm150_config bmm150_config = {
.i2c_master_dev_name = DT_INST_BUS_LABEL(0), .i2c = I2C_DT_SPEC_INST_GET(0),
.i2c_slave_addr = BMM150_I2C_ADDR,
}; };
static struct bmm150_data bmm150_data; static struct bmm150_data bmm150_data;

View file

@ -82,8 +82,6 @@
#define BMM150_MASK_DRDY_LATCHING BIT(1) #define BMM150_MASK_DRDY_LATCHING BIT(1)
#define BMM150_MASK_DRDY_INT3_POLARITY BIT(0) #define BMM150_MASK_DRDY_INT3_POLARITY BIT(0)
#define BMM150_I2C_ADDR DT_INST_REG_ADDR(0)
#if defined(CONFIG_BMM150_SAMPLING_REP_XY) || \ #if defined(CONFIG_BMM150_SAMPLING_REP_XY) || \
defined(CONFIG_BMM150_SAMPLING_REP_Z) defined(CONFIG_BMM150_SAMPLING_REP_Z)
#define BMM150_SET_ATTR_REP #define BMM150_SET_ATTR_REP
@ -96,8 +94,7 @@
struct bmm150_config { struct bmm150_config {
char *i2c_master_dev_name; struct i2c_dt_spec i2c;
uint16_t i2c_slave_addr;
}; };
struct bmm150_trim_regs { struct bmm150_trim_regs {
@ -118,7 +115,6 @@ struct bmm150_trim_regs {
} __packed; } __packed;
struct bmm150_data { struct bmm150_data {
const struct device *i2c;
struct k_sem sem; struct k_sem sem;
struct bmm150_trim_regs tregs; struct bmm150_trim_regs tregs;
int rep_xy, rep_z, odr, max_odr; int rep_xy, rep_z, odr, max_odr;