drivers: sensor: fxos8700: 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:47:34 +02:00 committed by Carles Cufí
commit 47b3e65290
3 changed files with 74 additions and 104 deletions

View file

@ -22,7 +22,6 @@ static int fxos8700_set_odr(const struct device *dev,
const struct sensor_value *val) const struct sensor_value *val)
{ {
const struct fxos8700_config *config = dev->config; const struct fxos8700_config *config = dev->config;
struct fxos8700_data *data = dev->data;
uint8_t dr; uint8_t dr;
enum fxos8700_power power; enum fxos8700_power power;
@ -87,10 +86,9 @@ static int fxos8700_set_odr(const struct device *dev,
} }
/* Change the attribute and restore power mode. */ /* Change the attribute and restore power mode. */
return i2c_reg_update_byte(data->i2c, config->i2c_address, return i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_CTRLREG1,
FXOS8700_REG_CTRLREG1, FXOS8700_CTRLREG1_DR_MASK | FXOS8700_CTRLREG1_ACTIVE_MASK,
FXOS8700_CTRLREG1_DR_MASK | FXOS8700_CTRLREG1_ACTIVE_MASK, dr | power);
dr | power);
} }
static int fxos8700_set_mt_ths(const struct device *dev, static int fxos8700_set_mt_ths(const struct device *dev,
@ -98,7 +96,6 @@ static int fxos8700_set_mt_ths(const struct device *dev,
{ {
#ifdef CONFIG_FXOS8700_MOTION #ifdef CONFIG_FXOS8700_MOTION
const struct fxos8700_config *config = dev->config; const struct fxos8700_config *config = dev->config;
struct fxos8700_data *data = dev->data;
uint64_t micro_ms2 = abs(val->val1 * 1000000LL + val->val2); uint64_t micro_ms2 = abs(val->val1 * 1000000LL + val->val2);
uint64_t ths = micro_ms2 / FXOS8700_FF_MT_THS_SCALE; uint64_t ths = micro_ms2 / FXOS8700_FF_MT_THS_SCALE;
@ -109,9 +106,8 @@ static int fxos8700_set_mt_ths(const struct device *dev,
LOG_DBG("Set FF_MT_THS to %d", (uint8_t)ths); LOG_DBG("Set FF_MT_THS to %d", (uint8_t)ths);
return i2c_reg_update_byte(data->i2c, config->i2c_address, return i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_FF_MT_THS,
FXOS8700_REG_FF_MT_THS, FXOS8700_FF_MT_THS_MASK, (uint8_t)ths);
FXOS8700_FF_MT_THS_MASK, (uint8_t)ths);
#else #else
return -ENOTSUP; return -ENOTSUP;
#endif #endif
@ -163,8 +159,8 @@ static int fxos8700_sample_fetch(const struct device *dev,
__ASSERT(num_bytes <= sizeof(buffer), "Too many bytes to read"); __ASSERT(num_bytes <= sizeof(buffer), "Too many bytes to read");
if (i2c_burst_read(data->i2c, config->i2c_address, config->start_addr, if (i2c_burst_read_dt(&config->i2c, config->start_addr, buffer,
buffer, num_bytes)) { num_bytes)) {
LOG_ERR("Could not fetch sample"); LOG_ERR("Could not fetch sample");
ret = -EIO; ret = -EIO;
goto exit; goto exit;
@ -185,8 +181,8 @@ static int fxos8700_sample_fetch(const struct device *dev,
} }
#ifdef CONFIG_FXOS8700_TEMP #ifdef CONFIG_FXOS8700_TEMP
if (i2c_reg_read_byte(data->i2c, config->i2c_address, FXOS8700_REG_TEMP, if (i2c_reg_read_byte_dt(&config->i2c, FXOS8700_REG_TEMP,
&data->temp)) { &data->temp)) {
LOG_ERR("Could not fetch temperature"); LOG_ERR("Could not fetch temperature");
ret = -EIO; ret = -EIO;
goto exit; goto exit;
@ -366,12 +362,9 @@ static int fxos8700_channel_get(const struct device *dev,
int fxos8700_get_power(const struct device *dev, enum fxos8700_power *power) int fxos8700_get_power(const struct device *dev, enum fxos8700_power *power)
{ {
const struct fxos8700_config *config = dev->config; const struct fxos8700_config *config = dev->config;
struct fxos8700_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, FXOS8700_REG_CTRLREG1, &val)) {
FXOS8700_REG_CTRLREG1,
&val)) {
LOG_ERR("Could not get power setting"); LOG_ERR("Could not get power setting");
return -EIO; return -EIO;
} }
@ -384,12 +377,9 @@ int fxos8700_get_power(const struct device *dev, enum fxos8700_power *power)
int fxos8700_set_power(const struct device *dev, enum fxos8700_power power) int fxos8700_set_power(const struct device *dev, enum fxos8700_power power)
{ {
const struct fxos8700_config *config = dev->config; const struct fxos8700_config *config = dev->config;
struct fxos8700_data *data = dev->data;
return i2c_reg_update_byte(data->i2c, config->i2c_address, return i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_CTRLREG1,
FXOS8700_REG_CTRLREG1, FXOS8700_CTRLREG1_ACTIVE_MASK, power);
FXOS8700_CTRLREG1_ACTIVE_MASK,
power);
} }
static int fxos8700_init(const struct device *dev) static int fxos8700_init(const struct device *dev)
@ -399,11 +389,9 @@ static int fxos8700_init(const struct device *dev)
struct sensor_value odr = {.val1 = 6, .val2 = 250000}; struct sensor_value odr = {.val1 = 6, .val2 = 250000};
const struct device *rst; const struct device *rst;
/* 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;
} }
if (config->reset_name) { if (config->reset_name) {
@ -434,9 +422,8 @@ static int fxos8700_init(const struct device *dev)
* master. Therefore, do not check the return code of * master. Therefore, do not check the return code of
* the I2C transaction. * the I2C transaction.
*/ */
i2c_reg_write_byte(data->i2c, config->i2c_address, i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_CTRLREG2,
FXOS8700_REG_CTRLREG2, FXOS8700_CTRLREG2_RST_MASK);
FXOS8700_CTRLREG2_RST_MASK);
} }
/* The sensor requires us to wait 1 ms after a reset before /* The sensor requires us to wait 1 ms after a reset before
@ -449,8 +436,8 @@ static int fxos8700_init(const struct device *dev)
* compatible device and not some other type of device that happens to * compatible device and not some other type of device that happens to
* have the same I2C address. * have the same I2C address.
*/ */
if (i2c_reg_read_byte(data->i2c, config->i2c_address, if (i2c_reg_read_byte_dt(&config->i2c, FXOS8700_REG_WHOAMI,
FXOS8700_REG_WHOAMI, &data->whoami)) { &data->whoami)) {
LOG_ERR("Could not get WHOAMI value"); LOG_ERR("Could not get WHOAMI value");
return -EIO; return -EIO;
} }
@ -479,19 +466,17 @@ static int fxos8700_init(const struct device *dev)
return -EIO; return -EIO;
} }
if (i2c_reg_update_byte(data->i2c, config->i2c_address, if (i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_CTRLREG2,
FXOS8700_REG_CTRLREG2, FXOS8700_CTRLREG2_MODS_MASK,
FXOS8700_CTRLREG2_MODS_MASK, config->power_mode)) {
config->power_mode)) {
LOG_ERR("Could not set power scheme"); LOG_ERR("Could not set power scheme");
return -EIO; return -EIO;
} }
/* Set the mode (accel-only, mag-only, or hybrid) */ /* Set the mode (accel-only, mag-only, or hybrid) */
if (i2c_reg_update_byte(data->i2c, config->i2c_address, if (i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_M_CTRLREG1,
FXOS8700_REG_M_CTRLREG1, FXOS8700_M_CTRLREG1_MODE_MASK,
FXOS8700_M_CTRLREG1_MODE_MASK, config->mode)) {
config->mode)) {
LOG_ERR("Could not set mode"); LOG_ERR("Could not set mode");
return -EIO; return -EIO;
} }
@ -499,19 +484,17 @@ static int fxos8700_init(const struct device *dev)
/* Set hybrid autoincrement so we can read accel and mag channels in /* Set hybrid autoincrement so we can read accel and mag channels in
* one I2C transaction. * one I2C transaction.
*/ */
if (i2c_reg_update_byte(data->i2c, config->i2c_address, if (i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_M_CTRLREG2,
FXOS8700_REG_M_CTRLREG2, FXOS8700_M_CTRLREG2_AUTOINC_MASK,
FXOS8700_M_CTRLREG2_AUTOINC_MASK, FXOS8700_M_CTRLREG2_AUTOINC_MASK)) {
FXOS8700_M_CTRLREG2_AUTOINC_MASK)) {
LOG_ERR("Could not set hybrid autoincrement"); LOG_ERR("Could not set hybrid autoincrement");
return -EIO; return -EIO;
} }
/* 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, FXOS8700_REG_XYZ_DATA_CFG,
FXOS8700_REG_XYZ_DATA_CFG, FXOS8700_XYZ_DATA_CFG_FS_MASK,
FXOS8700_XYZ_DATA_CFG_FS_MASK, RANGE2FS(config->range))) {
RANGE2FS(config->range))) {
LOG_ERR("Could not set range"); LOG_ERR("Could not set range");
return -EIO; return -EIO;
} }
@ -622,8 +605,7 @@ static const struct sensor_driver_api fxos8700_driver_api = {
#define FXOS8700_INIT(n) \ #define FXOS8700_INIT(n) \
static const struct fxos8700_config fxos8700_config_##n = { \ static const struct fxos8700_config fxos8700_config_##n = { \
.i2c_name = DT_INST_BUS_LABEL(n), \ .i2c = I2C_DT_SPEC_INST_GET(n), \
.i2c_address = DT_INST_REG_ADDR(n), \
.power_mode = DT_INST_PROP(n, power_mode), \ .power_mode = DT_INST_PROP(n, power_mode), \
.range = DT_INST_PROP(n, range), \ .range = DT_INST_PROP(n, range), \
FXOS8700_RESET(n) \ FXOS8700_RESET(n) \

View file

@ -125,13 +125,12 @@ enum fxos_trigger_type {
}; };
struct fxos8700_config { struct fxos8700_config {
char *i2c_name; struct i2c_dt_spec i2c;
#ifdef CONFIG_FXOS8700_TRIGGER #ifdef CONFIG_FXOS8700_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;
char *reset_name; char *reset_name;
uint8_t reset_pin; uint8_t reset_pin;
gpio_dt_flags_t reset_flags; gpio_dt_flags_t reset_flags;
@ -155,7 +154,6 @@ struct fxos8700_config {
}; };
struct fxos8700_data { struct fxos8700_data {
const struct device *i2c;
struct k_sem sem; struct k_sem sem;
#ifdef CONFIG_FXOS8700_TRIGGER #ifdef CONFIG_FXOS8700_TRIGGER
const struct device *dev; const struct device *dev;

View file

@ -61,9 +61,8 @@ static int fxos8700_handle_pulse_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, FXOS8700_REG_PULSE_SRC,
FXOS8700_REG_PULSE_SRC, &pulse_source)) {
&pulse_source)) {
LOG_ERR("Could not read pulse source"); LOG_ERR("Could not read pulse source");
} }
@ -99,9 +98,8 @@ static int fxos8700_handle_motion_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, FXOS8700_REG_FF_MT_SRC,
FXOS8700_REG_FF_MT_SRC, &motion_source)) {
&motion_source)) {
LOG_ERR("Could not read pulse source"); LOG_ERR("Could not read pulse source");
} }
@ -143,9 +141,8 @@ static void fxos8700_handle_int(const struct device *dev)
/* Interrupt status register */ /* Interrupt status register */
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, FXOS8700_REG_INT_SOURCE,
FXOS8700_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;
} }
@ -169,9 +166,8 @@ static void fxos8700_handle_int(const struct device *dev)
/* Magnetometer interrupt source register */ /* Magnetometer interrupt source register */
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, FXOS8700_REG_M_INT_SRC,
FXOS8700_REG_M_INT_SRC, &int_source)) {
&int_source)) {
LOG_ERR("Could not read magnetometer interrupt source"); LOG_ERR("Could not read magnetometer interrupt source");
int_source = 0U; int_source = 0U;
} }
@ -270,10 +266,8 @@ int fxos8700_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, FXOS8700_REG_CTRLREG4, mask,
FXOS8700_REG_CTRLREG4, 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;
@ -296,40 +290,39 @@ exit:
static int fxos8700_pulse_init(const struct device *dev) static int fxos8700_pulse_init(const struct device *dev)
{ {
const struct fxos8700_config *config = dev->config; const struct fxos8700_config *config = dev->config;
struct fxos8700_data *data = dev->data;
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_CFG,
FXOS8700_REG_PULSE_CFG, config->pulse_cfg)) { config->pulse_cfg)) {
return -EIO; return -EIO;
} }
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_THSX,
FXOS8700_REG_PULSE_THSX, config->pulse_ths[0])) { config->pulse_ths[0])) {
return -EIO; return -EIO;
} }
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_THSY,
FXOS8700_REG_PULSE_THSY, config->pulse_ths[1])) { config->pulse_ths[1])) {
return -EIO; return -EIO;
} }
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_THSZ,
FXOS8700_REG_PULSE_THSZ, config->pulse_ths[2])) { config->pulse_ths[2])) {
return -EIO; return -EIO;
} }
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_TMLT,
FXOS8700_REG_PULSE_TMLT, config->pulse_tmlt)) { config->pulse_tmlt)) {
return -EIO; return -EIO;
} }
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_LTCY,
FXOS8700_REG_PULSE_LTCY, config->pulse_ltcy)) { config->pulse_ltcy)) {
return -EIO; return -EIO;
} }
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_WIND,
FXOS8700_REG_PULSE_WIND, config->pulse_wind)) { config->pulse_wind)) {
return -EIO; return -EIO;
} }
@ -341,23 +334,21 @@ static int fxos8700_pulse_init(const struct device *dev)
static int fxos8700_motion_init(const struct device *dev) static int fxos8700_motion_init(const struct device *dev)
{ {
const struct fxos8700_config *config = dev->config; const struct fxos8700_config *config = dev->config;
struct fxos8700_data *data = dev->data;
/* Set Mode 4, Motion detection with ELE = 1, OAE = 1 */ /* Set Mode 4, Motion detection with ELE = 1, OAE = 1 */
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c,
FXOS8700_REG_FF_MT_CFG, FXOS8700_REG_FF_MT_CFG,
FXOS8700_FF_MT_CFG_ELE | FXOS8700_FF_MT_CFG_ELE |
FXOS8700_FF_MT_CFG_OAE | FXOS8700_FF_MT_CFG_OAE |
FXOS8700_FF_MT_CFG_ZEFE | FXOS8700_FF_MT_CFG_ZEFE |
FXOS8700_FF_MT_CFG_YEFE | FXOS8700_FF_MT_CFG_YEFE |
FXOS8700_FF_MT_CFG_XEFE)) { FXOS8700_FF_MT_CFG_XEFE)) {
return -EIO; return -EIO;
} }
/* Set motion threshold to maximimum */ /* Set motion threshold to maximimum */
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_FF_MT_THS,
FXOS8700_REG_FF_MT_THS, FXOS8700_REG_FF_MT_THS)) {
FXOS8700_REG_FF_MT_THS)) {
return -EIO; return -EIO;
} }
@ -369,7 +360,6 @@ static int fxos8700_motion_init(const struct device *dev)
static int fxos8700_m_vecm_init(const struct device *dev) static int fxos8700_m_vecm_init(const struct device *dev)
{ {
const struct fxos8700_config *config = dev->config; const struct fxos8700_config *config = dev->config;
struct fxos8700_data *data = dev->data;
uint8_t m_vecm_cfg = config->mag_vecm_cfg; uint8_t m_vecm_cfg = config->mag_vecm_cfg;
/* Route the interrupt to INT1 pin */ /* Route the interrupt to INT1 pin */
@ -378,8 +368,8 @@ static int fxos8700_m_vecm_init(const struct device *dev)
#endif #endif
/* Set magnetic vector-magnitude function */ /* Set magnetic vector-magnitude function */
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_M_VECM_CFG,
FXOS8700_REG_M_VECM_CFG, m_vecm_cfg)) { m_vecm_cfg)) {
LOG_ERR("Could not set magnetic vector-magnitude function"); LOG_ERR("Could not set magnetic vector-magnitude function");
return -EIO; return -EIO;
} }
@ -387,14 +377,14 @@ static int fxos8700_m_vecm_init(const struct device *dev)
/* Set magnetic vector-magnitude function threshold values: /* Set magnetic vector-magnitude function threshold values:
* handle both MSB and LSB registers * handle both MSB and LSB registers
*/ */
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_M_VECM_THS_MSB,
FXOS8700_REG_M_VECM_THS_MSB, config->mag_vecm_ths[0])) { config->mag_vecm_ths[0])) {
LOG_ERR("Could not set magnetic vector-magnitude function threshold MSB value"); LOG_ERR("Could not set magnetic vector-magnitude function threshold MSB value");
return -EIO; return -EIO;
} }
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_M_VECM_THS_LSB,
FXOS8700_REG_M_VECM_THS_LSB, config->mag_vecm_ths[1])) { config->mag_vecm_ths[1])) {
LOG_ERR("Could not set magnetic vector-magnitude function threshold LSB value"); LOG_ERR("Could not set magnetic vector-magnitude function threshold LSB value");
return -EIO; return -EIO;
} }
@ -436,8 +426,8 @@ int fxos8700_trigger_init(const struct device *dev)
ctrl_reg5 |= FXOS8700_MOTION_MASK; ctrl_reg5 |= FXOS8700_MOTION_MASK;
#endif #endif
if (i2c_reg_write_byte(data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_CTRLREG5,
FXOS8700_REG_CTRLREG5, ctrl_reg5)) { ctrl_reg5)) {
LOG_ERR("Could not configure interrupt pin routing"); LOG_ERR("Could not configure interrupt pin routing");
return -EIO; return -EIO;
} }