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:
parent
1e44af1e2c
commit
47b3e65290
3 changed files with 74 additions and 104 deletions
|
@ -22,7 +22,6 @@ static int fxos8700_set_odr(const struct device *dev,
|
|||
const struct sensor_value *val)
|
||||
{
|
||||
const struct fxos8700_config *config = dev->config;
|
||||
struct fxos8700_data *data = dev->data;
|
||||
uint8_t dr;
|
||||
enum fxos8700_power power;
|
||||
|
||||
|
@ -87,8 +86,7 @@ static int fxos8700_set_odr(const struct device *dev,
|
|||
}
|
||||
|
||||
/* Change the attribute and restore power mode. */
|
||||
return i2c_reg_update_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_CTRLREG1,
|
||||
return i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_CTRLREG1,
|
||||
FXOS8700_CTRLREG1_DR_MASK | FXOS8700_CTRLREG1_ACTIVE_MASK,
|
||||
dr | power);
|
||||
}
|
||||
|
@ -98,7 +96,6 @@ static int fxos8700_set_mt_ths(const struct device *dev,
|
|||
{
|
||||
#ifdef CONFIG_FXOS8700_MOTION
|
||||
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 ths = micro_ms2 / FXOS8700_FF_MT_THS_SCALE;
|
||||
|
||||
|
@ -109,8 +106,7 @@ static int fxos8700_set_mt_ths(const struct device *dev,
|
|||
|
||||
LOG_DBG("Set FF_MT_THS to %d", (uint8_t)ths);
|
||||
|
||||
return i2c_reg_update_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_FF_MT_THS,
|
||||
return i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_FF_MT_THS,
|
||||
FXOS8700_FF_MT_THS_MASK, (uint8_t)ths);
|
||||
#else
|
||||
return -ENOTSUP;
|
||||
|
@ -163,8 +159,8 @@ static int fxos8700_sample_fetch(const struct device *dev,
|
|||
|
||||
__ASSERT(num_bytes <= sizeof(buffer), "Too many bytes to read");
|
||||
|
||||
if (i2c_burst_read(data->i2c, config->i2c_address, config->start_addr,
|
||||
buffer, num_bytes)) {
|
||||
if (i2c_burst_read_dt(&config->i2c, config->start_addr, buffer,
|
||||
num_bytes)) {
|
||||
LOG_ERR("Could not fetch sample");
|
||||
ret = -EIO;
|
||||
goto exit;
|
||||
|
@ -185,7 +181,7 @@ static int fxos8700_sample_fetch(const struct device *dev,
|
|||
}
|
||||
|
||||
#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)) {
|
||||
LOG_ERR("Could not fetch temperature");
|
||||
ret = -EIO;
|
||||
|
@ -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)
|
||||
{
|
||||
const struct fxos8700_config *config = dev->config;
|
||||
struct fxos8700_data *data = dev->data;
|
||||
uint8_t val = *power;
|
||||
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_CTRLREG1,
|
||||
&val)) {
|
||||
if (i2c_reg_read_byte_dt(&config->i2c, FXOS8700_REG_CTRLREG1, &val)) {
|
||||
LOG_ERR("Could not get power setting");
|
||||
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)
|
||||
{
|
||||
const struct fxos8700_config *config = dev->config;
|
||||
struct fxos8700_data *data = dev->data;
|
||||
|
||||
return i2c_reg_update_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_CTRLREG1,
|
||||
FXOS8700_CTRLREG1_ACTIVE_MASK,
|
||||
power);
|
||||
return i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_CTRLREG1,
|
||||
FXOS8700_CTRLREG1_ACTIVE_MASK, power);
|
||||
}
|
||||
|
||||
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};
|
||||
const struct device *rst;
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
if (config->reset_name) {
|
||||
|
@ -434,8 +422,7 @@ static int fxos8700_init(const struct device *dev)
|
|||
* master. Therefore, do not check the return code of
|
||||
* the I2C transaction.
|
||||
*/
|
||||
i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_CTRLREG2,
|
||||
i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_CTRLREG2,
|
||||
FXOS8700_CTRLREG2_RST_MASK);
|
||||
}
|
||||
|
||||
|
@ -449,8 +436,8 @@ static int fxos8700_init(const struct device *dev)
|
|||
* compatible device 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,
|
||||
FXOS8700_REG_WHOAMI, &data->whoami)) {
|
||||
if (i2c_reg_read_byte_dt(&config->i2c, FXOS8700_REG_WHOAMI,
|
||||
&data->whoami)) {
|
||||
LOG_ERR("Could not get WHOAMI value");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -479,8 +466,7 @@ static int fxos8700_init(const struct device *dev)
|
|||
return -EIO;
|
||||
}
|
||||
|
||||
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_CTRLREG2,
|
||||
if (i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_CTRLREG2,
|
||||
FXOS8700_CTRLREG2_MODS_MASK,
|
||||
config->power_mode)) {
|
||||
LOG_ERR("Could not set power scheme");
|
||||
|
@ -488,8 +474,7 @@ static int fxos8700_init(const struct device *dev)
|
|||
}
|
||||
|
||||
/* Set the mode (accel-only, mag-only, or hybrid) */
|
||||
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_M_CTRLREG1,
|
||||
if (i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_M_CTRLREG1,
|
||||
FXOS8700_M_CTRLREG1_MODE_MASK,
|
||||
config->mode)) {
|
||||
LOG_ERR("Could not set mode");
|
||||
|
@ -499,8 +484,7 @@ static int fxos8700_init(const struct device *dev)
|
|||
/* Set hybrid autoincrement so we can read accel and mag channels in
|
||||
* one I2C transaction.
|
||||
*/
|
||||
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_M_CTRLREG2,
|
||||
if (i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_M_CTRLREG2,
|
||||
FXOS8700_M_CTRLREG2_AUTOINC_MASK,
|
||||
FXOS8700_M_CTRLREG2_AUTOINC_MASK)) {
|
||||
LOG_ERR("Could not set hybrid autoincrement");
|
||||
|
@ -508,8 +492,7 @@ static int fxos8700_init(const struct device *dev)
|
|||
}
|
||||
|
||||
/* Set the full-scale range */
|
||||
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_XYZ_DATA_CFG,
|
||||
if (i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_XYZ_DATA_CFG,
|
||||
FXOS8700_XYZ_DATA_CFG_FS_MASK,
|
||||
RANGE2FS(config->range))) {
|
||||
LOG_ERR("Could not set range");
|
||||
|
@ -622,8 +605,7 @@ static const struct sensor_driver_api fxos8700_driver_api = {
|
|||
|
||||
#define FXOS8700_INIT(n) \
|
||||
static const struct fxos8700_config fxos8700_config_##n = { \
|
||||
.i2c_name = DT_INST_BUS_LABEL(n), \
|
||||
.i2c_address = DT_INST_REG_ADDR(n), \
|
||||
.i2c = I2C_DT_SPEC_INST_GET(n), \
|
||||
.power_mode = DT_INST_PROP(n, power_mode), \
|
||||
.range = DT_INST_PROP(n, range), \
|
||||
FXOS8700_RESET(n) \
|
||||
|
|
|
@ -125,13 +125,12 @@ enum fxos_trigger_type {
|
|||
};
|
||||
|
||||
struct fxos8700_config {
|
||||
char *i2c_name;
|
||||
struct i2c_dt_spec i2c;
|
||||
#ifdef CONFIG_FXOS8700_TRIGGER
|
||||
char *gpio_name;
|
||||
uint8_t gpio_pin;
|
||||
gpio_dt_flags_t gpio_flags;
|
||||
#endif
|
||||
uint8_t i2c_address;
|
||||
char *reset_name;
|
||||
uint8_t reset_pin;
|
||||
gpio_dt_flags_t reset_flags;
|
||||
|
@ -155,7 +154,6 @@ struct fxos8700_config {
|
|||
};
|
||||
|
||||
struct fxos8700_data {
|
||||
const struct device *i2c;
|
||||
struct k_sem sem;
|
||||
#ifdef CONFIG_FXOS8700_TRIGGER
|
||||
const struct device *dev;
|
||||
|
|
|
@ -61,8 +61,7 @@ static int fxos8700_handle_pulse_int(const struct device *dev)
|
|||
|
||||
k_sem_take(&data->sem, K_FOREVER);
|
||||
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_PULSE_SRC,
|
||||
if (i2c_reg_read_byte_dt(&config->i2c, FXOS8700_REG_PULSE_SRC,
|
||||
&pulse_source)) {
|
||||
LOG_ERR("Could not read pulse source");
|
||||
}
|
||||
|
@ -99,8 +98,7 @@ static int fxos8700_handle_motion_int(const struct device *dev)
|
|||
|
||||
k_sem_take(&data->sem, K_FOREVER);
|
||||
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_FF_MT_SRC,
|
||||
if (i2c_reg_read_byte_dt(&config->i2c, FXOS8700_REG_FF_MT_SRC,
|
||||
&motion_source)) {
|
||||
LOG_ERR("Could not read pulse source");
|
||||
}
|
||||
|
@ -143,8 +141,7 @@ static void fxos8700_handle_int(const struct device *dev)
|
|||
/* Interrupt status register */
|
||||
k_sem_take(&data->sem, K_FOREVER);
|
||||
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_INT_SOURCE,
|
||||
if (i2c_reg_read_byte_dt(&config->i2c, FXOS8700_REG_INT_SOURCE,
|
||||
&int_source)) {
|
||||
LOG_ERR("Could not read interrupt source");
|
||||
int_source = 0U;
|
||||
|
@ -169,8 +166,7 @@ static void fxos8700_handle_int(const struct device *dev)
|
|||
/* Magnetometer interrupt source register */
|
||||
k_sem_take(&data->sem, K_FOREVER);
|
||||
|
||||
if (i2c_reg_read_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_M_INT_SRC,
|
||||
if (i2c_reg_read_byte_dt(&config->i2c, FXOS8700_REG_M_INT_SRC,
|
||||
&int_source)) {
|
||||
LOG_ERR("Could not read magnetometer interrupt source");
|
||||
int_source = 0U;
|
||||
|
@ -270,9 +266,7 @@ int fxos8700_trigger_set(const struct device *dev,
|
|||
}
|
||||
|
||||
/* Configure the sensor interrupt */
|
||||
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_CTRLREG4,
|
||||
mask,
|
||||
if (i2c_reg_update_byte_dt(&config->i2c, FXOS8700_REG_CTRLREG4, mask,
|
||||
handler ? mask : 0)) {
|
||||
LOG_ERR("Could not configure interrupt");
|
||||
ret = -EIO;
|
||||
|
@ -296,40 +290,39 @@ exit:
|
|||
static int fxos8700_pulse_init(const struct device *dev)
|
||||
{
|
||||
const struct fxos8700_config *config = dev->config;
|
||||
struct fxos8700_data *data = dev->data;
|
||||
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_PULSE_CFG, config->pulse_cfg)) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_CFG,
|
||||
config->pulse_cfg)) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_PULSE_THSX, config->pulse_ths[0])) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_THSX,
|
||||
config->pulse_ths[0])) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_PULSE_THSY, config->pulse_ths[1])) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_THSY,
|
||||
config->pulse_ths[1])) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_PULSE_THSZ, config->pulse_ths[2])) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_THSZ,
|
||||
config->pulse_ths[2])) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_PULSE_TMLT, config->pulse_tmlt)) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_TMLT,
|
||||
config->pulse_tmlt)) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_PULSE_LTCY, config->pulse_ltcy)) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_LTCY,
|
||||
config->pulse_ltcy)) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_PULSE_WIND, config->pulse_wind)) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_PULSE_WIND,
|
||||
config->pulse_wind)) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
|
@ -341,10 +334,9 @@ static int fxos8700_pulse_init(const struct device *dev)
|
|||
static int fxos8700_motion_init(const struct device *dev)
|
||||
{
|
||||
const struct fxos8700_config *config = dev->config;
|
||||
struct fxos8700_data *data = dev->data;
|
||||
|
||||
/* 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_FF_MT_CFG_ELE |
|
||||
FXOS8700_FF_MT_CFG_OAE |
|
||||
|
@ -355,8 +347,7 @@ static int fxos8700_motion_init(const struct device *dev)
|
|||
}
|
||||
|
||||
/* Set motion threshold to maximimum */
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_FF_MT_THS,
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_FF_MT_THS,
|
||||
FXOS8700_REG_FF_MT_THS)) {
|
||||
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)
|
||||
{
|
||||
const struct fxos8700_config *config = dev->config;
|
||||
struct fxos8700_data *data = dev->data;
|
||||
uint8_t m_vecm_cfg = config->mag_vecm_cfg;
|
||||
|
||||
/* Route the interrupt to INT1 pin */
|
||||
|
@ -378,8 +368,8 @@ static int fxos8700_m_vecm_init(const struct device *dev)
|
|||
#endif
|
||||
|
||||
/* Set magnetic vector-magnitude function */
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_M_VECM_CFG, m_vecm_cfg)) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_M_VECM_CFG,
|
||||
m_vecm_cfg)) {
|
||||
LOG_ERR("Could not set magnetic vector-magnitude function");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -387,14 +377,14 @@ static int fxos8700_m_vecm_init(const struct device *dev)
|
|||
/* Set magnetic vector-magnitude function threshold values:
|
||||
* handle both MSB and LSB registers
|
||||
*/
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_M_VECM_THS_MSB, config->mag_vecm_ths[0])) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_M_VECM_THS_MSB,
|
||||
config->mag_vecm_ths[0])) {
|
||||
LOG_ERR("Could not set magnetic vector-magnitude function threshold MSB value");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_M_VECM_THS_LSB, config->mag_vecm_ths[1])) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_M_VECM_THS_LSB,
|
||||
config->mag_vecm_ths[1])) {
|
||||
LOG_ERR("Could not set magnetic vector-magnitude function threshold LSB value");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -436,8 +426,8 @@ int fxos8700_trigger_init(const struct device *dev)
|
|||
ctrl_reg5 |= FXOS8700_MOTION_MASK;
|
||||
#endif
|
||||
|
||||
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
|
||||
FXOS8700_REG_CTRLREG5, ctrl_reg5)) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, FXOS8700_REG_CTRLREG5,
|
||||
ctrl_reg5)) {
|
||||
LOG_ERR("Could not configure interrupt pin routing");
|
||||
return -EIO;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue