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:
parent
4243e8e99c
commit
5f4be73ba0
3 changed files with 26 additions and 44 deletions
|
@ -36,8 +36,8 @@ static int fxas21002_sample_fetch(const struct device *dev,
|
|||
k_sem_take(&data->sem, K_FOREVER);
|
||||
|
||||
/* Read all the channels in one I2C transaction. */
|
||||
if (i2c_burst_read(data->i2c, config->i2c_address,
|
||||
FXAS21002_REG_OUTXMSB, buffer, sizeof(buffer))) {
|
||||
if (i2c_burst_read_dt(&config->i2c, FXAS21002_REG_OUTXMSB, buffer,
|
||||
sizeof(buffer))) {
|
||||
LOG_ERR("Could not fetch sample");
|
||||
ret = -EIO;
|
||||
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)
|
||||
{
|
||||
const struct fxas21002_config *config = dev->config;
|
||||
struct fxas21002_data *data = dev->data;
|
||||
uint8_t val = *power;
|
||||
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_address,
|
||||
FXAS21002_REG_CTRLREG1,
|
||||
&val)) {
|
||||
if (i2c_reg_read_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG1, &val)) {
|
||||
LOG_ERR("Could not get power setting");
|
||||
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)
|
||||
{
|
||||
const struct fxas21002_config *config = dev->config;
|
||||
struct fxas21002_data *data = dev->data;
|
||||
|
||||
return i2c_reg_update_byte(data->i2c, config->i2c_address,
|
||||
FXAS21002_REG_CTRLREG1,
|
||||
FXAS21002_CTRLREG1_POWER_MASK,
|
||||
power);
|
||||
return i2c_reg_update_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG1,
|
||||
FXAS21002_CTRLREG1_POWER_MASK, power);
|
||||
}
|
||||
|
||||
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 ctrlreg1;
|
||||
|
||||
/* Get the I2C device */
|
||||
data->i2c = device_get_binding(config->i2c_name);
|
||||
if (data->i2c == NULL) {
|
||||
LOG_ERR("Could not find I2C device");
|
||||
return -EINVAL;
|
||||
if (!device_is_ready(config->i2c.bus)) {
|
||||
LOG_ERR("I2C bus device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* 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
|
||||
* address.
|
||||
*/
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_address,
|
||||
FXAS21002_REG_WHOAMI, &whoami)) {
|
||||
if (i2c_reg_read_byte_dt(&config->i2c, FXAS21002_REG_WHOAMI, &whoami)) {
|
||||
LOG_ERR("Could not get WHOAMI value");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -224,30 +215,27 @@ static int fxas21002_init(const struct device *dev)
|
|||
* acknowledgment (ACK) of the written byte to the master. Therefore,
|
||||
* do not check the return code of the I2C transaction.
|
||||
*/
|
||||
i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXAS21002_REG_CTRLREG1, FXAS21002_CTRLREG1_RST_MASK);
|
||||
i2c_reg_write_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG1,
|
||||
FXAS21002_CTRLREG1_RST_MASK);
|
||||
|
||||
/* Wait for the reset sequence to complete */
|
||||
do {
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_address,
|
||||
FXAS21002_REG_CTRLREG1, &ctrlreg1)) {
|
||||
if (i2c_reg_read_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG1,
|
||||
&ctrlreg1)) {
|
||||
LOG_ERR("Could not get ctrlreg1 value");
|
||||
return -EIO;
|
||||
}
|
||||
} while (ctrlreg1 & FXAS21002_CTRLREG1_RST_MASK);
|
||||
|
||||
/* Set the full-scale range */
|
||||
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
|
||||
FXAS21002_REG_CTRLREG0,
|
||||
FXAS21002_CTRLREG0_FS_MASK,
|
||||
config->range)) {
|
||||
if (i2c_reg_update_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG0,
|
||||
FXAS21002_CTRLREG0_FS_MASK, config->range)) {
|
||||
LOG_ERR("Could not set range");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
/* Set the output data rate */
|
||||
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
|
||||
FXAS21002_REG_CTRLREG1,
|
||||
if (i2c_reg_update_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG1,
|
||||
FXAS21002_CTRLREG1_DR_MASK,
|
||||
config->dr << FXAS21002_CTRLREG1_DR_SHIFT)) {
|
||||
LOG_ERR("Could not set output data rate");
|
||||
|
@ -290,8 +278,7 @@ static const struct sensor_driver_api fxas21002_driver_api = {
|
|||
};
|
||||
|
||||
static const struct fxas21002_config fxas21002_config = {
|
||||
.i2c_name = DT_INST_BUS_LABEL(0),
|
||||
.i2c_address = DT_INST_REG_ADDR(0),
|
||||
.i2c = I2C_DT_SPEC_INST_GET(0),
|
||||
.whoami = CONFIG_FXAS21002_WHOAMI,
|
||||
.range = CONFIG_FXAS21002_RANGE,
|
||||
.dr = CONFIG_FXAS21002_DR,
|
||||
|
|
|
@ -57,20 +57,18 @@ enum fxas21002_channel {
|
|||
};
|
||||
|
||||
struct fxas21002_config {
|
||||
char *i2c_name;
|
||||
struct i2c_dt_spec i2c;
|
||||
#ifdef CONFIG_FXAS21002_TRIGGER
|
||||
char *gpio_name;
|
||||
uint8_t gpio_pin;
|
||||
gpio_dt_flags_t gpio_flags;
|
||||
#endif
|
||||
uint8_t i2c_address;
|
||||
uint8_t whoami;
|
||||
enum fxas21002_range range;
|
||||
uint8_t dr;
|
||||
};
|
||||
|
||||
struct fxas21002_data {
|
||||
const struct device *i2c;
|
||||
struct k_sem sem;
|
||||
#ifdef CONFIG_FXAS21002_TRIGGER
|
||||
const struct device *dev;
|
||||
|
|
|
@ -55,8 +55,7 @@ static void fxas21002_handle_int(const struct device *dev)
|
|||
|
||||
k_sem_take(&data->sem, K_FOREVER);
|
||||
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_address,
|
||||
FXAS21002_REG_INT_SOURCE,
|
||||
if (i2c_reg_read_byte_dt(&config->i2c, FXAS21002_REG_INT_SOURCE,
|
||||
&int_source)) {
|
||||
LOG_ERR("Could not read interrupt source");
|
||||
int_source = 0U;
|
||||
|
@ -134,9 +133,7 @@ int fxas21002_trigger_set(const struct device *dev,
|
|||
}
|
||||
|
||||
/* Configure the sensor interrupt */
|
||||
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
|
||||
FXAS21002_REG_CTRLREG2,
|
||||
mask,
|
||||
if (i2c_reg_update_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG2, mask,
|
||||
handler ? mask : 0)) {
|
||||
LOG_ERR("Could not configure interrupt");
|
||||
ret = -EIO;
|
||||
|
@ -188,8 +185,8 @@ int fxas21002_trigger_init(const struct device *dev)
|
|||
ctrl_reg2 |= FXAS21002_CTRLREG2_CFG_DRDY_MASK;
|
||||
#endif
|
||||
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXAS21002_REG_CTRLREG2, ctrl_reg2)) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXAS21002_REG_CTRLREG2,
|
||||
ctrl_reg2)) {
|
||||
LOG_ERR("Could not configure interrupt pin routing");
|
||||
return -EIO;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue