drivers: sensor: fxas21002: 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-19 08:09:55 +02:00 committed by Carles Cufí
commit 5f4be73ba0
3 changed files with 26 additions and 44 deletions

View file

@ -36,8 +36,8 @@ static int fxas21002_sample_fetch(const struct device *dev,
k_sem_take(&data->sem, K_FOREVER); k_sem_take(&data->sem, K_FOREVER);
/* Read all the channels in one I2C transaction. */ /* Read all the channels in one I2C transaction. */
if (i2c_burst_read(data->i2c, config->i2c_address, if (i2c_burst_read_dt(&config->i2c, FXAS21002_REG_OUTXMSB, buffer,
FXAS21002_REG_OUTXMSB, buffer, sizeof(buffer))) { sizeof(buffer))) {
LOG_ERR("Could not fetch sample"); LOG_ERR("Could not fetch sample");
ret = -EIO; ret = -EIO;
goto exit; goto exit;
@ -137,12 +137,9 @@ static int fxas21002_channel_get(const struct device *dev,
int fxas21002_get_power(const struct device *dev, enum fxas21002_power *power) int fxas21002_get_power(const struct device *dev, enum fxas21002_power *power)
{ {
const struct fxas21002_config *config = dev->config; const struct fxas21002_config *config = dev->config;
struct fxas21002_data *data = dev->data;
uint8_t val = *power; uint8_t val = *power;
if (i2c_reg_read_byte(data->i2c, config->i2c_address, if (i2c_reg_read_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG1, &val)) {
FXAS21002_REG_CTRLREG1,
&val)) {
LOG_ERR("Could not get power setting"); LOG_ERR("Could not get power setting");
return -EIO; return -EIO;
} }
@ -155,12 +152,9 @@ int fxas21002_get_power(const struct device *dev, enum fxas21002_power *power)
int fxas21002_set_power(const struct device *dev, enum fxas21002_power power) int fxas21002_set_power(const struct device *dev, enum fxas21002_power power)
{ {
const struct fxas21002_config *config = dev->config; const struct fxas21002_config *config = dev->config;
struct fxas21002_data *data = dev->data;
return i2c_reg_update_byte(data->i2c, config->i2c_address, return i2c_reg_update_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG1,
FXAS21002_REG_CTRLREG1, FXAS21002_CTRLREG1_POWER_MASK, power);
FXAS21002_CTRLREG1_POWER_MASK,
power);
} }
uint32_t fxas21002_get_transition_time(enum fxas21002_power start, uint32_t fxas21002_get_transition_time(enum fxas21002_power start,
@ -196,19 +190,16 @@ static int fxas21002_init(const struct device *dev)
uint8_t whoami; uint8_t whoami;
uint8_t ctrlreg1; uint8_t ctrlreg1;
/* Get the I2C device */ if (!device_is_ready(config->i2c.bus)) {
data->i2c = device_get_binding(config->i2c_name); LOG_ERR("I2C bus device not ready");
if (data->i2c == NULL) { return -ENODEV;
LOG_ERR("Could not find I2C device");
return -EINVAL;
} }
/* Read the WHOAMI register to make sure we are talking to FXAS21002 /* Read the WHOAMI register to make sure we are talking to FXAS21002
* and not some other type of device that happens to have the same I2C * and not some other type of device that happens to have the same I2C
* address. * address.
*/ */
if (i2c_reg_read_byte(data->i2c, config->i2c_address, if (i2c_reg_read_byte_dt(&config->i2c, FXAS21002_REG_WHOAMI, &whoami)) {
FXAS21002_REG_WHOAMI, &whoami)) {
LOG_ERR("Could not get WHOAMI value"); LOG_ERR("Could not get WHOAMI value");
return -EIO; return -EIO;
} }
@ -224,32 +215,29 @@ static int fxas21002_init(const struct device *dev)
* acknowledgment (ACK) of the written byte to the master. Therefore, * acknowledgment (ACK) of the written byte to the master. Therefore,
* do not check the return code of the I2C transaction. * do not check the return code of the I2C transaction.
*/ */
i2c_reg_write_byte(data->i2c, config->i2c_address, i2c_reg_write_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG1,
FXAS21002_REG_CTRLREG1, FXAS21002_CTRLREG1_RST_MASK); FXAS21002_CTRLREG1_RST_MASK);
/* Wait for the reset sequence to complete */ /* Wait for the reset sequence to complete */
do { do {
if (i2c_reg_read_byte(data->i2c, config->i2c_address, if (i2c_reg_read_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG1,
FXAS21002_REG_CTRLREG1, &ctrlreg1)) { &ctrlreg1)) {
LOG_ERR("Could not get ctrlreg1 value"); LOG_ERR("Could not get ctrlreg1 value");
return -EIO; return -EIO;
} }
} while (ctrlreg1 & FXAS21002_CTRLREG1_RST_MASK); } while (ctrlreg1 & FXAS21002_CTRLREG1_RST_MASK);
/* Set the full-scale range */ /* Set the full-scale range */
if (i2c_reg_update_byte(data->i2c, config->i2c_address, if (i2c_reg_update_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG0,
FXAS21002_REG_CTRLREG0, FXAS21002_CTRLREG0_FS_MASK, config->range)) {
FXAS21002_CTRLREG0_FS_MASK,
config->range)) {
LOG_ERR("Could not set range"); LOG_ERR("Could not set range");
return -EIO; return -EIO;
} }
/* Set the output data rate */ /* Set the output data rate */
if (i2c_reg_update_byte(data->i2c, config->i2c_address, if (i2c_reg_update_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG1,
FXAS21002_REG_CTRLREG1, FXAS21002_CTRLREG1_DR_MASK,
FXAS21002_CTRLREG1_DR_MASK, config->dr << FXAS21002_CTRLREG1_DR_SHIFT)) {
config->dr << FXAS21002_CTRLREG1_DR_SHIFT)) {
LOG_ERR("Could not set output data rate"); LOG_ERR("Could not set output data rate");
return -EIO; return -EIO;
} }
@ -290,8 +278,7 @@ static const struct sensor_driver_api fxas21002_driver_api = {
}; };
static const struct fxas21002_config fxas21002_config = { static const struct fxas21002_config fxas21002_config = {
.i2c_name = DT_INST_BUS_LABEL(0), .i2c = I2C_DT_SPEC_INST_GET(0),
.i2c_address = DT_INST_REG_ADDR(0),
.whoami = CONFIG_FXAS21002_WHOAMI, .whoami = CONFIG_FXAS21002_WHOAMI,
.range = CONFIG_FXAS21002_RANGE, .range = CONFIG_FXAS21002_RANGE,
.dr = CONFIG_FXAS21002_DR, .dr = CONFIG_FXAS21002_DR,

View file

@ -57,20 +57,18 @@ enum fxas21002_channel {
}; };
struct fxas21002_config { struct fxas21002_config {
char *i2c_name; struct i2c_dt_spec i2c;
#ifdef CONFIG_FXAS21002_TRIGGER #ifdef CONFIG_FXAS21002_TRIGGER
char *gpio_name; char *gpio_name;
uint8_t gpio_pin; uint8_t gpio_pin;
gpio_dt_flags_t gpio_flags; gpio_dt_flags_t gpio_flags;
#endif #endif
uint8_t i2c_address;
uint8_t whoami; uint8_t whoami;
enum fxas21002_range range; enum fxas21002_range range;
uint8_t dr; uint8_t dr;
}; };
struct fxas21002_data { struct fxas21002_data {
const struct device *i2c;
struct k_sem sem; struct k_sem sem;
#ifdef CONFIG_FXAS21002_TRIGGER #ifdef CONFIG_FXAS21002_TRIGGER
const struct device *dev; const struct device *dev;

View file

@ -55,9 +55,8 @@ static void fxas21002_handle_int(const struct device *dev)
k_sem_take(&data->sem, K_FOREVER); k_sem_take(&data->sem, K_FOREVER);
if (i2c_reg_read_byte(data->i2c, config->i2c_address, if (i2c_reg_read_byte_dt(&config->i2c, FXAS21002_REG_INT_SOURCE,
FXAS21002_REG_INT_SOURCE, &int_source)) {
&int_source)) {
LOG_ERR("Could not read interrupt source"); LOG_ERR("Could not read interrupt source");
int_source = 0U; int_source = 0U;
} }
@ -134,10 +133,8 @@ int fxas21002_trigger_set(const struct device *dev,
} }
/* Configure the sensor interrupt */ /* Configure the sensor interrupt */
if (i2c_reg_update_byte(data->i2c, config->i2c_address, if (i2c_reg_update_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG2, mask,
FXAS21002_REG_CTRLREG2, handler ? mask : 0)) {
mask,
handler ? mask : 0)) {
LOG_ERR("Could not configure interrupt"); LOG_ERR("Could not configure interrupt");
ret = -EIO; ret = -EIO;
goto exit; goto exit;
@ -188,8 +185,8 @@ int fxas21002_trigger_init(const struct device *dev)
ctrl_reg2 |= FXAS21002_CTRLREG2_CFG_DRDY_MASK; ctrl_reg2 |= FXAS21002_CTRLREG2_CFG_DRDY_MASK;
#endif #endif
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG2,
FXAS21002_REG_CTRLREG2, ctrl_reg2)) { ctrl_reg2)) {
LOG_ERR("Could not configure interrupt pin routing"); LOG_ERR("Could not configure interrupt pin routing");
return -EIO; return -EIO;
} }